Module: Gitlab::Client::Users

Included in:
Gitlab::Client
Defined in:
lib/gitlab/client/users.rb

Overview

Defines methods related to users.

Instance Method Summary collapse

Instance Method Details

#activate_user(user_id) ⇒ Boolean

Activate the specified user. Available only for admin.

Examples:

Gitlab.activate_user(15)

Parameters:

  • user_id (Integer)

    The Id of user

Returns:

  • (Boolean)

    success or not



131
132
133
# File 'lib/gitlab/client/users.rb', line 131

def activate_user(user_id)
  post("/users/#{user_id}/activate")
end

#activities(options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

Gets a list of user activities (for admin access only).

Examples:

Gitlab.activities

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :page (Integer)

    The page number.

  • :per_page (Integer)

    The number of results per page.

  • :from (String)

    The start date for paginated results.

Returns:



169
170
171
# File 'lib/gitlab/client/users.rb', line 169

def activities(options = {})
  get('/user/activities', query: options)
end

#add_email(email, user_id = nil, skip_confirmation = nil) ⇒ Gitlab::ObjectifiedHash

Creates a new email Will create a new email an authorized user if no user ID passed.

Examples:

Gitlab.add_email('[email protected]')
Gitlab.add_email('[email protected]', 2)

Parameters:

  • email (String)

    Email address

  • user_id (Integer) (defaults to: nil)

    The ID of a user.

  • skip_confirmation (Boolean) (defaults to: nil)

    Skip confirmation and assume e-mail is verified

Returns:



277
278
279
280
281
282
283
284
# File 'lib/gitlab/client/users.rb', line 277

def add_email(email, user_id = nil, skip_confirmation = nil)
  url = user_id.to_i.zero? ? '/user/emails' : "/users/#{user_id}/emails"
  if skip_confirmation.nil?
    post(url, body: { email: email })
  else
    post(url, body: { email: email, skip_confirmation: skip_confirmation })
  end
end

#add_user_custom_attribute(key, value, user_id) ⇒ Gitlab::ObjectifiedHash

Creates a new custom_attribute

Examples:

Gitlab.add_custom_attribute('some_new_key', 'some_new_value', 2)

Parameters:

  • key (String)

    The custom_attributes key

  • value (String)

    The custom_attributes value

  • user_id (Integer)

    The ID of a user.

Returns:



348
349
350
351
# File 'lib/gitlab/client/users.rb', line 348

def add_user_custom_attribute(key, value, user_id)
  url = "/users/#{user_id}/custom_attributes/#{key}"
  put(url, body: { value: value })
end

#approve_user(user_id) ⇒ Boolean

Approves the specified user. Available only for admin.

Examples:

Gitlab.approve_user(15)

Parameters:

  • user_id (Integer)

    The Id of user

Returns:

  • (Boolean)

    success or not



142
143
144
# File 'lib/gitlab/client/users.rb', line 142

def approve_user(user_id)
  post("/users/#{user_id}/approve")
end

#block_user(user_id) ⇒ Boolean

Blocks the specified user. Available only for admin.

Examples:

Gitlab.block_user(15)

Parameters:

  • user_id (Integer)

    The Id of user

Returns:

  • (Boolean)

    success or not



98
99
100
# File 'lib/gitlab/client/users.rb', line 98

def block_user(user_id)
  post("/users/#{user_id}/block")
end

#create_ssh_key(title, key, options = {}) ⇒ Gitlab::ObjectifiedHash

Creates a new SSH key.

Examples:

Gitlab.create_ssh_key('key title', 'key body')

Parameters:

  • title (String)

    The title of an SSH key.

  • key (String)

    The SSH key body.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :user_id (Integer)

    id of the user to associate the key with

Returns:



214
215
216
217
218
219
220
221
# File 'lib/gitlab/client/users.rb', line 214

def create_ssh_key(title, key, options = {})
  user_id = options.delete :user_id
  if user_id.to_i.zero?
    post('/user/keys', body: { title: title, key: key })
  else
    post("/users/#{user_id}/keys", body: { title: title, key: key })
  end
end

#create_user(*args) ⇒ Gitlab::ObjectifiedHash

Creates a new user. Requires authentication from an admin account.

Examples:

Gitlab.create_user('[email protected]', 'secret', 'joe', { name: 'Joe Smith' })
or
Gitlab.create_user('[email protected]', 'secret', 'joe')

Parameters:

  • email(required) (String)

    The email of a user.

  • password(required) (String)

    The password of a user.

  • username(required) (String)

    The username of a user.

  • options (Hash)

    A customizable set of options.

Returns:

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
# File 'lib/gitlab/client/users.rb', line 52

def create_user(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  raise ArgumentError, 'Missing required parameters' unless args[2]

  body = { email: args[0], password: args[1], username: args[2], name: args[0] }
  body.merge!(options)
  post('/users', body: body)
end

#create_user_impersonation_token(user_id, name, scopes, expires_at = nil) ⇒ Gitlab::ObjectifiedHash

Create impersonation token

Examples:

Gitlab.create_user_impersonation_token(2, "token", ["api", "read_user"])
Gitlab.create_user_impersonation_token(2, "token", ["api", "read_user"], "1970-01-01")

Parameters:

  • user_id (Integer)

    The ID of the user.

  • name (String)

    Name for impersonation token.

  • scopes (Array<String>)

    Array of scopes for the impersonation token

  • expires_at (String) (defaults to: nil)

    Date for impersonation token expiration in ISO format.

Returns:



401
402
403
404
405
# File 'lib/gitlab/client/users.rb', line 401

def create_user_impersonation_token(user_id, name, scopes, expires_at = nil)
  body = { name: name, scopes: scopes }
  body[:expires_at] = expires_at if expires_at
  post("/users/#{user_id}/impersonation_tokens", body: body)
end

#deactivate_user(user_id) ⇒ Boolean

Deactivates the specified user. Available only for admin.

Examples:

Gitlab.deactivate_user(15)

Parameters:

  • user_id (Integer)

    The Id of user

Returns:

  • (Boolean)

    success or not



120
121
122
# File 'lib/gitlab/client/users.rb', line 120

def deactivate_user(user_id)
  post("/users/#{user_id}/deactivate")
end

#delete_email(id, user_id = nil) ⇒ Boolean

Delete email Will delete a email an authorized user if no user ID passed.

Examples:

Gitlab.delete_email(2)
Gitlab.delete_email(3, 2)

Parameters:

  • id (Integer)

    Email address ID

  • user_id (Integer) (defaults to: nil)

    The ID of a user.

Returns:

  • (Boolean)


296
297
298
299
# File 'lib/gitlab/client/users.rb', line 296

def delete_email(id, user_id = nil)
  url = user_id.to_i.zero? ? "/user/emails/#{id}" : "/users/#{user_id}/emails/#{id}"
  delete(url)
end

#delete_ssh_key(id, options = {}) ⇒ Gitlab::ObjectifiedHash

Deletes an SSH key.

Examples:

Gitlab.delete_ssh_key(1)

Parameters:

  • id (Integer)

    The ID of a user’s SSH key.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :user_id (Integer)

    id of the user to associate the key with

Returns:



232
233
234
235
236
237
238
239
# File 'lib/gitlab/client/users.rb', line 232

def delete_ssh_key(id, options = {})
  user_id = options.delete :user_id
  if user_id.to_i.zero?
    delete("/user/keys/#{id}")
  else
    delete("/users/#{user_id}/keys/#{id}")
  end
end

#delete_user(user_id) ⇒ Gitlab::ObjectifiedHash

Deletes a user.

Examples:

Gitlab.delete_user(1)

Parameters:

  • id (Integer)

    The ID of a user.

Returns:



87
88
89
# File 'lib/gitlab/client/users.rb', line 87

def delete_user(user_id)
  delete("/users/#{user_id}")
end

#delete_user_custom_attribute(key, user_id) ⇒ Boolean

Delete custom_attribute Will delete a custom_attribute

Examples:

Gitlab.delete_user_custom_attribute('somekey', 2)

Parameters:

  • key (String)

    The custom_attribute key to delete

  • user_id (Integer)

    The ID of a user.

Returns:

  • (Boolean)


362
363
364
# File 'lib/gitlab/client/users.rb', line 362

def delete_user_custom_attribute(key, user_id)
  delete("/users/#{user_id}/custom_attributes/#{key}")
end

#disable_two_factor(user_id) ⇒ Gitlab::ObjectifiedHash

Disables two factor authentication (2FA) for the specified user.

Examples:

Gitlab.disable_two_factor(1)

Parameters:

  • id (Integer)

    The ID of a user.

Returns:



426
427
428
# File 'lib/gitlab/client/users.rb', line 426

def disable_two_factor(user_id)
  patch("/users/#{user_id}/disable_two_factor")
end

#edit_user(user_id, options = {}) ⇒ Gitlab::ObjectifiedHash

Updates a user.

Examples:

Gitlab.edit_user(15, { email: '[email protected]', projects_limit: 20 })

Parameters:

  • id (Integer)

    The ID of a user.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :email (String)

    The email of a user.

  • :password (String)

    The password of a user.

  • :name (String)

    The name of a user. Defaults to email.

  • :skype (String)

    The skype of a user.

  • :linkedin (String)

    The linkedin of a user.

  • :twitter (String)

    The twitter of a user.

  • :projects_limit (Integer)

    The limit of projects for a user.

Returns:



76
77
78
# File 'lib/gitlab/client/users.rb', line 76

def edit_user(user_id, options = {})
  put("/users/#{user_id}", body: options)
end

#email(id) ⇒ Gitlab::ObjectifiedHash

Get a single email.

Examples:

Gitlab.email(3)

Parameters:

  • id (Integer)

    The ID of a email.

Returns:



262
263
264
# File 'lib/gitlab/client/users.rb', line 262

def email(id)
  get("/user/emails/#{id}")
end

#emails(user_id = nil) ⇒ Gitlab::ObjectifiedHash

Gets user emails. Will return emails an authorized user if no user ID passed.

Examples:

Gitlab.emails
Gitlab.emails(2)

Parameters:

  • user_id (Integer) (defaults to: nil)

    The ID of a user.

Returns:



250
251
252
253
# File 'lib/gitlab/client/users.rb', line 250

def emails(user_id = nil)
  url = user_id.to_i.zero? ? '/user/emails' : "/users/#{user_id}/emails"
  get(url)
end

#revoke_user_impersonation_token(user_id, impersonation_token_id) ⇒ Gitlab::ObjectifiedHash

Revoke an impersonation token

Examples:

Gitlab.revoke_user_impersonation_token(1, 1)

Parameters:

  • user_id (Integer)

    The ID of the user.

  • impersonation_token_id (Integer)

    ID of the impersonation token.

Returns:



415
416
417
# File 'lib/gitlab/client/users.rb', line 415

def revoke_user_impersonation_token(user_id, impersonation_token_id)
  delete("/users/#{user_id}/impersonation_tokens/#{impersonation_token_id}")
end

#session(email, password) ⇒ Gitlab::ObjectifiedHash

Note:

This method doesn’t require private_token to be set.

Creates a new user session.

Examples:

Gitlab.session('[email protected]', 'secret12345')

Parameters:

  • email (String)

    The email of a user.

  • password (String)

    The password of a user.

Returns:



155
156
157
# File 'lib/gitlab/client/users.rb', line 155

def session(email, password)
  post('/session', body: { email: email, password: password }, unauthenticated: true)
end

#ssh_key(id) ⇒ Gitlab::ObjectifiedHash

Gets information about SSH key.

Examples:

Gitlab.ssh_key(1)

Parameters:

  • id (Integer)

    The ID of a user’s SSH key.

Returns:



200
201
202
# File 'lib/gitlab/client/users.rb', line 200

def ssh_key(id)
  get("/user/keys/#{id}")
end

#ssh_keys(options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

Gets a list of user’s SSH keys.

Examples:

Gitlab.ssh_keys
Gitlab.ssh_keys({ user_id: 2 })

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :page (Integer)

    The page number.

  • :per_page (Integer)

    The number of results per page.

  • :user_id (Integer)

    The ID of the user to retrieve the keys for.

Returns:



184
185
186
187
188
189
190
191
# File 'lib/gitlab/client/users.rb', line 184

def ssh_keys(options = {})
  user_id = options.delete :user_id
  if user_id.to_i.zero?
    get('/user/keys', query: options)
  else
    get("/users/#{user_id}/keys", query: options)
  end
end

#unblock_user(user_id) ⇒ Boolean

Unblocks the specified user. Available only for admin.

Examples:

Gitlab.unblock_user(15)

Parameters:

  • user_id (Integer)

    The Id of user

Returns:

  • (Boolean)

    success or not



109
110
111
# File 'lib/gitlab/client/users.rb', line 109

def unblock_user(user_id)
  post("/users/#{user_id}/unblock")
end

#user(id = nil) ⇒ Gitlab::ObjectifiedHash

Gets information about a user. Will return information about an authorized user if no ID passed.

Examples:

Gitlab.user
Gitlab.user(2)

Parameters:

  • id (Integer) (defaults to: nil)

    The ID of a user.

Returns:



30
31
32
# File 'lib/gitlab/client/users.rb', line 30

def user(id = nil)
  id.to_i.zero? ? get('/user') : get("/users/#{id}")
end

#user_custom_attribute(key, user_id) ⇒ Gitlab::ObjectifiedHash

Gets single user custom_attribute.

Examples:

Gitlab.user_custom_attribute(key, 2)

Parameters:

  • key (String)

    The custom_attributes key

  • user_id (Integer)

    The ID of a user.

Returns:



335
336
337
# File 'lib/gitlab/client/users.rb', line 335

def user_custom_attribute(key, user_id)
  get("/users/#{user_id}/custom_attributes/#{key}")
end

#user_custom_attributes(user_id) ⇒ Gitlab::ObjectifiedHash

Gets user custom_attributes.

Examples:

Gitlab.user_custom_attributes(2)

Parameters:

  • user_id (Integer)

    The ID of a user.

Returns:



323
324
325
# File 'lib/gitlab/client/users.rb', line 323

def user_custom_attributes(user_id)
  get("/users/#{user_id}/custom_attributes")
end

#user_impersonation_token(user_id, impersonation_token_id) ⇒ Gitlab::ObjectifiedHash

Get impersonation token information

Examples:

Gitlab.user_impersonation_token(1, 1)

Parameters:

  • user_id (Integer)

    The ID of the user.

  • impersonation_token_id (Integer)

    ID of the impersonation token.

Returns:



386
387
388
# File 'lib/gitlab/client/users.rb', line 386

def user_impersonation_token(user_id, impersonation_token_id)
  get("/users/#{user_id}/impersonation_tokens/#{impersonation_token_id}")
end

#user_impersonation_tokens(user_id) ⇒ Array<Gitlab::ObjectifiedHash>

Get all impersonation tokens for a user

Examples:

Gitlab.user_impersonation_tokens(1)

Parameters:

  • user_id (Integer)

    The ID of the user.

  • state (String)

    Filter impersonation tokens by state {}

Returns:



374
375
376
# File 'lib/gitlab/client/users.rb', line 374

def user_impersonation_tokens(user_id)
  get("/users/#{user_id}/impersonation_tokens")
end

#user_search(search, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

Search for groups by name

Examples:

Gitlab.user_search('gitlab')

Parameters:

  • search (String)

    A string to search for in user names and paths.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :per_page (String)

    Number of user to return per page

  • :page (String)

    The page to retrieve

Returns:



311
312
313
314
# File 'lib/gitlab/client/users.rb', line 311

def user_search(search, options = {})
  options[:search] = search
  get('/users', query: options)
end

#users(options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

Gets a list of users.

Examples:

Gitlab.users

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :page (Integer)

    The page number.

  • :per_page (Integer)

    The number of results per page.

Returns:



17
18
19
# File 'lib/gitlab/client/users.rb', line 17

def users(options = {})
  get('/users', query: options)
end