Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-pylib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
f250cb22
Commit
f250cb22
authored
4 years ago
by
florian
Browse files
Options
Downloads
Patches
Plain Diff
WIP: Replace entity errors by HTTP errors
parent
8621f3a7
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/caosdb/common/administration.py
+29
-26
29 additions, 26 deletions
src/caosdb/common/administration.py
with
29 additions
and
26 deletions
src/caosdb/common/administration.py
+
29
−
26
View file @
f250cb22
...
@@ -28,8 +28,9 @@ from lxml import etree
...
@@ -28,8 +28,9 @@ from lxml import etree
from
caosdb.common.utils
import
xml2str
from
caosdb.common.utils
import
xml2str
from
caosdb.connection.connection
import
get_connection
from
caosdb.connection.connection
import
get_connection
from
caosdb.exceptions
import
(
AuthorizationException
,
ClientErrorException
,
from
caosdb.exceptions
import
(
ClientErrorException
,
EntityDoesNotExistError
)
HTTPAuthorizationException
,
ResourceNotFoundException
)
def
set_server_property
(
key
,
value
):
def
set_server_property
(
key
,
value
):
...
@@ -104,10 +105,10 @@ def _retrieve_user(name, realm=None, **kwargs):
...
@@ -104,10 +105,10 @@ def _retrieve_user(name, realm=None, **kwargs):
con
=
get_connection
()
con
=
get_connection
()
try
:
try
:
return
con
.
_http_request
(
method
=
"
GET
"
,
path
=
"
User/
"
+
(
realm
+
"
/
"
+
name
if
realm
is
not
None
else
name
),
**
kwargs
).
read
()
return
con
.
_http_request
(
method
=
"
GET
"
,
path
=
"
User/
"
+
(
realm
+
"
/
"
+
name
if
realm
is
not
None
else
name
),
**
kwargs
).
read
()
except
AuthorizationException
as
e
:
except
HTTP
AuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to retrieve this user.
"
e
.
msg
=
"
You are not permitted to retrieve this user.
"
raise
raise
except
EntityDoesNotExistError
as
e
:
except
ResourceNotFoundException
as
e
:
e
.
msg
=
"
User does not exist.
"
e
.
msg
=
"
User does not exist.
"
raise
raise
...
@@ -116,10 +117,10 @@ def _delete_user(name, **kwargs):
...
@@ -116,10 +117,10 @@ def _delete_user(name, **kwargs):
con
=
get_connection
()
con
=
get_connection
()
try
:
try
:
return
con
.
_http_request
(
method
=
"
DELETE
"
,
path
=
"
User/
"
+
name
,
**
kwargs
).
read
()
return
con
.
_http_request
(
method
=
"
DELETE
"
,
path
=
"
User/
"
+
name
,
**
kwargs
).
read
()
except
AuthorizationException
as
e
:
except
HTTP
AuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to delete this user.
"
e
.
msg
=
"
You are not permitted to delete this user.
"
raise
raise
except
EntityDoesNotExistError
as
e
:
except
ResourceNotFoundException
as
e
:
e
.
msg
=
"
User does not exist.
"
e
.
msg
=
"
User does not exist.
"
raise
raise
...
@@ -142,10 +143,10 @@ def _update_user(name, realm=None, password=None, status=None,
...
@@ -142,10 +143,10 @@ def _update_user(name, realm=None, password=None, status=None,
params
[
"
entity
"
]
=
str
(
entity
)
params
[
"
entity
"
]
=
str
(
entity
)
try
:
try
:
return
con
.
put_form_data
(
entity_uri_segment
=
"
User/
"
+
(
realm
+
"
/
"
+
name
if
realm
is
not
None
else
name
),
params
=
params
,
**
kwargs
).
read
()
return
con
.
put_form_data
(
entity_uri_segment
=
"
User/
"
+
(
realm
+
"
/
"
+
name
if
realm
is
not
None
else
name
),
params
=
params
,
**
kwargs
).
read
()
except
EntityDoesNotExistError
as
e
:
except
ResourceNotFoundException
as
e
:
e
.
msg
=
"
User does not exist.
"
e
.
msg
=
"
User does not exist.
"
raise
raise
except
AuthorizationException
as
e
:
except
HTTP
AuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to update this user.
"
e
.
msg
=
"
You are not permitted to update this user.
"
raise
raise
except
ClientErrorException
as
e
:
except
ClientErrorException
as
e
:
...
@@ -171,7 +172,7 @@ def _insert_user(name, password=None, status=None, email=None, entity=None, **kw
...
@@ -171,7 +172,7 @@ def _insert_user(name, password=None, status=None, email=None, entity=None, **kw
params
[
"
entity
"
]
=
entity
params
[
"
entity
"
]
=
entity
try
:
try
:
return
con
.
post_form_data
(
entity_uri_segment
=
"
User
"
,
params
=
params
,
**
kwargs
).
read
()
return
con
.
post_form_data
(
entity_uri_segment
=
"
User
"
,
params
=
params
,
**
kwargs
).
read
()
except
AuthorizationException
as
e
:
except
HTTP
AuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to insert a new user.
"
e
.
msg
=
"
You are not permitted to insert a new user.
"
raise
e
raise
e
except
ClientErrorException
as
e
:
except
ClientErrorException
as
e
:
...
@@ -187,7 +188,7 @@ def _insert_role(name, description, **kwargs):
...
@@ -187,7 +188,7 @@ def _insert_role(name, description, **kwargs):
con
=
get_connection
()
con
=
get_connection
()
try
:
try
:
return
con
.
post_form_data
(
entity_uri_segment
=
"
Role
"
,
params
=
{
"
role_name
"
:
name
,
"
role_description
"
:
description
},
**
kwargs
).
read
()
return
con
.
post_form_data
(
entity_uri_segment
=
"
Role
"
,
params
=
{
"
role_name
"
:
name
,
"
role_description
"
:
description
},
**
kwargs
).
read
()
except
AuthorizationException
as
e
:
except
HTTP
AuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to insert a new role.
"
e
.
msg
=
"
You are not permitted to insert a new role.
"
raise
raise
except
ClientErrorException
as
e
:
except
ClientErrorException
as
e
:
...
@@ -200,10 +201,10 @@ def _update_role(name, description, **kwargs):
...
@@ -200,10 +201,10 @@ def _update_role(name, description, **kwargs):
con
=
get_connection
()
con
=
get_connection
()
try
:
try
:
return
con
.
put_form_data
(
entity_uri_segment
=
"
Role/
"
+
name
,
params
=
{
"
role_description
"
:
description
},
**
kwargs
).
read
()
return
con
.
put_form_data
(
entity_uri_segment
=
"
Role/
"
+
name
,
params
=
{
"
role_description
"
:
description
},
**
kwargs
).
read
()
except
AuthorizationException
as
e
:
except
HTTP
AuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to update this role.
"
e
.
msg
=
"
You are not permitted to update this role.
"
raise
raise
except
EntityDoesNotExistError
as
e
:
except
ResourceNotFoundException
as
e
:
e
.
msg
=
"
Role does not exist.
"
e
.
msg
=
"
Role does not exist.
"
raise
raise
...
@@ -212,10 +213,10 @@ def _retrieve_role(name, **kwargs):
...
@@ -212,10 +213,10 @@ def _retrieve_role(name, **kwargs):
con
=
get_connection
()
con
=
get_connection
()
try
:
try
:
return
con
.
_http_request
(
method
=
"
GET
"
,
path
=
"
Role/
"
+
name
,
**
kwargs
).
read
()
return
con
.
_http_request
(
method
=
"
GET
"
,
path
=
"
Role/
"
+
name
,
**
kwargs
).
read
()
except
AuthorizationException
as
e
:
except
HTTP
AuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to retrieve this role.
"
e
.
msg
=
"
You are not permitted to retrieve this role.
"
raise
raise
except
EntityDoesNotExistError
as
e
:
except
ResourceNotFoundException
as
e
:
e
.
msg
=
"
Role does not exist.
"
e
.
msg
=
"
Role does not exist.
"
raise
raise
...
@@ -224,10 +225,10 @@ def _delete_role(name, **kwargs):
...
@@ -224,10 +225,10 @@ def _delete_role(name, **kwargs):
con
=
get_connection
()
con
=
get_connection
()
try
:
try
:
return
con
.
_http_request
(
method
=
"
DELETE
"
,
path
=
"
Role/
"
+
name
,
**
kwargs
).
read
()
return
con
.
_http_request
(
method
=
"
DELETE
"
,
path
=
"
Role/
"
+
name
,
**
kwargs
).
read
()
except
AuthorizationException
as
e
:
except
HTTP
AuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to delete this role.
"
e
.
msg
=
"
You are not permitted to delete this role.
"
raise
raise
except
EntityDoesNotExistError
as
e
:
except
ResourceNotFoundException
as
e
:
e
.
msg
=
"
Role does not exist.
"
e
.
msg
=
"
Role does not exist.
"
raise
raise
...
@@ -241,11 +242,12 @@ def _set_roles(username, roles, realm=None, **kwargs):
...
@@ -241,11 +242,12 @@ def _set_roles(username, roles, realm=None, **kwargs):
body
=
xml2str
(
xml
)
body
=
xml2str
(
xml
)
con
=
get_connection
()
con
=
get_connection
()
try
:
try
:
body
=
con
.
_http_request
(
method
=
"
PUT
"
,
path
=
"
UserRoles/
"
+
(
realm
+
"
/
"
+
username
if
realm
is
not
None
else
username
),
body
=
body
,
**
kwargs
).
read
()
body
=
con
.
_http_request
(
method
=
"
PUT
"
,
path
=
"
UserRoles/
"
+
(
realm
+
"
/
"
+
except
AuthorizationException
as
e
:
username
if
realm
is
not
None
else
username
),
body
=
body
,
**
kwargs
).
read
()
except
HTTPAuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to set this user
'
s roles.
"
e
.
msg
=
"
You are not permitted to set this user
'
s roles.
"
raise
raise
except
EntityDoesNotExistError
as
e
:
except
ResourceNotFoundException
as
e
:
e
.
msg
=
"
User does not exist.
"
e
.
msg
=
"
User does not exist.
"
raise
raise
except
ClientErrorException
as
e
:
except
ClientErrorException
as
e
:
...
@@ -264,11 +266,12 @@ def _set_roles(username, roles, realm=None, **kwargs):
...
@@ -264,11 +266,12 @@ def _set_roles(username, roles, realm=None, **kwargs):
def
_get_roles
(
username
,
realm
=
None
,
**
kwargs
):
def
_get_roles
(
username
,
realm
=
None
,
**
kwargs
):
con
=
get_connection
()
con
=
get_connection
()
try
:
try
:
body
=
con
.
_http_request
(
method
=
"
GET
"
,
path
=
"
UserRoles/
"
+
(
realm
+
"
/
"
+
username
if
realm
is
not
None
else
username
),
**
kwargs
).
read
()
body
=
con
.
_http_request
(
method
=
"
GET
"
,
path
=
"
UserRoles/
"
+
(
except
AuthorizationException
as
e
:
realm
+
"
/
"
+
username
if
realm
is
not
None
else
username
),
**
kwargs
).
read
()
except
HTTPAuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to retrieve this user
'
s roles.
"
e
.
msg
=
"
You are not permitted to retrieve this user
'
s roles.
"
raise
raise
except
EntityDoesNotExistError
as
e
:
except
ResourceNotFoundException
as
e
:
e
.
msg
=
"
User does not exist.
"
e
.
msg
=
"
User does not exist.
"
raise
raise
ret
=
set
()
ret
=
set
()
...
@@ -308,10 +311,10 @@ Returns
...
@@ -308,10 +311,10 @@ Returns
con
=
get_connection
()
con
=
get_connection
()
try
:
try
:
return
con
.
_http_request
(
method
=
"
PUT
"
,
path
=
"
PermissionRules/
"
+
role
,
body
=
body
,
**
kwargs
).
read
()
return
con
.
_http_request
(
method
=
"
PUT
"
,
path
=
"
PermissionRules/
"
+
role
,
body
=
body
,
**
kwargs
).
read
()
except
AuthorizationException
as
e
:
except
HTTP
AuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to set this role
'
s permissions.
"
e
.
msg
=
"
You are not permitted to set this role
'
s permissions.
"
raise
raise
except
EntityDoesNotExistError
as
e
:
except
ResourceNotFoundException
as
e
:
e
.
msg
=
"
Role does not exist.
"
e
.
msg
=
"
Role does not exist.
"
raise
raise
...
@@ -320,10 +323,10 @@ def _get_permissions(role, **kwargs):
...
@@ -320,10 +323,10 @@ def _get_permissions(role, **kwargs):
con
=
get_connection
()
con
=
get_connection
()
try
:
try
:
return
PermissionRule
.
_parse_body
(
con
.
_http_request
(
method
=
"
GET
"
,
path
=
"
PermissionRules/
"
+
role
,
**
kwargs
).
read
())
return
PermissionRule
.
_parse_body
(
con
.
_http_request
(
method
=
"
GET
"
,
path
=
"
PermissionRules/
"
+
role
,
**
kwargs
).
read
())
except
AuthorizationException
as
e
:
except
HTTP
AuthorizationException
as
e
:
e
.
msg
=
"
You are not permitted to retrieve this role
'
s permissions.
"
e
.
msg
=
"
You are not permitted to retrieve this role
'
s permissions.
"
raise
raise
except
EntityDoesNotExistError
as
e
:
except
ResourceNotFoundException
as
e
:
e
.
msg
=
"
Role does not exist.
"
e
.
msg
=
"
Role does not exist.
"
raise
raise
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment