Class: Confluence::User

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

Defined Under Namespace

Classes: LdapPersonNotFound

Constant Summary collapse

DISABLED_SUFFIX =
"(ACCOUNT DISABLED)"
DEFAULT_GROUP =
'confluence-users'
VALID_ATTRS =
[:name, :fullname, :email]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ User

Unrecognized attributes are ignored



14
15
16
17
18
19
20
# File 'lib/confluence/user.rb', line 14

def initialize(attrs = {})
  @new_record = true
  @errors = []
  VALID_ATTRS.each do |attr|
    self.send("#{attr}=", attrs[attr] || attrs[attr.to_s])
  end
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



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

def email
  @email
end

#fullnameObject

Returns the value of attribute fullname.



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

def fullname
  @fullname
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.activeArray<Confluence::User>

Retrieves all users where their accounts are currently enabled.

Returns:



265
266
267
# File 'lib/confluence/user.rb', line 265

def active()
  self.all.reject { |u| u[:fullname].include?("ACCOUNT DISABLED") }
end

.allArray<Confluence::User>

Retrieves all users, both expired and active.

Returns:



284
285
286
# File 'lib/confluence/user.rb', line 284

def all()
  all_names.map { |name| find_by_name(name) }
end

.all_namesArray<String>

Returns a list of all Confluence user names.

in Confluence.

Returns:

  • (Array<String>)

    where each entry is the user’s name



275
276
277
# File 'lib/confluence/user.rb', line 275

def all_names()
  conn.getActiveUsers(true) 
end

.connObject



215
216
217
# File 'lib/confluence/user.rb', line 215

def conn()
  Confluence.conn
end

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


306
307
308
# File 'lib/confluence/user.rb', line 306

def exists?(name)
  conn.hasUser(name)
end

.expiredArray<Confluence::User>

Retrieves all users where their accoutns have been disabled.

Returns:



256
257
258
# File 'lib/confluence/user.rb', line 256

def expired()
  self.all.select { |u| u[:fullname].include?("ACCOUNT DISABLED") }
end

.find_by_name(name) ⇒ Confluence::User?

Finds a given Confluence user by their username.

nil.

Parameters:

  • the (String)

    username.

Returns:



295
296
297
298
299
300
301
302
303
304
# File 'lib/confluence/user.rb', line 295

def find_by_name(name)
  begin
    u = self.new(conn.getUser(name.to_s))
    u.instance_variable_set(:@new_record, false)
    u
  rescue(RuntimeError) => e
    logger.debug(e.message)
    return nil
  end
end

.find_or_create_from_ldap(name) ⇒ Object

Finds an existing Confluence user by their name (which also happens to be their ldap_uid). If they do not exist in Confluence, we look them up in LDAP and then add them to Confluence finally returning the newly created user object.



229
230
231
232
233
# File 'lib/confluence/user.rb', line 229

def find_or_create_from_ldap(name)
  user = find_or_new_from_ldap(name)
  user.save if user.new_record?
  user
end

.find_or_new_from_ldap(name) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/confluence/user.rb', line 235

def find_or_new_from_ldap(name)
  if (u = find_by_name(name))
    return u
  elsif (p = UCB::LDAP::Person.find_by_uid(name)).nil?
    msg = "User not found in LDAP: #{name}"
    logger.debug(msg)
    raise(LdapPersonNotFound, msg)
  else
    self.new({
      :name => p.uid.to_s,
      :fullname => "#{p.first_name} #{p.last_name}",
      :email => p.email || "[email protected]"
    })
  end
end

.loggerObject



219
220
221
# File 'lib/confluence/user.rb', line 219

def logger()
  Confluence.logger
end

.new_from_ldap(ldap_person) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/confluence/user.rb', line 22

def self.new_from_ldap(ldap_person)
  @new_record = true
  @errors = []
  self.new({
    :name => ldap_person.uid,        
    :fullname => "#{ldap_person.first_name} + #{ldap_person.last_name}",
    :email => ldap_person.email || "[email protected]"
  })
end

Instance Method Details

#[](key) ⇒ Object

Lets confluence XML-RPC access this object as if it was a Hash. returns nil if key is not in VALID_ATTRS



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

def [](key)
  self.send(key) if VALID_ATTRS.include?(key.to_sym)
end

#connObject



201
202
203
# File 'lib/confluence/user.rb', line 201

def conn()
  self.class.conn
end

#deletetrue, false

Deletes the user from Confluence.

Returns:

  • (true, false)

    result of whether operation was successful.



156
157
158
159
160
161
162
163
164
165
# File 'lib/confluence/user.rb', line 156

def delete()
  @errors.clear
  conn.removeUser(name.to_s)
  self.freeze
  return true
rescue(RuntimeError) => e
  logger.debug(e.message)
  @errors << e.message
  return false
end

#disabletrue, false

Flags this user as disabled (inactive) and removes them from all groups. Update happens immediately.

false

Returns:

  • (true, false)

    true if the operation was successfull, otherwise



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/confluence/user.rb', line 174

def disable()
  @errors.clear
  if disabled?
    logger.debug("#{self} has already been disabled")
    return true
  end

  groups.each { |grp| leave_group(grp) }
  self.fullname = "#{self.fullname} #{DISABLED_SUFFIX}"
  result = self.save()
  logger.debug("Disabled user: #{self}")
  result
end

#disabled?true, false

Predicate indicating if the current user is disabled (inactive)

Returns:

  • (true, false)


193
194
195
# File 'lib/confluence/user.rb', line 193

def disabled?
  fullname.include?(DISABLED_SUFFIX) && groups.empty?
end

#errorsArray<String>

List of errors associated with this record.

Returns:

  • (Array<String>)


210
211
212
# File 'lib/confluence/user.rb', line 210

def errors()
  @errors ||= []
end

#groupsArray<String>

List of all groups this user has membership in.

Returns:

  • (Array<String>)

    names of all groups.



81
82
83
84
# File 'lib/confluence/user.rb', line 81

def groups()
  return [] if new_record?
  conn.getUserGroups(self.name)
end

#join_group(grp) ⇒ true, false

Gives user membership in a group.

Parameters:

  • the (String)

    name of the group

Returns:

  • (true, false)

    result of whether group membership was successful.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/confluence/user.rb', line 92

def join_group(grp)
  @errors.clear
  unless groups.include?(grp)
    conn.addUserToGroup(self.name, grp)
    logger.debug("User [#{self}] added to group: #{grp}")
    return true
  else
    @errors << "User is already in group: #{grp}"
    return false
  end
rescue(RuntimeError) => e
  logger.debug(e.message)
  @errors << e.message
  return false
end

#leave_group(grp) ⇒ true, false

Removes user from a group.

Parameters:

  • the (String)

    name of the group

Returns:

  • (true, false)

    result of whether removal from group was successful.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/confluence/user.rb', line 114

def leave_group(grp)
  @errors.clear
  if groups.include?(grp)
    conn.removeUserFromGroup(self.name, grp)
    logger.debug("User [#{self}] removed from group: #{grp}")
    return true
  else
    @errors << "User not in group: #{grp}"
    return false
  end
rescue(RuntimeError) => e
  logger.debug(e.message)
  @errors << e.message
  return false
end

#loggerObject



197
198
199
# File 'lib/confluence/user.rb', line 197

def logger()
  self.class.logger
end

#new_record?true, false

Predicate that determines if this [User] record has been persisted.

persisted, evaluates to false if it has not been persisted.

Returns:

  • (true, false)

    evaluates to true if the record has not been



55
56
57
# File 'lib/confluence/user.rb', line 55

def new_record?
  @new_record
end

#savetrue, false

Persists any changes to this user. If the user record is new, a new record is created.

Returns:

  • (true, false)

    result of whether operation was successful.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/confluence/user.rb', line 136

def save()
  @errors.clear
  if new_record?
    conn.addUser(self.to_hash, Confluence.config[:user_default_password])
    @new_record = false
  else
    conn.editUser(self.to_hash)
  end
  return true
rescue(RuntimeError) => e
  logger.debug(e.message)
  @errors << e.message
  return false
end

#to_hashHash<String,String>

Creates a [Hash] representation of this user object.

Examples:

user.to_hash
#=> {"name" => "runner", "fullname" => "Steven Hansen", "[email protected]"}

Returns:

  • (Hash<String,String>)


72
73
74
# File 'lib/confluence/user.rb', line 72

def to_hash()
  {"name" => name, "fullname" => fullname, "email" => email}
end

#to_sObject



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

def to_s()
  "name=#{name}, fullname=#{fullname}, email=#{email}"
end