Module: LinuxStat::User
- Defined in:
- lib/linux_stat/user.rb
Class Method Summary collapse
-
.get_euid ⇒ Object
Returns the effective user ID as integer It directly calls LinuxStat::Sysconf.get_euid.
-
.get_gid ⇒ Object
Returns the group ID as integer It directly calls LinuxStat::Sysconf.get_uid.
-
.get_login ⇒ Object
Calls LinuxStat::Sysconf.get_login() The username is returned as a String.
-
.get_uid ⇒ Object
Returns the user ID as integer It directly calls LinuxStat::Sysconf.get_uid.
-
.get_user ⇒ Object
(also: get_current_user)
Returns the user ID as integer It directly calls LinuxStat::Sysconf.get_uid and LinuxStat::Sysconf.get_gid and then reads /etc/passwd and matches the values with UID and GID.
-
.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.
-
.gids ⇒ Object
Returns all the Group identifiers as Hash.
-
.home_by_gid(id = get_gid) ⇒ Object
home_by_gid(id = get_uid) Gets the home of the user corresponding to the GID.
-
.home_by_username(user = get_user) ⇒ Object
home_by_username(user = get_user) Where user is the name of the user.
-
.home_directories ⇒ Object
Returns all the home directories as Hash.
-
.homes_by_uid(id = get_uid) ⇒ Object
home_by_uid(id = get_uid) Gets all the users home directory with user id.
-
.ids ⇒ Object
Returns all the Group ids directories as Hash.
-
.list ⇒ Object
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.
-
.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.
-
.uids ⇒ Object
Returns all the user IDs as Hash.
-
.username_by_gid(gid = get_gid) ⇒ Object
def username_by_gid(gid = get_gid) Where gid is the group id of the user.
-
.usernames_by_uid(uid = get_uid) ⇒ Object
def usernames_by_uid(gid = get_uid) Where uid is the group id of the user.
Class Method Details
.get_euid ⇒ Object
Returns the effective user ID as integer It directly calls LinuxStat::Sysconf.get_euid
113 114 115 |
# File 'lib/linux_stat/user.rb', line 113 def get_euid LinuxStat::Sysconf.get_euid end |
.get_gid ⇒ Object
Returns the group ID as integer It directly calls LinuxStat::Sysconf.get_uid
107 108 109 |
# File 'lib/linux_stat/user.rb', line 107 def get_gid LinuxStat::Sysconf.get_gid end |
.get_login ⇒ Object
Calls LinuxStat::Sysconf.get_login() The username is returned as a String. It doesn’t get affected by ENV
But if the name isn’t available (say inside a container), it will return an empty String. This is meant for speed but not for reliability. To get more reliable output, you might try LinuxStat::User.get_user()
124 125 126 |
# File 'lib/linux_stat/user.rb', line 124 def get_login LinuxStat::Sysconf.get_login end |
.get_uid ⇒ Object
Returns the user ID as integer It directly calls LinuxStat::Sysconf.get_uid
101 102 103 |
# File 'lib/linux_stat/user.rb', line 101 def get_uid LinuxStat::Sysconf.get_uid end |
.get_user ⇒ Object Also known as: get_current_user
Returns the user ID as integer It directly calls LinuxStat::Sysconf.get_uid and LinuxStat::Sysconf.get_gid and then reads /etc/passwd and matches the values with UID and GID.
It doesn’t get affected with the assignment of USER environment variable If either /etc/passwd is readable or LinuxStat::Sysconf.get_login() is not empty.
But if /etc/passwd isn’t readable (which is weird), it will fall back to sysconf.h’s get_login() It that’s not available, like in docker, falls back to ENV.to_s
Note that this is not cached or memoized so use this at your own processing expense. It should return the username under most robust circumstances. But if nothing is available for some reason, it will return an empty String.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/linux_stat/user.rb', line 80 def get_user unless passwd_readable? _l = LinuxStat::Sysconf.get_login().freeze return _l.empty? ? ENV['USER'.freeze].to_s : _l end uid, gid = LinuxStat::Sysconf.get_uid, LinuxStat::Sysconf.get_gid username = '' passwd.each { |x| splitted = x.split(?:).freeze if splitted[2].to_i == uid && splitted[3].to_i == gid username = splitted[0] break end } username 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.
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/linux_stat/user.rb', line 177 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 |
.gids ⇒ Object
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.
46 47 48 49 50 51 |
# File 'lib/linux_stat/user.rb', line 46 def gids return {} unless passwd_readable? passwd_splitted.reduce({}) { |h, x| h.merge!(x[0].to_sym => x[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.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/linux_stat/user.rb', line 261 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
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/linux_stat/user.rb', line 220 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_directories ⇒ Object
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.
59 60 61 62 63 64 65 |
# File 'lib/linux_stat/user.rb', line 59 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.
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/linux_stat/user.rb', line 243 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 |
.ids ⇒ Object
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 |
# File 'lib/linux_stat/user.rb', line 18 def ids return {} unless passwd_readable? passwd_splitted.reduce({}) { |h, x| h.merge!(x[0].to_sym => { uid: x[2].to_i, gid: x[3].to_i }) } end |
.list ⇒ Object
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.
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/linux_stat/user.rb', line 201 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 |
.uids ⇒ Object
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.
33 34 35 36 37 38 |
# File 'lib/linux_stat/user.rb', line 33 def uids return {} unless passwd_readable? passwd_splitted.reduce({}) { |h, x| h.merge!(x[0].to_sym => x[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.
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/linux_stat/user.rb', line 153 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.
138 139 140 141 142 143 144 145 146 |
# File 'lib/linux_stat/user.rb', line 138 def usernames_by_uid(uid = get_uid) return [] unless passwd_readable? usernames = [] passwd_splitted.each { |x| usernames << x[0] if x[2].to_i == uid } usernames end |