Filter select list in Smart Action and standard forms by current user tags

Hello!

Feature(s) impacted

Rails 6.0
Users belong to department and departments belong to organization.

We set up a user tag to filter by organization ($currentUser.tags.organization). This works well to scope collections but I also need to use it in forms.

When creating a new user via a smart action, I need to filter the department collection in the select list to show only those departments belonging to the current user’s organization, not all departments in the db.

Ideally if I had access to the current user or $currentUser.tags.organization in forest_liana/collections/user.rb, then I could use the on load hook to filter the collection.

I guess my question is what is the best way to do this? The select list in the regular forms also need to filter by departments belonging only to the current user’s organization.

Edit:
In forest/users_controller.rb I have access to @jwt_decoded_token, so I was able to get the organization name like so @jwt_decoded_token[“tags”][0][“value”].

Unfortunately I haven’t been able to figure out if there is a similar JWT token accessible in forest_liana/collections/user.rb when a smart action is triggered?

Thanks!

Hi @muz :wave: You can access in your controller to forest_user object who have the informations you want.

organizationTag = forest_user['tags'].find {|tag| tag['key'] == 'organization'};
organizationTag['value'] # => organization name

EDIT: I din’t see you asking for load hook
If you use load hook, you need to get user form context

:load => -> (context) {
  organizationTag = context[:user]['tags'].find {|tag| tag['key'] == 'organization'};
  organizationTag['value'] # => organization name
}

Let me know if that helps!

1 Like

Hi @Arnaud_Moncel , thanks for getting back to me!

I am getting error when triggering the smart action - the NoMethodError in the dev logs:
NoMethodError (undefined method ’ for nil:NilClass):`

when I added the code above, and same thing when just for adding this line to test it:

:hooks => {
      :load => -> (context) {
          context[:user]['tags']
}

Does context[:user] have access to the current user’s tags? I have set my user’s tags, so I assumed that it would have access to the tags when I am trying to add a user via a smart action.

Thanks again.

Sorry about that, i forgot to mention that is available with forest_liana gem >= 7.4.2

Oh, OK. We are on forest_liana (7.4.0), so will try to update to latest version.

Just in case, is there another way to access the user tags outside of controllers, such as say in forest_liana/collections/user.rb ?

Thanks again!

In which context do you want to have this information?
Can you give me a piece of code or an exemple in order to understand well? :pray:

Just meant in the context of the on load hooks we were discussing, so I can use the organization to filter departments, etc.

If upgrading to >= 7.4.2 is the only/best way to do it, that is fine too. I was just curious if you knew of other ways to accomplish the same thing.

Thank you!

Unfortunately this is the only way to get this information is to use the user from the context.

OK, will give that a try I will let you know how it goes.

Thanks!

1 Like

Hi @Arnaud_Moncel

Upgraded to forest_liana 7.4.5 and it worked!

Thanks again!

Would it be possible to make a feature request?

In the standard form, say for adding users, it would be great if there was a way to filter the collections from a relationship. For example, only listing departments that belong to the current user’s organization.

Hi @muz :wave: you can configure a dropdown widget on your belongsTo field and add a filter based on $currentUser.tags.organization.


Let me know :grinning:

2 Likes

Hi @Arnaud_Moncel

Yep, that worked. I have so much to learn!

Thank you for all your help, I really appreciate it!

3 Likes