Class: Cfruby::Users::LinuxUserManager
- Inherits:
-
UserManager
- Object
- UserManager
- Cfruby::Users::LinuxUserManager
- Defined in:
- lib/libcfruby/osmodules/linux-generic.rb
Overview
Implementation of the UserManager class for generic linux systems
Instance Method Summary collapse
-
#add_group(group, gid = nil) ⇒ Object
adds a group to the system with an optional fixed uid.
-
#add_user(user, password = nil, uid = nil) ⇒ Object
adds a user to the system with an optional fixed uid.
-
#delete_group(group) ⇒ Object
deletes a group from the system.
-
#delete_user(user, removehome = false) ⇒ Object
deletes a user from the system.
-
#group?(group) ⇒ Boolean
returns true if group exists, false otherwise.
-
#groups ⇒ Object
returns a list of all the groups on the system.
-
#users ⇒ Object
returns a list of all the users on the system.
Methods inherited from UserManager
#add_user_to_group, #delete_user_from_group, #get_gid, #get_group, #get_name, #get_uid, #remove_user_from_group, #set_groups, #set_password, #user?
Instance Method Details
#add_group(group, gid = nil) ⇒ Object
adds a group to the system with an optional fixed uid
123 124 125 126 127 128 129 130 131 |
# File 'lib/libcfruby/osmodules/linux-generic.rb', line 123 def add_group(group, gid=nil) Cfruby.controller.attempt("Adding group \"#{groupname}\"", 'destructive') { if(gid == nil) `/usr/sbin/pw groupadd '#{shellescape(group)}'` else `/usr/sbin/pw groupadd '#{shellescape(group)}' -g #{gid.to_i()}` end } end |
#add_user(user, password = nil, uid = nil) ⇒ Object
adds a user to the system with an optional fixed uid
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/libcfruby/osmodules/linux-generic.rb', line 85 def add_user(user, password=nil, uid=nil) newuser = nil if(!user.respond_to?(:username)) newuser = UserInfo.new() newuser.username = user.to_s if(uid != nil) newuser.uid = uid.to_i() end else newuser = user end Cfruby.controller.attempt("Adding user \"#{newuser.username}\"", 'destructive') { if(uid == nil) `/usr/sbin/useradd #{shellescape(newuser.username)}` else `/usr/sbin/useradd #{shellescape(newuser.username)} -u #{uid.to_i()}` end if(newuser.gid != nil) `/usr/sbin/useradd -D #{shellescape(newuser.username)} -g #{newuser.gid}` end if(newuser.shell != nil) `/usr/sbin/useradd -D #{shellescape(newuser.username)} -s #{newuser.shell}` end if(newuser.homedir != nil) `/usr/sbin/useradd #{shellescape(newuser.username)} -b '#{shellescape(newuser.homedir)}' -m` end # set the password if(password != nil) set_password(newuser.username, password) end } end |
#delete_group(group) ⇒ Object
deletes a group from the system
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/libcfruby/osmodules/linux-generic.rb', line 215 def delete_group(group) groupname = nil if(group.respond_to(:groupname)) groupname = group.groupname else groupname = group end `pw groupdel #{groupname}` end |
#delete_user(user, removehome = false) ⇒ Object
deletes a user from the system
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/libcfruby/osmodules/linux-generic.rb', line 197 def delete_user(user, removehome=false) username = nil if(user.respond_to?(:username)) username = user.username else username = user.to_s end Cfruby.controller.attempt("Removing user \"#{username}\"", 'nonreversible', 'destructive') { if(removehome == true) `/usr/sbin/userdel #{username} -r` else `/usr/sbin/userdel #{username}` end } end |
#group?(group) ⇒ Boolean
returns true if group exists, false otherwise
135 136 137 |
# File 'lib/libcfruby/osmodules/linux-generic.rb', line 135 def group?(group) return(infile(group, '/etc/group')) end |
#groups ⇒ Object
returns a list of all the groups on the system
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/libcfruby/osmodules/linux-generic.rb', line 166 def groups() userlist = users() grouplist = GroupList.new() File.open('/etc/group', File::RDONLY) { |fp| regex = /^([a-zA-Z0-9-]+):[^:]+:([0-9]+):([^:]*)/ fp.each_line() { |line| match = regex.match(line) if(match != nil) group = GroupInfo.new() group.groupname = match[1] group.gid = match[2].to_i() group.members = UserList.new() if(match[3] != nil) users = match[3].split(/,/) users.each() { |username| if(userlist.has_key?(username)) group.members[username] = userlist[username] end } end grouplist[group.groupname] = group end } } return(grouplist) end |
#users ⇒ Object
returns a list of all the users on the system
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/libcfruby/osmodules/linux-generic.rb', line 141 def users() userlist = UserList.new() File.open('/etc/passwd', File::RDONLY) { |fp| regex = /^([a-zA-Z0-9-]+):[^:]+:([0-9]+):([0-9]+):([^:]*):([^:]*):([^:]*)$/ fp.each_line() { |line| match = regex.match(line.chomp) if(match != nil) user = UserInfo.new() user.username = match[1] user.uid = match[2].to_i() user.gid = match[3].to_i() user.fullname = match[4] user.homedir = match[5] user.shell = match[6] userlist[user.username] = user end } } return(userlist) end |