docbrown/docs/backend/roles.md
Andy Zhao 955bdcb2bc
Update docs with Forem instead of dev.to (#9316)
* Rename all GitHub links from thepracticaldev/dev.to to forem/forem

* Use new site name

* Rename to Forem

* Rename more dev.to to forem

* Remove unnecessary redirects

* Rename DEV to Forem

* Use Forem instead of DEV for branding

* Use Forem instead of DEV for licensing

* Use seedling instead of DEV logo
2020-07-15 13:29:11 -04:00

1.9 KiB

title
Roles

Roles

What is a role?

If authorization is about who has permission to be allowed to do what you want to do, then Roles are common patterns of authorization across users - reducing the administrative overhead.

Why do I need to know about roles?

Some bugs can only be seen for users with specific roles. You will need to change the role to reproduce a problem.

How do we implement roles in Forem?

Roles are implemented in this application using Rolify. The list of roles can be found in app/models/role.rb and you can search for has_role in the codebase to find which pages need which roles.

A new user starts without any roles, and there is no administrative way of adding roles to users yet. To assign a user a role you will have to run commands at the console.

Example of adding permissions to a user

  • open the Rails console
rails console
  • after verifying the user test_user_name is missing the pro role we proceed to add it and then verify the role has been added:
> user = User.find_by(username: "test_user_name")
> user.has_role? :pro
=> false

> user.add_role :pro
=> #<Role:
...
name: "pro"
.. >

> user.has_role? :pro
=> true

Another common requirement is changing to the administrative role, and an example of this is found on the admin page.

Verification

A more complex query to list all the users and their roles:

User.joins(:roles).order(:id).group(:id).pluck(:id, :username, Arel.sql("array_agg(roles.name)"))

Further Reading

  1. Rolify README.md
  2. What is the purpose of Rolify?
  3. Admin