This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathacl.py
68 lines (55 loc) · 2.1 KB
/
acl.py
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
import main
from google.appengine.api import users
from google.appengine.api import oauth
class ACL(object):
def __init__(self, default_acl, read, write):
self._default_acl = default_acl
self._read = self._to_list(read)
self._write = self._to_list(write)
def can_read(self, user, acl_r=None, acl_w=None):
default_acl = self._default_acl or main.DEFAULT_CONFIG['service']['default_permissions']
acl_r = acl_r or self._read or default_acl['read'] or []
acl_w = acl_w or self._write or default_acl['write'] or []
if u'all' in acl_r or len(acl_r) == 0:
return True
elif user is not None and u'login' in acl_r:
return True
elif user is not None and (user.email() in acl_r or user.email() in acl_w):
return True
elif self._is_admin(user):
return True
else:
return False
def can_write(self, user, acl_r=None, acl_w=None):
default_acl = self._default_acl or main.DEFAULT_CONFIG['service']['default_permissions']
acl_w = acl_w or self._write or default_acl['write'] or []
if (not self.can_read(user, acl_r, acl_w)) and (user is None or user.email() not in acl_w):
return False
elif 'all' in acl_w:
return True
elif (len(acl_w) == 0 or u'login' in acl_w) and user is not None:
return True
elif user is not None and user.email() in acl_w:
return True
elif self._is_admin(user):
return True
else:
return False
def _to_list(self, acl):
if type(acl) in [list, tuple]:
return acl
if acl is None or len(acl) == 0:
return []
else:
return [token.strip() for token in acl.split(',')]
def _is_admin(self, user):
if not user:
return False
if users.is_current_user_admin():
return True
try:
if oauth.is_current_user_admin():
return True
except oauth.OAuthRequestError:
pass
return False