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.



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

def attributes
  @attributes ||= find_attributes
end

#groupsObject



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

def groups
  @groups ||= {}
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

Class Method Details

.authenticate(name, password) ⇒ Object

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



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

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, :password => password, :token => response[:out].to_s
end

.find_by_token(token) ⇒ Object



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

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.



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

def display_name
  attributes[:display_name]
end

#emailObject

Returns the principal’s email address.



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

def email
  attributes[:mail]
end

#member_of?(group) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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)


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

def requires_password_change?
  attributes[:requires_password_change]
end