Class: CanDo::User

Inherits:
Object
  • Object
show all
Defined in:
lib/models/user.rb

Instance Method Summary collapse

Instance Method Details

#assign_roles(roles) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/models/user.rb', line 15

def assign_roles(roles)
  self.class.db.transaction do
    self.remove_all_roles
    roles.each do |r|
      begin
        role = r.is_a?(CanDo::Role) ? r : CanDo::Role.where(:id => r).first!
        self.add_role(role)
      rescue Sequel::UniqueConstraintViolation => e
        puts "user already has role '#{r}'"
      rescue Sequel::NoMatchingRow
        raise Role::UndefinedRole.new("Role '#{r}' does not exist")
      end
    end
  end
  self
end

#can(capability) ⇒ Object



11
12
13
# File 'lib/models/user.rb', line 11

def can(capability)
  capabilities.include?(capability.to_s)
end

#capabilitiesObject



7
8
9
# File 'lib/models/user.rb', line 7

def capabilities
  roles.inject([]){|a,role| a << role.capabilities}.flatten.uniq.map(&:id)
end

#role_namesObject



32
33
34
# File 'lib/models/user.rb', line 32

def role_names
  roles.map(&:id)
end

#to_sObject



36
37
38
# File 'lib/models/user.rb', line 36

def to_s
  "#{id}\t#{role_names.join(",")}\t#{capabilities.join(",")}"
end