Roles: robustifie

This commit is contained in:
Emmanuel Viennet 2022-03-14 10:55:39 +01:00
parent 72cad15b37
commit 370404341c

View File

@ -270,6 +270,8 @@ class User(UserMixin, db.Model):
"""Add a role to this user.
:param role: Role to add.
"""
if not isinstance(role, Role):
raise ScoValueError("add_role: rôle invalide")
self.user_roles.append(UserRole(user=self, role=role, dept=dept))
def add_roles(self, roles, dept):
@ -281,7 +283,9 @@ class User(UserMixin, db.Model):
def set_roles(self, roles, dept):
"set roles in the given dept"
self.user_roles = [UserRole(user=self, role=r, dept=dept) for r in roles]
self.user_roles = [
UserRole(user=self, role=r, dept=dept) for r in roles if isinstance(r, Role)
]
def get_roles(self):
"iterator on my roles"
@ -292,7 +296,11 @@ class User(UserMixin, db.Model):
"""string repr. of user's roles (with depts)
e.g. "Ens_RT, Ens_Info, Secr_CJ"
"""
return ",".join(f"{r.role.name or ''}_{r.dept or ''}" for r in self.user_roles)
return ",".join(
f"{r.role.name or ''}_{r.dept or ''}"
for r in self.user_roles
if r is not None
)
def is_administrator(self):
"True if i'm an active SuperAdmin"