b66c35eea2146f89d16ba46086d40d88825e6a9b
[sfa.git] / util / rights.py
1 # rights.py
2 #
3 # support for privileges according to GENI specification
4
5 # privilege_table:
6 #
7 # a list of priviliges and what operations are allowed per privilege
8
9 privilege_table = {"authority": ["*"],
10                    "refresh": ["remove", "update"],
11                    "resolve": ["resolve", "list", "getcredential"],
12                    "sa": ["*"],
13                    "embed": ["getticket", "createslice", "deleteslice", "updateslice"],
14                    "bind": ["getticket", "loanresources"],
15                    "control": ["updateslice", "stopslice", "startslice", "deleteslice"],
16                    "info": ["listslices", "listcomponentresources", "getsliceresources"],
17                    "ma": ["*"]}
18
19 # a "Right" is a single privilege.
20
21 class Right:
22    def __init__(self, kind):
23       self.kind = kind
24
25    def can_perform(self, op_name):
26       allowed_ops = privilege_table.get(self.kind.lower(), None)
27       if not allowed_ops:
28          return False
29
30       # if "*" is specified, then all ops are permitted
31       if "*" in allowed_ops:
32          return True
33
34       return (op_name.lower() in allowed_ops)
35
36 # a "RightList" is a list of privileges
37
38 class RightList:
39     def __init__(self, string=None):
40         self.rights = []
41         if string:
42             self.load_from_string(string)
43
44     def load_from_string(self, string):
45         self.rights = []
46
47         # none == no rights, so leave the list empty
48         if not string:
49             return
50
51         parts = string.split(",")
52         for part in parts:
53             self.rights.append(Right(part))
54
55     def save_to_string(self):
56         right_names = []
57         for right in self.rights:
58             right_names.append(right.kind)
59
60         return ",".join(right_names)
61
62     def can_perform(self, op_name):
63         for right in self.rights:
64             if right.can_perform(op_name):
65                 return True
66         return False
67
68