From d8aacdaa943db3be8b59ad612fb79a52aeb12eb4 Mon Sep 17 00:00:00 2001 From: shimamoto Date: Fri, 26 May 2017 14:45:01 +0900 Subject: [PATCH] Add account hook (deleted only). --- .../core/controller/AccountController.scala | 6 +++++- .../core/controller/SystemSettingsController.scala | 6 +++++- .../scala/gitbucket/core/plugin/AccountHook.scala | 10 ++++++++++ src/main/scala/gitbucket/core/plugin/Plugin.scala | 13 +++++++++++++ .../gitbucket/core/plugin/PluginRegistory.scala | 5 +++++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/main/scala/gitbucket/core/plugin/AccountHook.scala diff --git a/src/main/scala/gitbucket/core/controller/AccountController.scala b/src/main/scala/gitbucket/core/controller/AccountController.scala index 51dd1d124..ec883aa3e 100644 --- a/src/main/scala/gitbucket/core/controller/AccountController.scala +++ b/src/main/scala/gitbucket/core/controller/AccountController.scala @@ -206,9 +206,13 @@ trait AccountControllerBase extends AccountManagementControllerBase { // FileUtils.deleteDirectory(getWikiRepositoryDir(userName, repositoryName)) // FileUtils.deleteDirectory(getTemporaryDir(userName, repositoryName)) // } -// // Remove from GROUP_MEMBER, COLLABORATOR and REPOSITORY + // Remove from GROUP_MEMBER and COLLABORATOR removeUserRelatedData(userName) updateAccount(account.copy(isRemoved = true)) + + // call hooks + PluginRegistry().getAccountHooks.foreach(_.deleted(userName)) + session.invalidate redirect("/") } diff --git a/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala b/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala index b175eb36b..306764b84 100644 --- a/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala +++ b/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala @@ -225,7 +225,7 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase { // FileUtils.deleteDirectory(getWikiRepositoryDir(userName, repositoryName)) // FileUtils.deleteDirectory(getTemporaryDir(userName, repositoryName)) // } - // Remove from GROUP_MEMBER, COLLABORATOR and REPOSITORY + // Remove from GROUP_MEMBER and COLLABORATOR removeUserRelatedData(userName) } @@ -239,6 +239,10 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase { isRemoved = form.isRemoved)) updateImage(userName, form.fileId, form.clearImage) + + // call hooks + if(form.isRemoved) PluginRegistry().getAccountHooks.foreach(_.deleted(userName)) + redirect("/admin/users") } } getOrElse NotFound() diff --git a/src/main/scala/gitbucket/core/plugin/AccountHook.scala b/src/main/scala/gitbucket/core/plugin/AccountHook.scala new file mode 100644 index 000000000..b6db88516 --- /dev/null +++ b/src/main/scala/gitbucket/core/plugin/AccountHook.scala @@ -0,0 +1,10 @@ +package gitbucket.core.plugin + +import gitbucket.core.model.Profile._ +import profile.api._ + +trait AccountHook { + + def deleted(userName: String)(implicit session: Session): Unit = () + +} diff --git a/src/main/scala/gitbucket/core/plugin/Plugin.scala b/src/main/scala/gitbucket/core/plugin/Plugin.scala index 5be1caaa9..3d2b51bcf 100644 --- a/src/main/scala/gitbucket/core/plugin/Plugin.scala +++ b/src/main/scala/gitbucket/core/plugin/Plugin.scala @@ -71,6 +71,16 @@ abstract class Plugin { */ def repositoryRoutings(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[GitRepositoryRouting] = Nil + /** + * Override to add account hooks. + */ + val accountHooks: Seq[AccountHook] = Nil + + /** + * Override to add account hooks. + */ + def accountHooks(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[AccountHook] = Nil + /** * Override to add receive hooks. */ @@ -241,6 +251,9 @@ abstract class Plugin { (repositoryRoutings ++ repositoryRoutings(registry, context, settings)).foreach { routing => registry.addRepositoryRouting(routing) } + (accountHooks ++ accountHooks(registry, context, settings)).foreach { accountHook => + registry.addAccountHook(accountHook) + } (receiveHooks ++ receiveHooks(registry, context, settings)).foreach { receiveHook => registry.addReceiveHook(receiveHook) } diff --git a/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala b/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala index b81afa0dd..c6dcc6272 100644 --- a/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala +++ b/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala @@ -33,6 +33,7 @@ class PluginRegistry { "md" -> MarkdownRenderer, "markdown" -> MarkdownRenderer ) private val repositoryRoutings = new ListBuffer[GitRepositoryRouting] + private val accountHooks = new ListBuffer[AccountHook] private val receiveHooks = new ListBuffer[ReceiveHook] receiveHooks += new ProtectedBranchReceiveHook() @@ -103,6 +104,10 @@ class PluginRegistry { } } + def addAccountHook(accountHook: AccountHook): Unit = accountHooks += accountHook + + def getAccountHooks: Seq[AccountHook] = accountHooks.toSeq + def addReceiveHook(commitHook: ReceiveHook): Unit = receiveHooks += commitHook def getReceiveHooks: Seq[ReceiveHook] = receiveHooks.toSeq