Module: Wright::Util::User

Defined in:
lib/wright/util/user.rb

Overview

Internal: Various user utility functions.

Class Method Summary collapse

Class Method Details

.group_to_gid(group) ⇒ Object

Internal: Get a group’s gid.

group - The group name or gid.

Examples

Wright::Util::User.group_to_gid('root')
# => 0

Wright::Util::User.group_to_gid(0)
# => 0

Returns the integer gid of the given group or nil if group was nil.



40
41
42
43
# File 'lib/wright/util/user.rb', line 40

def self.group_to_gid(group)
  return nil if group.nil?
  group.is_a?(String) ? Etc.getgrnam(group).gid : group.to_i
end

.owner_to_owner_group(owner) ⇒ Object

Internal: Split a colon-separated owner string into owner and

group.

owner - The owner string

Examples

Wright::Util::User.owner_to_owner_group('foo:bar')
# => ["foo", "bar"]

Wright::Util::User.owner_to_owner_group('foo')
# => ["foo", nil]

Wright::Util::User.owner_to_owner_group(23)
# => [23, nil]

Returns the owner and group. Returns nil if no group was

specified. Non-string owners are returned unmodified.

Raises ArgumentError if the owner string contains more than

one colon.


65
66
67
68
69
70
71
72
# File 'lib/wright/util/user.rb', line 65

def self.owner_to_owner_group(owner)
  group = nil
  return [owner, group] unless owner.is_a?(String)

  fail ArgumentError, "Invalid owner: '#{owner}'" if owner.count(':') > 1
  owner, group = owner.split(':')
  [owner, group]
end

.user_to_uid(user) ⇒ Object

Internal: Get a user’s uid.

user - The user name or uid.

Examples

Wright::Util::User.user_to_uid('root')
# => 0

Wright::Util::User.user_to_uid(0)
# => 0

Returns the integer uid of the given user or nil if user was nil.



21
22
23
24
# File 'lib/wright/util/user.rb', line 21

def self.user_to_uid(user)
  return nil if user.nil?
  user.is_a?(String) ? Etc.getpwnam(user).uid : user.to_i
end