@@ -23,7 +23,7 @@ import play.api.libs.json.{JsObject, Json}
23
23
24
24
import java .util .regex .Pattern
25
25
import java .util .{List => JList , Map => JMap }
26
- import javax .inject .{Inject , Named , Singleton }
26
+ import javax .inject .{Inject , Named , Provider , Singleton }
27
27
import scala .util .{Failure , Success , Try }
28
28
29
29
@ Singleton
@@ -32,14 +32,33 @@ class UserSrv @Inject() (
32
32
roleSrv : RoleSrv ,
33
33
auditSrv : AuditSrv ,
34
34
attachmentSrv : AttachmentSrv ,
35
+ organisationSrvProvider : Provider [OrganisationSrv ],
35
36
@ Named (" integrity-check-actor" ) integrityCheckActor : ActorRef
36
37
) extends VertexSrv [User ] {
38
+ lazy val organisationSrv : OrganisationSrv = organisationSrvProvider.get
39
+
37
40
val defaultUserDomain : Option [String ] = configuration.getOptional[String ](" auth.defaultUserDomain" )
38
41
val fullUserNameRegex : Pattern = " [\\ p{Graph}&&[^@.]](?:[\\ p{Graph}&&[^@]]*)*@\\ p{Alnum}+(?:[\\ p{Alnum}-.])*" .r.pattern
39
42
40
43
val userAttachmentSrv = new EdgeSrv [UserAttachment , User , Attachment ]
41
44
42
- def checkUser (user : User ): Try [User ] = {
45
+ def addOrCreateUser (user : User , avatar : Option [FFile ], organisation : Organisation with Entity , profile : Profile with Entity )(implicit
46
+ graph : Graph ,
47
+ authContext : AuthContext
48
+ ): Try [RichUser ] =
49
+ getByName(user.login)
50
+ .getOrFail(" User" )
51
+ .orElse {
52
+ for {
53
+ validUser <- checkUserLogin(user)
54
+ _ <- checkUserQuota(organisation)
55
+ createdUser <- createEntity(validUser)
56
+ _ <- avatar.map(setAvatar(createdUser, _)).flip
57
+ } yield createdUser
58
+ }
59
+ .flatMap(addUserToOrganisation(_, organisation, profile))
60
+
61
+ def checkUserLogin (user : User ): Try [User ] = {
43
62
val login =
44
63
if (! user.login.contains('@' ) && defaultUserDomain.isDefined) s " ${user.login}@ ${defaultUserDomain.get}" .toLowerCase
45
64
else user.login.toLowerCase
@@ -48,6 +67,19 @@ class UserSrv @Inject() (
48
67
else Failure (BadRequestError (s " User login is invalid, it must be an email address (found: ${user.login}) " ))
49
68
}
50
69
70
+ def checkUserQuota (organisation : Organisation with Entity )(implicit
71
+ graph : Graph ,
72
+ authContext : AuthContext
73
+ ): Try [Unit ] = {
74
+ val userQuota = configuration.getOptional[Long ](" quota.organisation.user.count" )
75
+ val userCount = organisationSrv.get(organisation).users.getCount
76
+
77
+ userQuota.fold[Try [Unit ]](Success (()))(quota =>
78
+ if (userCount < quota) Success (())
79
+ else Failure (BadRequestError (s " User quota is reached, this organisation cannot have more users " ))
80
+ )
81
+ }
82
+
51
83
// TODO return Try[Unit]
52
84
def addUserToOrganisation (user : User with Entity , organisation : Organisation with Entity , profile : Profile with Entity )(implicit
53
85
graph : Graph ,
@@ -64,21 +96,6 @@ class UserSrv @Inject() (
64
96
} yield richUser
65
97
}
66
98
67
- def addOrCreateUser (user : User , avatar : Option [FFile ], organisation : Organisation with Entity , profile : Profile with Entity )(implicit
68
- graph : Graph ,
69
- authContext : AuthContext
70
- ): Try [RichUser ] =
71
- getByName(user.login)
72
- .getOrFail(" User" )
73
- .orElse {
74
- for {
75
- validUser <- checkUser(user)
76
- createdUser <- createEntity(validUser)
77
- _ <- avatar.map(setAvatar(createdUser, _)).flip
78
- } yield createdUser
79
- }
80
- .flatMap(addUserToOrganisation(_, organisation, profile))
81
-
82
99
def canSetPassword (user : User with Entity )(implicit graph : Graph , authContext : AuthContext ): Boolean = {
83
100
val userOrganisations = get(user).organisations.value(_.name).toSet
84
101
val operatorOrganisations = current.organisations(Permissions .manageUser).value(_.name).toSeq
0 commit comments