Created
July 1, 2009 23:12
-
-
Save ahawkins/139144 to your computer and use it in GitHub Desktop.
This file contains 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 KeepUp | |
module Permissions | |
module UserExtension | |
def self.included(klass) | |
klass.class_eval do | |
has_one :permission, :as => :authorizable | |
has_and_belongs_to_many :roles, :join_table => :user_roles | |
alias :can? :has_permission? | |
after_create :create_new_permission | |
attr_protected :permission_attributes,:role_attributes,:role_ids | |
accepts_nested_attributes_for :permission, :roles | |
end | |
end | |
def plays role | |
role = role.to_s if role.is_a? Symbol | |
r = Role.find_by_name role.downcase | |
raise "#{role} does not exist." if r.nil? | |
roles << r | |
true | |
end | |
def plays? role | |
role = role.to_s if role.is_a? Symbol | |
raise "#{role} does not exist" if Role.find_by_name(role.downcase).nil? | |
roles.find_by_name(role.downcase) != nil | |
end | |
def does_not_play? role | |
!plays? role | |
end | |
def has_permission? perm | |
return true if has_local_permission? perm | |
roles.each do |role| | |
return true if role.has_local_permission? perm | |
end | |
false | |
end | |
private | |
def roles_attributes=(attributes) | |
keys = [] | |
attributes.each_value do |value| | |
keys << value unless value.nil? | |
end | |
self.role_ids = keys | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment