Skip to content
Snippets Groups Projects
Commit 255dd09b authored by Jiří Setnička's avatar Jiří Setnička
Browse files

Rights: Testování práv pro editaci práv

Stanovena hierarchie rolí podle pořadí v rights.roles.
parent cfe24e34
No related branches found
No related tags found
1 merge request!6Práva organizátorů
...@@ -29,6 +29,9 @@ class Role: ...@@ -29,6 +29,9 @@ class Role:
rights: Set[Right] rights: Set[Right]
# Order in this list represents hierarchy for assign_rights right
# (garant could assign role of garant or garant_kraj, but garant_kraj cannot
# assign role garant).
roles: List[Role] = [ roles: List[Role] = [
Role( Role(
role=db.RoleType.garant, role=db.RoleType.garant,
...@@ -87,6 +90,7 @@ roles: List[Role] = [ ...@@ -87,6 +90,7 @@ roles: List[Role] = [
roles_by_type: Dict[db.RoleType, Role] = {r.role: r for r in roles} roles_by_type: Dict[db.RoleType, Role] = {r.role: r for r in roles}
role_order_by_type: Dict[db.RoleType, int] = {r.role: i for (i, r) in enumerate(roles)}
class Rights: class Rights:
...@@ -110,11 +114,15 @@ class Rights: ...@@ -110,11 +114,15 @@ class Rights:
rs = " ".join([r.role.name for r in self.roles]) rs = " ".join([r.role.name for r in self.roles])
return f"Rights(uid={self.user.user_id} roles=<{rs}>)" return f"Rights(uid={self.user.user_id} roles=<{rs}>)"
def get_for(self, place: Optional[db.Place] = None, year: Optional[int] = None, cat: Optional[str] = None, seq: Optional[int] = None): def get_for(
self, place: Optional[db.Place] = None, year: Optional[int] = None,
cat: Optional[str] = None, seq: Optional[int] = None,
min_role: Optional[db.RoleType] = None):
"""Posbírá role a práva, která se vztahují k danému místu (možno i tranzitivně) a soutěži. """Posbírá role a práva, která se vztahují k danému místu (možno i tranzitivně) a soutěži.
Pokud place=None, omezení role na místo se nebere v úvahu. Pokud place=None, omezení role na místo se nebere v úvahu.
Pokud year==None, vyhovují role s libovolným ročníkem; pokud year=0, vyhovují jen ty s neuvedeným ročníkem. Pokud year==None, vyhovují role s libovolným ročníkem; pokud year=0, vyhovují jen ty s neuvedeným ročníkem.
Podobně cat a seq.""" Podobně cat a seq.
Pokud min_role!=None, tak se uvažují jen role, které jsou v hierarchii alespoň na úrovni min_role."""
self.current_place = place self.current_place = place
self.current_roles = [] self.current_roles = []
...@@ -124,7 +132,8 @@ class Rights: ...@@ -124,7 +132,8 @@ class Rights:
if ((at is None or role.place_id == at.place_id) if ((at is None or role.place_id == at.place_id)
and (role.year is None or year is None or role.year == year) and (role.year is None or year is None or role.year == year)
and (role.category is None or cat is None or role.category == cat) and (role.category is None or cat is None or role.category == cat)
and (role.seq is None or seq is None or role.seq == seq)): and (role.seq is None or seq is None or role.seq == seq)
and (min_role is None or role_order_by_type[min_role] >= role_order_by_type[role.role])):
self.current_roles.append(role) self.current_roles.append(role)
r = roles_by_type[role.role] r = roles_by_type[role.role]
self.current_rights |= r.rights self.current_rights |= r.rights
...@@ -172,3 +181,19 @@ class Rights: ...@@ -172,3 +181,19 @@ class Rights:
elif user.is_org: elif user.is_org:
return self.have_right(Right.edit_orgs) return self.have_right(Right.edit_orgs)
return self.have_right(Right.edit_users) return self.have_right(Right.edit_users)
def can_set_role(self, role: db.UserRole):
# We will use get_for but we need to slightly modify its behavior. Can
# set role with * year only if current user has role with * year, thus we
# need to pass year=0 for * year (cat and seq similarly).
print("YYYY", role.role)
self.get_for(
place=role.place,
year=role.year or 0,
cat=role.category or '',
seq=role.seq or 0,
min_role=role.role
)
print(self.current_place)
print(self.current_rights)
return self.have_right(Right.assign_rights)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment