Skip to content

Commit

Permalink
Pushed changes to libsnarl.
Browse files Browse the repository at this point in the history
  • Loading branch information
Licenser committed Aug 26, 2014
1 parent b6ee955 commit 7925cf1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
6 changes: 1 addition & 5 deletions apps/wiggle/src/wiggle_handler.erl
Expand Up @@ -16,7 +16,7 @@
get_persmissions/1,
timeout_cache_with_invalid/6,
timeout_cache/5,
list/8, list/9
list/9
]).

initial_state(Req) ->
Expand Down Expand Up @@ -236,10 +236,6 @@ timeout_cache_with_invalid(Cache, Value, TTL1, TTL2, Invalid, Fun) ->
R
end.

list(ListFn, Token, Permission, FullList, Filter, TTLEntry, FullCache, ListCache) ->
ConvertFn = fun(E) -> E end,
list(ListFn, ConvertFn, Token, Permission, FullList, Filter, TTLEntry, FullCache, ListCache).

list(ListFn, ConvertFn, Token, Permission, FullList, Filter, TTLEntry, FullCache, ListCache) ->
Fun = list_fn(ListFn, ConvertFn, Permission, FullList, Filter),
case application:get_env(wiggle, TTLEntry) of
Expand Down
6 changes: 4 additions & 2 deletions apps/wiggle/src/wiggle_org_handler.erl
Expand Up @@ -137,7 +137,8 @@ read(Req, State = #state{token = Token, path = [], full_list=FullList, full_list
Permission = [{must, 'allowed',
[<<"orgs">>, {<<"res">>, <<"uuid">>}, <<"get">>],
Permissions}],
Res = wiggle_handler:list(fun ls_org:list/2, Token, Permission,
Res = wiggle_handler:list(fun ls_org:list/2,
fun ft_org:to_json/1, Token, Permission,
FullList, Filter, org_list_ttl, ?FULL_CACHE,
?LIST_CACHE),

Expand All @@ -148,7 +149,8 @@ read(Req, State = #state{path = [_Org], obj = OrgObj}) ->
{OrgObj, Req, State};

read(Req, State = #state{path = [_Org, <<"triggers">>], obj = OrgObj}) ->
{jsxd:get(<<"triggers">>, [], OrgObj), Req, State}.
%% can't get the ft_role:triggers since the json conversion would miss
{jsxd:get(<<"triggers">>, [], ft_org:to_json(OrgObj)), Req, State}.

%%--------------------------------------------------------------------
%% PUT
Expand Down
9 changes: 5 additions & 4 deletions apps/wiggle/src/wiggle_role_handler.erl
Expand Up @@ -47,7 +47,7 @@ get(State = #state{path = [Role, <<"permissions">> | Permission]}) ->
{[], {ok, Obj}} ->
{ok, Obj};
{P, {ok, Obj}} ->
case lists:member(P, jsxd:get(<<"permissions">>, [], Obj)) of
case lists:member(P, ft_role:permissions(Obj)) of
true ->
{ok, Obj};
_ -> not_found
Expand Down Expand Up @@ -117,7 +117,8 @@ read(Req, State = #state{token = Token, path = [], full_list=FullList, full_list
Permission = [{must, 'allowed',
[<<"roles">>, {<<"res">>, <<"uuid">>}, <<"get">>],
Permissions}],
Res = wiggle_handler:list(fun ls_role:list/2, Token, Permission,
Res = wiggle_handler:list(fun ls_role:list/2,
fun ft_role:to_json/1, Token, Permission,
FullList, Filter, role_list_ttl, ?FULL_CACHE,
?LIST_CACHE),
?MSniffle(?P(State), Start1),
Expand All @@ -127,11 +128,11 @@ read(Req, State = #state{path = [_Role], obj = RoleObj}) ->
RoleObj1 = jsxd:update(<<"permissions">>,
fun (Permissions) ->
lists:map(fun jsonify_permissions/1, Permissions)
end, [], RoleObj),
end, [], ft_role:to_json(RoleObj)),
{RoleObj1, Req, State};

read(Req, State = #state{path = [_Role, <<"permissions">>], obj = RoleObj}) ->
{lists:map(fun jsonify_permissions/1, jsxd:get(<<"permissions">>, [], RoleObj)), Req, State}.
{lists:map(fun jsonify_permissions/1, ft_role:permissions(RoleObj)), Req, State}.

%%--------------------------------------------------------------------
%% PUT
Expand Down
24 changes: 12 additions & 12 deletions apps/wiggle/src/wiggle_user_handler.erl
Expand Up @@ -75,10 +75,11 @@ get(State = #state{path = [User, <<"permissions">> | Permission]}) ->
{[], {ok, Obj}} ->
{ok, Obj};
{P, {ok, Obj}} ->
case lists:member(P, jsxd:get(<<"permissions">>, [], Obj)) of
case lists:member(P, ft_user:permissions(Obj)) of
true ->
{ok, Obj};
_ -> not_found
_ ->
not_found
end
end;

Expand All @@ -88,7 +89,7 @@ get(State = #state{method = <<"DELETE">>,
not_found ->
not_found;
{ok, Obj} ->
case lists:member(Role, jsxd:get(<<"roles">>, [], Obj)) of
case lists:member(Role, ft_user:roles(Obj)) of
true ->
{ok, Obj};
_ ->
Expand Down Expand Up @@ -229,7 +230,8 @@ read(Req, State = #state{token = Token, path = [], full_list=FullList, full_list
Permission = [{must, 'allowed',
[<<"users">>, {<<"res">>, <<"uuid">>}, <<"get">>],
Permissions}],
Res = wiggle_handler:list(fun ls_user:list/2, Token, Permission,
Res = wiggle_handler:list(fun ls_user:list/2,
fun ft_user:to_json/1, Token, Permission,
FullList, Filter, user_list_ttl, ?FULL_CACHE,
?LIST_CACHE),

Expand All @@ -240,24 +242,24 @@ read(Req, State = #state{path = [_User], obj = UserObj}) ->
UserObj1 = jsxd:update(<<"permissions">>,
fun (Permissions) ->
lists:map(fun jsonify_permissions/1, Permissions)
end, [], UserObj),
end, [], ft_user:to_json(UserObj)),
UserObj2 = jsxd:delete(<<"password">>, UserObj1),
{UserObj2, Req, State};

read(Req, State = #state{path = [_User, <<"permissions">>], obj = UserObj}) ->
{lists:map(fun jsonify_permissions/1, jsxd:get(<<"permissions">>, [], UserObj)), Req, State};
{lists:map(fun jsonify_permissions/1, ft_user:permissions(UserObj)), Req, State};

read(Req, State = #state{path = [_User, <<"roles">>], obj = UserObj}) ->
{jsxd:get(<<"roles">>, [], UserObj), Req, State};
{ft_user:roles(UserObj), Req, State};

read(Req, State = #state{path = [_User, <<"orgs">>], obj = UserObj}) ->
{jsxd:get(<<"orgs">>, [], UserObj), Req, State};
{ft_user:orgs(UserObj), Req, State};

read(Req, State = #state{path = [_User, <<"keys">>], obj = UserObj}) ->
{jsxd:get(<<"keys">>, [], UserObj), Req, State};
{ft_user:keys(UserObj), Req, State};

read(Req, State = #state{path = [_User, <<"yubikeys">>], obj = UserObj}) ->
{jsxd:get(<<"yubikeys">>, [], UserObj), Req, State}.
{ft_user:yubikeys(UserObj), Req, State}.

%%--------------------------------------------------------------------
%% PUT
Expand All @@ -281,8 +283,6 @@ create(Req, State = #state{token = Token, path = [], version = Version}, Decoded
write(Req, State = #state{path = [User]}, [{<<"password">>, Password}]) ->
Start = now(),
ok = ls_user:passwd(User, Password),
e2qc:evict(?CACHE, User),
e2qc:teardown(?FULL_CACHE),
?MSnarl(?P(State), Start),
{true, Req, State};

Expand Down

0 comments on commit 7925cf1

Please sign in to comment.