/
UsersResponderV1.scala
671 lines (572 loc) · 28.8 KB
/
UsersResponderV1.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.responders.v1
import akka.http.scaladsl.util.FastFuture
import akka.pattern._
import java.util.UUID
import scala.concurrent.Future
import dsp.errors.ApplicationCacheException
import dsp.errors.ForbiddenException
import dsp.errors.NotFoundException
import org.knora.webapi._
import org.knora.webapi.messages.OntologyConstants
import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionDataGetADM
import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionsDataADM
import org.knora.webapi.messages.store.triplestoremessages._
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.messages.util.ResponderData
import org.knora.webapi.messages.util.rdf.SparqlSelectResult
import org.knora.webapi.messages.util.rdf.VariableResultsRow
import org.knora.webapi.messages.v1.responder.projectmessages.ProjectInfoByIRIGetV1
import org.knora.webapi.messages.v1.responder.projectmessages.ProjectInfoV1
import org.knora.webapi.messages.v1.responder.usermessages.UserProfileTypeV1.UserProfileType
import org.knora.webapi.messages.v1.responder.usermessages._
import org.knora.webapi.responders.Responder
import org.knora.webapi.util.cache.CacheUtil
/**
* Provides information about Knora users to other responders.
*/
class UsersResponderV1(responderData: ResponderData) extends Responder(responderData.actorDeps) {
// The IRI used to lock user creation and update
val USERS_GLOBAL_LOCK_IRI = "http://rdfh.ch/users"
val USER_PROFILE_CACHE_NAME = "userProfileCache"
/**
* Receives a message of type [[UsersResponderRequestV1]], and returns an appropriate response message.
*/
def receive(msg: UsersResponderRequestV1) = msg match {
case UsersGetV1(userProfile) => usersGetV1(userProfile)
case UsersGetRequestV1(userProfileV1) => usersGetRequestV1(userProfileV1)
case UserDataByIriGetV1(userIri, short) => userDataByIriGetV1(userIri, short)
case UserProfileByIRIGetV1(userIri, profileType) =>
userProfileByIRIGetV1(userIri, profileType)
case UserProfileByIRIGetRequestV1(userIri, profileType, userProfile) =>
userProfileByIRIGetRequestV1(userIri, profileType, userProfile)
case UserProfileByEmailGetV1(email, profileType) =>
userProfileByEmailGetV1(email, profileType)
case UserProfileByEmailGetRequestV1(email, profileType, userProfile) =>
userProfileByEmailGetRequestV1(email, profileType, userProfile)
case UserProjectMembershipsGetRequestV1(userIri, userProfile, apiRequestID) =>
userProjectMembershipsGetRequestV1(userIri, userProfile, apiRequestID)
case UserProjectAdminMembershipsGetRequestV1(userIri, userProfile, apiRequestID) =>
userProjectAdminMembershipsGetRequestV1(userIri, userProfile, apiRequestID)
case UserGroupMembershipsGetRequestV1(userIri, userProfile, apiRequestID) =>
userGroupMembershipsGetRequestV1(userIri, userProfile, apiRequestID)
case other => handleUnexpectedMessage(other, log, this.getClass.getName)
}
/**
* Gets all the users and returns them as a sequence of [[UserDataV1]].
*
* @return all the users as a sequence of [[UserDataV1]].
*/
private def usersGetV1(userProfileV1: UserProfileV1): Future[Seq[UserDataV1]] =
// log.debug("usersGetV1")
for {
_ <- Future(
if (!userProfileV1.permissionData.isSystemAdmin) {
throw ForbiddenException("SystemAdmin permissions are required.")
}
)
sparqlQueryString <- Future(
org.knora.webapi.messages.twirl.queries.sparql.v1.txt
.getUsers()
.toString()
)
usersResponse <- appActor.ask(SparqlSelectRequest(sparqlQueryString)).mapTo[SparqlSelectResult]
usersResponseRows: Seq[VariableResultsRow] = usersResponse.results.bindings
usersWithProperties: Map[String, Map[String, String]] =
usersResponseRows.groupBy(_.rowMap("s")).map { case (userIri: IRI, rows: Seq[VariableResultsRow]) =>
(userIri, rows.map(row => (row.rowMap("p"), row.rowMap("o"))).toMap)
}
users = usersWithProperties.map { case (userIri: IRI, propsMap: Map[String, String]) =>
UserDataV1(
lang = propsMap.get(OntologyConstants.KnoraAdmin.PreferredLanguage) match {
case Some(langList) => langList
case None => responderData.appConfig.fallbackLanguage
},
user_id = Some(userIri),
email = propsMap.get(OntologyConstants.KnoraAdmin.Email),
firstname = propsMap.get(OntologyConstants.KnoraAdmin.GivenName),
lastname = propsMap.get(OntologyConstants.KnoraAdmin.FamilyName),
status = propsMap.get(OntologyConstants.KnoraAdmin.Status).map(_.toBoolean)
)
}.toSeq
} yield users
/**
* Gets all the users and returns them as a [[UsersGetResponseV1]].
*
* @param userProfileV1 the type of the requested profile (restricted of full).
* @return all the users as a [[UsersGetResponseV1]].
*/
private def usersGetRequestV1(userProfileV1: UserProfileV1): Future[UsersGetResponseV1] =
for {
maybeUsersListToReturn <- usersGetV1(userProfileV1)
result = maybeUsersListToReturn match {
case users: Seq[UserDataV1] if users.nonEmpty => UsersGetResponseV1(users = users)
case _ => throw NotFoundException(s"No users found")
}
} yield result
/**
* Gets basic information about a Knora user, and returns it in a [[UserDataV1]].
*
* @param userIri the IRI of the user.
* @return a [[UserDataV1]] describing the user.
*/
private def userDataByIriGetV1(userIri: IRI, short: Boolean): Future[Option[UserDataV1]] =
// log.debug("userDataByIriGetV1 - userIri: {}", userIri)
for {
sparqlQueryString <- Future(
org.knora.webapi.messages.twirl.queries.sparql.v1.txt
.getUserByIri(
userIri = userIri
)
.toString()
)
// _ = log.debug("userDataByIRIGetV1 - sparqlQueryString: {}", sparqlQueryString)
userDataQueryResponse <- appActor.ask(SparqlSelectRequest(sparqlQueryString)).mapTo[SparqlSelectResult]
maybeUserDataV1 <- userDataQueryResponse2UserDataV1(userDataQueryResponse, short)
// _ = log.debug("userDataByIriGetV1 - maybeUserDataV1: {}", maybeUserDataV1)
} yield maybeUserDataV1
/**
* Gets information about a Knora user, and returns it in a [[UserProfileV1]]. If possible, tries to retrieve the
* user profile from cache. If not, it retrieves it from the triplestore and writes it to the cache.
*
* @param userIri the IRI of the user.
* @param profileType the type of the requested profile (restricted of full).
*
* @return a [[UserProfileV1]] describing the user.
*/
private def userProfileByIRIGetV1(
userIri: IRI,
profileType: UserProfileType
): Future[Option[UserProfileV1]] =
// log.debug(s"userProfileByIRIGetV1: userIri = $userIRI', clean = '$profileType'")
CacheUtil.get[UserProfileV1](USER_PROFILE_CACHE_NAME, userIri) match {
case Some(userProfile) =>
// found a user profile in the cache
log.debug(s"userProfileByIRIGetV1 - cache hit: $userProfile")
FastFuture.successful(Some(userProfile.ofType(profileType)))
case None =>
for {
sparqlQueryString <- Future(
org.knora.webapi.messages.twirl.queries.sparql.v1.txt
.getUserByIri(
userIri = userIri
)
.toString()
)
// _ = log.debug(s"userProfileByIRIGetV1 - sparqlQueryString: {}", sparqlQueryString)
userDataQueryResponse <- appActor.ask(SparqlSelectRequest(sparqlQueryString)).mapTo[SparqlSelectResult]
maybeUserProfileV1 <- userDataQueryResponse2UserProfileV1(
userDataQueryResponse = userDataQueryResponse
)
_ = if (maybeUserProfileV1.nonEmpty) {
writeUserProfileV1ToCache(maybeUserProfileV1.get)
}
result = maybeUserProfileV1.map(_.ofType(profileType))
// _ = log.debug("userProfileByIRIGetV1 - maybeUserProfileV1: {}", MessageUtil.toSource(maybeUserProfileV1))
} yield result // UserProfileV1(userData, groups, projects_info, sessionId, isSystemUser, permissionData)
}
/**
* Gets information about a Knora user, and returns it as a [[UserProfileResponseV1]].
*
* @param userIRI the IRI of the user.
* @param profileType the type of the requested profile (restriced or full).
*
* @param userProfile the requesting user's profile.
* @return a [[UserProfileResponseV1]]
*/
private def userProfileByIRIGetRequestV1(
userIRI: IRI,
profileType: UserProfileType,
userProfile: UserProfileV1
): Future[UserProfileResponseV1] =
for {
_ <- Future(
if (!userProfile.permissionData.isSystemAdmin && !userProfile.userData.user_id.contains(userIRI)) {
throw ForbiddenException("SystemAdmin permissions are required.")
}
)
maybeUserProfileToReturn <- userProfileByIRIGetV1(
userIri = userIRI,
profileType = profileType
)
result = maybeUserProfileToReturn match {
case Some(up) => UserProfileResponseV1(up)
case None => throw NotFoundException(s"User '$userIRI' not found")
}
} yield result
/**
* Gets information about a Knora user, and returns it in a [[UserProfileV1]]. If possible, tries to retrieve the user profile
* from cache. If not, it retrieves it from the triplestore and writes it to the cache.
*
* @param email the email of the user.
* @param profileType the type of the requested profile (restricted or full).
*
* @return a [[UserProfileV1]] describing the user.
*/
private def userProfileByEmailGetV1(
email: String,
profileType: UserProfileType
): Future[Option[UserProfileV1]] =
// log.debug(s"userProfileByEmailGetV1: username = '{}', type = '{}'", email, profileType)
CacheUtil.get[UserProfileV1](USER_PROFILE_CACHE_NAME, email) match {
case Some(userProfile) =>
// found a user profile in the cache
log.debug(s"userProfileByIRIGetV1 - cache hit: $userProfile")
FastFuture.successful(Some(userProfile.ofType(profileType)))
case None =>
for {
sparqlQueryString <- Future(
org.knora.webapi.messages.twirl.queries.sparql.v1.txt
.getUserByEmail(
email = email
)
.toString()
)
// _ = log.debug(s"userProfileByEmailGetV1 - sparqlQueryString: $sparqlQueryString")
userDataQueryResponse <- appActor.ask(SparqlSelectRequest(sparqlQueryString)).mapTo[SparqlSelectResult]
// _ = log.debug(MessageUtil.toSource(userDataQueryResponse))
maybeUserProfileV1 <- userDataQueryResponse2UserProfileV1(
userDataQueryResponse = userDataQueryResponse
)
_ = if (maybeUserProfileV1.nonEmpty) {
writeUserProfileV1ToCache(maybeUserProfileV1.get)
}
result = maybeUserProfileV1.map(_.ofType(profileType))
// _ = log.debug("userProfileByEmailGetV1 - maybeUserProfileV1: {}", MessageUtil.toSource(maybeUserProfileV1))
} yield result // UserProfileV1(userDataV1, groupIris, projectIris)
}
/**
* Gets information about a Knora user, and returns it as a [[UserProfileResponseV1]].
*
* @param email the email of the user.
* @param profileType the type of the requested profile (restricted or full).
*
* @param userProfile the requesting user's profile.
* @return a [[UserProfileResponseV1]]
* @throws NotFoundException if the user with the supplied email is not found.
*/
private def userProfileByEmailGetRequestV1(
email: String,
profileType: UserProfileType,
userProfile: UserProfileV1
): Future[UserProfileResponseV1] =
for {
maybeUserProfileToReturn <- userProfileByEmailGetV1(
email = email,
profileType = profileType
)
result = maybeUserProfileToReturn match {
case Some(up: UserProfileV1) => UserProfileResponseV1(up)
case None => throw NotFoundException(s"User '$email' not found")
}
} yield result
/**
* Returns the user's project memberships, where the result contains the IRIs of the projects the user is member of.
*
* @param userIri the user's IRI.
* @param userProfileV1 the user profile of the requesting user.
* @param apiRequestID the unique api request ID.
* @return a [[UserProjectMembershipsGetResponseV1]].
*/
def userProjectMembershipsGetRequestV1(
userIri: IRI,
userProfileV1: UserProfileV1,
apiRequestID: UUID
): Future[UserProjectMembershipsGetResponseV1] =
for {
sparqlQueryString <- Future(
org.knora.webapi.messages.twirl.queries.sparql.v1.txt
.getUserByIri(
userIri = userIri
)
.toString()
)
// _ = log.debug("userDataByIRIGetV1 - sparqlQueryString: {}", sparqlQueryString)
userDataQueryResponse <- appActor.ask(SparqlSelectRequest(sparqlQueryString)).mapTo[SparqlSelectResult]
groupedUserData: Map[String, Seq[String]] = userDataQueryResponse.results.bindings.groupBy(_.rowMap("p")).map {
case (predicate, rows) => predicate -> rows.map(_.rowMap("o"))
}
/* the projects the user is member of */
projectIris: Seq[IRI] = groupedUserData.get(OntologyConstants.KnoraAdmin.IsInProject) match {
case Some(projects) => projects
case None => Seq.empty[IRI]
}
// _ = log.debug("userProjectMembershipsGetRequestV1 - userIri: {}, projectIris: {}", userIri, projectIris)
} yield UserProjectMembershipsGetResponseV1(projects = projectIris)
/**
* Returns the user's project admin group memberships, where the result contains the IRIs of the projects the user
* is a member of the project admin group.
*
* @param userIri the user's IRI.
* @param userProfileV1 the user profile of the requesting user.
* @param apiRequestID the unique api request ID.
* @return a [[UserProjectMembershipsGetResponseV1]].
*/
def userProjectAdminMembershipsGetRequestV1(
userIri: IRI,
userProfileV1: UserProfileV1,
apiRequestID: UUID
): Future[UserProjectAdminMembershipsGetResponseV1] =
for {
sparqlQueryString <- Future(
org.knora.webapi.messages.twirl.queries.sparql.v1.txt
.getUserByIri(
userIri = userIri
)
.toString()
)
// _ = log.debug("userDataByIRIGetV1 - sparqlQueryString: {}", sparqlQueryString)
userDataQueryResponse <- appActor.ask(SparqlSelectRequest(sparqlQueryString)).mapTo[SparqlSelectResult]
groupedUserData: Map[String, Seq[String]] = userDataQueryResponse.results.bindings.groupBy(_.rowMap("p")).map {
case (predicate, rows) => predicate -> rows.map(_.rowMap("o"))
}
/* the projects the user is member of */
projectIris: Seq[IRI] = groupedUserData.get(OntologyConstants.KnoraAdmin.IsInProjectAdminGroup) match {
case Some(projects) => projects
case None => Seq.empty[IRI]
}
// _ = log.debug("userProjectAdminMembershipsGetRequestV1 - userIri: {}, projectIris: {}", userIri, projectIris)
} yield UserProjectAdminMembershipsGetResponseV1(projects = projectIris)
/**
* Returns the user's custom (without ProjectMember and ProjectAdmin) group memberships
*
* @param userIri the user's IRI.
* @param userProfileV1 the user profile of the requesting user.
* @param apiRequestID the unique api request ID.
* @return a [[UserGroupMembershipsGetResponseV1]]
*/
def userGroupMembershipsGetRequestV1(
userIri: IRI,
userProfileV1: UserProfileV1,
apiRequestID: UUID
): Future[UserGroupMembershipsGetResponseV1] =
for {
sparqlQueryString <- Future(
org.knora.webapi.messages.twirl.queries.sparql.v1.txt
.getUserByIri(
userIri = userIri
)
.toString()
)
// _ = log.debug("userDataByIRIGetV1 - sparqlQueryString: {}", sparqlQueryString)
userDataQueryResponse <- appActor.ask(SparqlSelectRequest(sparqlQueryString)).mapTo[SparqlSelectResult]
groupedUserData: Map[String, Seq[String]] = userDataQueryResponse.results.bindings.groupBy(_.rowMap("p")).map {
case (predicate, rows) => predicate -> rows.map(_.rowMap("o"))
}
/* the groups the user is member of */
groupIris: Seq[IRI] = groupedUserData.get(OntologyConstants.KnoraAdmin.IsInGroup) match {
case Some(projects) => projects
case None => Seq.empty[IRI]
}
// _ = log.debug("userDataByIriGetV1 - maybeUserDataV1: {}", maybeUserDataV1)
} yield UserGroupMembershipsGetResponseV1(groups = groupIris)
////////////////////
// Helper Methods //
////////////////////
/**
* Helper method used to create a [[UserDataV1]] from the [[SparqlSelectResult]] containing user data.
*
* @param userDataQueryResponse a [[SparqlSelectResult]] containing user data.
* @param short denotes if all information should be returned. If short == true, then no token and password should be returned.
* @return a [[UserDataV1]] containing the user's basic data.
*/
private def userDataQueryResponse2UserDataV1(
userDataQueryResponse: SparqlSelectResult,
short: Boolean
): Future[Option[UserDataV1]] =
// log.debug("userDataQueryResponse2UserDataV1 - " + MessageUtil.toSource(userDataQueryResponse))
if (userDataQueryResponse.results.bindings.nonEmpty) {
val returnedUserIri = userDataQueryResponse.getFirstRow.rowMap("s")
val groupedUserData: Map[String, Seq[String]] =
userDataQueryResponse.results.bindings.groupBy(_.rowMap("p")).map { case (predicate, rows) =>
predicate -> rows.map(_.rowMap("o"))
}
// _ = log.debug(s"userDataQueryResponse2UserProfileV1 - groupedUserData: ${MessageUtil.toSource(groupedUserData)}")
val userDataV1 = UserDataV1(
lang = groupedUserData.get(OntologyConstants.KnoraAdmin.PreferredLanguage) match {
case Some(langList) => langList.head
case None => responderData.appConfig.fallbackLanguage
},
user_id = Some(returnedUserIri),
email = groupedUserData.get(OntologyConstants.KnoraAdmin.Email).map(_.head),
firstname = groupedUserData.get(OntologyConstants.KnoraAdmin.GivenName).map(_.head),
lastname = groupedUserData.get(OntologyConstants.KnoraAdmin.FamilyName).map(_.head),
password = if (!short) {
groupedUserData.get(OntologyConstants.KnoraAdmin.Password).map(_.head)
} else None,
status = groupedUserData.get(OntologyConstants.KnoraAdmin.Status).map(_.head.toBoolean)
)
// _ = log.debug(s"userDataQueryResponse - userDataV1: {}", MessageUtil.toSource(userDataV1)")
FastFuture.successful(Some(userDataV1))
} else {
FastFuture.successful(None)
}
/**
* Helper method used to create a [[UserProfileV1]] from the [[SparqlSelectResult]] containing user data.
*
* @param userDataQueryResponse a [[SparqlSelectResult]] containing user data.
* @return a [[UserProfileV1]] containing the user's data.
*/
private def userDataQueryResponse2UserProfileV1(
userDataQueryResponse: SparqlSelectResult
): Future[Option[UserProfileV1]] =
// log.debug("userDataQueryResponse2UserProfileV1 - userDataQueryResponse: {}", MessageUtil.toSource(userDataQueryResponse))
if (userDataQueryResponse.results.bindings.nonEmpty) {
val returnedUserIri = userDataQueryResponse.getFirstRow.rowMap("s")
val groupedUserData: Map[String, Seq[String]] =
userDataQueryResponse.results.bindings.groupBy(_.rowMap("p")).map { case (predicate, rows) =>
predicate -> rows.map(_.rowMap("o"))
}
// log.debug("userDataQueryResponse2UserProfileV1 - groupedUserData: {}", MessageUtil.toSource(groupedUserData))
val userDataV1 = UserDataV1(
lang = groupedUserData.get(OntologyConstants.KnoraAdmin.PreferredLanguage) match {
case Some(langList) => langList.head
case None => responderData.appConfig.fallbackLanguage
},
user_id = Some(returnedUserIri),
email = groupedUserData.get(OntologyConstants.KnoraAdmin.Email).map(_.head),
firstname = groupedUserData.get(OntologyConstants.KnoraAdmin.GivenName).map(_.head),
lastname = groupedUserData.get(OntologyConstants.KnoraAdmin.FamilyName).map(_.head),
password = groupedUserData.get(OntologyConstants.KnoraAdmin.Password).map(_.head),
status = groupedUserData.get(OntologyConstants.KnoraAdmin.Status).map(_.head.toBoolean)
)
// log.debug("userDataQueryResponse2UserProfileV1 - userDataV1: {}", MessageUtil.toSource(userDataV1))
/* the projects the user is member of */
val projectIris: Seq[IRI] = groupedUserData.get(OntologyConstants.KnoraAdmin.IsInProject) match {
case Some(projects) => projects
case None => Seq.empty[IRI]
}
// log.debug(s"userDataQueryResponse2UserProfileV1 - projectIris: ${MessageUtil.toSource(projectIris)}")
/* the groups the user is member of (only explicit groups) */
val groupIris = groupedUserData.get(OntologyConstants.KnoraAdmin.IsInGroup) match {
case Some(groups) => groups
case None => Seq.empty[IRI]
}
// log.debug(s"userDataQueryResponse2UserProfileV1 - groupIris: ${MessageUtil.toSource(groupIris)}")
/* the projects for which the user is implicitly considered a member of the 'http://www.knora.org/ontology/knora-base#ProjectAdmin' group */
val isInProjectAdminGroups =
groupedUserData.getOrElse(OntologyConstants.KnoraAdmin.IsInProjectAdminGroup, Vector.empty[IRI])
/* is the user implicitly considered a member of the 'http://www.knora.org/ontology/knora-base#SystemAdmin' group */
val isInSystemAdminGroup =
groupedUserData.get(OntologyConstants.KnoraAdmin.IsInSystemAdminGroup).exists(p => p.head.toBoolean)
for {
/* get the user's permission profile from the permissions responder */
permissionData <- appActor
.ask(
PermissionDataGetADM(
projectIris = projectIris,
groupIris = groupIris,
isInProjectAdminGroups = isInProjectAdminGroups,
isInSystemAdminGroup = isInSystemAdminGroup,
requestingUser = KnoraSystemInstances.Users.SystemUser
)
)
.mapTo[PermissionsDataADM]
maybeProjectInfoFutures: Seq[Future[Option[ProjectInfoV1]]] =
projectIris.map { projectIri =>
appActor
.ask(
ProjectInfoByIRIGetV1(
iri = projectIri,
userProfileV1 = None
)
)
.mapTo[Option[ProjectInfoV1]]
}
maybeProjectInfos: Seq[Option[ProjectInfoV1]] <- Future.sequence(maybeProjectInfoFutures)
projectInfos = maybeProjectInfos.flatten
projectInfoMap: Map[IRI, ProjectInfoV1] = projectInfos.map(projectInfo => projectInfo.id -> projectInfo).toMap
/* construct the user profile from the different parts */
up = UserProfileV1(
userData = userDataV1,
groups = groupIris,
projects_info = projectInfoMap,
sessionId = None,
permissionData = permissionData
)
// _ = log.debug("Retrieved UserProfileV1: {}", up.toString)
result: Option[UserProfileV1] = Some(up)
} yield result
} else {
FastFuture.successful(None)
}
/**
* Helper method for checking if a user exists.
*
* @param userIri the IRI of the user.
* @return a [[Boolean]].
*/
def userExists(userIri: IRI): Future[Boolean] =
for {
askString <- Future(
org.knora.webapi.messages.twirl.queries.sparql.v1.txt.checkUserExists(userIri = userIri).toString
)
// _ = log.debug("userExists - query: {}", askString)
checkUserExistsResponse <- appActor.ask(SparqlAskRequest(askString)).mapTo[SparqlAskResponse]
result = checkUserExistsResponse.result
} yield result
/**
* Helper method for checking if a project exists.
*
* @param projectIri the IRI of the project.
* @return a [[Boolean]].
*/
def projectExists(projectIri: IRI): Future[Boolean] =
for {
askString <- Future(
org.knora.webapi.messages.twirl.queries.sparql.admin.txt
.checkProjectExistsByIri(projectIri = projectIri)
.toString
)
// _ = log.debug("projectExists - query: {}", askString)
checkUserExistsResponse <- appActor.ask(SparqlAskRequest(askString)).mapTo[SparqlAskResponse]
result = checkUserExistsResponse.result
} yield result
/**
* Helper method for checking if a group exists.
*
* @param groupIri the IRI of the group.
* @return a [[Boolean]].
*/
def groupExists(groupIri: IRI): Future[Boolean] =
for {
askString <-
Future(
org.knora.webapi.messages.twirl.queries.sparql.admin.txt.checkGroupExistsByIri(groupIri = groupIri).toString
)
// _ = log.debug("groupExists - query: {}", askString)
checkUserExistsResponse <- appActor.ask(SparqlAskRequest(askString)).mapTo[SparqlAskResponse]
result = checkUserExistsResponse.result
} yield result
/**
* Writes the user profile to cache.
*
* @param userProfile a [[UserProfileV1]].
* @return true if writing was successful.
* @throws ApplicationCacheException when there is a problem with writing the user's profile to cache.
*/
private def writeUserProfileV1ToCache(userProfile: UserProfileV1): Boolean = {
val iri = if (userProfile.userData.user_id.nonEmpty) {
userProfile.userData.user_id.get
} else {
throw ApplicationCacheException("A user profile without an IRI is invalid. Not writing to cache.")
}
val email = if (userProfile.userData.email.nonEmpty) {
userProfile.userData.email.get
} else {
throw ApplicationCacheException("A user profile without an email is invalid. Not writing to cache.")
}
CacheUtil.put(USER_PROFILE_CACHE_NAME, iri, userProfile)
if (CacheUtil.get(USER_PROFILE_CACHE_NAME, iri).isEmpty) {
throw ApplicationCacheException("Writing the user's profile to cache was not successful.")
}
CacheUtil.put(USER_PROFILE_CACHE_NAME, email, userProfile)
if (CacheUtil.get(USER_PROFILE_CACHE_NAME, email).isEmpty) {
throw ApplicationCacheException("Writing the user's profile to cache was not successful.")
}
true
}
}