|
1 | 1 | Users and Permissions |
2 | 2 | --------------------- |
3 | 3 |
|
4 | | -Python-arango provides operations for managing users and permissions. Most of |
| 4 | +ArangoDB provides operations for managing users and permissions. Most of |
5 | 5 | these operations can only be performed by admin users via ``_system`` database. |
| 6 | + |
| 7 | +.. code-block:: python |
| 8 | +
|
| 9 | + from arangoasync import ArangoClient |
| 10 | + from arangoasync.auth import Auth |
| 11 | + from arangoasync.typings import UserInfo |
| 12 | +
|
| 13 | + # Initialize the client for ArangoDB. |
| 14 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 15 | + auth = Auth(username="root", password="passwd") |
| 16 | +
|
| 17 | + # Connect to "_system" database as root user. |
| 18 | + sys_db = await client.db("_system", auth=auth) |
| 19 | +
|
| 20 | + # List all users. |
| 21 | + users = await sys_db.users() |
| 22 | +
|
| 23 | + johndoe = UserInfo( |
| 24 | + user="johndoe@gmail.com", |
| 25 | + password="first_password", |
| 26 | + active=True, |
| 27 | + extra={"team": "backend", "title": "engineer"} |
| 28 | + ) |
| 29 | +
|
| 30 | + # Create a new user. |
| 31 | + await sys_db.create_user(johndoe) |
| 32 | +
|
| 33 | + # Check if a user exists. |
| 34 | + assert await sys_db.has_user(johndoe.user) is True |
| 35 | + assert await sys_db.has_user("johndoe@gmail.com") is True |
| 36 | +
|
| 37 | + # Retrieve details of a user. |
| 38 | + user_info = await sys_db.user(johndoe.user) |
| 39 | + assert user_info.user == "johndoe@gmail.com" |
| 40 | +
|
| 41 | + # Update an existing user. |
| 42 | + johndoe["password"] = "second_password" |
| 43 | + await sys_db.update_user(johndoe) |
| 44 | +
|
| 45 | + # Replace an existing user. |
| 46 | + johndoe["password"] = "third_password" |
| 47 | + await sys_db.replace_user(johndoe) |
| 48 | +
|
| 49 | + # Retrieve user permissions for all databases and collections. |
| 50 | + await sys_db.permissions(johndoe.user) |
| 51 | +
|
| 52 | + # Retrieve user permission for "test" database. |
| 53 | + perm = await sys_db.permission( |
| 54 | + username="johndoe@gmail.com", |
| 55 | + database="test" |
| 56 | + ) |
| 57 | +
|
| 58 | + # Retrieve user permission for "students" collection in "test" database. |
| 59 | + perm = await sys_db.permission( |
| 60 | + username="johndoe@gmail.com", |
| 61 | + database="test", |
| 62 | + collection="students" |
| 63 | + ) |
| 64 | +
|
| 65 | + # Update user permission for "test" database. |
| 66 | + await sys_db.update_permission( |
| 67 | + username="johndoe@gmail.com", |
| 68 | + permission="rw", |
| 69 | + database="test" |
| 70 | + ) |
| 71 | +
|
| 72 | + # Update user permission for "students" collection in "test" database. |
| 73 | + await sys_db.update_permission( |
| 74 | + username="johndoe@gmail.com", |
| 75 | + permission="ro", |
| 76 | + database="test", |
| 77 | + collection="students" |
| 78 | + ) |
| 79 | +
|
| 80 | + # Reset user permission for "test" database. |
| 81 | + await sys_db.reset_permission( |
| 82 | + username="johndoe@gmail.com", |
| 83 | + database="test" |
| 84 | + ) |
| 85 | +
|
| 86 | + # Reset user permission for "students" collection in "test" database. |
| 87 | + await sys_db.reset_permission( |
| 88 | + username="johndoe@gmail.com", |
| 89 | + database="test", |
| 90 | + collection="students" |
| 91 | + ) |
| 92 | +
|
| 93 | +See :class:`arangoasync.database.StandardDatabase` for API specification. |
0 commit comments