Skip to content

Commit f9a95ed

Browse files
author
irinaschubert
authored
fix: inconsistencies in groups and projects (DEV-261) (#121)
* Make user's group optional in ontology creation * Make user's projects optional in ontology creation * Update dsp-tools-create.md * omit the use of the tmp variable * remove lambda functions inside loops * Update onto_create_ontology.py
1 parent f5ae471 commit f9a95ed

2 files changed

Lines changed: 78 additions & 50 deletions

File tree

‎docs/dsp-tools-create.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ Example:
401401
}
402402
```
403403

404-
The `users` element is optional. It is currently not recommended using it.
404+
The `users` element is optional.
405405

406406
### Ontologies
407407

‎knora/dsplib/utils/onto_create_ontology.py‎

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def create_ontology(input_file: str,
125125
# create the lists
126126
list_root_nodes = create_lists(input_file, lists_file, server, user, password, verbose)
127127

128-
# create the groups
128+
# create the group(s)
129129
if verbose:
130130
print("Create groups...")
131131

@@ -148,73 +148,101 @@ def create_ontology(input_file: str,
148148
print("Groups:")
149149
new_group.print() # project.set_default_permissions(new_group.id)
150150

151-
# create the users
151+
# create the user(s)
152152
if verbose:
153153
print("Create users...")
154154
all_groups: List[Group] = []
155155
all_projects: List[Project] = []
156156
users = data_model["project"].get('users')
157157
if users is not None:
158158
for user in users:
159+
159160
sysadmin = False
160161
group_ids: Set[str] = set()
161-
for groupname in user["groups"]:
162-
#
163-
# First we determine the groups the user is in because we can do this in one call
164-
# groupname has the form [proj_shortname]:groupname|"SystemAdmin"
165-
# (projectname omitted = current project)
166-
#
167-
tmp = groupname.split(':')
168-
if len(tmp) > 1:
169-
group = None
170-
if tmp[0] and tmp[0] != '':
171-
# we have 'proj_shortname:groupname
172-
if not all_groups:
173-
all_groups = Group.getAllGroups(con)
174-
tmp_group = list(
175-
filter(lambda g: g.project.shortname == tmp[0] and g.name == tmp[1], all_groups))
176-
assert len(tmp_group) == 1
177-
group = tmp_group[0]
162+
163+
user_groups = user.get("groups")
164+
# if "groups" is provided, add user to the group(s)
165+
if user_groups:
166+
for full_group_name in user_groups:
167+
if verbose:
168+
print(f"Add user to group: {full_group_name}")
169+
# full_group_name has the form [project_shortname]:group_name or SystemAdmin
170+
# if project_shortname is omitted, the group belongs to the current project
171+
tmp = full_group_name.split(':')
172+
173+
if len(tmp) == 2:
174+
project_shortname = tmp[0]
175+
group_name = tmp[1]
176+
177+
group = None
178+
if project_shortname: # full_group_name refers to an already existing group on DSP
179+
# get all groups vom DSP
180+
if not all_groups:
181+
all_groups = Group.getAllGroups(con)
182+
183+
# check that group exists
184+
for g in all_groups:
185+
if g.project.shortname == project_shortname and g.name == group_name:
186+
group = g
187+
188+
else: # full_group_name refers to a group inside the same ontology
189+
group = new_groups.get(group_name)
190+
assert group is not None
191+
group_ids.add(group.id)
192+
elif len(tmp) == 1:
193+
if tmp[0] == "SystemAdmin":
194+
sysadmin = True
178195
else:
179-
# we have ':groupname' and add to current project
180-
group = new_groups.get(tmp[1])
181-
assert group is not None
182-
group_ids.add(group.id)
183-
else:
184-
if tmp[0] == "SystemAdmin":
185-
sysadmin = True
196+
print(f"ERROR Provided group name {full_group_name} for user {user.username} is not valid.")
186197

187198
project_infos: Dict[str, bool] = {}
188-
for projectname in user["projects"]:
189-
# determine the project memberships of the user
190-
# projectname has the form [projectname]:"member"|"admin" (projectname omitted = current project)
191-
tmp = projectname.split(':')
192-
assert len(tmp) == 2
193-
if tmp[0]:
194-
# we have 'proj_shortname:"member"|"admin"'
195-
if not all_projects:
196-
all_projects = project.getAllProjects(con)
197-
tmp_project = list(filter(lambda g: g.shortname == tmp[0], all_projects))
198-
assert len(tmp_project) == 1
199-
in_project = tmp_project[0]
200-
else:
201-
# we have ':"member"|"admin"'
202-
in_project = project
203-
if tmp[1] == "admin":
204-
project_infos[in_project.id] = True
205-
else:
206-
project_infos[in_project.id] = False
199+
200+
user_projects = user.get("projects")
201+
# if "groups" is provided, add user to the group(s)
202+
if user_projects:
203+
for full_project_name in user_projects:
204+
if verbose:
205+
print(f"Add user to project: {full_project_name}")
206+
# full_project_name has the form [project_name]:member or [project_name]:admin
207+
# if project_name is omitted, the user is added to the current project
208+
tmp = full_project_name.split(':')
209+
210+
if not len(tmp) == 2:
211+
print(f"ERROR Provided project name {full_project_name} for user {user.username} is not valid.")
212+
continue
213+
214+
project_name = tmp[0]
215+
project_role = tmp[1]
216+
217+
in_project = None
218+
219+
if project_name: # project_name is provided
220+
# get all projects vom DSP
221+
if not all_projects:
222+
all_projects = project.getAllProjects(con)
223+
224+
# check that project exists
225+
for p in all_projects:
226+
if p.shortname == project_name:
227+
in_project = p
228+
229+
else: # no project_name provided
230+
in_project = project
231+
232+
if project_role == "admin":
233+
project_infos[in_project.id] = True
234+
else:
235+
project_infos[in_project.id] = False
236+
207237
user_existing = False
208238
tmp_user = None
209239
try:
210-
tmp_user = User(con,
211-
username=user["username"]).read()
240+
tmp_user = User(con, username=user["username"]).read()
212241
except BaseError as err:
213242
pass
214243
if tmp_user is None:
215244
try:
216-
tmp_user = User(con,
217-
email=user["email"]).read()
245+
tmp_user = User(con, email=user["email"]).read()
218246
except BaseError as err:
219247
pass
220248
if tmp_user:

0 commit comments

Comments
 (0)