Skip to content

Commit

Permalink
code review update - minor refactor of process_parts
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Paradise <[email protected]>
  • Loading branch information
Marc Paradise committed Aug 30, 2016
1 parent 651d800 commit d5183e3
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/oc_erchef/apps/oc_chef_authz/src/oc_chef_authz_acl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fetch(Type, AuthzId, Granular) ->
Result = oc_chef_authz_http:request(Path, get, ?DEFAULT_HEADERS, [], SuperuserId),
case Result of
{ok, Record} ->
ids_to_names(Record, Granular);
convert_all_ids_to_names(Record, Granular);
{error, forbidden} ->
forbidden;
Other ->
Expand Down Expand Up @@ -334,15 +334,15 @@ convert_actor_ids_to_names(AuthzIds) ->
oc_chef_group:find_users_names(RemainingAuthzIds, fun chef_sql:select_rows/1),
{ClientNames, UserNames, DefunctActorAuthzIds}.

ids_to_names(Record, Granular) ->
Record1 = process_part(<<"create">>, Record, Granular),
Record2 = process_part(<<"read">>, Record1, Granular),
Record3 = process_part(<<"update">>, Record2, Granular),
Record4 = process_part(<<"delete">>, Record3, Granular),
process_part(<<"grant">>, Record4, Granular).

-spec process_part(binary(), ejson_term(), granular|undefined) -> ejson_term().
process_part(Part, Record, Granular) ->
convert_all_ids_to_names(Record, Granular) ->
convert_ids_to_names_in_part([<<"create">>, <<"read">>, <<"update">>, <<"delete">>],
Record, Granular).

-spec convert_ids_to_names_in_part(list(binary()), ejson_term(), granular|undefined) -> ejson_term().
convert_ids_to_names_in_part([], Record, _Granular) ->
Record;
convert_ids_to_names_in_part([Part | Rest], Record, Granular) ->
Members = ej:get({Part}, Record),
ActorIds = ej:get({<<"actors">>}, Members),
GroupIds = ej:get({<<"groups">>}, Members),
Expand All @@ -351,7 +351,9 @@ process_part(Part, Record, Granular) ->
oc_chef_authz_cleanup:add_authz_ids(DefunctActorAuthzIds, DefunctGroupAuthzIds),
Members1 = part_with_actors(Members, ClientNames, UserNames, Granular),
Members2 = ej:set({<<"groups">>}, Members1, GroupNames),
ej:set({Part}, Record, Members2).
NewRecord = ej:set({Part}, Record, Members2),
convert_ids_to_names_in_part(Rest, NewRecord, Granular).


part_with_actors(PartRecord, Clients, Users, granular) ->
PartRecord0 = ej:set({<<"users">>}, PartRecord, Users),
Expand Down

0 comments on commit d5183e3

Please sign in to comment.