Automatically add new users to a user group

Why

When you create a person, you usually need to add it to a user group as well. You may want to automate this, in particular if you need to differentiate the user group based on the person’s category.

Solution

You can set the user group by adding a hasusergroup property to the resource. In your site’s main .erl file, add a rsc_update observer:

-export([
    observe_rsc_update/3
]).

observe_rsc_update(#rsc_update{action = insert, id = Id}, {ok, Props} = Acc, Context) ->
    %% Where 'vip' is a subcategory of 'person'
    case m_rsc:is_a(Id, vip, Context) of
        false ->
            %% Do nothing
            Acc;
        true ->
            %% Add hasusergroup property
            {ok, Props#{ <<"hasusergroup">> => m_rsc:rid(acl_user_group_vips, Context)}}
    end;
observe_rsc_update(#rsc_update{}, Acc, _Context) ->
    %% Fall through
    Acc.