
Security News
Astral Launches pyx: A Python-Native Package Registry
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
= RoleModel
RoleModel is the framework agnostic, efficient and declarative way to do (user) roles. Assigned roles will be efficiently stored as a bitmask directly into your model within a configurable attribute.
It works like this:
require 'rubygems' require 'role_model'
class User attr_accessor :roles_mask # just for demo purposes # in real life this would usually be a persistent attribute, # e.g. if your User model is persisted in a SQL-DB add an integer # column named roles_mask to your users table -- just remove/replace # above attr_accessor line with whatever is needed for your # persistence solution
include RoleModel
# if you want to use a different integer attribute to store the
# roles in, set it with roles_attribute :my_roles_attribute,
# :roles_mask is the default name
roles_attribute :roles_mask
# declare the valid roles -- do not change the order if you add more
# roles later, always append them at the end!
roles :admin, :manager, :author
end
u = User.new => #<User ...>
u.roles = [:admin] # ['admin'] works as well => [:admin]
u.roles << :manager => [:admin, :manager]
User.valid_roles => [:admin, :manager, :author]
u.roles # also: u.role_symbols for DeclarativeAuthorization compatibility => [:admin, :manager]
u.has_role? :author # has_role? is also aliased to is? => false
u.has_any_role? :author, :manager # has_any_role? is also aliased to is_any_of? => true
u.has_all_roles? :author, :manager # has_all_roles? is also aliased to is? => false
u.roles_mask => 3
User.mask_for :admin, :author => 5
Once you have included RoleModel, your model is perfectly fit to be used together with a role-based authorization solution, such as http://github.com/ryanb/cancan or http://github.com/stffn/declarative_authorization .
== Installation
gem install role_model
== Reasoning
Whenever I introduce a role-based authorization scheme into a project, the code gets coupled somehow to the available roles. So it usually does not make any sense to have a separate Role model stored within the database. This Role model will contain a predefined set of roles anyhow -- changing that set will need to be reflected within the authorization code. Putting the available roles right into the model that actually uses them, makes things much easier and efficient.
== Note on Patches/Pull Requests
== Credits
RoleModel is an implementation of the Role Based Authorization scheme proposed by Ryan Bates (http://wiki.github.com/ryanb/cancan/role-based-authorization).
== Copyright
Copyright (c) 2010 Martin Rehfeld. See LICENSE for details.
FAQs
Unknown package
We found that role_model demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.