forked from Netflix/security_monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
72 lines (58 loc) · 1.99 KB
/
Copy pathmodels.py
File metadata and controls
72 lines (58 loc) · 1.99 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
class RBACRole(object):
"""
This model provides core permission functionality.
"""
roles = {}
def __init__(self, name=None):
self.name = name
if not hasattr(self.__class__, 'parents'):
self.parents = set()
if not hasattr(self.__class__, 'children'):
self.children = set()
RBACRole.roles[name] = self
def add_parent(self, parent):
"""
Add a parent to this role,
and add role itself to the parent's children set.
you should override this function if neccessary.
"""
parent.children.add(self)
self.parents.add(parent)
def add_parents(self, *parents):
"""Add parents to this role. Also should override if neccessary.
Example::
editor_of_articles = RoleMixin('editor_of_articles')
editor_of_photonews = RoleMixin('editor_of_photonews')
editor_of_all = RoleMixin('editor_of_all')
editor_of_all.add_parents(editor_of_articles, editor_of_photonews)
:param parents: Parents to add.
"""
for parent in parents:
self.add_parent(parent)
def get_parents(self):
for parent in self.parents:
yield parent
for grandparent in parent.get_parents():
yield grandparent
def get_children(self):
for child in self.children:
yield child
for grandchild in child.get_children():
yield grandchild
@staticmethod
def get_by_name(name):
"""A static method to return the role which has the input name.
:param name: The name of role.
"""
return RBACRole.roles[name]
class RBACUserMixin(object):
"""
Provides basic role functionality to users.
"""
def get_roles(self):
roles = [RBACRole.roles["anonymous"]]
if self.role:
role = RBACRole.roles[self.role]
if role:
roles.append(role)
return roles