Skip to content

Commit

Permalink
read exisiting account document to get tree when importing enabled fi…
Browse files Browse the repository at this point in the history
…eld (#6614)

When trying to disable an account via the Account Manager app, the account is not disabled.

This bug was introduce when account creation/deletion was moved to core.
Before that move, we properly load and merge the request document with doc from database
so we had the `pvt_tree` field when we were checking ig the requestor is in
account's tree.
But this got moved into after validation and in `post/1,2` function and so we
don't set enabled anymore.
  • Loading branch information
icehess authored Aug 5, 2020
1 parent 7d2db11 commit 72b235e
Showing 1 changed file with 21 additions and 39 deletions.
60 changes: 21 additions & 39 deletions applications/crossbar/src/modules/cb_accounts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,10 @@ add_pvt_api_key(Context) ->
%% @doc
%% @end
%%------------------------------------------------------------------------------

-spec post(cb_context:context(), path_token()) -> cb_context:context().
post(Context, AccountId) ->
{'ok', Existing} = kzd_accounts:fetch(AccountId),
New = kz_json:merge(kz_doc:private_fields(Existing), cb_context:doc(Context)),
New = kz_json:merge(kz_doc:private_fields(Existing), maybe_import_enabled(Context, Existing)),
case kzd_accounts:save(New) of
{'ok', SavedAccount} ->
Context1 = crossbar_doc:handle_datamgr_success(SavedAccount, Context),
Expand Down Expand Up @@ -515,48 +514,31 @@ get_parent_id_from_req(Context) ->

-spec extra_validation(kz_term:ne_binary(), cb_context:context()) -> cb_context:context().
extra_validation(AccountId, Context) ->
Extra = [fun(_, C) -> maybe_import_enabled(C) end
,fun disallow_direct_clients/2
],
lists:foldl(fun(F, C) -> F(AccountId, C) end
,Context
,Extra
).

-spec maybe_import_enabled(cb_context:context()) ->
cb_context:context().
maybe_import_enabled(Context) ->
disallow_direct_clients(AccountId, Context).

-spec maybe_import_enabled(cb_context:context(), kz_json:object()) ->
kz_json:object().
maybe_import_enabled(Context, ExistingDoc) ->
case cb_context:auth_account_id(Context) =:= cb_context:account_id(Context) of
'true' ->
NewDoc = kz_json:delete_key(<<"enabled">>, cb_context:doc(Context)),
cb_context:set_doc(Context, NewDoc);
kz_json:delete_key(<<"enabled">>, cb_context:doc(Context));
'false' ->
lager:debug("this should be success: ~p", [cb_context:resp_status(Context)]),
maybe_import_enabled(Context, cb_context:resp_status(Context))
end.

-spec maybe_import_enabled(cb_context:context(), crossbar_status()) ->
cb_context:context().
maybe_import_enabled(Context, 'success') ->
AuthAccountId = cb_context:auth_account_id(Context),
Doc = cb_context:doc(Context),
Enabled = kz_json:get_value(<<"enabled">>, Doc),
NewDoc = kz_json:delete_key(<<"enabled">>, Doc),
lager:debug("import enabled: ~p", [Enabled]),
case lists:member(AuthAccountId, kzd_accounts:tree(Doc)) of
'false' -> cb_context:set_doc(Context, NewDoc);
'true' -> maybe_import_enabled(Context, NewDoc, Enabled)
AuthAccountId = cb_context:auth_account_id(Context),
Doc = cb_context:doc(Context),
Enabled = kz_json:is_true(<<"enabled">>, Doc, 'undefined'),
NewDoc = kz_json:delete_key(<<"enabled">>, Doc),
case lists:member(AuthAccountId, kzd_accounts:tree(ExistingDoc)) of
'false' -> NewDoc;
'true' ->
lager:debug("importing enabled from req and it is set to: ~p", [Enabled]),
import_enabled(NewDoc, Enabled)
end
end.

-spec maybe_import_enabled(cb_context:context(), kz_json:object(), kz_term:api_binary()) ->
cb_context:context().
maybe_import_enabled(Context, _, 'undefined') -> Context;
maybe_import_enabled(Context, Doc, IsEnabled) ->
NewDoc = case kz_term:is_true(IsEnabled) of
'true' -> kzd_accounts:enable(Doc);
'false' -> kzd_accounts:disable(Doc)
end,
cb_context:set_doc(Context, NewDoc).
-spec import_enabled(kz_json:object(), kz_term:api_boolean()) -> kz_json:object().
import_enabled(Doc, 'undefined') -> Doc;
import_enabled(Doc, 'true') -> kzd_accounts:enable(Doc);
import_enabled(Doc, 'false') -> kzd_accounts:disable(Doc).

-spec disallow_direct_clients(kz_term:api_binary(), cb_context:context()) -> cb_context:context().
disallow_direct_clients(AccountId, Context) ->
Expand Down

0 comments on commit 72b235e

Please sign in to comment.