-
Notifications
You must be signed in to change notification settings - Fork 18
/
UsersMessagesADM.scala
704 lines (635 loc) · 26.2 KB
/
UsersMessagesADM.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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
* Copyright © 2021 - 2024 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.messages.admin.responder.usersmessages
import org.apache.pekko.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json.*
import zio.prelude.Validation
import java.util.UUID
import dsp.errors.BadRequestException
import dsp.errors.ValidationException
import dsp.valueobjects.LanguageCode
import org.knora.webapi.*
import org.knora.webapi.core.RelayedMessage
import org.knora.webapi.messages.ResponderRequest.KnoraRequestADM
import org.knora.webapi.messages.admin.responder.AdminKnoraResponseADM
import org.knora.webapi.messages.admin.responder.KnoraResponseADM
import org.knora.webapi.messages.admin.responder.groupsmessages.GroupADM
import org.knora.webapi.messages.admin.responder.groupsmessages.GroupsADMJsonProtocol
import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionsADMJsonProtocol
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectADM
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectsADMJsonProtocol
import org.knora.webapi.slice.admin.domain.model.*
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// API requests
/**
* Represents an API request payload that asks the Knora API server to create a new user.
*
* @param id the optional IRI of the user to be created (unique).
* @param username the username of the user to be created (unique).
* @param email the email of the user to be created (unique).
* @param givenName the given name of the user to be created.
* @param familyName the family name of the user to be created
* @param password the password of the user to be created.
* @param status the status of the user to be created (active = true, inactive = false).
* @param lang the default language of the user to be created.
* @param systemAdmin the system admin membership.
*/
case class CreateUserApiRequestADM(
id: Option[IRI] = None,
username: String,
email: String,
givenName: String,
familyName: String,
password: String,
status: Boolean,
lang: String,
systemAdmin: Boolean
) {
def toJsValue: JsValue = UsersADMJsonProtocol.createUserApiRequestADMFormat.write(this)
}
/**
* Represents an API request payload that asks the Knora API server to update an existing user. Information that can
* be changed are: user's username, email, given name, family name, language, user status, and system admin membership.
*
* @param username the new username. Needs to be unique on the server.
* @param email the new email address. Needs to be unique on the server.
* @param givenName the new given name.
* @param familyName the new family name.
* @param lang the new ISO 639-1 code of the new preferred language.
* @param status the new user status (active = true, inactive = false).
* @param systemAdmin the new system admin membership status.
*/
case class ChangeUserApiRequestADM(
username: Option[String] = None,
email: Option[String] = None,
givenName: Option[String] = None,
familyName: Option[String] = None,
lang: Option[String] = None,
status: Option[Boolean] = None,
systemAdmin: Option[Boolean] = None
) {
val parametersCount: Int = List(
username,
email,
givenName,
familyName,
lang,
status,
systemAdmin
).flatten.size
// something needs to be sent, i.e. everything 'None' is not allowed
if (parametersCount == 0) throw BadRequestException("No data sent in API request.")
/* check that only allowed information for the 3 cases (changing status, systemAdmin and basic information) is sent and not more. */
// change status case
if (status.isDefined) {
if (parametersCount > 1) throw BadRequestException("Too many parameters sent for change request.")
}
// change system admin membership case
if (systemAdmin.isDefined) {
if (parametersCount > 1) throw BadRequestException("Too many parameters sent for change request.")
}
// change basic user information case
if (parametersCount > 5) throw BadRequestException("Too many parameters sent for change request.")
def toJsValue: JsValue = UsersADMJsonProtocol.changeUserApiRequestADMFormat.write(this)
}
case class ChangeUserPasswordApiRequestADM(requesterPassword: Option[String], newPassword: Option[String]) {
def toJsValue: JsValue = UsersADMJsonProtocol.changeUserPasswordApiRequestADMFormat.write(this)
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Messages
/**
* An abstract trait representing message that can be sent to `UsersResponderADM`.
*/
sealed trait UsersResponderRequestADM extends KnoraRequestADM with RelayedMessage
/**
* Get all information about all users in form of a sequence of [[User]]. Returns an empty sequence if
* no users are found. Administration permission checking is skipped.
*
* @param userInformationTypeADM the extent of the information returned.
* @param requestingUser the user that is making the request.
*/
case class UsersGetADM(
userInformationTypeADM: UserInformationTypeADM = UserInformationTypeADM.Short,
requestingUser: User
) extends UsersResponderRequestADM
/**
* Get all information about all users in form of [[UsersGetResponseADM]]. The UsersResponderRequestADM returns either
* something or a NotFound exception if there are no users found. Administration permission checking is performed.
*
* @param userInformationTypeADM the extent of the information returned.
* @param requestingUser the user initiating the request.
*/
case class UsersGetRequestADM(
userInformationTypeADM: UserInformationTypeADM = UserInformationTypeADM.Short,
requestingUser: User
) extends UsersResponderRequestADM
/**
* A message that requests a user's profile by IRI. A successful response will be a [[User]].
*
* @param identifier the IRI of the user to be queried.
* @param userInformationTypeADM the extent of the information returned.
* @param requestingUser the user initiating the request.
*/
case class UserGetByIriADM(
identifier: UserIri,
userInformationTypeADM: UserInformationTypeADM = UserInformationTypeADM.Short,
requestingUser: User
) extends UsersResponderRequestADM
/**
* A message that requests a user's profile by username. A successful response will be a [[User]].
*
* @param username the username of the user to be queried.
* @param userInformationTypeADM the extent of the information returned.
* @param requestingUser the user initiating the request.
*/
case class UserGetByUsernameADM(
username: Username,
userInformationTypeADM: UserInformationTypeADM = UserInformationTypeADM.Short,
requestingUser: User
) extends UsersResponderRequestADM
/**
* A message that requests a user's profile by email. A successful response will be a [[User]].
*
* @param email the email of the user to be queried.
* @param userInformationTypeADM the extent of the information returned.
* @param requestingUser the user initiating the request.
*/
case class UserGetByEmailADM(
email: Email,
userInformationTypeADM: UserInformationTypeADM = UserInformationTypeADM.Short,
requestingUser: User
) extends UsersResponderRequestADM
/**
* A message that requests a user's profile by email. A successful response will be a [[UserResponseADM]].
*
* @param email the email of the user to be queried.
* @param userInformationTypeADM the extent of the information returned.
* @param requestingUser the user initiating the request.
*/
case class UserGetByEmailRequestADM(
email: Email,
userInformationTypeADM: UserInformationTypeADM = UserInformationTypeADM.Short,
requestingUser: User
) extends UsersResponderRequestADM
/**
* A message that requests a user's profile by username. A successful response will be a [[UserResponseADM]].
*
* @param username the username of the user to be queried.
* @param userInformationTypeADM the extent of the information returned.
* @param requestingUser the user initiating the request.
*/
case class UserGetByUsernameRequestADM(
username: Username,
userInformationTypeADM: UserInformationTypeADM = UserInformationTypeADM.Short,
requestingUser: User
) extends UsersResponderRequestADM
/**
* Requests the creation of a new user.
*
* @param userCreatePayloadADM the [[UserCreatePayloadADM]] information used for creating the new user.
* @param requestingUser the user creating the new user.
* @param apiRequestID the ID of the API request.
*/
case class UserCreateRequestADM(
userCreatePayloadADM: UserCreatePayloadADM,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Request updating of an existing user.
*
* @param userIri the IRI of the user to be updated.
* @param userUpdateBasicInformationPayload the [[UserUpdateBasicInformationPayloadADM]] object containing the data to be updated.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserChangeBasicInformationRequestADM(
userIri: IRI,
userUpdateBasicInformationPayload: UserUpdateBasicInformationPayloadADM,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Request updating the users password.
*
* @param userIri the IRI of the user to be updated.
* @param userUpdatePasswordPayload the [[UserUpdatePasswordPayloadADM]] object containing the old and new password.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserChangePasswordRequestADM(
userIri: IRI,
userUpdatePasswordPayload: UserUpdatePasswordPayloadADM,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Request updating the users status ('knora-base:isActiveUser' property)
*
* @param userIri the IRI of the user to be updated.
* @param status the [[UserStatus]] containing the new status (true / false).
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserChangeStatusRequestADM(
userIri: IRI,
status: UserStatus,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Request updating the users system admin status ('knora-base:isInSystemAdminGroup' property)
*
* @param userIri the IRI of the user to be updated.
* @param systemAdmin the [[SystemAdmin]] value object containing the new system admin membership status (true / false).
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserChangeSystemAdminMembershipStatusRequestADM(
userIri: IRI,
systemAdmin: SystemAdmin,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Requests user's project memberships.
*
* @param userIri the IRI of the user.
* @param requestingUser the user initiating the request.
*/
case class UserProjectMembershipsGetRequestADM(
userIri: IRI,
requestingUser: User
) extends UsersResponderRequestADM
/**
* Requests adding the user to a project.
*
* @param userIri the IRI of the user to be updated.
* @param projectIri the IRI of the project.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserProjectMembershipAddRequestADM(
userIri: IRI,
projectIri: IRI,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Requests removing the user from a project.
*
* @param userIri the IRI of the user to be updated.
* @param projectIri the IRI of the project.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserProjectMembershipRemoveRequestADM(
userIri: IRI,
projectIri: IRI,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Requests user's project admin memberships.
*
* @param userIri the IRI of the user.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserProjectAdminMembershipsGetRequestADM(
userIri: IRI,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Requests adding the user to a project as project admin.
*
* @param userIri the IRI of the user to be updated.
* @param projectIri the IRI of the project.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserProjectAdminMembershipAddRequestADM(
userIri: IRI,
projectIri: IRI,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Requests removing the user from a project as project admin.
*
* @param userIri the IRI of the user to be updated.
* @param projectIri the IRI of the project.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserProjectAdminMembershipRemoveRequestADM(
userIri: IRI,
projectIri: IRI,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Requests user's group memberships.
*
* @param userIri the IRI of the user.
* @param requestingUser the user initiating the request.
*/
case class UserGroupMembershipsGetRequestADM(
userIri: IRI,
requestingUser: User
) extends UsersResponderRequestADM
/**
* Requests adding the user to a group.
*
* @param userIri the IRI of the user to be updated.
* @param groupIri the IRI of the group.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserGroupMembershipAddRequestADM(
userIri: IRI,
groupIri: IRI,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
/**
* Requests removing the user from a group.
*
* @param userIri the IRI of the user to be updated.
* @param groupIri the IRI of the group.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class UserGroupMembershipRemoveRequestADM(
userIri: IRI,
groupIri: IRI,
requestingUser: User,
apiRequestID: UUID
) extends UsersResponderRequestADM
// Responses
/**
* Represents an answer to a request for a list of all users.
*
* @param users a sequence of user profiles of the requested type.
*/
case class UsersGetResponseADM(users: Seq[User]) extends AdminKnoraResponseADM {
def toJsValue: JsValue = UsersADMJsonProtocol.usersGetResponseADMFormat.write(this)
}
/**
* Represents an answer to a user profile request.
*
* @param user the user's information of the requested type.
*/
case class UserResponseADM(user: User) extends AdminKnoraResponseADM {
def toJsValue: JsValue = UsersADMJsonProtocol.userProfileResponseADMFormat.write(this)
}
/**
* Represents an answer to a request for a list of all projects the user is member of.
*
* @param projects a sequence of projects the user is member of.
*/
case class UserProjectMembershipsGetResponseADM(projects: Seq[ProjectADM]) extends KnoraResponseADM {
def toJsValue: JsValue = UsersADMJsonProtocol.userProjectMembershipsGetResponseADMFormat.write(this)
}
/**
* Represents an answer to a request for a list of all projects the user is member of the project admin group.
*
* @param projects a sequence of projects the user is member of the project admin group.
*/
case class UserProjectAdminMembershipsGetResponseADM(projects: Seq[ProjectADM]) extends KnoraResponseADM {
def toJsValue: JsValue = UsersADMJsonProtocol.userProjectAdminMembershipsGetResponseADMFormat.write(this)
}
/**
* Represents an answer to a request for a list of all groups the user is member of.
*
* @param groups a sequence of groups the user is member of.
*/
case class UserGroupMembershipsGetResponseADM(groups: Seq[GroupADM]) extends KnoraResponseADM {
def toJsValue: JsValue = UsersADMJsonProtocol.userGroupMembershipsGetResponseADMFormat.write(this)
}
/**
* Represents an answer to a user creating/modifying operation.
*
* @param user the new user profile of the created/modified user.
*/
case class UserOperationResponseADM(user: User) extends AdminKnoraResponseADM {
def toJsValue: JsValue = UsersADMJsonProtocol.userOperationResponseADMFormat.write(this)
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Components of messages
/**
* UserInformationTypeADM types:
* full: everything
* restricted: everything without sensitive information, i.e. token, password, session.
* short: like restricted and additionally without groups, projects and permissions.
* public: temporary: givenName, familyName
*
* Mainly used in combination with the 'ofType' method, to make sure that a request receiving this information
* also returns the user profile of the correct type. Should be used in cases where we don't want to expose
* sensitive information to the outside world. Since in API Admin [[User]] is returned with some responses,
* we use 'restricted' in those cases.
*/
sealed trait UserInformationTypeADM
object UserInformationTypeADM {
case object Public extends UserInformationTypeADM
case object Short extends UserInformationTypeADM
case object Restricted extends UserInformationTypeADM
case object Full extends UserInformationTypeADM
}
/**
* Payload used for updating an existing user.
*
* @param username the new username.
* @param email the new email address. Needs to be unique on the server.
* @param givenName the new given name.
* @param familyName the new family name.
* @param status the new status.
* @param lang the new language.
* @param projects the new project memberships list.
* @param projectsAdmin the new projects admin membership list.
* @param groups the new group memberships list.
* @param systemAdmin the new system admin membership
*/
case class UserChangeRequestADM(
username: Option[Username] = None,
email: Option[Email] = None,
givenName: Option[GivenName] = None,
familyName: Option[FamilyName] = None,
status: Option[UserStatus] = None,
lang: Option[LanguageCode] = None,
projects: Option[Seq[IRI]] = None,
projectsAdmin: Option[Seq[IRI]] = None,
groups: Option[Seq[IRI]] = None,
systemAdmin: Option[SystemAdmin] = None
) {
val parametersCount: Int = List(
username,
email,
givenName,
familyName,
status,
lang,
projects,
projectsAdmin,
groups,
systemAdmin
).flatten.size
// something needs to be sent, i.e. everything 'None' is not allowed
if (parametersCount == 0) {
throw BadRequestException("No data sent in API request.")
}
// change status case
if (status.isDefined && parametersCount > 1) {
throw BadRequestException("Too many parameters sent for user status change.")
}
// change system admin membership case
if (systemAdmin.isDefined && parametersCount > 1) {
throw BadRequestException("Too many parameters sent for system admin membership change.")
}
// change project memberships (could also involve changing projectAdmin memberships)
if (
projects.isDefined && projectsAdmin.isDefined && parametersCount > 2 ||
projects.isDefined && projectsAdmin.isEmpty && parametersCount > 1
) {
throw BadRequestException("Too many parameters sent for project membership change.")
}
// change projectAdmin memberships only (without changing project memberships)
if (projectsAdmin.isDefined && projects.isEmpty && parametersCount > 1) {
throw BadRequestException("Too many parameters sent for projectAdmin membership change.")
}
// change group memberships
if (groups.isDefined && parametersCount > 1) {
throw BadRequestException("Too many parameters sent for group membership change.")
}
// change basic user information case
if (parametersCount > 5) {
throw BadRequestException("Too many parameters sent for basic user information change.")
}
}
/**
* Payload used for updating basic information of an existing user.
*
* @param username the new username.
* @param email the new email address. Needs to be unique on the server.
* @param givenName the new given name.
* @param familyName the new family name.
* @param lang the new language.
*/
case class UserUpdateBasicInformationPayloadADM(
username: Option[Username] = None,
email: Option[Email] = None,
givenName: Option[GivenName] = None,
familyName: Option[FamilyName] = None,
lang: Option[LanguageCode] = None
) {
def isAtLeastOneParamSet: Boolean = Seq(username, email, givenName, familyName, lang).flatten.nonEmpty
}
object UserUpdateBasicInformationPayloadADM {
private def validateWithOptionOrNone[I, O, E](opt: Option[I], f: I => Validation[E, O]): Validation[E, Option[O]] =
opt.map(f(_).map(Some(_))).getOrElse(Validation.succeed(None))
def make(req: ChangeUserApiRequestADM): Validation[ValidationException, UserUpdateBasicInformationPayloadADM] =
Validation.validateWith(
validateWithOptionOrNone(req.username, Username.validationFrom).mapError(ValidationException(_)),
validateWithOptionOrNone(req.email, Email.validationFrom).mapError(ValidationException(_)),
validateWithOptionOrNone(req.givenName, GivenName.validationFrom).mapError(ValidationException(_)),
validateWithOptionOrNone(req.familyName, FamilyName.validationFrom).mapError(ValidationException(_)),
validateWithOptionOrNone(req.lang, LanguageCode.make)
)(UserUpdateBasicInformationPayloadADM.apply)
}
case class UserUpdatePasswordPayloadADM(requesterPassword: Password, newPassword: Password)
object UserUpdatePasswordPayloadADM {
def make(apiRequest: ChangeUserPasswordApiRequestADM): Validation[String, UserUpdatePasswordPayloadADM] = {
val requesterPasswordValidation = apiRequest.requesterPassword
.map(Password.validationFrom)
.getOrElse(Validation.fail("The requester's password is missing."))
val newPasswordValidation = apiRequest.newPassword
.map(Password.validationFrom)
.getOrElse(Validation.fail("The new password is missing."))
Validation.validateWith(requesterPasswordValidation, newPasswordValidation)(UserUpdatePasswordPayloadADM.apply)
}
}
/**
* Represents an answer to a group membership request.
*
* @param members the group's members.
*/
case class GroupMembersGetResponseADM(members: Seq[User]) extends AdminKnoraResponseADM {
def toJsValue = UsersADMJsonProtocol.groupMembersGetResponseADMFormat.write(this)
}
final case class UserCreatePayloadADM(
id: Option[UserIri] = None,
username: Username,
email: Email,
givenName: GivenName,
familyName: FamilyName,
password: Password,
status: UserStatus,
lang: LanguageCode,
systemAdmin: SystemAdmin
)
object UserCreatePayloadADM {
def make(apiRequest: CreateUserApiRequestADM): Validation[String, UserCreatePayloadADM] =
Validation
.validateWith(
apiRequest.id
.map(UserIri.validationFrom(_).map(Some(_)).mapError(ValidationException(_)))
.getOrElse(Validation.succeed(None)),
Username.validationFrom(apiRequest.username).mapError(ValidationException(_)),
Email.validationFrom(apiRequest.email).mapError(ValidationException(_)),
GivenName.validationFrom(apiRequest.givenName).mapError(ValidationException(_)),
FamilyName.validationFrom(apiRequest.familyName).mapError(ValidationException(_)),
Password.validationFrom(apiRequest.password).mapError(ValidationException(_)),
Validation.succeed(UserStatus.from(apiRequest.status)),
LanguageCode.make(apiRequest.lang),
Validation.succeed(SystemAdmin.from(apiRequest.systemAdmin))
)(UserCreatePayloadADM.apply)
.mapError(_.getMessage)
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// JSON formatting
/**
* A spray-json protocol for formatting objects as JSON.
*/
object UsersADMJsonProtocol
extends SprayJsonSupport
with DefaultJsonProtocol
with ProjectsADMJsonProtocol
with GroupsADMJsonProtocol
with PermissionsADMJsonProtocol {
implicit val userADMFormat: JsonFormat[User] = jsonFormat12(User)
implicit val groupMembersGetResponseADMFormat: RootJsonFormat[GroupMembersGetResponseADM] =
jsonFormat(GroupMembersGetResponseADM, "members")
implicit val createUserApiRequestADMFormat: RootJsonFormat[CreateUserApiRequestADM] = jsonFormat(
CreateUserApiRequestADM,
"id",
"username",
"email",
"givenName",
"familyName",
"password",
"status",
"lang",
"systemAdmin"
)
implicit val changeUserApiRequestADMFormat: RootJsonFormat[ChangeUserApiRequestADM] =
jsonFormat(ChangeUserApiRequestADM, "username", "email", "givenName", "familyName", "lang", "status", "systemAdmin")
implicit val changeUserPasswordApiRequestADMFormat: RootJsonFormat[ChangeUserPasswordApiRequestADM] = jsonFormat(
ChangeUserPasswordApiRequestADM,
"requesterPassword",
"newPassword"
)
implicit val usersGetResponseADMFormat: RootJsonFormat[UsersGetResponseADM] = jsonFormat1(UsersGetResponseADM)
implicit val userProfileResponseADMFormat: RootJsonFormat[UserResponseADM] = jsonFormat1(UserResponseADM)
implicit val userProjectMembershipsGetResponseADMFormat: RootJsonFormat[UserProjectMembershipsGetResponseADM] =
jsonFormat1(UserProjectMembershipsGetResponseADM)
implicit val userProjectAdminMembershipsGetResponseADMFormat
: RootJsonFormat[UserProjectAdminMembershipsGetResponseADM] = jsonFormat1(UserProjectAdminMembershipsGetResponseADM)
implicit val userGroupMembershipsGetResponseADMFormat: RootJsonFormat[UserGroupMembershipsGetResponseADM] =
jsonFormat1(UserGroupMembershipsGetResponseADM)
implicit val userOperationResponseADMFormat: RootJsonFormat[UserOperationResponseADM] = jsonFormat1(
UserOperationResponseADM
)
}