Module: Kanrisuru::Core::User

Extended by:
OsPackage::Define
Defined in:
lib/kanrisuru/core/user.rb,
lib/kanrisuru/core/user/types.rb,
lib/kanrisuru/core/user/parsers/user.rb,
lib/kanrisuru/core/user/parsers/getent.rb,
lib/kanrisuru/core/user/parsers/groups.rb,
lib/kanrisuru/core/user/commands/get_uid.rb,
lib/kanrisuru/core/user/commands/is_user.rb,
lib/kanrisuru/core/user/commands/get_user.rb,
lib/kanrisuru/core/user/commands/create_user.rb,
lib/kanrisuru/core/user/commands/delete_user.rb,
lib/kanrisuru/core/user/commands/update_user.rb

Defined Under Namespace

Modules: Parser Classes: FilePath, User, UserGroup

Instance Method Summary collapse

Methods included from OsPackage::Define

extended, os_define

Instance Method Details

#create_user(user, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kanrisuru/core/user/commands/create_user.rb', line 6

def create_user(user, opts = {})
  group         = opts[:group]
  groups        = opts[:groups]
  shell         = opts[:shell] || '/bin/false'

  command = Kanrisuru::Command.new("useradd #{user}")

  if Kanrisuru::Util.present?(opts[:uid])
    command.append_arg('-u', opts[:uid])
    command.append_flag('-o', opts[:non_unique])
  end

  command.append_flag('-r', opts[:system])
  command.append_arg('-s', shell)
  command.append_arg('-d', opts[:home])

  case opts[:createhome]
  when true
    command.append_flag('-m')
    command.append_arg('-k', opts[:skeleton])
  when false
    command.append_flag('-M')
  end

  if Kanrisuru::Util.present?(group) && group?(group)
    command.append_arg('-g', group)
  elsif group?(user)
    command.append_flag('-N')
  end

  command.append_arg('-G', groups.join(',')) if Kanrisuru::Util.present?(groups)

  command.append_arg('-p', opts[:password])
  command.append_arg('-e', opts[:expires]) ## YYYY-MM-DD

  execute_shell(command)

  Kanrisuru::Result.new(command) do
    get_user(user).data
  end
end

#delete_user(user, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/kanrisuru/core/user/commands/delete_user.rb', line 6

def delete_user(user, opts = {})
  force = opts[:force]

  return false if get_uid(user).failure?

  command = Kanrisuru::Command.new("userdel #{user}")
  command.append_flag('-f', force)

  execute_shell(command)

  Kanrisuru::Result.new(command)
end

#get_uid(user) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/kanrisuru/core/user/commands/get_uid.rb', line 6

def get_uid(user)
  command = Kanrisuru::Command.new("id -u #{user}")

  execute(command)

  Kanrisuru::Result.new(command, &:to_i)
end

#get_user(user) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kanrisuru/core/user/commands/get_user.rb', line 6

def get_user(user)
  command = Kanrisuru::Command.new("id #{user}")
  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    if Kanrisuru::Util.numeric?(user)
      uid = user.to_i
      user = Parser::User.parse(cmd)
    else
      ## Get user id
      result = get_uid(user)
      break if result.failure?

      uid = result.to_i
    end

    ## Get all groups for the user, with gid and group name
    array = Parser::Groups.parse(cmd)

    groups = array.map do |str|
      gid  = str.delete('^0-9').to_i
      name = str.delete('0-9')
      UserGroup.new(gid, name)
    end

    ## Get home / shell path information
    command2 = Kanrisuru::Command.new("getent passwd #{user}")
    command2 | "awk -F: '{print $6, $7}'"

    execute(command2)

    result = Kanrisuru::Result.new(command2) do |cmd2|
      Parser::Getent.parse(cmd2)
    end

    ## TODO: Raise custom error to change parent result to use nested error and mark
    ## as failure.
    break if result.failure?

    home = result[0]
    shell = result[1]

    User.new(uid, user, home, shell, groups)
  end
end

#update_user(user, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kanrisuru/core/user/commands/update_user.rb', line 6

def update_user(user, opts = {})
  group         = opts[:group]
  groups        = opts[:groups]

  command = Kanrisuru::Command.new("usermod #{user}")

  if Kanrisuru::Util.present?(opts[:home])
    command.append_arg('-d', opts[:home])
    command.append_flag('-m', opts[:move_home])
  end

  command.append_arg('-s', opts[:shell])

  if Kanrisuru::Util.present?(opts[:uid])
    command.append_arg('-u', opts[:uid])
    command.append_flag('-o', opts[:non_unique])
  end

  command.append_arg('-g', group) if Kanrisuru::Util.present?(group) && group?(group)

  if Kanrisuru::Util.present?(groups)
    command.append_arg('-G', Kanrisuru::Util.array_join_string(groups, ','))
    command.append_flag('-a', opts[:append])
  end

  case opts[:locked]
  when true
    command.append_flag('-L')
    command.append_arg('-e', 1)
  when false
    command.append_flag('-U')
    command.append_arg('-e', 99_999)
  else
    ## Ensure expires isn't added twice.
    command.append_arg('-e', opts[:expires]) ## YYYY-MM-DD

    ## Can't use password with lock / unlock flag.
    command.append_arg('-p', opts[:password])
  end

  execute_shell(command)

  Kanrisuru::Result.new(command) do |_command|
    get_user(user).data
  end
end

#user?(user) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
# File 'lib/kanrisuru/core/user/commands/is_user.rb', line 6

def user?(user)
  result = get_uid(user)
  return false if result.failure?

  Kanrisuru::Util.present?(result.data) && result.data.instance_of?(Integer)
end