Skip to content

Commit

Permalink
KAZOO-3409: update the apps link payload
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anderson committed Mar 10, 2015
1 parent 584d771 commit 04b1590
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 29 deletions.
13 changes: 11 additions & 2 deletions applications/crossbar/src/crossbar_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@
,enable_account/1
,change_pvt_enabled/2
]).
-export([load_apps/2]).
-export([get_path/2]).
-export([get_user_lang/2
,get_account_lang/1
,get_language/1
,get_language/2
]).
-export([get_user_timezone/2
,get_account_timezone/1
Expand Down Expand Up @@ -631,7 +634,6 @@ populate_resp(JObj, AccountId, UserId) ->
).

-spec load_apps(ne_binary(), api_binary()) -> api_object().
load_apps(_, 'undefined') -> 'undefined';
load_apps(AccountId, UserId) ->
MasterAccountDb = get_master_account_db(),
Lang = get_language(AccountId, UserId),
Expand Down Expand Up @@ -664,8 +666,15 @@ filter_apps([JObj|JObjs], Lang, Acc) ->
),
filter_apps(JObjs, Lang, [FormatedApp|Acc]).

-spec get_language(ne_binary()) -> api_binary().
get_language(AccountId) -> get_language(AccountId, 'undefined').

-spec get_language(ne_binary(), api_binary()) -> api_binary().
get_language(_, 'undefined') -> 'undefined';
get_language(AccountId, 'undefined') ->
case ?MODULE:get_account_lang(AccountId) of
{'ok', Lang} -> Lang;
'error' -> ?DEFAULT_LANGUAGE
end;
get_language(AccountId, UserId) ->
case ?MODULE:get_user_lang(AccountId, UserId) of
{'ok', Lang} -> Lang;
Expand Down
59 changes: 50 additions & 9 deletions applications/crossbar/src/modules/cb_apps_link.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,54 @@ resource_exists(?AUTHORIZE) -> 'true'.
%% @end
%%--------------------------------------------------------------------
-spec validate(cb_context:context(), path_token()) -> cb_context:context().
validate(#cb_context{auth_doc=Doc}=Context, ?AUTHORIZE) ->
RequestNouns = cb_context:req_nouns(Context),
RequestedAccountId = case props:get_value(<<"accounts">>, RequestNouns) of
'undefined' -> wh_json:get_value(<<"account_id">>, Doc, <<>>);
[Else] -> Else
end,
AccountId = wh_json:get_value(<<"account_id">>, Doc),
JObj = wh_json:set_value(<<"account_id">>, RequestedAccountId, Doc),
crossbar_util:response(crossbar_util:response_auth(JObj, AccountId), Context).
validate(Context, ?AUTHORIZE) ->
JObj = wh_json:from_list(
[{<<"auth-token">>, auth_info(Context)}
,{<<"account">>, account_info(Context)}
]
),
crossbar_util:response(JObj, Context).

-spec account_info(cb_context:context()) -> wh_json:object().
account_info(Context) ->
AccountId = get_request_account(Context),
{'ok', MasterAccountId} = whapps_util:get_master_account_id(),
wh_json:from_list(
[{<<"account_id">>, AccountId}
,{<<"account_name">>, whapps_util:get_account_name(AccountId)}
,{<<"language">>, crossbar_util:get_language(AccountId)}
,{<<"is_reseller">>, wh_services:is_reseller(AccountId)}
,{<<"reseller_id">>, wh_services:find_reseller_id(AccountId)}
,{<<"is_master">>, AccountId =:= MasterAccountId}
]
).

-spec auth_info(cb_context:context()) -> wh_json:object().
auth_info(Context) ->
JObj = cb_context:auth_doc(Context),
AccountId = cb_context:auth_account_id(Context),
OwnerId = wh_json:get_value(<<"owner_id">>, JObj),
{'ok', MasterAccountId} = whapps_util:get_master_account_id(),
wh_json:from_list(
props:filter_undefined(
[{<<"account_id">>, AccountId}
,{<<"owner_id">>, OwnerId}
,{<<"account_name">>, whapps_util:get_account_name(AccountId)}
,{<<"method">>, wh_json:get_value(<<"method">>, JObj)}
,{<<"created">>, wh_json:get_value(<<"pvt_created">>, JObj)}
,{<<"language">>, crossbar_util:get_language(AccountId, OwnerId)}
,{<<"is_reseller">>, wh_services:is_reseller(AccountId)}
,{<<"reseller_id">>, wh_services:find_reseller_id(AccountId)}
,{<<"apps">>, crossbar_util:load_apps(AccountId, OwnerId)}
,{<<"is_master">>, AccountId =:= MasterAccountId}
]
)
).

-spec get_request_account(cb_context:context()) -> ne_binary().
get_request_account(Context) ->
RequestNouns = cb_context:req_nouns(Context),
case props:get_value(<<"accounts">>, RequestNouns) of
'undefined' -> cb_context:auth_account_id(Context);
[Else] -> Else
end.
19 changes: 1 addition & 18 deletions applications/crossbar/src/modules/cb_search.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

-export([init/0
,allowed_methods/0
,authorize/1, authorize/2
,resource_exists/0
,validate/1
]).
Expand All @@ -38,23 +37,7 @@
init() ->
_ = crossbar_bindings:bind(<<"*.allowed_methods.search">>, ?MODULE, 'allowed_methods'),
_ = crossbar_bindings:bind(<<"*.resource_exists.search">>, ?MODULE, 'resource_exists'),
_ = crossbar_bindings:bind(<<"*.validate.search">>, ?MODULE, 'validate'),
_ = crossbar_bindings:bind(<<"*.authorize">>, ?MODULE, 'authorize').

%%--------------------------------------------------------------------
%% @public
%% @doc
%% Authorize the request
%% @end

-spec authorize(cb_context:context()) -> 'false'.
-spec authorize(cb_context:context(), path_token()) -> boolean().

authorize(Context) ->
cb_context:auth_token(Context) =/= 'undefined'.

authorize(Context, _Module) ->
cb_context:auth_token(Context) =/= 'undefined'.
_ = crossbar_bindings:bind(<<"*.validate.search">>, ?MODULE, 'validate').

%%--------------------------------------------------------------------
%% @public
Expand Down

0 comments on commit 04b1590

Please sign in to comment.