Library for extending gem https://github.com/RolifyCommunity/rolify which allows you to define some restrictions for defining roles for users.
Library created for rolify issue RolifyCommunity/rolify#488
I had to override the standard behavior of add_role
command to add some logic when the Role to a Resource can be inserted or not.
My library now supporting only two ways of defining a role
- Adding a role to a user for instance of the defined class
- Adding a role to the user without any instance or class
I'll try to extend this library to support more cases (which I did not need)
- Adding a role to User for a whole Class (not only instances of a class)
-
config
- contains configuration file
rolify_roles.yml
where you define Roles that can be created and to which instance it can be associated initializers
- You have to require new library in your application initializer
- contains configuration file
-
app/models
- In a model User, that will be probably used to provide an authentication solution, you have to include this Library for a usage
-
lib
- Contains a logic with defined restrictions described in Motivation
User.last.add_role :multiple_resource_role, Project.last
User.last.add_role :multiple_resource_role, Audit.last
User.last.add_role :single_resource_role, Project.last
User.last.add_role :resourceless_role
User.last.add_role :undefined
- RolifyRolesException: Role is not defined!
-
User.last.add_role :multiple_resource_role
- RolifyRolesException: Not allowed - Target resource cannot be blank!
-
User.last.add_role :multiple_resource_role, Project
- Not allowed - Forbidden resource Class for role multiple_resource_role
-
User.last.add_role :multiple_resource_role, Object.new
- Not allowed - Forbidden resource Object for role multiple_resource_role
-
User.last.add_role :single_resource_role
- RolifyRolesException: Not allowed - Target resource cannot be blank!
-
User.last.add_role :single_resource_role, Project
- Not allowed - Forbidden resource Class for role single_resource_role
-
User.last.add_role :single_resource_role, Object.new
- Not allowed - Forbidden resource Object for role single_resource_role
-
User.last.add_role :resourceless_role, Project
- RolifyRolesException: Not allowed - Role resourceless_role can't be assigned to any resource!
-
User.last.add_role :resourceless_role, Project.first
- RolifyRolesException: Not allowed - Role resourceless_role can't be assigned to any resource!
I've added a docker compose file, which creates an one container for Rails application and second for a mysql database
To try the library out run following commands
cd RolifyRestriction
docker-compose build
docker-compose up -d
# Keep containers up and runningsudo docker-compose run web bash
# Opens bash terminal at rails applicationrake db:create
# Create database on mysql containerrake db:migrate
# Run migrations to create tablerake db:seed
# Insert test data into databaserails c
# Open rails console- Test it! - Commands are listed above