Users and current user

The Gitlab API exposes user-related method that can be manipulated by admins only.

The currently logged-in user is also exposed.

Users

Examples

Get the list of users:

users = gl.users.list()

Search users whose username match the given string:

users = gl.users.list(search='oo')

Get a single user:

# by ID
user = gl.users.get(2)
# by username
user = gl.users.list(username='root')[0]

Create a user:

user = gl.users.create({'email': 'john@doe.com',
                        'password': 's3cur3s3cr3T',
                        'username': 'jdoe',
                        'name': 'John Doe'})

Update a user:

user.name = 'Real Name'
user.save()

Delete a user:

gl.users.delete(2)
user.delete()

Block/Unblock a user:

user.block()
user.unblock()

User custom attributes

Examples

List custom attributes for a user:

attrs = user.customattributes.list()

Get a custom attribute for a user:

attr = user.customattributes.get(attr_key)

Set (create or update) a custom attribute for a user:

attr = user.customattributes.set(attr_key, attr_value)

Delete a custom attribute for a user:

attr.delete()
# or
user.customattributes.delete(attr_key)

Search users by custom attribute:

user.customattributes.set('role': 'QA')
gl.users.list(custom_attributes={'role': 'QA'})

User impersonation tokens

References

List impersonation tokens for a user:

i_t = user.impersonationtokens.list(state='active')
i_t = user.impersonationtokens.list(state='inactive')

Get an impersonation token for a user:

i_t = user.impersonationtokens.get(i_t_id)

Create and use an impersonation token for a user:

i_t = user.impersonationtokens.create({'name': 'token1', 'scopes': ['api']})
# use the token to create a new gitlab connection
user_gl = gitlab.Gitlab(gitlab_url, private_token=i_t.token)

Revoke (delete) an impersonation token for a user:

i_t = user.impersonationtokens.list(state='active')
i_t = user.impersonationtokens.list(state='inactive')

Current User

References

Examples

Get the current user:

gl.auth()
current_user = gl.user

GPG keys

References

You can manipulate GPG keys for the current user and for the other users if you are admin.

Exemples

List GPG keys for a user:

gpgkeys = user.gpgkeys.list()

Get an GPG gpgkey for a user:

gpgkey = user.gpgkeys.get(1)

Create an GPG gpgkey for a user:

# get the key with `gpg --export -a GPG_KEY_ID`
k = user.gpgkeys.create({'key': public_key_content})

Delete an GPG gpgkey for a user:

user.gpgkeys.delete(1)
# or
gpgkey.delete()

SSH keys

References

You can manipulate SSH keys for the current user and for the other users if you are admin.

Exemples

List SSH keys for a user:

keys = user.keys.list()

Get an SSH key for a user:

key = user.keys.get(1)

Create an SSH key for a user:

k = user.keys.create({'title': 'my_key',
                      'key': open('/home/me/.ssh/id_rsa.pub').read()})

Delete an SSH key for a user:

user.keys.delete(1)
# or
key.delete()

Emails

References

You can manipulate emails for the current user and for the other users if you are admin.

Exemples

List emails for a user:

emails = user.emails.list()

Get an email for a user:

email = gl.user_emails.list(1, user_id=1)
# or
email = user.emails.get(1)

Create an email for a user:

k = user.emails.create({'email': 'foo@bar.com'})

Delete an email for a user:

user.emails.delete(1)
# or
email.delete()

Users activities

Examples

Get the users activities:

activities = gl.user_activities.list(all=True, as_list=False)