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
84ae1c80
Commit
84ae1c80
authored
1 year ago
by
Henrik tom Wörden
Browse files
Options
Downloads
Patches
Plain Diff
ENH: added old example
parent
94de0ce0
No related branches found
No related tags found
No related merge requests found
Pipeline
#41276
passed
1 year ago
Stage: code_style
Stage: linting
Stage: test
Stage: deploy
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/set_permissions.py
+41
-49
41 additions, 49 deletions
examples/set_permissions.py
with
41 additions
and
49 deletions
examples/set_permissions.py
+
41
−
49
View file @
84ae1c80
...
...
@@ -25,13 +25,15 @@
As a result, only a specific user or group may access it.
This script assumes that the user specified in the pycaosdb.ini
configuration can create new entities.
This script assumes that data similar to the demo server of IndiScale (at
demo.indiscale.com) exists on the server specified in the pycaosdb.ini
configuration.
"""
import
caosdb
as
db
from
caosdb
import
administration
as
admin
import
lxml
def
assert_user_and_role
():
...
...
@@ -48,27 +50,27 @@ out : tuple
"""
try
:
human_user
=
admin
.
_retrieve_user
(
"
jane
"
)
admin
.
_update_user
(
name
=
"
jane
"
,
status
=
"
ACTIVE
"
)
except
db
.
HTTPResourceNotFound
Error
:
_activate_user
(
"
jane
"
)
except
db
.
EntityDoesNotExist
Error
:
human_user
=
admin
.
_insert_user
(
"
jane
"
,
password
=
"
Human_Rememberable_Password_1234
"
,
status
=
"
ACTIVE
"
)
try
:
alien_user
=
admin
.
_retrieve_user
(
"
xaxys
"
)
admin
.
_upd
ate_user
(
name
=
"
xaxys
"
,
status
=
"
ACTIVE
"
)
except
db
.
HTTPResourceNotFound
Error
:
_activ
ate_user
(
"
xaxys
"
)
except
db
.
EntityDoesNotExist
Error
:
alien_user
=
admin
.
_insert_user
(
"
xaxys
"
,
password
=
"
4321_Syxax
"
,
status
=
"
ACTIVE
"
)
# At the moment, the return value is only "ok" for successful insertions.
try
:
human_role
=
admin
.
_retrieve_role
(
"
human
"
)
except
db
.
HTTPResourceNotFound
Error
:
except
db
.
EntityDoesNotExist
Error
:
human_role
=
admin
.
_insert_role
(
"
human
"
,
"
An Earthling.
"
)
try
:
alien_role
=
admin
.
_retrieve_role
(
"
alien
"
)
except
db
.
HTTPResourceNotFound
Error
:
except
db
.
EntityDoesNotExist
Error
:
alien_role
=
admin
.
_insert_role
(
"
alien
"
,
"
An Extra-terrestrial.
"
)
admin
.
_set_roles
(
"
jane
"
,
[
"
human
"
])
...
...
@@ -78,6 +80,24 @@ out : tuple
(
"
xaxys
"
,
list
(
admin
.
_get_roles
(
"
xaxys
"
))))
def
_activate_user
(
user
):
"""
Set the user state to
"
ACTIVE
"
if necessary.
Parameters
----------
user : str
The user to activate.
Returns
-------
None
"""
user_xml
=
lxml
.
etree
.
fromstring
(
admin
.
_retrieve_user
(
user
))
if
user_xml
.
xpath
(
"
User
"
)[
0
].
attrib
[
"
status
"
]
!=
"
ACTIVE
"
:
admin
.
_update_user
(
user
,
status
=
"
ACTIVE
"
)
def
get_entities
(
count
=
1
):
"""
Retrieve one or more entities.
...
...
@@ -91,11 +111,9 @@ Returns
out : Container
A container of retrieved entities, the length is given by the parameter count.
"""
cont
=
db
.
execute_query
(
"
FIND RECORD
'
Human Food
'"
,
flags
=
{
"
P
"
:
"
0L{n}
"
.
format
(
n
=
count
)})
cont
=
db
.
execute_query
(
"
FIND RECORD Guitar
"
,
flags
=
{
"
P
"
:
"
0L{n}
"
.
format
(
n
=
count
)})
if
len
(
cont
)
!=
count
:
raise
db
.
CaosDBException
(
msg
=
"
Incorrect number of entitities returned.
"
)
raise
db
.
CaosDBException
(
msg
=
"
Incorrect number of entitities returned.
"
)
return
cont
...
...
@@ -120,8 +138,7 @@ general : bool, optional
# Set general permissions
if
general
:
grant
=
admin
.
PermissionRule
(
action
=
"
grant
"
,
permission
=
"
RETRIEVE:OWNER
"
)
grant
=
admin
.
PermissionRule
(
action
=
"
grant
"
,
permission
=
"
RETRIEVE:OWNER
"
)
deny
=
admin
.
PermissionRule
(
action
=
"
deny
"
,
permission
=
"
RETRIEVE:FILE
"
)
admin
.
_set_permissions
(
role
=
role_grant
,
permission_rules
=
[
grant
])
...
...
@@ -172,12 +189,9 @@ None
for
ent
in
cont
:
ent
.
retrieve
()
print
(
"
Successfully retrieved all entities.
"
)
except
db
.
TransactionError
as
te
:
if
te
.
has_error
(
db
.
AuthorizationError
):
print
(
ent
)
print
(
"
Could not retrieve this entity although it should have been possible!
"
)
else
:
raise
te
except
db
.
AuthorizationException
:
print
(
ent
)
print
(
"
Could not retrieve this entity although it should have been possible!
"
)
# Switch to user without permissions
db
.
configure_connection
(
username
=
denied_user
[
0
],
password
=
denied_user
[
1
],
...
...
@@ -192,45 +206,23 @@ None
denied_all
=
False
print
(
ent
)
print
(
"
Could retrieve this entity although it should not have been possible!
"
)
except
db
.
TransactionError
as
te
:
# Only do something if an error wasn't caused by an
# AuthorizationError
if
not
te
.
has_error
(
db
.
AuthorizationError
):
raise
te
except
db
.
AuthorizationException
:
pass
if
denied_all
:
print
(
"
Retrieval of all entities was successfully denied.
"
)
def
create_test_entities
():
"""
Create some test entities.
After calling this function, there will be a RecordType
"
Human Food
"
with the corresponding Records
"
Bread
"
,
"
Tomatoes
"
, and
"
Twinkies
"
inserted in the database.
"""
rt
=
db
.
RecordType
(
name
=
"
Human Food
"
,
description
=
"
Food that can be eaten only by humans
"
).
insert
()
food
=
(
"
Bread
"
,
"
Tomatoes
"
,
"
Twinkies
"
)
cont
=
db
.
Container
()
for
i
in
range
(
len
(
food
)):
rec
=
db
.
Record
(
food
[
i
])
rec
.
add_parent
(
name
=
"
Human Food
"
)
cont
.
append
(
rec
)
cont
.
insert
()
def
main
():
"""
The main function of this script.
"""
"""
Create some test entities
"""
create_test_entities
()
"""
Create new users
"""
db
.
connection
.
connection
.
get_connection
().
_login
()
human
,
alien
=
assert_user_and_role
()
"""
Load the newly created entities.
"""
# public, private, undefined entities
entities
=
get_entities
(
count
=
3
)
"""
Set permission for the entities (only humans are allowed to eat human food)
"""
set_permission
(
human
[
1
][
0
],
alien
[
1
][
0
],
entities
)
"""
Test the permissions
"""
test_permission
((
human
[
0
],
"
Human_Rememberable_Password_1234
"
),
(
alien
[
0
],
"
4321_Syxax
"
),
entities
)
...
...
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