Class: EnfCli::Cmd::User

Inherits:
EnfThor
  • Object
show all
Defined in:
lib/enfcli/commands/user.rb

Overview

This class handles the commands that maniupulate users and roles

Instance Method Summary collapse

Methods inherited from EnfThor

capture_stdout, command_help, handle_argument_error, help

Instance Method Details

#activate_userObject



302
303
304
305
306
307
308
309
# File 'lib/enfcli/commands/user.rb', line 302

def activate_user
  try_with_rescue_in_session do
    ## call the api
    status = { status: "ACTIVE" }
    EnfApi::UserManager.instance.update_user_status options[:email], status
    say "Activated user!", :green
  end
end

#add_user_roleObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/enfcli/commands/user.rb', line 281

def add_user_role
  try_with_rescue_in_session do
    ## get options
    email = options[:email]
    role = options[:role]
    role = role.upcase if role
    cidr = EnfCli::IPV6Cidr.new(options[:cidr]).to_s

    ## call api
    role_hash = [{ cidr: cidr, role: role }]
    resp = EnfApi::UserManager.instance.add_user_role email, role_hash
    resp_roles = resp[:data]

    ## display response
    display_roles resp_roles
  end
end

#deactivate_userObject



261
262
263
264
265
266
267
268
269
# File 'lib/enfcli/commands/user.rb', line 261

def deactivate_user
  try_with_rescue_in_session do
    ## call the api
    status = { status: "INACTIVE" }
    EnfApi::UserManager.instance.update_user_status options[:email], status

    say "Deactivated user!", :green
  end
end

#delete_inviteObject



131
132
133
134
135
136
137
138
# File 'lib/enfcli/commands/user.rb', line 131

def delete_invite
  try_with_rescue_in_session do
    id = options[:id]
    # call api
    EnfApi::UserManager.instance.delete_invite id
    say "Invite: #{id} successfully deleted", :green
  end
end

#delete_user_rolesObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/enfcli/commands/user.rb', line 242

def delete_user_roles
  try_with_rescue_in_session do
    user_id = options[:email]
    roles = options[:roles]
    roles = roles.upcase if roles
    network = options[:network]

    if roles[0..6] == "NETWORK" && !network
      raise EnfCli::ERROR, "--network option must be included for --roles=#{roles}"
    end

    EnfApi::UserManager.instance.delete_user_roles user_id, roles, network
    say "Role: #{roles} successfully removed from user: #{user_id}", :green
  end
end

#get_user_detailsObject



179
180
181
182
183
184
185
186
187
# File 'lib/enfcli/commands/user.rb', line 179

def get_user_details
  try_with_rescue_in_session do
    # call the api
    data = EnfApi::UserManager.instance.get_user options[:email]
    user = data[:data][0]

    display_user_details user
  end
end

#list_invitesObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/enfcli/commands/user.rb', line 155

def list_invites
  try_with_rescue_in_session do
    # use the domain network of the user
    domain = nil

    # only XAPTUM_ADMIN can specify --domain (but doesn't have to)
    if EnfCli::CTX.instance.xaptum_admin?
      domain = options[:domain] if options[:domain]
    elsif options[:domain]
      say "Warning: Ignoring command option --domain #{options[:domain]}", :yellow
    end

    # call the api
    data = EnfApi::UserManager.instance.list_invites domain
    invites = data[:data]

    display_invites invites
  end
end

#list_user_rolesObject



221
222
223
224
225
226
227
228
229
230
# File 'lib/enfcli/commands/user.rb', line 221

def list_user_roles
  try_with_rescue_in_session do
    # call api
    data = EnfApi::UserManager.instance.list_user_roles options[:email], options[:network]
    roles = data[:data]

    # print roles
    display_roles roles
  end
end

#list_usersObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/enfcli/commands/user.rb', line 195

def list_users
  try_with_rescue_in_session do
    domain = options[:domain]
    network = options[:network]

    ## initalize query param
    query_param = ""
    if domain
      query_param = "?domain=#{domain}"
    elsif network
      query_param = "?network=#{network}"
    end

    # call the api
    data = EnfApi::UserManager.instance.list_users query_param
    users = data[:data]

    display_users users
  end
end

#resend_inviteObject



143
144
145
146
147
148
149
150
# File 'lib/enfcli/commands/user.rb', line 143

def resend_invite
  try_with_rescue_in_session do
    id = options[:id]
    # call api
    EnfApi::UserManager.instance.resend_invite id
    say "Resent invite: #{id}!", :green
  end
end

#send_inviteObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/enfcli/commands/user.rb', line 80

def send_invite
  try_with_rescue_in_session do
    # get params
    name = options[:name].join(" ").gsub(/\A"+(.*?)"+\Z/m, '\1')
    email = options[:email]

    # get correct domain
    domain = EnfCli::CTX.instance.session[:domain]
    raise EnfCli::ERROR, "User not in a valid domain!" unless domain

    # check if admin
    if EnfCli::CTX.instance.xaptum_admin?
      raise EnfCli::ERROR, "--domain is required" unless options[:domain]

      domain = options[:domain]
    elsif options[:domain]
      say "Warning: Ignoring command option --domain #{options[:domain]}", :yellow
    end

    invite_hash = { email: email,
                    full_name: name,
                    domain: domain }

    role = options[:role]
    role = role.upcase if role
    network = options[:network]

    roles_hash = nil

    case role
    when "XAPTUM_ADMIN", "IAM_ADMIN"
      roles_hash = [{ cidr: "::/0", role: role }]
    when "DOMAIN_ADMIN", "DOMAIN_USER", "CAPTIVE_ADMIN"
      roles_hash = [{ cidr: domain, role: role }]
    when "NETWORK_ADMIN", "NETWORK_USER"
      roles_hash = [{ cidr: network, role: role }]
    end

    if roles_hash
      invite_hash[:roles] = roles_hash
    end

    resp_data = EnfApi::UserManager.instance.invite invite_hash
    invite = resp_data[:data]
    display_invites invite
  end
end