Module: Wright::Util::User Private

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

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Various user utility functions.

Class Method Summary collapse

Class Method Details

.group_to_gid(group) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a group's gid.

Examples:

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

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

Parameters:

  • group (String, Integer)

    the group's name or gid

Returns:

  • (Integer)

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


37
38
39
# File 'lib/wright/util/user.rb', line 37

def self.group_to_gid(group)
  to_id(group, :group)
end

.id_iterator(id_type) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


90
91
92
93
# File 'lib/wright/util/user.rb', line 90

def self.id_iterator(id_type)
  fail ArgumentError unless [:uid, :gid].include?(id_type)
  id_type == :uid ? Etc.method(:passwd) : Etc.method(:group)
end

.next_free_gid(gid_range) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the next free gid in a range.

Examples:

Wright::Util::User.next_free_gid(1...500)
# => 11

Parameters:

  • gid_range (Range)

    the gid range

Returns:

  • (Integer)

    the next free gid

Raises:

  • (RuntimeError)

    if there are no free gids in the range


73
74
75
# File 'lib/wright/util/user.rb', line 73

def self.next_free_gid(gid_range)
  next_free_id(gid_range, :gid)
end

.next_free_id(id_range, id_type) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wright/util/user.rb', line 77

def self.next_free_id(id_range, id_type)
  iterator = id_iterator(id_type)
  used_ids = []
  iterator.call do |o|
    id = o.method(id_type).call
    used_ids << id if id_range.include?(id)
  end
  free_ids = id_range.to_a - used_ids
  fail "No free #{id_type} in range #{id_range}" if free_ids.empty?
  free_ids.min
end

.next_free_uid(uid_range) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the next free uid in a range.

Examples:

Wright::Util::User.next_free_uid(1...500)
# => 2

Parameters:

  • uid_range (Range)

    the uid range

Returns:

  • (Integer)

    the next free uid

Raises:

  • (RuntimeError)

    if there are no free uids in the range


59
60
61
# File 'lib/wright/util/user.rb', line 59

def self.next_free_uid(uid_range)
  next_free_id(uid_range, :uid)
end

.safe_getgrnam(groupname) ⇒ Struct::Group?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a group entry.

Examples:

Wright::Util::User.safe_getgrnam('this_group_does_not_exist')
# => nil

Parameters:

  • groupname (String)

    the name of the group

Returns:

  • (Struct::Group, nil)

    the group entry or nil if the group does not exist


122
123
124
125
126
# File 'lib/wright/util/user.rb', line 122

def self.safe_getgrnam(groupname)
  Etc.getgrnam(groupname)
rescue ArgumentError
  nil
end

.safe_getpwnam(username) ⇒ Struct::Passwd?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a user entry.

Examples:

Wright::Util::User.safe_getpwnam('this_user_does_not_exist')
# => nil

Parameters:

  • username (String)

    the name of the user

Returns:

  • (Struct::Passwd, nil)

    the user entry or nil if the user does not exist


106
107
108
109
110
# File 'lib/wright/util/user.rb', line 106

def self.safe_getpwnam(username)
  Etc.getpwnam(username)
rescue ArgumentError
  nil
end

.to_id(object, type) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


41
42
43
44
45
46
# File 'lib/wright/util/user.rb', line 41

def self.to_id(object, type)
  fail ArgumentError unless [:group, :user].include?(type)
  return nil if object.nil?
  return object.to_i unless object.is_a?(String)
  type == :user ? Etc.getpwnam(object).uid : Etc.getgrnam(object).gid
end

.user_to_uid(user) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a user's uid.

Examples:

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

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

Parameters:

  • user (String, Integer)

    the user's name or uid

Returns:

  • (Integer)

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


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

def self.user_to_uid(user)
  to_id(user, :user)
end