cps86
December 20, 2024, 5:08pm
1
Hello
From ruby, is it possible to check if current user is a member of XYZ group or role?
I tried with:
if User.current.roles.exists?(name: 'My Custom Role')
But it seems it doesn’t work as it lists only Default Group which is not true.
I am using Foreman 3.13
ekohl
December 20, 2024, 5:49pm
2
I didn’t check if this works, but I think this is more likely to work:
User.current.roles.where(name: 'My Custom Role').exists?
Foreman uses ActiveRecord as a database abstraction so Active Record Query Interface — Ruby on Rails Guides may be helpful to understand it.
Though it may help to describe what you’re trying to achieve. Usually we introduce specific permissions which can be assigned to roles. Then you verify a user has a specific permission.
Also make sure to use cached_roles
[1], if you just check roles
it wouldn’t give you those, that are assigned through user groups or their children.
[1] foreman/app/models/user.rb at develop · theforeman/foreman · GitHub