Class: Cfruby::Users::UserManager

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Method Details

#add_group(group, gid = nil) ⇒ Object

Adds a group to the system with an optional fixed gid



96
97
# File 'lib/libcfruby/users.rb', line 96

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)



91
92
# File 'lib/libcfruby/users.rb', line 91

def add_user(user, password=nil, uid=nil)
end

#add_user_to_group(username, groupname) ⇒ Object

Adds username to groupname



101
102
# File 'lib/libcfruby/users.rb', line 101

def add_user_to_group(user, group)
end

#delete_group(group) ⇒ Object

Deletes group from the system



121
122
# File 'lib/libcfruby/users.rb', line 121

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



116
117
# File 'lib/libcfruby/users.rb', line 116

def delete_user(username, removehome=false)
end

#delete_user_from_group(username, groupname) ⇒ Object

Deletes username from groupname



138
139
140
141
142
143
144
145
146
# File 'lib/libcfruby/users.rb', line 138

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



193
194
195
196
197
198
199
200
# File 'lib/libcfruby/users.rb', line 193

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



184
185
186
187
188
189
# File 'lib/libcfruby/users.rb', line 184

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



176
177
178
179
180
181
# File 'lib/libcfruby/users.rb', line 176

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



167
168
169
170
171
172
173
# File 'lib/libcfruby/users.rb', line 167

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

Returns:

  • (Boolean)


162
163
# File 'lib/libcfruby/users.rb', line 162

def group?(group)
end

#groupsObject

Returns a GroupList of all the groups on the system



209
210
# File 'lib/libcfruby/users.rb', line 209

def groups()
end

#remove_user_from_group(user, group) ⇒ Object

Removes user from group



106
107
# File 'lib/libcfruby/users.rb', line 106

def remove_user_from_group(user, group)
end

#set_groups(user, grouplist) ⇒ Object

Ensures that user belongs to exactly the given grouplist



111
112
# File 'lib/libcfruby/users.rb', line 111

def set_groups(user, grouplist)
end

#set_password(user, newpassword) ⇒ Object

Sets the password of user to newpassword



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/libcfruby/users.rb', line 214

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

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
# File 'lib/libcfruby/users.rb', line 150

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

#usersObject

Returns a UserList of all the users on the system



204
205
# File 'lib/libcfruby/users.rb', line 204

def users()
end