Class: CFoundry::V1::User

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

Overview

Class for representing a user on a given target (via Client).

Does not guarantee that the user exists; used for both user creation and retrieval, as the attributes are all lazily retrieved. Setting attributes does not perform any requests; use #update! to commit your changes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, client, manifest = nil) ⇒ User

Create a User object.

You’ll usually call Client#user instead



15
16
17
18
19
# File 'lib/cfoundry/v1/user.rb', line 15

def initialize(email, client, manifest = nil)
  @email = email
  @client = client
  @manifest = manifest
end

Instance Attribute Details

#emailObject (readonly)

User email.



9
10
11
# File 'lib/cfoundry/v1/user.rb', line 9

def email
  @email
end

Instance Method Details

#admin?Boolean

Check if the user is an administrator.

Returns:

  • (Boolean)


59
60
61
# File 'lib/cfoundry/v1/user.rb', line 59

def admin?
  manifest[:admin]
end

#change_password!(new, old) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/cfoundry/v1/user.rb', line 74

def change_password!(new, old)
  if @client.base.uaa
    @client.base.uaa.change_password(guid, new, old)
  else
    self.password = new
    update!
  end
end

#create!Object

Create the user on the target.

Call this after setting the various attributes.



40
41
42
# File 'lib/cfoundry/v1/user.rb', line 40

def create!
  @client.base.create_user(@manifest.merge(:email => @email))
end

#delete!Object

Delete the user from the target.



33
34
35
# File 'lib/cfoundry/v1/user.rb', line 33

def delete!
  @client.base.delete_user(@email)
end

#eql?(other) ⇒ Boolean Also known as: ==

Basic equality test by email.

Returns:

  • (Boolean)


27
28
29
# File 'lib/cfoundry/v1/user.rb', line 27

def eql?(other)
  other.is_a?(self.class) && other.email == @email
end

#exists?Boolean

Check if the user exists on the target.

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/cfoundry/v1/user.rb', line 51

def exists?
  @client.base.user(@email)
  true
rescue CFoundry::Denied
  false
end

#guidObject



70
71
72
# File 'lib/cfoundry/v1/user.rb', line 70

def guid
  @guid ||= @client.base.token_data[:user_id]
end

#inspectObject

Show string representing the user.



22
23
24
# File 'lib/cfoundry/v1/user.rb', line 22

def inspect
  "#<User '#@email'>"
end

#password=(str) ⇒ Object

Set the user’s password.

Call #update! after using this.



66
67
68
# File 'lib/cfoundry/v1/user.rb', line 66

def password=(str)
  manifest[:password] = str
end

#update!(what = {}) ⇒ Object

Update user attributes.



45
46
47
48
# File 'lib/cfoundry/v1/user.rb', line 45

def update!(what = {})
  @client.base.update_user(@email, manifest.merge(what))
  @manifest = nil
end