Class: Confluence::User
- Inherits:
-
Object
- Object
- Confluence::User
- 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
-
#email ⇒ Object
Returns the value of attribute email.
-
#fullname ⇒ Object
Returns the value of attribute fullname.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.active ⇒ Array<Confluence::User>
Retrieves all users where their accounts are currently enabled.
-
.all ⇒ Array<Confluence::User>
Retrieves all users, both expired and active.
-
.all_names ⇒ Array<String>
Returns a list of all Confluence user names.
- .conn ⇒ Object
- .exists?(name) ⇒ Boolean
-
.expired ⇒ Array<Confluence::User>
Retrieves all users where their accoutns have been disabled.
-
.find_by_name(name) ⇒ Confluence::User?
Finds a given Confluence user by their username.
-
.find_or_create_from_ldap(name) ⇒ Object
Finds an existing Confluence user by their name (which also happens to be their ldap_uid).
- .find_or_new_from_ldap(name) ⇒ Object
- .logger ⇒ Object
- .new_from_ldap(ldap_person) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
Lets confluence XML-RPC access this object as if it was a Hash.
- #conn ⇒ Object
-
#delete ⇒ true, false
Deletes the user from Confluence.
-
#disable ⇒ true, false
Flags this user as disabled (inactive) and removes them from all groups.
-
#disabled? ⇒ true, false
Predicate indicating if the current user is disabled (inactive).
-
#errors ⇒ Array<String>
List of errors associated with this record.
-
#groups ⇒ Array<String>
List of all groups this user has membership in.
-
#initialize(attrs = {}) ⇒ User
constructor
Unrecognized attributes are ignored.
-
#join_group(grp) ⇒ true, false
Gives user membership in a group.
-
#leave_group(grp) ⇒ true, false
Removes user from a group.
- #logger ⇒ Object
-
#new_record? ⇒ true, false
Predicate that determines if this [User] record has been persisted.
-
#save ⇒ true, false
Persists any changes to this user.
-
#to_hash ⇒ Hash<String,String>
Creates a [Hash] representation of this user object.
- #to_s ⇒ Object
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
#email ⇒ Object
Returns the value of attribute email.
9 10 11 |
# File 'lib/confluence/user.rb', line 9 def email @email end |
#fullname ⇒ Object
Returns the value of attribute fullname.
9 10 11 |
# File 'lib/confluence/user.rb', line 9 def fullname @fullname end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/confluence/user.rb', line 9 def name @name end |
Class Method Details
.active ⇒ Array<Confluence::User>
Retrieves all users where their accounts are currently enabled.
265 266 267 |
# File 'lib/confluence/user.rb', line 265 def active() self.all.reject { |u| u[:fullname].include?("ACCOUNT DISABLED") } end |
.all ⇒ Array<Confluence::User>
Retrieves all users, both expired and active.
284 285 286 |
# File 'lib/confluence/user.rb', line 284 def all() all_names.map { |name| find_by_name(name) } end |
.all_names ⇒ Array<String>
Returns a list of all Confluence user names.
in Confluence.
275 276 277 |
# File 'lib/confluence/user.rb', line 275 def all_names() conn.getActiveUsers(true) end |
.conn ⇒ Object
215 216 217 |
# File 'lib/confluence/user.rb', line 215 def conn() Confluence.conn end |
.exists?(name) ⇒ Boolean
306 307 308 |
# File 'lib/confluence/user.rb', line 306 def exists?(name) conn.hasUser(name) end |
.expired ⇒ Array<Confluence::User>
Retrieves all users where their accoutns have been disabled.
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.
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.) 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 |
.logger ⇒ Object
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 |
#conn ⇒ Object
201 202 203 |
# File 'lib/confluence/user.rb', line 201 def conn() self.class.conn end |
#delete ⇒ true, false
Deletes the user from Confluence.
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.) @errors << e. return false end |
#disable ⇒ true, false
Flags this user as disabled (inactive) and removes them from all groups. Update happens immediately.
false
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)
193 194 195 |
# File 'lib/confluence/user.rb', line 193 def disabled? fullname.include?(DISABLED_SUFFIX) && groups.empty? end |
#errors ⇒ Array<String>
List of errors associated with this record.
210 211 212 |
# File 'lib/confluence/user.rb', line 210 def errors() @errors ||= [] end |
#groups ⇒ Array<String>
List of all groups this user has membership in.
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.
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.) @errors << e. return false end |
#leave_group(grp) ⇒ true, false
Removes user from a group.
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.) @errors << e. return false end |
#logger ⇒ Object
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.
55 56 57 |
# File 'lib/confluence/user.rb', line 55 def new_record? @new_record end |
#save ⇒ true, false
Persists any changes to this user. If the user record is new, a new record is created.
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.) @errors << e. return false end |
#to_hash ⇒ Hash<String,String>
Creates a [Hash] representation of this user object.
72 73 74 |
# File 'lib/confluence/user.rb', line 72 def to_hash() {"name" => name, "fullname" => fullname, "email" => email} end |
#to_s ⇒ Object
59 60 61 |
# File 'lib/confluence/user.rb', line 59 def to_s() "name=#{name}, fullname=#{fullname}, email=#{email}" end |