Module: Vigilante::WatchedOperator
- Defined in:
- lib/vigilante/watched_operator.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_authorization(role, extent = nil) ⇒ Object
-
#add_role(role) ⇒ Object
convenience method: needed?.
-
#add_to_extent(extent, role = nil) ⇒ Object
Extent-specific.
- #extent_role(extent) ⇒ Object
- #extent_roles ⇒ Object
- #find_authorization(role, extent = nil) ⇒ Object
- #find_or_create_authorization(role, extent = nil) ⇒ Object
- #has_extent? ⇒ Boolean
- #permits ⇒ Object
-
#permits=(permissions) ⇒ Object
keep permits.
-
#roles ⇒ Object
convenience method: list all assigned roles (without extent?).
-
#roles=(roles_arr) ⇒ Object
an array containing any of existing ability-names will add the corresponding ability.
- #show_roles ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/vigilante/watched_operator.rb', line 4 def self.included(base) base.has_many :authorizations, :foreign_key => 'operator_id', :dependent => :destroy base.has_many :abilities, :through => :authorizations base.accepts_nested_attributes_for :authorizations, :reject_if => proc {|x| x[:ability_id].blank?}, :allow_destroy => true base.attr_accessible :authorizations_attributes end |
Instance Method Details
#add_authorization(role, extent = nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/vigilante/watched_operator.rb', line 12 def (role, extent=nil) ability = Ability.find_by_name(role.downcase) raise StandardError.new("Role #{role} is not converted to a corresponding authorization. It does not exist.") if ability.nil? # extent_params = {} # unless extent.nil? # extent_params[:extent] = extent.id # extent_params[:extent_type] = extent.class.name # end = ::Authorization.create(:operator_id => self.id, :ability_id => ability.id) unless extent.nil? .add_extent(extent) end << end |
#add_role(role) ⇒ Object
convenience method: needed?
62 63 64 |
# File 'lib/vigilante/watched_operator.rb', line 62 def add_role(role) (role) end |
#add_to_extent(extent, role = nil) ⇒ Object
Extent-specific
79 80 81 82 83 |
# File 'lib/vigilante/watched_operator.rb', line 79 def add_to_extent(extent, role = nil) #### TODO: configure default role!!! role = 'can-read-all' if role.nil? (role, extent) end |
#extent_role(extent) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/vigilante/watched_operator.rb', line 85 def extent_role(extent) return nil if extent.nil? self.reload..each do |auth| if auth.has_extent? return auth.ability.name if auth.match_extent(extent) end end nil end |
#extent_roles ⇒ Object
99 100 101 |
# File 'lib/vigilante/watched_operator.rb', line 99 def extent_roles self..select{|x| x.has_extent? }.collect{|x| x.ability.name } end |
#find_authorization(role, extent = nil) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/vigilante/watched_operator.rb', line 30 def (role, extent=nil) .each do |auth| return auth if auth.ability.name.downcase == role.downcase && (auth.match_extent(extent) || extent == :any) end nil end |
#find_or_create_authorization(role, extent = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/vigilante/watched_operator.rb', line 37 def (role, extent = nil) auth = (role, :any) if auth.nil? auth = (role, extent) elsif extent.present? auth.add_extent(extent) end auth end |
#has_extent? ⇒ Boolean
95 96 97 |
# File 'lib/vigilante/watched_operator.rb', line 95 def has_extent? asp_roles.count >= 1 end |
#permits ⇒ Object
112 113 114 |
# File 'lib/vigilante/watched_operator.rb', line 112 def permits ||= end |
#permits=(permissions) ⇒ Object
keep permits
107 108 109 110 |
# File 'lib/vigilante/watched_operator.rb', line 107 def permits=() # make a copy! = {}.merge() end |
#roles ⇒ Object
convenience method: list all assigned roles (without extent?)
67 68 69 70 71 |
# File 'lib/vigilante/watched_operator.rb', line 67 def roles # we can't use abilities as those are not defined when creating a new operator that is not yet saved #result = abilities.collect(&:name) .collect{|auth| auth.ability.try(:name)} end |
#roles=(roles_arr) ⇒ Object
an array containing any of existing ability-names
will add the corresponding ability
54 55 56 57 58 59 |
# File 'lib/vigilante/watched_operator.rb', line 54 def roles=(roles_arr) self. = [] roles_arr.each do |role| (role) unless role.blank? end end |
#show_roles ⇒ Object
73 74 75 |
# File 'lib/vigilante/watched_operator.rb', line 73 def show_roles .collect {|a| a.ability.try(:name) + "[" + a..collect{|e| e.extent}.join(',') + "]"} end |