~0x44/nova/bug838466

« back to all changes in this revision

Viewing changes to nova/auth/rbac.py

  • Committer: Eric Day
  • Date: 2010-08-19 05:14:34 UTC
  • mto: This revision was merged to the branch mainline in revision 250.
  • Revision ID: eday@oddments.org-20100819051434-pjr6439si1tfsekx
Cleaned up pep8/pylint style issues in nova/auth. There are still a few pylint warnings in manager.py, but the patch is already fairly large.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#    License for the specific language governing permissions and limitations
17
17
#    under the License.
18
18
 
 
19
"""Role-based access control decorators to use fpr wrapping other
 
20
methods with."""
 
21
 
19
22
from nova import exception
20
 
from nova.auth import manager
21
23
 
22
24
 
23
25
def allow(*roles):
24
 
    def wrap(f):
25
 
        def wrapped_f(self, context, *args, **kwargs):
 
26
    """Allow the given roles access the wrapped function."""
 
27
 
 
28
    def wrap(func): # pylint: disable-msg=C0111
 
29
 
 
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)
28
34
            for role in roles:
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()
32
 
        return wrapped_f
 
38
 
 
39
        return wrapped_func
 
40
 
33
41
    return wrap
34
42
 
35
43
 
36
44
def deny(*roles):
37
 
    def wrap(f):
38
 
        def wrapped_f(self, context, *args, **kwargs):
 
45
    """Deny the given roles access the wrapped function."""
 
46
 
 
47
    def wrap(func): # pylint: disable-msg=C0111
 
48
 
 
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)
41
53
            for role in roles:
42
54
                if __matches_role(context, role):
43
55
                    raise exception.NotAuthorized()
44
 
            return f(self, context, *args, **kwargs)
45
 
        return wrapped_f
 
56
            return func(self, context, *args, **kwargs)
 
57
 
 
58
        return wrapped_func
 
59
 
46
60
    return wrap
47
61
 
48
62
 
49
63
def __matches_role(context, role):
 
64
    """Check if a role is allowed."""
50
65
    if role == 'all':
51
66
        return True
52
67
    if role == 'none':
53
68
        return False
54
69
    return context.project.has_role(context.user.id, role)
55