Couple more checkout related functions
[public/library.git] / permissions.py
1 import grp
2 import os
3
4 import exceptions
5
6 class _PermissionLevel:
7     def __init__(self, group_name, pretty_name):
8         self.group_name = group_name
9         self.pretty_name = pretty_name
10
11 PERMISSION_OFFICE = _PermissionLevel("office", "Office worker")
12 PERMISSION_LIBCOM = _PermissionLevel("libcom", "Library Committee")
13
14 def check_permissions(permission_level):
15     def decorator(fn):
16         def wrapped_function(*args, **kwargs):
17             if not has_permission(permission_level):
18                 raise exceptions.PermissionsError(permission_level.pretty_name)
19             return fn(*args, **kwargs)
20
21         return wrapped_function
22
23     return decorator
24
25 def has_permission(permission_level):
26     return permission_level.group_name in _CURRENT_GROUPS_GETTER()
27
28 def _current_group_names():
29     group_ids = os.getgroups()
30     group_names = [grp.getgrgid(group_id).gr_name for group_id in group_ids]
31     return group_names
32
33 # Hack to allow dependency injection for testing
34 _CURRENT_GROUPS_GETTER = _current_group_names