Class: Kraut::Principal

Inherits:
Object
  • Object
show all
Includes:
Mapper
Defined in:
lib/kraut/principal.rb

Overview

Kraut::Principal

Represents a principal registered with Crowd.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mapper

#initialize, #mass_assign!

Instance Attribute Details

#attributesObject

Returns a Hash of attributes for the principal.



49
50
51
# File 'lib/kraut/principal.rb', line 49

def attributes
  @attributes ||= find_attributes
end

#groupsObject



61
62
63
# File 'lib/kraut/principal.rb', line 61

def groups
  @groups ||= {}
end

#nameObject

Returns the value of attribute name.



30
31
32
# File 'lib/kraut/principal.rb', line 30

def name
  @name
end

#passwordObject

Returns the value of attribute password.



30
31
32
# File 'lib/kraut/principal.rb', line 30

def password
  @password
end

#tokenObject

Returns the value of attribute token.



30
31
32
# File 'lib/kraut/principal.rb', line 30

def token
  @token
end

Class Method Details

.authenticate(name, password) ⇒ Object

Expects a name and password and returns a new authenticated Kraut::Principal.



14
15
16
17
18
19
20
21
# File 'lib/kraut/principal.rb', line 14

def self.authenticate(name, password)
  response = Client.auth_request :authenticate_principal, :in1 => {
    "aut:application" => Application.name,
    "aut:credential" => { "aut:credential" => password }, "aut:name" => name
  }
  
  new :name => name.strip, :password => password, :token => response[:out].to_s
end

.find_by_token(token) ⇒ Object



23
24
25
26
27
28
# File 'lib/kraut/principal.rb', line 23

def self.find_by_token(token)
  response = Client.auth_request :find_principal_by_token, :in1 => token.to_s
  
  # assumption: this works without failure since the auth_request raises an error if the request was not successful!
  new :name => response[:out][:name].to_s, :token => token.to_s
end

Instance Method Details

#display_nameObject

Returns the principal name to display.



33
34
35
# File 'lib/kraut/principal.rb', line 33

def display_name
  attributes[:display_name]
end

#emailObject

Returns the principal’s email address.



38
39
40
# File 'lib/kraut/principal.rb', line 38

def email
  attributes[:mail]
end

#member_of?(group) ⇒ Boolean

Returns whether the principal is a member of a given group.

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/kraut/principal.rb', line 56

def member_of?(group)
  return groups[group] unless groups[group].nil?
  groups[group] = Client.auth_request(:is_group_member, :in1 => group, :in2 => name)[:out]
end

#requires_password_change?Boolean

Returns whether the principal’s password is expired. Principals with an expired password are still able to authenticate and access your application if you do not use this method.

Returns:

  • (Boolean)


44
45
46
# File 'lib/kraut/principal.rb', line 44

def requires_password_change?
  attributes[:requires_password_change]
end