Skip to content

Commit

Permalink
mod_acl_user_groups: add acl_collab_groups_modify notification (0.x) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed Mar 27, 2023
1 parent 4093537 commit 6b289d6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
20 changes: 20 additions & 0 deletions include/zotonic_notifications.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,26 @@
%% Either return an updated #context or undefined
-record(acl_logoff, {}).

%% @doc Modify the list of user groups of an user. Called internally
%% by the ACL modules when fetching the list of user groups an user
%% is member of.
%% Type: foldl
%% Return: ``[ m_rsc:resource_id() ]``
-record(acl_user_groups_modify, {
id :: m_rsc:resource_id() | undefined,
groups :: list( m_rsc:resource_id() )
}).

%% @doc Modify the list of collaboration groups of an user. Called internally
%% by the ACL modules when fetching the list of collaboration groups an user
%% is member of.
%% Type: foldl
%% Return: ``[ m_rsc:resource_id() ]``
-record(acl_collab_groups_modify, {
id :: m_rsc:resource_id() | undefined,
groups :: list( m_rsc:resource_id() )
}).


% Handle the confirm of an user.
% 'auth_confirm' - foldl over the #context
Expand Down
38 changes: 30 additions & 8 deletions modules/mod_acl_user_groups/support/acl_user_groups_checks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -446,37 +446,59 @@ session_state(Context) ->
% Fetch all usergroups the user is member of.
% Anonymous user are member of `acl_user_group_anonymous`.
% Users are member of `acl_user_group_members`, unless they have hasusergroup connections to other groups.
-spec has_user_groups(integer()|undefined, #context{}) -> list(integer()).
has_user_groups(undefined, Context) ->
-spec has_user_groups(UserId, Context) -> Groups when
UserId :: m_rsc:resource_id() | undefined,
Context :: z:context(),
Groups :: list(m_rsc:resource_id()).
has_user_groups(UserId, Context) ->
Groups = has_user_groups_1(UserId, Context),
z_notifier:foldl(
#acl_user_groups_modify{
id = UserId,
groups = Groups
}, Groups, Context).

has_user_groups_1(undefined, Context) ->
[ m_rsc:rid(acl_user_group_anonymous, Context) ];
has_user_groups(1, Context) ->
has_user_groups_1(1, Context) ->
MgrId = m_rsc:rid(acl_user_group_managers, Context),
Groups = m_edge:objects(1, hasusergroup, Context),
case {MgrId, lists:member(MgrId, Groups)} of
{undefined, _} -> Groups;
{_, true} -> Groups;
{_, false} -> Groups ++ [MgrId]
end;
has_user_groups(UserId, Context) ->
has_user_groups_1(UserId, Context) ->
case m_edge:objects(UserId, hasusergroup, Context) of
[] ->
[ m_rsc:rid(acl_user_group_members, Context) ];
Us ->
Us
end.

% Fetch all collaboration groups the current is member of.
% Fetch all collaboration groups the current user is member of.
-spec has_collab_groups(#context{}) -> list(integer()).
has_collab_groups(#context{acl=#aclug{collab_groups=Ids}}) ->
Ids;
has_collab_groups(_Context) ->
[].

% Fetch all collaboration groups the user is member of.
-spec has_collab_groups(integer()|undefined, #context{}) -> list(integer()).
has_collab_groups(undefined, _Context) ->
[];
-spec has_collab_groups(UserId, Context) -> CollabGroups when
UserId :: m_rsc:resource_id() | undefined,
Context :: z:context(),
CollabGroups :: [ m_rsc:resource_id() ].
has_collab_groups(UserId, Context) ->
CollabGroups = has_collab_groups_1(UserId, Context),
z_notifier:foldl(
#acl_collab_groups_modify{
id = UserId,
groups = CollabGroups
}, CollabGroups, Context).

has_collab_groups_1(undefined, _Context) ->
[];
has_collab_groups_1(UserId, Context) ->
m_edge:subjects(UserId, hascollabmanager, Context)
++ m_edge:subjects(UserId, hascollabmember, Context).

Expand Down

0 comments on commit 6b289d6

Please sign in to comment.