-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsecurity.py
More file actions
280 lines (231 loc) · 8.3 KB
/
Copy pathsecurity.py
File metadata and controls
280 lines (231 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from __future__ import annotations
from onegov.agency import AgencyApp
from onegov.agency.collections import ExtendedAgencyCollection
from onegov.agency.models import AgencyMembershipMoveWithinAgency
from onegov.agency.models import AgencyMembershipMoveWithinPerson
from onegov.agency.models import AgencyMove
from onegov.agency.models.ticket import AgencyMutationTicket
from onegov.agency.models.ticket import PersonMutationTicket
from onegov.core.security.roles import (
get_roles_setting as get_roles_setting_base)
from onegov.core.security.rules import has_permission_logged_in
from onegov.people import Agency
from onegov.people import AgencyCollection
from onegov.people import AgencyMembership
from onegov.people import Person
from onegov.ticket.collection import ArchivedTicketCollection
from onegov.ticket.collection import TicketCollection
from onegov.user import RoleMapping
from sqlalchemy.orm import object_session
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from morepath.authentication import NoIdentity
from onegov.core import Identity
from onegov.core.security.roles import Intent
from sqlalchemy.orm import Session
@AgencyApp.replace_setting_section(section='roles')
def get_roles_setting() -> dict[str, set[type[Intent]]]:
# NOTE: Without a supporter role for now
return get_roles_setting_base()
def get_current_role(
session: Session,
identity: Identity | NoIdentity
) -> str | None:
""" Returns the current role of the identity. Elevates the role from member
to editor if any group role mapping with editor role is present.
"""
if identity.userid:
if identity.role == 'member' and identity.groupids:
roles = session.query(RoleMapping).filter(
RoleMapping.role == 'editor',
RoleMapping.group_id.in_(identity.groupids)
)
if session.query(roles.exists()).scalar():
return 'editor'
return identity.role
return None
def has_permission(
app: AgencyApp,
identity: Identity,
model: object,
permission: object
) -> bool:
""" Global permission with elevated roles. """
role = get_current_role(app.session(), identity)
assert role is not None
if permission in getattr(app.settings.roles, role):
return True
return False
def has_model_permission(
app: AgencyApp,
identity: Identity,
model: object,
permission: object
) -> bool:
""" Specific model permission with elevated roles for this model. """
# Check if the identity itself has the role
if permission in getattr(app.settings.roles, identity.role):
return True
# Check the role mappings of the model
if (
identity.role == 'member'
and identity.groupids
and hasattr(model, 'role_mappings')
and permission in getattr(app.settings.roles, 'editor', [])
):
roles = model.role_mappings.filter(
RoleMapping.role == 'editor',
RoleMapping.group_id.in_(identity.groupids)
)
session = object_session(model)
assert session is not None
if session.query(roles.exists()).scalar():
return True
# Check the role mappings of the parent
if parent := getattr(model, 'parent', None):
return has_model_permission(app, identity, parent, permission)
return False
@AgencyApp.permission_rule(model=object, permission=object)
def has_permission_all(
app: AgencyApp,
identity: Identity,
model: object,
permission: object
) -> bool:
if has_permission_logged_in(app, identity, model, permission):
return True
return has_permission(app, identity, model, permission)
@AgencyApp.permission_rule(model=Agency, permission=object)
def has_permission_agency(
app: AgencyApp,
identity: Identity,
model: Agency,
permission: object
) -> bool:
return has_model_permission(app, identity, model, permission)
@AgencyApp.permission_rule(model=AgencyMembership, permission=object)
def has_permission_agency_membership(
app: AgencyApp,
identity: Identity,
model: AgencyMembership,
permission: object
) -> bool:
if has_model_permission(app, identity, model, permission):
return True
if model.agency:
agency = model.agency
return has_model_permission(app, identity, agency, permission)
return False
@AgencyApp.permission_rule(model=Person, permission=object)
def has_permission_person(
app: AgencyApp,
identity: Identity,
model: Person,
permission: object
) -> bool:
if has_model_permission(app, identity, model, permission):
return True
memberships = model.memberships.all()
if not memberships:
if has_permission(app, identity, model, permission):
return True
for membership in memberships:
if membership.agency:
agency = membership.agency
if has_model_permission(app, identity, agency, permission):
return True
return False
@AgencyApp.permission_rule(model=ExtendedAgencyCollection, permission=object)
@AgencyApp.permission_rule(model=AgencyCollection, permission=object)
def has_permission_agency_collection(
app: AgencyApp,
identity: Identity,
model: AgencyCollection,
permission: object
) -> bool:
return has_permission_logged_in(app, identity, model, permission)
@AgencyApp.permission_rule(model=AgencyMove, permission=object)
def has_permission_agency_move(
app: AgencyApp,
identity: Identity,
model: AgencyMove,
permission: object
) -> bool:
if model.subject:
agency = model.subject
if not has_permission_agency(app, identity, agency, permission):
return False
if model.target:
agency = model.target
if not has_permission_agency(app, identity, agency, permission):
return False
return True
@AgencyApp.permission_rule(
model=AgencyMembershipMoveWithinAgency, permission=object
)
def has_permission_agency_membership_move_within_agency(
app: AgencyApp,
identity: Identity,
model: AgencyMembershipMoveWithinAgency,
permission: object
) -> bool:
if model.subject and model.subject.agency:
agency = model.subject.agency
if not has_permission_agency(app, identity, agency, permission):
return False
if model.target and model.target.agency:
agency = model.target.agency
if not has_permission_agency(app, identity, agency, permission):
return False
return True
@AgencyApp.permission_rule(
model=AgencyMembershipMoveWithinPerson, permission=object
)
def has_permission_agency_membership_move_within_person(
app: AgencyApp,
identity: Identity,
model: AgencyMembershipMoveWithinPerson,
permission: object
) -> bool:
if model.subject and model.subject.agency:
agency = model.subject.agency
if not has_permission_agency(app, identity, agency, permission):
return False
if model.target and model.target.agency:
agency = model.target.agency
if not has_permission_agency(app, identity, agency, permission):
return False
return True
@AgencyApp.permission_rule(model=AgencyMutationTicket, permission=object)
def has_permission_agency_mutation_ticket(
app: AgencyApp,
identity: Identity,
model: AgencyMutationTicket,
permission: object
) -> bool:
if model.handler and model.handler.agency:
agency = model.handler.agency
if not has_permission_agency(app, identity, agency, permission):
return False
return True
@AgencyApp.permission_rule(model=PersonMutationTicket, permission=object)
def has_permission_person_mutation_ticket(
app: AgencyApp,
identity: Identity,
model: PersonMutationTicket,
permission: object
) -> bool:
if model.handler and model.handler.person:
person = model.handler.person
if not has_permission_person(app, identity, person, permission):
return False
return True
@AgencyApp.permission_rule(model=TicketCollection, permission=object)
@AgencyApp.permission_rule(model=ArchivedTicketCollection, permission=object)
def has_permission_ticket_collection(
app: AgencyApp,
identity: Identity,
model: TicketCollection | ArchivedTicketCollection,
permission: object
) -> bool:
return has_permission_all(app, identity, model, permission)