Class: Cfruby::Users::UserManager
- Inherits:
-
Object
- Object
- Cfruby::Users::UserManager
- Defined in:
- lib/libcfruby/users.rb
Overview
The UserManager class serves only as an in-code description of the UserManager interface. It should be subclassed on an OS specific basis
Direct Known Subclasses
FreeBSDUserManager, LinuxUserManager, OSXUserManager, OpenBSDUserManager
Instance Method Summary collapse
-
#add_group(group, gid = nil) ⇒ Object
Adds a group to the system with an optional fixed gid.
-
#add_user(user, password = nil, uid = nil) ⇒ Object
Adds a user to the system.
-
#add_user_to_group(username, groupname) ⇒ Object
Adds
username
togroupname
. -
#delete_group(group) ⇒ Object
Deletes
group
from the system. -
#delete_user(username, removehome = false) ⇒ Object
Deletes
username
from the system. -
#delete_user_from_group(username, groupname) ⇒ Object
Deletes
username
fromgroupname
. -
#get_gid(groupname) ⇒ Object
Returns the gid of
groupname
. -
#get_group(gid) ⇒ Object
Returns the groupname of
gid
. -
#get_name(uid) ⇒ Object
Returns the username of
uid
. -
#get_uid(username) ⇒ Object
Returns the uid of
username
. -
#group?(group) ⇒ Boolean
Returns true if
group
exists, false otherwise. -
#groups ⇒ Object
Returns a GroupList of all the groups on the system.
-
#remove_user_from_group(user, group) ⇒ Object
Removes
user
fromgroup
. -
#set_groups(user, grouplist) ⇒ Object
Ensures that
user
belongs to exactly the givengrouplist
. -
#set_password(user, newpassword) ⇒ Object
Sets the password of
user
tonewpassword
. -
#user?(user) ⇒ Boolean
Returns true if
user
exists, false otherwise. -
#users ⇒ Object
Returns a UserList of all the users on the system.
Instance Method Details
#add_group(group, gid = nil) ⇒ Object
Adds a group to the system with an optional fixed gid
145 146 |
# File 'lib/libcfruby/users.rb', line 145 def add_group(group, gid=nil) end |
#add_user(user, password = nil, uid = nil) ⇒ Object
Adds a user to the system. If userinfo is a String then it will add the named user with an optional uid. If userinfo is an actual UserInfo object then uid is ignored and everything except password will be used from UserInfo (or generated if not supplied)
140 141 |
# File 'lib/libcfruby/users.rb', line 140 def add_user(user, password=nil, uid=nil) end |
#add_user_to_group(username, groupname) ⇒ Object
Adds username
to groupname
150 151 |
# File 'lib/libcfruby/users.rb', line 150 def add_user_to_group(user, group) end |
#delete_group(group) ⇒ Object
Deletes group
from the system
170 171 |
# File 'lib/libcfruby/users.rb', line 170 def delete_group(group) end |
#delete_user(username, removehome = false) ⇒ Object
Deletes username
from the system. If removehome
is true the users homedir will also be removed
165 166 |
# File 'lib/libcfruby/users.rb', line 165 def delete_user(username, removehome=false) end |
#delete_user_from_group(username, groupname) ⇒ Object
Deletes username
from groupname
187 188 189 190 191 192 193 194 195 |
# File 'lib/libcfruby/users.rb', line 187 def delete_user_from_group(username, groupname) if !user?(username) raise(NoSuchUserError, "No such user: #{username}") end if !group?(groupname) raise(NoSuchGroupError, "No such user: #{groupname}") end end |
#get_gid(groupname) ⇒ Object
Returns the gid of groupname
242 243 244 245 246 247 248 249 |
# File 'lib/libcfruby/users.rb', line 242 def get_gid(groupname) group = groups()[groupname] if(group == nil) raise(NoSuchGroupError, "Group \"#{groupname}\" could not be found") end return(group.gid) end |
#get_group(gid) ⇒ Object
Returns the groupname of gid
233 234 235 236 237 238 |
# File 'lib/libcfruby/users.rb', line 233 def get_group(gid) groups().each do |group| return group[1].groupname if group[1].gid == gid end nil end |
#get_name(uid) ⇒ Object
Returns the username of uid
225 226 227 228 229 230 |
# File 'lib/libcfruby/users.rb', line 225 def get_name(uid) users().each do |user| return user[1].username if user[1].uid == uid end nil end |
#get_uid(username) ⇒ Object
Returns the uid of username
216 217 218 219 220 221 222 |
# File 'lib/libcfruby/users.rb', line 216 def get_uid(username) user = users()[username] if(user == nil) raise(NoSuchUserError, "User \"#{username}\" could not be found") end return(user.uid) end |
#group?(group) ⇒ Boolean
Returns true if group
exists, false otherwise
211 212 |
# File 'lib/libcfruby/users.rb', line 211 def group?(group) end |
#groups ⇒ Object
Returns a GroupList of all the groups on the system
258 259 |
# File 'lib/libcfruby/users.rb', line 258 def groups() end |
#remove_user_from_group(user, group) ⇒ Object
Removes user
from group
155 156 |
# File 'lib/libcfruby/users.rb', line 155 def remove_user_from_group(user, group) end |
#set_groups(user, grouplist) ⇒ Object
Ensures that user
belongs to exactly the given grouplist
160 161 |
# File 'lib/libcfruby/users.rb', line 160 def set_groups(user, grouplist) end |
#set_password(user, newpassword) ⇒ Object
Sets the password of user
to newpassword
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/libcfruby/users.rb', line 263 def set_password(user, newpassword) # We are going to do this with an expect script instead of in pure ruby... # partly because doing it in pure ruby turns out to be pretty tricky since # ruby doesn't have very good tools for interacting with shell programs, but # also because Expect does, and we like doing things with the right tool Cfruby.controller.attempt("Changing password for \"#{user}\"", 'destructive') { # we must be running as root if(Process.euid() != 0) raise(ChangePasswordError, "Passwords can only be set by root") end # first check for the existence of expect haveexpect = `/usr/bin/env expect -v` if(haveexpect !~ /expect version/i) raise(ChangePasswordError, "Expect binary could not be found") end # create a specialized expect script to change the password # and run it changepass = <<CHANGEPASS #!/usr/bin/env expect spawn passwd #{Cfruby::Exec.shellescape(user)} set password "#{newpassword.gsub(/(")/, "\\\1")}" expect "New password:" send "$password\\r" expect "password:" send "$password\\r" expect eof CHANGEPASS scriptfile = Tempfile.new('cfruby') Cfruby::FileOps.chmod(scriptfile.path, "u+x,go-rwx") scriptfile.print(changepass) scriptfile.close(false) `cp #{scriptfile.path} ./footest` output = Cfruby::Exec.exec(scriptfile.path) } end |
#user?(user) ⇒ Boolean
Returns true if user
exists, false otherwise
199 200 201 202 203 204 205 206 207 |
# File 'lib/libcfruby/users.rb', line 199 def user?(user) if(users()[user] != nil) Cfruby.controller.inform('debug', "\"#{user}\" exists") return(true) else Cfruby.controller.inform('debug', "\"#{user}\" does not exist") return(false) end end |
#users ⇒ Object
Returns a UserList of all the users on the system
253 254 |
# File 'lib/libcfruby/users.rb', line 253 def users() end |