-
Notifications
You must be signed in to change notification settings - Fork 6
/
example.py
259 lines (206 loc) · 9.36 KB
/
example.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# -*- coding:utf-8 -*-
import hashlib
import traceback
import requests
from flask import current_app, g, has_request_context
from flask import session, abort
from api.extensions import cache
ACL_URI = "http://{xxx}/api/v1"
APP_ID = "{app id}"
APP_SECRET_KEY = "{app secret key}"
def get_access_token():
url = "{0}/acl/apps/token".format(ACL_URI)
payload = dict(app_id=APP_ID,
secret_key=hashlib.md5(APP_SECRET_KEY.encode('utf-8')).hexdigest())
try:
res = requests.post(url, data=payload).json()
return res.get("token")
except Exception as e:
current_app.logger.error(str(e))
class AccessTokenCache(object):
TOKEN_KEY = 'AccessToken'
@classmethod
def get(cls):
if cache.get(cls.TOKEN_KEY) is not None:
return cache.get(cls.TOKEN_KEY)
res = get_access_token() or ""
cache.set(cls.TOKEN_KEY, res, timeout=60 * 60)
return res
@classmethod
def clean(cls):
cache.clear(cls.TOKEN_KEY)
class ACLManager(object):
def __init__(self):
self.access_token = AccessTokenCache.get()
self.headers = {'App-Access-Token': self.access_token}
self.user_info = session["acl"] if has_request_context() and "acl" in session else {}
def build_user_info(self, username):
session["acl"] = self.get_user_info(username)
self.user_info = session["acl"]
def get_user_info(self, username):
url = '{0}/acl/users/info'.format(ACL_URI)
payload = dict(username=username)
try:
res = requests.get(url, data=payload, headers=self.headers).json()
if not res.get("result"):
return abort(400, res.get("message"))
result = res.get("result")
return result
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: get user info failed: {e}")
def has_permission(self, resource_name, resource_type, perm, rid=None):
if rid is None:
if "acl" not in session and hasattr(g, "user"):
self.build_user_info(g.user.username)
elif "acl" not in session:
abort(405)
rid = session.get('acl', {}).get('rid')
url = '{0}/acl/roles/has_perm'.format(ACL_URI)
payload = dict(rid=rid,
resource_name=resource_name,
resource_type_name=resource_type,
perm=perm)
try:
res = requests.get(url, data=payload, headers=self.headers).json()
return res.get("result")
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: has permission failed: {e}")
def get_resource_id(self, name, resource_type):
url = '{0}/acl/resources'.format(ACL_URI)
payload = dict(q=name, resource_type_id=resource_type)
try:
res = requests.get(url, data=payload, headers=self.headers).json()
for r in res.get('resources', []):
if r.get('name') == name:
return r.get('id')
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: get resource id failed: {e}")
def get_resources(self, resource_type_name=None):
if "acl" not in session and hasattr(g, "user"):
self.build_user_info(g.user.username)
elif "acl" not in session:
abort(405)
url = '{0}/acl/roles/{1}/resources'.format(ACL_URI, session.get('acl', {}).get('rid'))
payload = dict(resource_type_id=resource_type_name)
try:
res = requests.get(url, data=payload, headers=self.headers).json()
return res.get('resources')
except Exception as e:
current_app.logger.error(str(e))
current_app.logger.error(traceback.format_exc())
return abort(400, f"ACL: has permission failed: {e}")
def add_resource(self, name, resource_type_name=None, uid=None, rid=None):
if "acl" not in session and hasattr(g, "user"):
self.build_user_info(g.user.username)
elif "acl" not in session:
abort(405)
url = '{0}/acl/resources'.format(ACL_URI)
payload = dict(type_id=resource_type_name, name=name, uid=uid or g.user.uid)
try:
res = requests.post(url, data=payload, headers=self.headers).json()
if res.get("id") is None:
return abort(400, res.get("message"))
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: add resource failed: {e}")
self.grant_resource_to_role(name, resource_type_name, rid=rid)
def update_resource(self, old, new, resource_type):
resource_id = self.get_resource_id(old, resource_type)
url = '{0}/acl/resources/{1}'.format(ACL_URI, resource_id)
payload = dict(name=new)
try:
res = requests.put(url, data=payload, headers=self.headers).json()
if res.get("message") is not None:
return abort(400, res.get("message"))
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: add resource failed: {e}")
def grant_resource_to_role(self, name, resource_type, rid=None, perms=None):
if "acl" not in session and hasattr(g, "user"):
self.build_user_info(g.user.username)
elif "acl" not in session:
abort(405)
perms = perms or []
resource_id = self.get_resource_id(name, resource_type)
url = '{0}/acl/roles/{1}/resources/{2}/grant'.format(ACL_URI,
rid or self.user_info.get('rid'),
resource_id)
payload = dict(perms=perms)
try:
res = requests.post(url, json=payload, headers=self.headers).json()
if res.get("rid") is None:
return abort(400, res.get("message"))
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: grant resource to role failed: {e}")
def revoke_resource_to_role(self, name, resource_type, rid=None, perms=None):
if "acl" not in session and hasattr(g, "user"):
self.build_user_info(g.user.username)
elif "acl" not in session:
abort(405)
perms = perms or []
resource_id = self.get_resource_id(name, resource_type)
url = '{0}/acl/roles/{1}/resources/{2}/revoke'.format(ACL_URI,
rid or self.user_info.get('rid'),
resource_id)
payload = dict(perms=perms)
try:
res = requests.post(url, json=payload, headers=self.headers).json()
if res.get("rid") is None:
return abort(400, res.get("message"))
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: revoke resource to role failed: {e}")
def del_resource(self, name, resource_type):
if "acl" not in session and hasattr(g, "user"):
self.build_user_info(g.user.username)
elif "acl" not in session:
abort(405)
resource_id = self.get_resource_id(name, resource_type)
url = '{0}/acl/resources/{1}'.format(ACL_URI, resource_id)
payload = dict()
try:
res = requests.delete(url, data=payload, headers=self.headers).json()
if res.get("resource_id") is None:
return abort(400, res.get("message"))
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: del resource failed: {e}")
def get_permissions(self, resource_name, resource_type):
resource_id = self.get_resource_id(resource_name, resource_type)
url = '{}/acl/resources/{}/permissions'.format(ACL_URI, resource_id)
try:
return requests.get(url, headers=self.headers).json()
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: get resource permissions failed: {e}")
def get_roles(self, q=None):
url = '{}/acl/roles'.format(ACL_URI)
params = dict(page_size=100000)
if q:
params['q'] = q
try:
return requests.get(url, params=params, headers=self.headers).json()
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: get resource permissions failed: {e}")
def get_users(self, q=None):
url = '{}/acl/users'.format(ACL_URI)
params = dict(page_size=100000)
if q:
params['q'] = q
try:
return requests.get(url, params=params, headers=self.headers).json()
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: get resource permissions failed: {e}")
def get_users_by_rid(self, rid):
url = '{}/acl/roles/{}/users'.format(ACL_URI, rid)
try:
return requests.get(url, params=dict(page_size=100000), headers=self.headers).json()
except Exception as e:
current_app.logger.error(str(e))
return abort(400, f"ACL: get users by rid failed: {e}")