diff --git a/src/main/scala/gitbucket/core/controller/AccountController.scala b/src/main/scala/gitbucket/core/controller/AccountController.scala index caddeab..0b71485 100644 --- a/src/main/scala/gitbucket/core/controller/AccountController.scala +++ b/src/main/scala/gitbucket/core/controller/AccountController.scala @@ -155,7 +155,7 @@ get("/:userName/_edit")(oneselfOnly { val userName = params("userName") getAccountByUserName(userName).map { x => - html.edit(x, flash.get("info")) + html.edit(x, flash.get("info"), flash.get("error")) } getOrElse NotFound }) @@ -178,7 +178,11 @@ get("/:userName/_delete")(oneselfOnly { val userName = params("userName") - getAccountByUserName(userName, true).foreach { account => + getAccountByUserName(userName, true).map { account => + if(isLastAdministrator(userName)){ + flash += "error" -> "Account can't be removed because this is the last administrator." + redirect(s"/${userName}/_edit") + } else { // // Remove repositories // getRepositoryNamesOfUser(userName).foreach { repositoryName => // deleteRepository(userName, repositoryName) @@ -187,14 +191,12 @@ // FileUtils.deleteDirectory(getTemporaryDir(userName, repositoryName)) // } // // Remove from GROUP_MEMBER, COLLABORATOR and REPOSITORY -// removeUserRelatedData(userName) - - removeUserRelatedData(userName) - updateAccount(account.copy(isRemoved = true)) - } - - session.invalidate - redirect("/") + removeUserRelatedData(userName) + updateAccount(account.copy(isRemoved = true)) + session.invalidate + redirect("/") + } + } getOrElse NotFound }) get("/:userName/_ssh")(oneselfOnly { diff --git a/src/main/scala/gitbucket/core/service/AccountService.scala b/src/main/scala/gitbucket/core/service/AccountService.scala index dbdc3d9..5325787 100644 --- a/src/main/scala/gitbucket/core/service/AccountService.scala +++ b/src/main/scala/gitbucket/core/service/AccountService.scala @@ -97,6 +97,12 @@ Accounts filter (_.removed === false.bind) sortBy(_.userName) list } + def isLastAdministrator(userName: String)(implicit s: Session): Boolean = { + getAccountByUserName(userName).collect { case account if account.isAdmin => + (Accounts filter (_.removed === false.bind) filter (_.isAdmin === true.bind) map (_.userName.length)).first == 1 + } getOrElse false + } + def createAccount(userName: String, password: String, fullName: String, mailAddress: String, isAdmin: Boolean, url: Option[String]) (implicit s: Session): Unit = Accounts insert Account( diff --git a/src/main/twirl/gitbucket/core/account/edit.scala.html b/src/main/twirl/gitbucket/core/account/edit.scala.html index d303e18..d8bf434 100644 --- a/src/main/twirl/gitbucket/core/account/edit.scala.html +++ b/src/main/twirl/gitbucket/core/account/edit.scala.html @@ -1,4 +1,4 @@ -@(account: gitbucket.core.model.Account, info: Option[Any])(implicit context: gitbucket.core.controller.Context) +@(account: gitbucket.core.model.Account, info: Option[Any], error: Option[Any])(implicit context: gitbucket.core.controller.Context) @import gitbucket.core.util.LDAPUtil @import context._ @import gitbucket.core.view.helpers._ @@ -6,6 +6,7 @@