Class: UnixUser

Inherits:
Object
  • Object
show all
Defined in:
lib/unix_user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UnixUser

Returns a new instance of UnixUser.



46
47
48
49
50
51
52
53
54
# File 'lib/unix_user.rb', line 46

def initialize(options)
  @username = options[:username]
  @password = options[:password]
  @uid = options[:uid].to_i
  @gid = options[:gid].to_i
  @user_info = options[:user_info]
  @home_directory = options[:home_directory]
  @shell = options[:shell]
end

Instance Attribute Details

#gidObject (readonly)

——————————————————# Instance methods ——————————————————#



38
39
40
# File 'lib/unix_user.rb', line 38

def gid
  @gid
end

#home_directoryObject (readonly)

——————————————————# Instance methods ——————————————————#



38
39
40
# File 'lib/unix_user.rb', line 38

def home_directory
  @home_directory
end

#passwordObject (readonly)

——————————————————# Instance methods ——————————————————#



38
39
40
# File 'lib/unix_user.rb', line 38

def password
  @password
end

#shellObject (readonly)

——————————————————# Instance methods ——————————————————#



38
39
40
# File 'lib/unix_user.rb', line 38

def shell
  @shell
end

#uidObject (readonly)

——————————————————# Instance methods ——————————————————#



38
39
40
# File 'lib/unix_user.rb', line 38

def uid
  @uid
end

#user_infoObject (readonly)

——————————————————# Instance methods ——————————————————#



38
39
40
# File 'lib/unix_user.rb', line 38

def 
  @user_info
end

#usernameObject (readonly)

——————————————————# Instance methods ——————————————————#



38
39
40
# File 'lib/unix_user.rb', line 38

def username
  @username
end

Class Method Details

.find(username) ⇒ Object



29
30
31
32
# File 'lib/unix_user.rb', line 29

def self.find(username)
  users = list.select { |user| user.username == username }
  users.size != 0 ? users.first : nil
end

.listObject

——————————————————# Static methods ——————————————————#



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/unix_user.rb', line 9

def self.list
  passwd_file = File.open('/etc/passwd', 'r')
  passwd_data = passwd_file.read
  entries = passwd_data.split "\n"
  list = []
  entries.each do |entry|
    fields = entry.split(':')
    list << UnixUser.new({
                             :username => fields[0],
                             :password => fields[1],
                             :uid => fields[2],
                             :gid => fields[3],
                             :user_info => fields[4],
                             :home_directory => fields[5],
                             :shell => fields[6]
                         })
  end
  list
end

Instance Method Details

#locked?Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/unix_user.rb', line 56

def locked?
  m = /^.*?\s(.)/.match(status)
  m[1] == 'L'
end