Class: Sysutil::User
- Inherits:
-
Object
- Object
- Sysutil::User
- Defined in:
- lib/sysutil/user.rb
Class Method Summary collapse
- .add!(name, opts = {}) ⇒ Object
- .add_to_group!(user, groupname, opts = {}) ⇒ Object
- .current_user ⇒ Object
- .delete!(name, opts = {}) ⇒ Object
- .list ⇒ Object
- .list_groups(user) ⇒ Object
- .set_password!(user, password, confirmation) ⇒ Object
Class Method Details
.add!(name, opts = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sysutil/user.rb', line 27 def self.add!(name, opts={}) add_user_command = conditional_sudo + "adduser #{name}" out, err, status = Open3.capture3(add_user_command) if status.success? {success: true, output: out} else {success: false, output: "Error: #{err}"} end end |
.add_to_group!(user, groupname, opts = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/sysutil/user.rb', line 83 def self.add_to_group!(user, groupname, opts={}) flags = opts[:primary] ? '-g' : '-a -G' add_to_group_command = cond_sudo + "usermod #{flags} #{groupname} #{user}" out, err, status = Open3.capture3(add_to_group_command) if status.success? {success: true, output: out} else {success: false, output: "Error: #{err}"} end end |
.current_user ⇒ Object
8 9 10 |
# File 'lib/sysutil/user.rb', line 8 def self.current_user `echo -n $USER` end |
.delete!(name, opts = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sysutil/user.rb', line 61 def self.delete!(name, opts={}) delete_user_command = conditional_sudo + "userdel #{name}" if opts[:force] delete_user_command += ' --force' end if opts[:with_home] delete_user_command += ' --remove' end out, err, status = Open3.capture3(delete_user_command) if status.success? {success: true, output: out} else {success: false, output: "Error: #{err}"} end end |
.list ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sysutil/user.rb', line 12 def self.list list_users_command = 'cut -d: -f1 /etc/passwd' out, err, status = Open3.capture3(list_users_command) if status.success? # Split the output on the newline to make an array of users {success: true, output: out.split("\n")} else {success: false, message: "Error: #{err}"} end end |
.list_groups(user) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sysutil/user.rb', line 98 def self.list_groups(user) list_groups_command = "groups #{user}" out, err, status = Open3.capture3(list_groups_command) if status.success? groups = /(?<=\: )([a-z ]+)/.match(out).to_s.split(' ') {success: true, output: groups} else {success: true, message: "Error: #{err}"} end end |
.set_password!(user, password, confirmation) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sysutil/user.rb', line 41 def self.set_password!(user, password, confirmation) # TODO: Change user password in such a way that we don't need chpasswd set_password_command = conditional_sudo + "echo \"#{user}:#{password}\" | /usr/sbin/chpasswd" puts set_password_command if password == confirmation out, err, status = Open3.capture3(set_password_command) if status.success? {succesS: true, output: out} else {success: false, output: "Error: #{err}"} end else {success: false, output: "Error: Passwords don't match"} end end |