From 4b6da24d354ff97ab721848eb2f9782cdde4f911 Mon Sep 17 00:00:00 2001 From: Philipp Otto Date: Tue, 29 Nov 2022 13:51:21 +0100 Subject: [PATCH 1/4] add second (non-admin) user to initial data trigger --- app/controllers/InitialDataController.scala | 42 +++++++++++++++++++++ app/utils/WkConf.scala | 1 + conf/application.conf | 1 + 3 files changed, 44 insertions(+) diff --git a/app/controllers/InitialDataController.scala b/app/controllers/InitialDataController.scala index d7307e8239f..a04c4ffd8ca 100644 --- a/app/controllers/InitialDataController.scala +++ b/app/controllers/InitialDataController.scala @@ -53,6 +53,7 @@ class InitialDataService @Inject()(userService: UserService, implicit val ctx: GlobalAccessContext.type = GlobalAccessContext private val defaultUserEmail = conf.WebKnossos.SampleOrganization.User.email + private val defaultUserEmail2 = conf.WebKnossos.SampleOrganization.User.email2 private val defaultUserPassword = conf.WebKnossos.SampleOrganization.User.password private val defaultUserToken = conf.WebKnossos.SampleOrganization.User.token private val additionalInformation = """**Sample Organization** @@ -73,6 +74,8 @@ Samplecountry Team(organizationTeamId, defaultOrganization._id, defaultOrganization.name, isOrganizationTeam = true) private val userId = ObjectId.generate private val multiUserId = ObjectId.generate + private val userId2 = ObjectId.generate + private val multiUserId2 = ObjectId.generate private val defaultMultiUser = MultiUser( multiUserId, defaultUserEmail, @@ -94,6 +97,27 @@ Samplecountry isDeactivated = false, lastTaskTypeId = None ) + private val defaultMultiUser2 = MultiUser( + multiUserId2, + defaultUserEmail2, + userService.createPasswordInfo(defaultUserPassword), + isSuperUser = false, + ) + private val defaultUser2 = User( + userId2, + multiUserId2, + defaultOrganization._id, + "Second", + "User", + System.currentTimeMillis(), + Json.obj(), + userService.createLoginInfo(userId2), + isAdmin = false, + isDatasetManager = false, + isUnlisted = false, + isDeactivated = false, + lastTaskTypeId = None + ) private val defaultPublication = Publication( ObjectId("5c766bec6c01006c018c7459"), Some(System.currentTimeMillis()), @@ -114,6 +138,7 @@ Samplecountry _ <- insertOrganization() _ <- insertTeams() _ <- insertDefaultUser() + _ <- insertSecondDefaultUser() _ <- insertToken() _ <- insertTaskType() _ <- insertProject() @@ -149,6 +174,23 @@ Samplecountry } .toFox + private def insertSecondDefaultUser(): Fox[Unit] = + userService + .userFromMultiUserEmail(defaultUserEmail2) + .futureBox + .flatMap { + case Full(_) => Fox.successful(()) + case _ => + for { + _ <- multiUserDAO.insertOne(defaultMultiUser2) + _ <- userDAO.insertOne(defaultUser2) + _ <- userTeamRolesDAO.insertTeamMembership(defaultUser2._id, + TeamMembership(organizationTeam._id, isTeamManager = false)) + _ = logger.info("Inserted second default user") + } yield () + } + .toFox + private def insertToken(): Fox[Unit] = { val expiryTime = conf.Silhouette.TokenAuthenticator.authenticatorExpiry.toMillis tokenDAO.findOneByLoginInfo("credentials", defaultUser._id.id, TokenType.Authentication).futureBox.flatMap { diff --git a/app/utils/WkConf.scala b/app/utils/WkConf.scala index 36b9afe1186..575b931f33c 100644 --- a/app/utils/WkConf.scala +++ b/app/utils/WkConf.scala @@ -52,6 +52,7 @@ class WkConf @Inject()(configuration: Configuration) extends ConfigReader with L object User { val email: String = get[String]("webKnossos.sampleOrganization.user.email") + val email2: String = get[String]("webKnossos.sampleOrganization.user.email2") val password: String = get[String]("webKnossos.sampleOrganization.user.password") val token: String = get[String]("webKnossos.sampleOrganization.user.token") val isSuperUser: Boolean = get[Boolean]("webKnossos.sampleOrganization.user.isSuperUser") diff --git a/conf/application.conf b/conf/application.conf index dccbe48860b..b97632bba6f 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -63,6 +63,7 @@ webKnossos { enabled = true user { email = "sample@scm.io" + email2 = "sample2@scm.io" password = "secret" token = "secretSampleUserToken" isSuperUser = true From 64fc875068bde3d36f9d7080446ac9168b0b6def Mon Sep 17 00:00:00 2001 From: Philipp Otto Date: Tue, 29 Nov 2022 16:21:36 +0100 Subject: [PATCH 2/4] rename second user and DRY insertion method --- app/controllers/InitialDataController.scala | 37 ++++++--------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/app/controllers/InitialDataController.scala b/app/controllers/InitialDataController.scala index a04c4ffd8ca..1079af587d4 100644 --- a/app/controllers/InitialDataController.scala +++ b/app/controllers/InitialDataController.scala @@ -107,7 +107,7 @@ Samplecountry userId2, multiUserId2, defaultOrganization._id, - "Second", + "Non-Admin", "User", System.currentTimeMillis(), Json.obj(), @@ -137,8 +137,8 @@ Samplecountry _ <- assertNoOrganizationsPresent _ <- insertOrganization() _ <- insertTeams() - _ <- insertDefaultUser() - _ <- insertSecondDefaultUser() + _ <- insertDefaultUser(defaultUserEmail, defaultMultiUser, defaultUser, true) + _ <- insertDefaultUser(defaultUserEmail2, defaultMultiUser2, defaultUser2, false) _ <- insertToken() _ <- insertTaskType() _ <- insertProject() @@ -156,41 +156,24 @@ Samplecountry _ <- bool2Fox(organizations.isEmpty) ?~> "initialData.organizationsNotEmpty" } yield () - private def insertDefaultUser(): Fox[Unit] = + private def insertDefaultUser(userEmail: String, multiUser: MultiUser, user: User, isTeamManager: Boolean): Fox[Unit] = userService - .userFromMultiUserEmail(defaultUserEmail) + .userFromMultiUserEmail(userEmail) .futureBox .flatMap { case Full(_) => Fox.successful(()) case _ => for { - _ <- multiUserDAO.insertOne(defaultMultiUser) - _ <- userDAO.insertOne(defaultUser) - _ <- userExperiencesDAO.updateExperiencesForUser(defaultUser, Map("sampleExp" -> 10)) - _ <- userTeamRolesDAO.insertTeamMembership(defaultUser._id, - TeamMembership(organizationTeam._id, isTeamManager = true)) + _ <- multiUserDAO.insertOne(multiUser) + _ <- userDAO.insertOne(user) + _ <- userExperiencesDAO.updateExperiencesForUser(user, Map("sampleExp" -> 10)) + _ <- userTeamRolesDAO.insertTeamMembership(user._id, + TeamMembership(organizationTeam._id, isTeamManager = isTeamManager)) _ = logger.info("Inserted default user") } yield () } .toFox - private def insertSecondDefaultUser(): Fox[Unit] = - userService - .userFromMultiUserEmail(defaultUserEmail2) - .futureBox - .flatMap { - case Full(_) => Fox.successful(()) - case _ => - for { - _ <- multiUserDAO.insertOne(defaultMultiUser2) - _ <- userDAO.insertOne(defaultUser2) - _ <- userTeamRolesDAO.insertTeamMembership(defaultUser2._id, - TeamMembership(organizationTeam._id, isTeamManager = false)) - _ = logger.info("Inserted second default user") - } yield () - } - .toFox - private def insertToken(): Fox[Unit] = { val expiryTime = conf.Silhouette.TokenAuthenticator.authenticatorExpiry.toMillis tokenDAO.findOneByLoginInfo("credentials", defaultUser._id.id, TokenType.Authentication).futureBox.flatMap { From 1e9a54287f977ceb1cad945eab4829585bcd75ae Mon Sep 17 00:00:00 2001 From: Philipp Otto Date: Tue, 29 Nov 2022 16:48:17 +0100 Subject: [PATCH 3/4] format --- app/controllers/InitialDataController.scala | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/controllers/InitialDataController.scala b/app/controllers/InitialDataController.scala index 1079af587d4..2b2ea7483dc 100644 --- a/app/controllers/InitialDataController.scala +++ b/app/controllers/InitialDataController.scala @@ -156,7 +156,10 @@ Samplecountry _ <- bool2Fox(organizations.isEmpty) ?~> "initialData.organizationsNotEmpty" } yield () - private def insertDefaultUser(userEmail: String, multiUser: MultiUser, user: User, isTeamManager: Boolean): Fox[Unit] = + private def insertDefaultUser(userEmail: String, + multiUser: MultiUser, + user: User, + isTeamManager: Boolean): Fox[Unit] = userService .userFromMultiUserEmail(userEmail) .futureBox @@ -167,8 +170,9 @@ Samplecountry _ <- multiUserDAO.insertOne(multiUser) _ <- userDAO.insertOne(user) _ <- userExperiencesDAO.updateExperiencesForUser(user, Map("sampleExp" -> 10)) - _ <- userTeamRolesDAO.insertTeamMembership(user._id, - TeamMembership(organizationTeam._id, isTeamManager = isTeamManager)) + _ <- userTeamRolesDAO.insertTeamMembership( + user._id, + TeamMembership(organizationTeam._id, isTeamManager = isTeamManager)) _ = logger.info("Inserted default user") } yield () } From 034c091eeb5a7cb5b520a3c8e2f6d0c75d13efe5 Mon Sep 17 00:00:00 2001 From: Philipp Otto Date: Thu, 1 Dec 2022 11:14:00 +0100 Subject: [PATCH 4/4] name default organization team "Default" when inserting initial data (analoguous to general organization creation) --- app/controllers/InitialDataController.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/InitialDataController.scala b/app/controllers/InitialDataController.scala index bbedea7b368..d6977db7d95 100644 --- a/app/controllers/InitialDataController.scala +++ b/app/controllers/InitialDataController.scala @@ -71,7 +71,7 @@ Samplecountry "Sample Organization", PricingPlan.Custom) private val organizationTeam = - Team(organizationTeamId, defaultOrganization._id, defaultOrganization.name, isOrganizationTeam = true) + Team(organizationTeamId, defaultOrganization._id, "Default", isOrganizationTeam = true) private val userId = ObjectId.generate private val multiUserId = ObjectId.generate private val userId2 = ObjectId.generate