Module: LinuxStat::User

Defined in:
lib/linux_stat/user.rb

Class Method Summary collapse

Class Method Details

.get_euidObject

Returns the effective user ID as integer It directly calls LinuxStat::Sysconf.get_euid



93
94
95
# File 'lib/linux_stat/user.rb', line 93

def get_euid
	LinuxStat::Sysconf.get_euid
end

.get_gidObject

Returns the group ID as integer It directly calls LinuxStat::Sysconf.get_uid



87
88
89
# File 'lib/linux_stat/user.rb', line 87

def get_gid
	LinuxStat::Sysconf.get_gid
end

.get_uidObject

Returns the user ID as integer It directly calls LinuxStat::Sysconf.get_uid



81
82
83
# File 'lib/linux_stat/user.rb', line 81

def get_uid
	LinuxStat::Sysconf.get_uid
end

.get_userObject

Returns the user ID as integer It directly calls LinuxStat::Sysconf.get_user

It doesn’t get affected with the assignment of USER environment variable.



75
76
77
# File 'lib/linux_stat/user.rb', line 75

def get_user
	LinuxStat::Sysconf.get_user
end

.gid_by_username(username = get_user) ⇒ Object

gid_by_username(username = get_user) Where username is the username to look for, by default it is the current user.

It returns the gid by the username. For example:

LinuxStat::User.gid_by_username('root')
=> "0"

The return type is Integer. But if user passed doesn’t exist or if the info isn’t available, it will return nil.



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/linux_stat/user.rb', line 147

def gid_by_username(username = get_user)
	return nil unless passwd_readable?

	gid = nil
	passwd.each do |x|
		splitted = x.split(?:.freeze)
		if splitted[0] == username
			gid = splitted[3].to_i
			break
		end
	end
	gid
end

.gidsObject

Returns all the Group identifiers as Hash. For example:

LinuxStat::User.gids
=> {:root=>0, :bin=>1, :daemon=>2, :mail=>12, :ftp=>11}

But if the status isn’t available it will return an empty Hash.



49
50
51
52
53
54
55
# File 'lib/linux_stat/user.rb', line 49

def gids
	return {} unless passwd_readable?
	passwd.reduce({}) { |h, x|
		splitted = x.split(?:)
		h.merge!(splitted[0].to_sym => splitted[3].to_i)
	}
end

.home_by_gid(id = get_gid) ⇒ Object

home_by_gid(id = get_uid) Gets the home of the user corresponding to the GID. It returns a String in this format:

Assuming both the users share same UID.

If the info isn’t available, it will return an empty frozen String.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/linux_stat/user.rb', line 231

def home_by_gid(id = get_gid)
	return ''.freeze unless passwd_readable?

	home = ''
	passwd.each do |x|
		splitted = x.split(?:.freeze)

		if splitted[3].to_i == id
			home = splitted[5]
			break
		end
	end
	home
end

.home_by_username(user = get_user) ⇒ Object

home_by_username(user = get_user) Where user is the name of the user. Returns the user’s home. By default it returns the home of the current user.

If the info isn’t available, it will return ENV.to_s.freeze



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/linux_stat/user.rb', line 190

def home_by_username(user = get_user)
	return ENV['HOME'].to_s.freeze unless passwd_readable?

	home = ''
	passwd.each { |x|
		splitted = x.split(?:)
		if splitted[0] == user
			home = splitted[5]
			break
		end
	}
	home
end

.home_directoriesObject

Returns all the home directories as Hash. For example:

LinuxStat::User.home_directories
=> {:root=>"/root", :bin=>"/", :daemon=>"/", :mail=>"/var/spool/mail", :ftp=>"/srv/ftp", :http=>"/srv/http", :nobody=>"/"}

But if the status isn’t available it will return an empty Hash.



63
64
65
66
67
68
69
# File 'lib/linux_stat/user.rb', line 63

def home_directories
	return {} unless passwd_readable?
	passwd.reduce({}) { |h, x|
		splitted = x.split(?:)
		h.merge!(splitted[0].to_sym => splitted[5])
	}
end

.homes_by_uid(id = get_uid) ⇒ Object

home_by_uid(id = get_uid) Gets all the users home directory with user id. It returns an Array in this format:

LinuxStat::User.homes_by_uid(1001)
=> ["/home/userx", "/home/usery"]

Assuming both the users share same UID.

If the info isn’t available, it will return an empty Array.



213
214
215
216
217
218
219
220
221
222
# File 'lib/linux_stat/user.rb', line 213

def homes_by_uid(id = get_uid)
	return [] unless passwd_readable?

	home = []
	passwd.each do |x|
		splitted = x.split(?:.freeze)
		home << splitted[5] if splitted[2].to_i == id
	end
	home
end

.idsObject

Returns all the Group ids directories as Hash. For example:

{:root=>{:uid=>0, :gid=>0}, :bin=>{:uid=>1, :gid=>1}, :daemon=>{:uid=>2, :gid=>2}, :mail=>{:uid=>8, :gid=>12}, :ftp=>{:uid=>14, :gid=>11}}

But if the status isn’t available it will return an empty Hash.



18
19
20
21
22
23
24
25
26
27
# File 'lib/linux_stat/user.rb', line 18

def ids
	return {} unless passwd_readable?
	passwd.reduce({}) { |h, x|
		splitted = x.split(?:)

		h.merge!(splitted[0].to_sym => {
			uid: splitted[2].to_i, gid: splitted[3].to_i
		})
	}
end

.listObject

Returns an array of users as string For example:

["root", "bin", "daemon", "mail", "ftp", "http", "nobody"]

But if the status isn’t available it will return an empty Array.



8
9
10
11
# File 'lib/linux_stat/user.rb', line 8

def list
	return [] unless passwd_readable?
	passwd.map { |x| x[/.+?:/][0..-2].freeze }
end

.uid_by_username(username = get_user) ⇒ Object

uid_by_username(username = get_user) Where username is the username to look for, by default it is the current user.

It returns the uid by the username. For example:

LinuxStat::User.uid_by_username('root')
=> 0

The return type is Integer. But if user passed doesn’t exist or if the info isn’t available, it will return nil.



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/linux_stat/user.rb', line 171

def uid_by_username(username = get_user)
	return nil unless passwd_readable?

	uid = nil
	passwd.each do |x|
		splitted = x.split(?:.freeze)
		if splitted[0] == username
			uid = splitted[2].to_i
			break
		end
	end
	uid
end

.uidsObject

Returns all the user IDs as Hash. For example:

LinuxStat::User.uids
=> {:root=>0, :bin=>1, :daemon=>2, :mail=>8, :ftp=>14}

But if the status isn’t available it will return an empty Hash.



35
36
37
38
39
40
41
# File 'lib/linux_stat/user.rb', line 35

def uids
	return {} unless passwd_readable?
	passwd.reduce({}) { |h, x|
		splitted = x.split(?:)
		h.merge!(splitted[0].to_sym => splitted[2].to_i)
	}
end

.username_by_gid(gid = get_gid) ⇒ Object

def username_by_gid(gid = get_gid) Where gid is the group id of the user. By default it’s the gid of the current user.

It returns a String cotaining the username corresponding to the gid But if the info isn’t available it will return an empty frozen String.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/linux_stat/user.rb', line 123

def username_by_gid(gid = get_gid)
	return ''.freeze unless passwd_readable?

	username = ''
	passwd.each do |x|
		splitted = x.split(?:.freeze)
		if splitted[2].to_i == gid
			username = splitted[0]
			break
		end
	end
	username
end

.usernames_by_uid(uid = get_uid) ⇒ Object

def usernames_by_uid(gid = get_uid) Where uid is the group id of the user. By default it’s the uid of the current user.

It returns an Array containing the username corresponding to the uid.

For example:

LinuxStat::User.usernames_by_uid(1001)
=> ["userx", "usery"]

But if the info isn’t available it will return an empty Array.



107
108
109
110
111
112
113
114
115
116
# File 'lib/linux_stat/user.rb', line 107

def usernames_by_uid(uid = get_uid)
	return [] unless passwd_readable?

	usernames = []
	passwd.each do |x|
		splitted = x.split(?:.freeze)
		usernames << splitted[0] if splitted[2].to_i == uid
	end
	usernames
end