module Gitlab::Client::Users

Defines methods related to users. @see docs.gitlab.com/ce/api/users.html @see docs.gitlab.com/ce/api/session.html

Public Instance Methods

activate_user(user_id) click to toggle source

Activate the specified user. Available only for admin.

@example

Gitlab.activate_user(15)

@param [Integer] user_id The Id of user @return [Boolean] success or not

# File lib/gitlab/client/users.rb, line 147
def activate_user(user_id)
  post("/users/#{user_id}/activate")
end
activities(options = {}) click to toggle source

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

@example

Gitlab.activities

@param [Hash] options A customizable set of options. @option options [Integer] :page The page number. @option options [Integer] :per_page The number of results per page. @option options [String] :from The start date for paginated results. @return [Array<Gitlab::ObjectifiedHash>]

# File lib/gitlab/client/users.rb, line 185
def activities(options = {})
  get('/user/activities', query: options)
end
add_email(email, user_id = nil, skip_confirmation = nil) click to toggle source

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

@example

Gitlab.add_email('email@example.com')
Gitlab.add_email('email@example.com', 2)

@param [String] email Email address @param [Integer] user_id The ID of a user. @param [Boolean] skip_confirmation Skip confirmation and assume e-mail is verified @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 293
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) click to toggle source

Creates a new custom_attribute

@example

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

@param [String] key The custom_attributes key @param [String] value The custom_attributes value @param [Integer] user_id The ID of a user. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 377
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) click to toggle source

Approves the specified user. Available only for admin.

@example

Gitlab.approve_user(15)

@param [Integer] user_id The Id of user @return [Boolean] success or not

# File lib/gitlab/client/users.rb, line 158
def approve_user(user_id)
  post("/users/#{user_id}/approve")
end
block_user(user_id) click to toggle source

Blocks the specified user. Available only for admin.

@example

Gitlab.block_user(15)

@param [Integer] user_id The Id of user @return [Boolean] success or not

# File lib/gitlab/client/users.rb, line 114
def block_user(user_id)
  post("/users/#{user_id}/block")
end
create_personal_access_token(user_id, name, scopes, expires_at = nil) click to toggle source

Create personal access token

@example

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

@param [Integer] user_id The ID of the user. @param [String] name Name of the personal access token. @param [Array<String>] scopes Array of scopes for the impersonation token @param [String] expires_at Date for impersonation token expiration in ISO format. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 458
def create_personal_access_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}/personal_access_tokens", body: body)
end
create_service_account(*args) click to toggle source

Creates a service account. Requires authentication from an admin account.

@example

Gitlab.create_service_account('service_account_6018816a18e515214e0c34c2b33523fc', 'Service account user')

@param [String] name (required) The email of the service account. @param [String] username (required) The username of the service account. @return [Gitlab::ObjectifiedHash] Information about created service account.

# File lib/gitlab/client/users.rb, line 70
def create_service_account(*args)
  raise ArgumentError, 'Missing required parameters' unless args[1]

  body = { name: args[0], username: args[1] }
  post('/service_accounts', body: body)
end
create_ssh_key(title, key, options = {}) click to toggle source

Creates a new SSH key.

@example

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

@param [String] title The title of an SSH key. @param [String] key The SSH key body. @param [Hash] options A customizable set of options. @option options [Integer] :user_id id of the user to associate the key with @return [Gitlab::ObjectifiedHash] Information about created SSH key.

# File lib/gitlab/client/users.rb, line 230
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) click to toggle source

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

@example

Gitlab.create_user('joe@foo.org', 'secret', 'joe', { name: 'Joe Smith' })
or
Gitlab.create_user('joe@foo.org', 'secret', 'joe')

@param [String] email(required) The email of a user. @param [String] password(required) The password of a user. @param [String] username(required) The username of a user. @param [Hash] options A customizable set of options. @option options [String] :name The name of a user. Defaults to email. @option options [String] :skype The skype of a user. @option options [String] :linkedin The linkedin of a user. @option options [String] :twitter The twitter of a user. @option options [Integer] :projects_limit The limit of projects for a user. @return [Gitlab::ObjectifiedHash] Information about created user.

# 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) click to toggle source

Create impersonation token

@example

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

@param [Integer] user_id The ID of the user. @param [String] name Name for impersonation token. @param [Array<String>] scopes Array of scopes for the impersonation token @param [String] expires_at Date for impersonation token expiration in ISO format. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 430
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) click to toggle source

Deactivates the specified user. Available only for admin.

@example

Gitlab.deactivate_user(15)

@param [Integer] user_id The Id of user @return [Boolean] success or not

# File lib/gitlab/client/users.rb, line 136
def deactivate_user(user_id)
  post("/users/#{user_id}/deactivate")
end
delete_email(id, user_id = nil) click to toggle source

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

@example

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

@param [Integer] id Email address ID @param [Integer] user_id The ID of a user. @return [Boolean]

# File lib/gitlab/client/users.rb, line 312
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 = {}) click to toggle source

Deletes an SSH key.

@example

Gitlab.delete_ssh_key(1)

@param [Integer] id The ID of a user’s SSH key. @param [Hash] options A customizable set of options. @option options [Integer] :user_id id of the user to associate the key with @return [Gitlab::ObjectifiedHash] Information about deleted SSH key.

# File lib/gitlab/client/users.rb, line 248
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) click to toggle source

Deletes a user.

@example

Gitlab.delete_user(1)

@param [Integer] id The ID of a user. @return [Gitlab::ObjectifiedHash] Information about deleted user.

# File lib/gitlab/client/users.rb, line 103
def delete_user(user_id)
  delete("/users/#{user_id}")
end
delete_user_custom_attribute(key, user_id) click to toggle source

Delete custom_attribute Will delete a custom_attribute

@example

Gitlab.delete_user_custom_attribute('somekey', 2)

@param [String] key The custom_attribute key to delete @param [Integer] user_id The ID of a user. @return [Boolean]

# File lib/gitlab/client/users.rb, line 391
def delete_user_custom_attribute(key, user_id)
  delete("/users/#{user_id}/custom_attributes/#{key}")
end
disable_two_factor(user_id) click to toggle source

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

@example

Gitlab.disable_two_factor(1)

@param [Integer] id The ID of a user. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 517
def disable_two_factor(user_id)
  patch("/users/#{user_id}/disable_two_factor")
end
edit_user(user_id, options = {}) click to toggle source

Updates a user.

@example

Gitlab.edit_user(15, { email: 'joe.smith@foo.org', projects_limit: 20 })

@param [Integer] id The ID of a user. @param [Hash] options A customizable set of options. @option options [String] :email The email of a user. @option options [String] :password The password of a user. @option options [String] :name The name of a user. Defaults to email. @option options [String] :skype The skype of a user. @option options [String] :linkedin The linkedin of a user. @option options [String] :twitter The twitter of a user. @option options [Integer] :projects_limit The limit of projects for a user. @return [Gitlab::ObjectifiedHash] Information about created user.

# File lib/gitlab/client/users.rb, line 92
def edit_user(user_id, options = {})
  put("/users/#{user_id}", body: options)
end
email(id) click to toggle source

Get a single email.

@example

Gitlab.email(3)

@param [Integer] id The ID of a email. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 278
def email(id)
  get("/user/emails/#{id}")
end
emails(user_id = nil) click to toggle source

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

@example

Gitlab.emails
Gitlab.emails(2)

@param [Integer] user_id The ID of a user. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 266
def emails(user_id = nil)
  url = user_id.to_i.zero? ? '/user/emails' : "/users/#{user_id}/emails"
  get(url)
end
memberships(user_id) click to toggle source

Lists all projects and groups a user is a member of

@example

Gitlab.memberships(2)

@param [Integer] user_id The ID of the user.

# File lib/gitlab/client/users.rb, line 495
def memberships(user_id)
  get("/users/#{user_id}/memberships")
end
revoke_personal_access_token(personal_access_token_id) click to toggle source

Revoke a personal access token

@example

Gitlab.revoke_personal_access_token(1)

@param [Integer] personal_access_token_id ID of the personal access token. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 506
def revoke_personal_access_token(personal_access_token_id)
  delete("/personal_access_tokens/#{personal_access_token_id}")
end
revoke_user_impersonation_token(user_id, impersonation_token_id) click to toggle source

Revoke an impersonation token

@example

Gitlab.revoke_user_impersonation_token(1, 1)

@param [Integer] user_id The ID of the user. @param [Integer] impersonation_token_id ID of the impersonation token. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 485
def revoke_user_impersonation_token(user_id, impersonation_token_id)
  delete("/users/#{user_id}/impersonation_tokens/#{impersonation_token_id}")
end
rotate_personal_access_token(personal_access_token_id, expires_at = nil) click to toggle source

Rotate a personal access token

@example

Gitlab.rotate_personal_access_token(1)

@param [Integer] personal_access_token_id ID of the personal access token. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 471
def rotate_personal_access_token(personal_access_token_id, expires_at = nil)
  body = {}
  body[:expires_at] = expires_at if expires_at
  post("/personal_access_tokens/#{personal_access_token_id}/rotate", body: body)
end
session(email, password) click to toggle source

Creates a new user session.

@example

Gitlab.session('jack@example.com', 'secret12345')

@param [String] email The email of a user. @param [String] password The password of a user. @return [Gitlab::ObjectifiedHash] @note This method doesn’t require private_token to be set.

# File lib/gitlab/client/users.rb, line 171
def session(email, password)
  post('/session', body: { email: email, password: password }, unauthenticated: true)
end
ssh_key(id) click to toggle source

Gets information about SSH key.

@example

Gitlab.ssh_key(1)

@param [Integer] id The ID of a user’s SSH key. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 216
def ssh_key(id)
  get("/user/keys/#{id}")
end
ssh_keys(options = {}) click to toggle source

Gets a list of user’s SSH keys.

@example

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

@param [Hash] options A customizable set of options. @option options [Integer] :page The page number. @option options [Integer] :per_page The number of results per page. @option options [Integer] :user_id The ID of the user to retrieve the keys for. @return [Array<Gitlab::ObjectifiedHash>]

# File lib/gitlab/client/users.rb, line 200
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) click to toggle source

Unblocks the specified user. Available only for admin.

@example

Gitlab.unblock_user(15)

@param [Integer] user_id The Id of user @return [Boolean] success or not

# File lib/gitlab/client/users.rb, line 125
def unblock_user(user_id)
  post("/users/#{user_id}/unblock")
end
user(id = nil) click to toggle source

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

@example

Gitlab.user
Gitlab.user(2)

@param [Integer] id The ID of a user. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 30
def user(id = nil)
  id.to_i.zero? ? get('/user') : get("/users/#{id}")
end
user_by_username(username, options = {}) click to toggle source

Get user by username

@example

Gitlab.user_by_username('gitlab')

@param [String] username A username to get. @param [Hash] options A customizable set of options. @return [Array<Gitlab::ObjectifiedHash>]

# File lib/gitlab/client/users.rb, line 340
def user_by_username(username, options = {})
  options[:username] = username
  get('/users', query: options)
end
user_custom_attribute(key, user_id) click to toggle source

Gets single user custom_attribute.

@example

Gitlab.user_custom_attribute(key, 2)

@param [String] key The custom_attributes key @param [Integer] user_id The ID of a user. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 364
def user_custom_attribute(key, user_id)
  get("/users/#{user_id}/custom_attributes/#{key}")
end
user_custom_attributes(user_id) click to toggle source

Gets user custom_attributes.

@example

Gitlab.user_custom_attributes(2)

@param [Integer] user_id The ID of a user. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 352
def user_custom_attributes(user_id)
  get("/users/#{user_id}/custom_attributes")
end
user_impersonation_token(user_id, impersonation_token_id) click to toggle source

Get impersonation token information

@example

Gitlab.user_impersonation_token(1, 1)

@param [Integer] user_id The ID of the user. @param [Integer] impersonation_token_id ID of the impersonation token. @return [Gitlab::ObjectifiedHash]

# File lib/gitlab/client/users.rb, line 415
def user_impersonation_token(user_id, impersonation_token_id)
  get("/users/#{user_id}/impersonation_tokens/#{impersonation_token_id}")
end
user_impersonation_tokens(user_id) click to toggle source

Get all impersonation tokens for a user

@example

Gitlab.user_impersonation_tokens(1)

@param [Integer] user_id The ID of the user. @param [String] state Filter impersonation tokens by state {} @return [Array<Gitlab::ObjectifiedHash>]

# File lib/gitlab/client/users.rb, line 403
def user_impersonation_tokens(user_id)
  get("/users/#{user_id}/impersonation_tokens")
end
user_personal_access_tokens(user_id) click to toggle source

Get all personal access tokens for a user

@example

Gitlab.user_personal_access_tokens(1)

@param [Integer] user_id The ID of the user. @return [Array<Gitlab::ObjectifiedHash>]

# File lib/gitlab/client/users.rb, line 443
def user_personal_access_tokens(user_id)
  get("/personal_access_tokens?user_id=#{user_id}")
end
users(options = {}) click to toggle source

Gets a list of users.

@example

Gitlab.users

@param [Hash] options A customizable set of options. @option options [Integer] :page The page number. @option options [Integer] :per_page The number of results per page. @return [Array<Gitlab::ObjectifiedHash>]

# File lib/gitlab/client/users.rb, line 17
def users(options = {})
  get('/users', query: options)
end