fix(users): Readd `create` flag on set_roles
continuous-integration/woodpecker the build failed Details

This commit is contained in:
Ferdinand Thiessen 2021-12-19 13:06:36 +01:00
parent 25ba4d21aa
commit b8ac6eb462
1 changed files with 16 additions and 2 deletions

View File

@ -91,12 +91,26 @@ def update_user(user):
db.session.commit()
def set_roles(user: User, roles: list[str]):
def set_roles(user: User, roles: list[str], create=False):
"""Set roles of user
Args:
user: User to set roles of
roles: List of role names
create: If set to true, create not existing roles
Raises:
BadRequest if invalid arguments given or not all roles found while `create` is set to false
"""
from roleController import create_role
if not isinstance(roles, list) and any([not isinstance(r, str) for r in roles]):
raise BadRequest("Invalid role name")
fetched = Role.query.filter(Role.name.in_(roles)).all()
if len(fetched) < len(roles):
raise BadRequest("Invalid role name, role not found")
if not create:
raise BadRequest("Invalid role name, role not found")
# Create all new roles
fetched += [create_role(role_name) for role_name in roles if not any([role_name == r.name for r in fetched])]
user.roles_ = fetched