Created
March 26, 2011 15:28
-
-
Save virtualstaticvoid/888366 to your computer and use it in GitHub Desktop.
Simple role based access control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module AccessControl | |
| extend self | |
| def configure(&block) | |
| instance_eval(&block) | |
| end | |
| def definitions | |
| @definitions ||= Hash.new | |
| end | |
| def role(level, options={}) | |
| definitions[level] = Role.new(level, options) | |
| end | |
| def roles_with_permission(permission) | |
| definitions.select { |k,v| v.allows?(permission) }.map { |k,_| k } | |
| end | |
| def [](level) | |
| definitions[level] | |
| end | |
| class Role | |
| def initialize(name, options) | |
| @name = name | |
| @permissions = options[:permissions] | |
| @parent = options[:parent] | |
| end | |
| attr_reader :parent | |
| def permissions | |
| return @permissions unless parent | |
| @permissions + AccessControl[parent].permissions | |
| end | |
| def allows?(permission) | |
| permissions.include?(permission) | |
| end | |
| def to_s | |
| @name | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| AccessControl.configure do | |
| role "basic", | |
| :permissions => [:read_answers, :answer_questions] | |
| role "premium", | |
| :parent => "basic", | |
| :permissions => [:hide_advertisements] | |
| role "manager", | |
| :parent => "premium", | |
| :permissions => [:create_quizzes, :edit_quizzes] | |
| role "owner", | |
| :parent => "manager", | |
| :permissions => [:edit_users, :deactivate_users] | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment