16
16
# License for the specific language governing permissions and limitations
17
17
# under the License.
19
"""Role-based access control decorators to use fpr wrapping other
19
22
from nova import exception
20
from nova.auth import manager
25
def wrapped_f(self, context, *args, **kwargs):
26
"""Allow the given roles access the wrapped function."""
28
def wrap(func): # pylint: disable-msg=C0111
30
def wrapped_func(self, context, *args,
31
**kwargs): # pylint: disable-msg=C0111
26
32
if context.user.is_superuser():
27
return f(self, context, *args, **kwargs)
33
return func(self, context, *args, **kwargs)
29
35
if __matches_role(context, role):
30
return f(self, context, *args, **kwargs)
36
return func(self, context, *args, **kwargs)
31
37
raise exception.NotAuthorized()
38
def wrapped_f(self, context, *args, **kwargs):
45
"""Deny the given roles access the wrapped function."""
47
def wrap(func): # pylint: disable-msg=C0111
49
def wrapped_func(self, context, *args,
50
**kwargs): # pylint: disable-msg=C0111
39
51
if context.user.is_superuser():
40
return f(self, context, *args, **kwargs)
52
return func(self, context, *args, **kwargs)
42
54
if __matches_role(context, role):
43
55
raise exception.NotAuthorized()
44
return f(self, context, *args, **kwargs)
56
return func(self, context, *args, **kwargs)
49
63
def __matches_role(context, role):
64
"""Check if a role is allowed."""
54
69
return context.project.has_role(context.user.id, role)