Skip to content

Commit 92b4c82

Browse files
committed
add exp permissions example
1 parent 7d5e745 commit 92b4c82

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
import datetime
3+
import json
4+
5+
###############
6+
# DESCRIPTION #
7+
##############
8+
# In this script, we get all the experiments of a given user, and set the visibility (canread) and edition (canwrite) permissions to:
9+
# Base: only owner and admin + team group 2
10+
# This script would typically be run by an Admin user
11+
##############
12+
13+
# the python library for elabftw
14+
import elabapi_python
15+
16+
#########################
17+
# CONFIG #
18+
#########################
19+
# replace with the URL of your instance
20+
API_HOST_URL = 'https://elab.local:3148/api/v2'
21+
# replace with your api key
22+
API_KEY = 'apiKey4Test'
23+
#########################
24+
# END CONFIG #
25+
#########################
26+
27+
# Configure the api client
28+
configuration = elabapi_python.Configuration()
29+
configuration.api_key['api_key'] = API_KEY
30+
configuration.api_key_prefix['api_key'] = 'Authorization'
31+
configuration.host = API_HOST_URL
32+
configuration.debug = False
33+
configuration.verify_ssl = False
34+
35+
# create an instance of the API class
36+
api_client = elabapi_python.ApiClient(configuration)
37+
# fix issue with Authorization header not being properly set by the generated lib
38+
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)
39+
40+
#### SCRIPT START ##################
41+
# Load the experiments api
42+
experimentsApi = elabapi_python.ExperimentsApi(api_client)
43+
44+
# all experiments belonging to that user will get modified
45+
target_userid = 2
46+
# make sure this team group exists!
47+
# to get its id go to /api/v2/teams/current/teamgroups
48+
target_teamgroup = 2
49+
50+
# This is the permission setting we will assign to the experiments
51+
# base:20 means "User + Admin" (see https://github.com/elabftw/elabftw/blob/b193d9fc738ab2635e07317ad83f8c5c1c50413a/src/enums/BasePermissions.php#L17)
52+
canwrite = canread = { 'base': 20, 'teams': [], 'teamgroups': [target_teamgroup], 'users': [] }
53+
54+
# get a list of experiments for a given user
55+
experiments = experimentsApi.read_experiments(owner=target_userid,limit=9999)
56+
for exp in experiments:
57+
experimentsApi.patch_experiment(exp.id, body={'canread': json.dumps(canread), 'canwrite': json.dumps(canwrite)})

examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ Look for all timestamped experiments last week for a particular user and save a
5757
# [13-events.py](./13-events.py)
5858

5959
Get a list of events (booked slots) for the current team.
60+
61+
# [14-fix-user-exp-permissions.py](./14-fix-user-exp-permissions.py)
62+
63+
Look for all experiments of a given user and adjust permissions of their experiments.

0 commit comments

Comments
 (0)