dservercore 0.20.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dservercore/__init__.py +335 -0
- dservercore/annotations_routes.py +49 -0
- dservercore/base_uri_routes.py +153 -0
- dservercore/blueprint.py +15 -0
- dservercore/cli.py +268 -0
- dservercore/config.py +98 -0
- dservercore/config_routes.py +47 -0
- dservercore/date_utils.py +22 -0
- dservercore/extensions.py +8 -0
- dservercore/manifest_routes.py +49 -0
- dservercore/me_routes.py +50 -0
- dservercore/readme_routes.py +48 -0
- dservercore/schemas.py +83 -0
- dservercore/sort.py +260 -0
- dservercore/sql_models.py +173 -0
- dservercore/tags_routes.py +49 -0
- dservercore/templates/index.html +9 -0
- dservercore/uri_routes.py +197 -0
- dservercore/user_routes.py +169 -0
- dservercore/utils.py +1050 -0
- dservercore/utils_auth.py +74 -0
- dservercore/uuid_routes.py +44 -0
- dservercore/version.py +16 -0
- dservercore-0.20.0.dist-info/LICENSE +21 -0
- dservercore-0.20.0.dist-info/METADATA +857 -0
- dservercore-0.20.0.dist-info/RECORD +29 -0
- dservercore-0.20.0.dist-info/WHEEL +5 -0
- dservercore-0.20.0.dist-info/entry_points.txt +5 -0
- dservercore-0.20.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"""Routes for user management"""
|
|
2
|
+
from flask import (
|
|
3
|
+
abort,
|
|
4
|
+
jsonify,
|
|
5
|
+
)
|
|
6
|
+
from flask_jwt_extended import (
|
|
7
|
+
jwt_required,
|
|
8
|
+
get_jwt_identity,
|
|
9
|
+
)
|
|
10
|
+
from flask_smorest.pagination import PaginationParameters
|
|
11
|
+
|
|
12
|
+
import dservercore.utils
|
|
13
|
+
import dservercore.utils_auth
|
|
14
|
+
|
|
15
|
+
from dservercore.blueprint import Blueprint
|
|
16
|
+
from dservercore.sort import SortParameters, ASCENDING, DESCENDING
|
|
17
|
+
from dservercore.schemas import SummarySchema
|
|
18
|
+
from dservercore.sql_models import User, UserSchema, UserWithPermissionsSchema
|
|
19
|
+
from dservercore.utils import register_user, delete_user, summary_of_datasets_by_user
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
bp = Blueprint("users", __name__, url_prefix="/users")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@bp.route("", methods=["GET"])
|
|
26
|
+
@bp.sort(sort=["+username"], allowed_sort_fields=["username", "is_admin"])
|
|
27
|
+
@bp.paginate()
|
|
28
|
+
@bp.response(200, UserWithPermissionsSchema(many=True))
|
|
29
|
+
@bp.alt_response(401, description="Not registered")
|
|
30
|
+
@bp.alt_response(403, description="No permissions")
|
|
31
|
+
@jwt_required()
|
|
32
|
+
def users_get(pagination_parameters: PaginationParameters, sort_parameters: SortParameters):
|
|
33
|
+
"""List the users in dserver.
|
|
34
|
+
|
|
35
|
+
The user in the Authorization token needs to be admin.
|
|
36
|
+
"""
|
|
37
|
+
identity = get_jwt_identity()
|
|
38
|
+
|
|
39
|
+
if not dservercore.utils_auth.user_exists(identity):
|
|
40
|
+
abort(401)
|
|
41
|
+
|
|
42
|
+
if not dservercore.utils_auth.has_admin_rights(identity):
|
|
43
|
+
abort(403)
|
|
44
|
+
|
|
45
|
+
order_by_args = []
|
|
46
|
+
for field, order in sort_parameters.order.items():
|
|
47
|
+
if not hasattr(User, field):
|
|
48
|
+
continue
|
|
49
|
+
if order == DESCENDING:
|
|
50
|
+
order_by_args.append(getattr(User, field).desc())
|
|
51
|
+
else: # ascending
|
|
52
|
+
order_by_args.append(getattr(User, field))
|
|
53
|
+
|
|
54
|
+
query = User.query.order_by(*order_by_args).filter_by()
|
|
55
|
+
pagination_parameters.item_count = query.count()
|
|
56
|
+
return query.paginate(
|
|
57
|
+
page=pagination_parameters.page,
|
|
58
|
+
per_page=pagination_parameters.page_size,
|
|
59
|
+
error_out=True
|
|
60
|
+
).items
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@bp.route("/<username>", methods=["GET"])
|
|
64
|
+
@bp.response(200, UserWithPermissionsSchema)
|
|
65
|
+
@bp.alt_response(401, description="Not registered")
|
|
66
|
+
@bp.alt_response(403, description="No permissions")
|
|
67
|
+
@bp.alt_response(404, description="Not found")
|
|
68
|
+
@jwt_required()
|
|
69
|
+
def user_get(username):
|
|
70
|
+
"""Return a user's information.
|
|
71
|
+
|
|
72
|
+
A user can see his/her own profile.
|
|
73
|
+
An admin user can see other user's profiles.
|
|
74
|
+
"""
|
|
75
|
+
identity = get_jwt_identity()
|
|
76
|
+
|
|
77
|
+
if not dservercore.utils_auth.user_exists(identity):
|
|
78
|
+
abort(401)
|
|
79
|
+
|
|
80
|
+
# Return 403 if the user is not admin and the token username
|
|
81
|
+
# does not match up with the username in the URL.
|
|
82
|
+
if not dservercore.utils_auth.has_admin_rights(identity):
|
|
83
|
+
if identity != username:
|
|
84
|
+
abort(403)
|
|
85
|
+
|
|
86
|
+
if not dservercore.utils_auth.user_exists(username):
|
|
87
|
+
abort(404)
|
|
88
|
+
|
|
89
|
+
return dservercore.utils.get_user_info(username)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@bp.route("/<username>", methods=["PUT"])
|
|
93
|
+
@bp.arguments(UserSchema)
|
|
94
|
+
@bp.response(200)
|
|
95
|
+
@bp.alt_response(201, description="Created")
|
|
96
|
+
@bp.alt_response(401, description="Not registered")
|
|
97
|
+
@bp.alt_response(403, description="No permissions")
|
|
98
|
+
@jwt_required()
|
|
99
|
+
def user_put(data: UserSchema, username):
|
|
100
|
+
"""Create or update a user in dserver by replacing entry.
|
|
101
|
+
|
|
102
|
+
The user in the Authorization token needs to be admin.
|
|
103
|
+
"""
|
|
104
|
+
identity = get_jwt_identity()
|
|
105
|
+
|
|
106
|
+
if not dservercore.utils_auth.user_exists(identity):
|
|
107
|
+
abort(401)
|
|
108
|
+
|
|
109
|
+
if not dservercore.utils_auth.has_admin_rights(identity):
|
|
110
|
+
abort(403)
|
|
111
|
+
|
|
112
|
+
success_code = 201 # create
|
|
113
|
+
if dservercore.utils_auth.user_exists(username):
|
|
114
|
+
success_code = 200 # update
|
|
115
|
+
|
|
116
|
+
register_user(username, data)
|
|
117
|
+
|
|
118
|
+
return "", success_code
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@bp.route("/<username>", methods=["DELETE"])
|
|
122
|
+
@bp.response(200)
|
|
123
|
+
@bp.alt_response(401, description="Not registered")
|
|
124
|
+
@bp.alt_response(403, description="No permissions")
|
|
125
|
+
@jwt_required()
|
|
126
|
+
def user_delete(username):
|
|
127
|
+
"""Delete a user from dserver.
|
|
128
|
+
|
|
129
|
+
The user in the Authorization token needs to be admin.
|
|
130
|
+
"""
|
|
131
|
+
identity = get_jwt_identity()
|
|
132
|
+
|
|
133
|
+
if not dservercore.utils_auth.user_exists(identity):
|
|
134
|
+
abort(401)
|
|
135
|
+
|
|
136
|
+
if not dservercore.utils_auth.has_admin_rights(identity):
|
|
137
|
+
abort(403)
|
|
138
|
+
|
|
139
|
+
delete_user(username)
|
|
140
|
+
|
|
141
|
+
return "", 200
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@bp.route("/<username>/summary", methods=["GET"])
|
|
145
|
+
@bp.response(200, SummarySchema)
|
|
146
|
+
@bp.alt_response(401, description="Not registered")
|
|
147
|
+
@bp.alt_response(403, description="No permissions")
|
|
148
|
+
@bp.alt_response(404, description="Not found")
|
|
149
|
+
@jwt_required()
|
|
150
|
+
def user_summary_get(username):
|
|
151
|
+
"""Global summary of the datasets a user has access to."""
|
|
152
|
+
identity = get_jwt_identity()
|
|
153
|
+
|
|
154
|
+
if not dservercore.utils_auth.user_exists(identity):
|
|
155
|
+
# Authenticated user does not exist
|
|
156
|
+
abort(401)
|
|
157
|
+
|
|
158
|
+
# Return 403 if the user is not admin and the token username
|
|
159
|
+
# does not match up with the username in the URL.
|
|
160
|
+
if not dservercore.utils_auth.has_admin_rights(identity):
|
|
161
|
+
if identity != username:
|
|
162
|
+
abort(403)
|
|
163
|
+
|
|
164
|
+
if not dservercore.utils_auth.user_exists(username):
|
|
165
|
+
# Authenticated user is admin, but user summary requested does not exist
|
|
166
|
+
abort(404)
|
|
167
|
+
|
|
168
|
+
summary = summary_of_datasets_by_user(username)
|
|
169
|
+
return summary
|