Class: Crowd::Client::User

Inherits:
Object
  • Object
show all
Defined in:
lib/crowd-client/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, attributes = nil) ⇒ User

Returns a new instance of User.



6
7
8
9
10
# File 'lib/crowd-client/user.rb', line 6

def initialize(username, attributes=nil)
  self.username = username
  self.new_record = true
  assign_attributes attributes, true
end

Instance Attribute Details

#new_recordObject

Returns the value of attribute new_record.



4
5
6
# File 'lib/crowd-client/user.rb', line 4

def new_record
  @new_record
end

#usernameObject

Returns the value of attribute username.



4
5
6
# File 'lib/crowd-client/user.rb', line 4

def username
  @username
end

Class Method Details

.create(attributes) ⇒ Object



12
13
14
# File 'lib/crowd-client/user.rb', line 12

def self.create(attributes)
  new('', attributes).tap {|user| user.save }
end

Instance Method Details

#==(other) ⇒ Object



16
17
18
# File 'lib/crowd-client/user.rb', line 16

def ==(other)
  username == (other && other.username)
end

#activeObject



36
37
38
# File 'lib/crowd-client/user.rb', line 36

def active
  user_attributes['active']
end

#authenticate?(password) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/crowd-client/user.rb', line 83

def authenticate?(password)
  response = connection.post('authentication', :value => password) do |request|
    request.params[:username] = username
  end
  response.status == 200
end

#change_password(password) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/crowd-client/user.rb', line 90

def change_password(password)
  response = connection.put('user/password', :value => password) do |request|
    request.params[:username] = username
  end
  raise ::Crowd::Client::Exception::NotFound.new("User '#{user.username}' was not found") if response.status == 404
  raise ::Crowd::Client::Exception::UnknownError.new(response.body) if response.status != 204
end

#destroyObject



75
76
77
78
79
80
81
# File 'lib/crowd-client/user.rb', line 75

def destroy
  response = connection.delete('user') do |request|
    request.params[:username] = username
  end
  raise ::Crowd::Client::Exception::NotFound.new("User '#{user.username}' was not found") if response.status == 404
  raise ::Crowd::Client::Exception::UnknownError.new(response.body) if response.status != 204
end

#display_nameObject



28
29
30
# File 'lib/crowd-client/user.rb', line 28

def display_name
  user_attributes['display-name']
end

#emailObject



32
33
34
# File 'lib/crowd-client/user.rb', line 32

def email
  user_attributes['email']
end

#first_nameObject



20
21
22
# File 'lib/crowd-client/user.rb', line 20

def first_name
  user_attributes['first-name']
end

#groups(reload = false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/crowd-client/user.rb', line 40

def groups(reload=false)
  return @groups if @groups && !reload
  response = connection.get("user/group/nested") do |request|
    request.params[:username] = username
  end
  raise ::Crowd::Client::Exception::NotFound.new("User '#{user.username}' was not found") if response.status == 404
  @groups = response.body['groups'].collect do |group|
    ::Crowd::Client::Group.new(group['name'])
  end
end

#last_nameObject



24
25
26
# File 'lib/crowd-client/user.rb', line 24

def last_name
  user_attributes['last-name']
end

#reload!Object



51
52
53
54
# File 'lib/crowd-client/user.rb', line 51

def reload!
  @groups = nil
  user_attributes(true)
end

#saveObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/crowd-client/user.rb', line 61

def save
  if new_record
    response = connection.post('user', {'name' => username, 'active' => true}.merge(@attributes))
    self.new_record = false
    raise ::Crowd::Client::Exception::UnknownError.new(response.body) if response.status != 201
  else
    response = connection.put('user', {'name' => username}.merge(@attributes)) do |request|
      request.params[:username] = username
    end
    raise ::Crowd::Client::Exception::NotFound.new("User with username '#{username}' was not found.") if response.status == 404
    raise ::Crowd::Client::Exception::UnknownError.new(response.body) if response.status != 204
  end
end

#update_attributes(attributes) ⇒ Object



56
57
58
59
# File 'lib/crowd-client/user.rb', line 56

def update_attributes(attributes)
  assign_attributes attributes
  save
end