Class: OsCtld::Commands::User::Create

Inherits:
Logged
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
Defined in:
lib/osctld/commands/user/create.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods inherited from Logged

#base_execute

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, cmd, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, #request_stop, run, run!

Constructor Details

This class inherits a constructor from OsCtld::Commands::Base

Instance Method Details

#execute(u) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/osctld/commands/user/create.rb', line 61

def execute(u)
  manipulate(u) do
    zfs(:create, nil, u.dataset)

    File.chown(0, opts[:ugid], u.userdir)
    File.chmod(0751, u.userdir)

    Dir.mkdir(u.homedir) unless Dir.exist?(u.homedir)
    File.chown(opts[:ugid], opts[:ugid], u.homedir)
    File.chmod(0751, u.homedir)

    u.configure(opts[:ugid], opts[:uid_map], opts[:gid_map])
    DB::Users.add(u)
    UserControl::Supervisor.start_server(u)

    call_cmd!(Commands::User::Register, name: u.name, pool: u.pool.name)
    call_cmd!(Commands::User::SubUGIds)
  end

  ok
end

#findObject



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
52
53
54
55
56
57
58
59
# File 'lib/osctld/commands/user/create.rb', line 12

def find
  pool = DB::Pools.get_or_default(opts[:pool])
  error!('pool not found') unless pool

  rx = /^[a-z0-9_-]{1,29}$/

  if rx !~ opts[:name]
    error!("invalid name, allowed format: #{rx.source}")

  elsif opts[:ugid] < 1
    error!('invalid ugid: must be greater than 0')

  elsif !opts[:uid_map] || opts[:uid_map].empty?
    error!('missing UID map')

  elsif !opts[:gid_map] || opts[:gid_map].empty?
    error!('missing GID map')
  end

  # Check for duplicities
  if u = DB::Users.by_ugid(opts[:ugid])
    error!(
      "ugid #{opts[:ugid]} already taken by user "+
      "#{u.pool.name}:#{u.name}"
    )

  elsif u = DB::Users.by_name(opts[:name])
    error!(
      "name #{opts[:ugid]} already taken by user "+
      "#{u.pool.name}:#{u.name}"
    )
  end

  # Check UID/GID maps
  uid_map = IdMap.load(opts[:uid_map])
  gid_map = IdMap.load(opts[:gid_map])

  if !uid_map.valid?
    error!('UID map is not valid')

  elsif !gid_map.valid?
    error!('GID map is not valid')
  end

  u = User.new(pool, opts[:name], load: false)
  error!('user already exists') if DB::Users.contains?(u.name, pool)
  u
end