From 04b15906162499636125ac33b271b143ff6f87bd Mon Sep 17 00:00:00 2001 From: karl anderson Date: Tue, 10 Mar 2015 19:02:37 -0400 Subject: [PATCH] KAZOO-3409: update the apps link payload --- applications/crossbar/src/crossbar_util.erl | 13 +++- .../crossbar/src/modules/cb_apps_link.erl | 59 ++++++++++++++++--- .../crossbar/src/modules/cb_search.erl | 19 +----- 3 files changed, 62 insertions(+), 29 deletions(-) diff --git a/applications/crossbar/src/crossbar_util.erl b/applications/crossbar/src/crossbar_util.erl index b5f704d2f9e..8e3ff210529 100644 --- a/applications/crossbar/src/crossbar_util.erl +++ b/applications/crossbar/src/crossbar_util.erl @@ -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 @@ -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), @@ -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; diff --git a/applications/crossbar/src/modules/cb_apps_link.erl b/applications/crossbar/src/modules/cb_apps_link.erl index 52e323a8380..4dc5ee2ea86 100644 --- a/applications/crossbar/src/modules/cb_apps_link.erl +++ b/applications/crossbar/src/modules/cb_apps_link.erl @@ -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. diff --git a/applications/crossbar/src/modules/cb_search.erl b/applications/crossbar/src/modules/cb_search.erl index f1540166c3d..809d47a8354 100644 --- a/applications/crossbar/src/modules/cb_search.erl +++ b/applications/crossbar/src/modules/cb_search.erl @@ -12,7 +12,6 @@ -export([init/0 ,allowed_methods/0 - ,authorize/1, authorize/2 ,resource_exists/0 ,validate/1 ]). @@ -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