Module: AeUsers::Acts::Permissioned::InstanceMethods

Defined in:
lib/ae_users.rb

Instance Method Summary collapse

Instance Method Details

#grant(grantees, permissions = nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/ae_users.rb', line 198

def grant(grantees, permissions=nil)
  if not grantees.kind_of?(Array)
    grantees = [grantees]
  end

  if not permissions.kind_of?(Array)
    if permissions.nil?
      permissions = self.class.permission_names
    else
      permissions = [permissions]
    end
  end

  grantees.each do |grantee|
    if grantee.kind_of? Role
      permissions.each do |perm|
        if AeUsers.cache_permissions?
          grantee.members.each do |person|
            AeUsers.permission_cache.invalidate(person, self, perm)
          end
        end
        Permission.create :role => grantee, :permission => perm, :permissioned => self
      end
    elsif grantee.kind_of? Person
      permissions.each do |perm|
        if AeUsers.cache_permissions?
          AeUsers.permission_cache.invalidate(grantee, self, perm)
        end
        Permission.create :person => grantee, :permission => perm, :permissioned => self
      end
    end
  end
end

#permitted?(person, permission = nil) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/ae_users.rb', line 175

def permitted?(person, permission=nil)
  person.permitted? self, permission
end

#permitted_people(permission) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ae_users.rb', line 179

def permitted_people(permission)
  grants = permissions.select { |perm| perm.permission == permission }
  people = []
  grants.collect {|grant| grant.grantee}.each do |grantee|
    if grantee.kind_of? Person
      if not people.include? grantee
        people << grantee
      end
    elsif grantee.kind_of? Role
      grantee.people.each do |person|
        if not people.include? person
          people << person
        end
      end
    end
  end
  return people
end

#revoke(grantees, permissions = nil) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/ae_users.rb', line 232

def revoke(grantees, permissions=nil)
  if not grantees.kind_of?(Array)
    grantees = [grantees]
  end

  if not permissions.kind_of?(Array)
    if permissions.nil?
      permissions = self.class.permission_names
    else
      permissions = [permissions]
    end
  end

  grantees.each do |grantee|
    permissions.each do |perm|
      existing = if grantee.kind_of? Role
        if AeUsers.cache_permissions?
          grantee.members.each do |person|
            AeUsers.permission_cache.invalidate(person, self, perm)
          end
        end
        Permission.find_by_role_and_permission_type(grantee, perm)
      elsif grantee.kind_of? Person
        if AeUsers.cache_permissions?
          AeUsers.permission_cache.invalidate(pesron, self, perm)
        end
        Permission.find_by_person_and_permission_type(person, perm)
      end

      if existing
        existing.destroy
      end
    end
  end
end