-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserHandler.py
More file actions
241 lines (178 loc) · 6.84 KB
/
UserHandler.py
File metadata and controls
241 lines (178 loc) · 6.84 KB
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
import json, re
from flask import Blueprint, request
from flask_security import roles_required
from flask_security.utils import encrypt_password
from pgadmin.model import db, User, Role, ServerGroup, UserPreference, Setting, Server, Process
from responses import Result, ServerErrorResult, InvalidParameterResult, NotFoundResult
_user_management = Blueprint('_user_management', __name__, url_prefix='/api/user')
def validate_user(data):
new_data = dict()
email_filter = re.compile("^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9]"
"(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9]"
"(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
if ('newPassword' in data and data['newPassword'] != "" and
'confirmPassword' in data and data['confirmPassword'] != ""):
if data['newPassword'] == data['confirmPassword']:
new_data['password'] = encrypt_password(data['newPassword'])
else:
raise Exception("Passwords do not match.")
if 'email' in data and data['email'] != "":
if email_filter.match(data['email']):
new_data['email'] = data['email']
else:
raise Exception(_("Invalid email address."))
if 'role' in data and len(data['role']) != 0:
new_data['roles'] = data['role']
if 'active' in data and data['active'] != "":
new_data['active'] = data['active']
return new_data
def get_detailed_roles(rs):
roles = []
for r in rs:
roles.append({'id':r.id,'name':r.name})
return roles
@_user_management.route('/', methods=['GET'], defaults={'uid': None})
@_user_management.route('/<string:uid>', methods=['GET'])
def list(uid=None):
result = []
if uid:
u = User.query.get(uid)
if u is not None:
user_data = {'id': u.id,
'email': u.email,
'active': u.active,
'role': get_detailed_roles(u.roles)
}
result.append(user_data)
else:
users = User.query.all()
for u in users:
result.append({'id': u.id,
'email': u.email,
'active': u.active,
'role': get_detailed_roles(u.roles)
})
result = Result(200, "SUCCESS", "SUCCESS", extra_fields={"results":result})
return result.http_response()
@_user_management.route('/', methods=['POST'], endpoint='create_user')
@roles_required('Administrator')
def create():
"""
Returns:
"""
data = request.form if request.form else json.loads(
request.data, encoding='utf-8'
)
for f in ('email', 'role', 'active', 'newPassword', 'confirmPassword'):
if f in data and data[f] != '':
continue
else:
return InvalidParameterResult(errors=["Missing field: '{0}'".format(f)]).http_response()
try:
new_data = validate_user(data)
if 'roles' in new_data:
roles_obj = []
for r in new_data['roles']:
roles_obj.append(Role.query.get(r))
new_data['roles'] = roles_obj
except Exception as e:
return InvalidParameterResult(errors=[str(e)]).http_response()
try:
usr = User(email=new_data['email'],
roles=new_data['roles'],
active=new_data['active'],
password=new_data['password'])
db.session.add(usr)
db.session.commit()
# Add default server group for new user.
server_group = ServerGroup(user_id=usr.id, name="Servers")
db.session.add(server_group)
db.session.commit()
except Exception as e:
return ServerErrorResult(message=str(e)).http_response()
res = {'id': usr.id,
'email': usr.email,
'active': usr.active,
'role': get_detailed_roles(usr.roles)
}
result = Result(200, "SUCCESS", "SUCCESS", extra_fields={"results": [res]})
return result.http_response()
@_user_management.route('/<int:uid>', methods=['DELETE'], endpoint='delete_user')
@roles_required('Administrator')
def delete(uid):
"""
Args:
uid:
Returns:
"""
usr = User.query.get(uid)
if not usr:
return NotFoundResult(message="User with id {0} is not found".format(uid))
try:
Setting.query.filter_by(user_id=uid).delete()
UserPreference.query.filter_by(uid=uid).delete()
Server.query.filter_by(user_id=uid).delete()
ServerGroup.query.filter_by(user_id=uid).delete()
Process.query.filter_by(user_id=uid).delete()
# Finally delete user
db.session.delete(usr)
db.session.commit()
return Result(200,"SUCCESS","User Deleted Successfully").http_response()
except Exception as e:
return ServerErrorResult(message=str(e)).http_response()
@_user_management.route('/<int:uid>', methods=['PUT'], endpoint='update_user')
@roles_required('Administrator')
def update(uid):
"""
Args:
uid:
Returns:
"""
usr = User.query.get(uid)
if not usr:
return NotFoundResult(message="User with id {0} is not found".format(uid)).http_response()
data = request.form if request.form else json.loads(
request.data, encoding='utf-8'
)
try:
new_data = validate_user(data)
if 'roles' in new_data:
roles_obj = []
for r in new_data['roles']:
roles_obj.append(Role.query.get(r))
new_data['roles'] = roles_obj
except Exception as e:
return InvalidParameterResult(errors=[str(e)]).http_response()
try:
for k, v in new_data.items():
setattr(usr, k, v)
db.session.commit()
res = {'id': usr.id,
'email': usr.email,
'active': usr.active,
'role': get_detailed_roles(usr.roles)
}
result = Result(200, "SUCCESS", "SUCCESS", extra_fields={"results": [res]})
return result.http_response()
except Exception as e:
return ServerErrorResult(message=str(e)).http_response()
@_user_management.route('/role/', methods=['GET'], defaults={'rid': None}, endpoint='roles')
@_user_management.route('/role/<int:rid>', methods=['GET'], endpoint='role')
@roles_required('Administrator')
def role(rid):
"""
Args:
rid: Role id
Returns: List of pgAdmin4 users roles or single role if rid is provided.
"""
result = []
if rid:
r = Role.query.get(rid)
if r is not None:
result.append({'id': r.id, 'name': r.name})
else:
roles = Role.query.all()
for r in roles:
result.append({'id': r.id,'name': r.name})
result = Result(200, "SUCCESS", "SUCCESS", extra_fields={"results": result})
return result.http_response()