api.pyfabapi/fabapi/user_system.py

28 lines
963 B
Python
Raw Normal View History

2022-04-28 20:57:40 +02:00
import capnp
from .schema import usersystem_capnp
from .schema import role_capnp
2022-04-28 17:55:41 +02:00
2022-04-28 20:57:40 +02:00
async def add_user(usersystem, username, password, roles=None):
2022-04-28 17:55:41 +02:00
"""
2022-04-28 20:57:40 +02:00
Add a new user with a given username and password.
2022-04-28 17:55:41 +02:00
2022-04-28 20:57:40 +02:00
If provided a given list of roles is granted to the newly created user.
Returns the new user
"""
2022-04-28 17:55:41 +02:00
2022-04-28 20:57:40 +02:00
new_user_fallible = await usersystem.manage.addUserFallible(username, password).a_wait()
if new_user_fallible.which._str() == "successful":
new_user = new_user_fallible.successful
if roles:
for r in roles:
new_user.admin.addRole(role_capnp.Role(name=r))
return new_user
else:
return new_user_fallible.failed.error
2022-04-28 17:55:41 +02:00
2022-04-28 20:57:40 +02:00
async def del_user(usersystem, username):
user_optional = await usersystem.search.getUserByName(username).a_wait()
if user_optional.which._str() == "just":
user = user_optional.just
await usersystem.manage.removeUser(user).a_wait()