mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-01 02:56:08 +01:00
Separate Authenticators to the trait from ControllerBase.
This commit is contained in:
@@ -26,101 +26,6 @@ abstract class ControllerBase extends ScalatraFilter with ClientSideValidationFo
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows only the repository owner and administrators.
|
||||
*/
|
||||
protected def ownerOnly(action: => Any) = {
|
||||
{
|
||||
context.loginAccount match {
|
||||
case Some(x) if(x.userType == AccountService.Administrator) => action
|
||||
case Some(x) if(request.getRequestURI.split("/")(1) == x.userName) => action
|
||||
case _ => redirect("/signin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows only the repository owner and administrators.
|
||||
*/
|
||||
protected def ownerOnly[T](action: T => Any) = {
|
||||
(form: T) => {
|
||||
context.loginAccount match {
|
||||
case Some(x) if(x.userType == AccountService.Administrator) => action(form)
|
||||
case Some(x) if(request.getRequestURI.split("/")(1) == x.userName) => action(form)
|
||||
case _ => redirect("/signin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows only signed in users.
|
||||
*/
|
||||
protected def usersOnly(action: => Any) = {
|
||||
{
|
||||
context.loginAccount match {
|
||||
case Some(x) => action
|
||||
case None => redirect("/signin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows only signed in users.
|
||||
*/
|
||||
protected def usersOnly[T](action: T => Any) = {
|
||||
(form: T) => {
|
||||
context.loginAccount match {
|
||||
case Some(x) => action(form)
|
||||
case None => redirect("/signin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Allows only collaborators and administrators.
|
||||
// */
|
||||
// protected def collaboratorsOnly(action: => Any) = {
|
||||
// {
|
||||
// context.loginAccount match {
|
||||
// case Some(x) if(x.userType == AccountService.Administrator) => action
|
||||
// case Some(x) if(request.getRequestURI.split("/")(1) == x.userName) => action
|
||||
// case Some(x) => {
|
||||
// val paths = request.getRequestURI.split("/")
|
||||
// if(getCollaborators(paths(1), paths(2)).contains(x.userName)){
|
||||
// action
|
||||
// } else {
|
||||
// redirect("/signin")
|
||||
// }
|
||||
// }
|
||||
// case None => redirect("/signin")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Allows only collaborators and administrators.
|
||||
// */
|
||||
// protected def collaboratorsOnly[T](action: T => Any) = {
|
||||
// (form: T) => {
|
||||
// context.loginAccount match {
|
||||
// case Some(x) if(x.userType == AccountService.Administrator) => action(form)
|
||||
// case Some(x) if(request.getRequestURI.split("/")(1) == x.userName) => action(form)
|
||||
// case Some(x) => {
|
||||
// val paths = request.getRequestURI.split("/")
|
||||
// if(getCollaborators(paths(1), paths(2)).contains(x.userName)){
|
||||
// action(form)
|
||||
// } else {
|
||||
// redirect("/signin")
|
||||
// }
|
||||
// }
|
||||
// case None => redirect("/signin")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// protected def adminOnly()
|
||||
|
||||
}
|
||||
|
||||
case class Context(path: String, loginAccount: Option[Account])
|
||||
@@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import util.Directory._
|
||||
import util.UsersOnlyAuthenticator
|
||||
import service._
|
||||
import java.io.File
|
||||
import org.eclipse.jgit.api.Git
|
||||
@@ -9,12 +10,13 @@ import org.apache.commons.io._
|
||||
import jp.sf.amateras.scalatra.forms._
|
||||
|
||||
class CreateRepositoryController extends CreateRepositoryControllerBase
|
||||
with RepositoryService with AccountService with WikiService
|
||||
with RepositoryService with AccountService with WikiService with UsersOnlyAuthenticator
|
||||
|
||||
/**
|
||||
* Creates new repository.
|
||||
*/
|
||||
trait CreateRepositoryControllerBase extends ControllerBase { self: RepositoryService with WikiService =>
|
||||
trait CreateRepositoryControllerBase extends ControllerBase {
|
||||
self: RepositoryService with WikiService with UsersOnlyAuthenticator =>
|
||||
|
||||
case class RepositoryCreationForm(name: String, description: String) // TODO Option?
|
||||
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package app
|
||||
|
||||
import service._
|
||||
import util.OwnerOnlyAuthenticator
|
||||
import jp.sf.amateras.scalatra.forms._
|
||||
|
||||
class SettingsController extends SettingsControllerBase with RepositoryService with AccountService
|
||||
class SettingsController extends SettingsControllerBase
|
||||
with RepositoryService with AccountService with OwnerOnlyAuthenticator
|
||||
|
||||
|
||||
trait SettingsControllerBase extends ControllerBase { self: RepositoryService with AccountService =>
|
||||
trait SettingsControllerBase extends ControllerBase {
|
||||
self: RepositoryService with AccountService with OwnerOnlyAuthenticator =>
|
||||
|
||||
case class CollaboratorForm(userName: String)
|
||||
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package app
|
||||
|
||||
import service._
|
||||
import util.JGitUtil
|
||||
import util.{CollaboratorsOnlyAuthenticator, JGitUtil}
|
||||
import util.Directory._
|
||||
import jp.sf.amateras.scalatra.forms._
|
||||
|
||||
class WikiController extends WikiControllerBase
|
||||
with WikiService with RepositoryService with AccountService
|
||||
with WikiService with RepositoryService with AccountService with CollaboratorsOnlyAuthenticator
|
||||
|
||||
trait WikiControllerBase extends ControllerBase { self: WikiService with RepositoryService =>
|
||||
trait WikiControllerBase extends ControllerBase {
|
||||
self: WikiService with RepositoryService with CollaboratorsOnlyAuthenticator =>
|
||||
|
||||
case class WikiPageEditForm(pageName: String, content: String, message: Option[String], currentPageName: String)
|
||||
|
||||
@@ -81,7 +82,7 @@ trait WikiControllerBase extends ControllerBase { self: WikiService with Reposit
|
||||
}
|
||||
}
|
||||
|
||||
get("/:owner/:repository/wiki/:page/_edit")(usersOnly {
|
||||
get("/:owner/:repository/wiki/:page/_edit")(collaboratorsOnly {
|
||||
val owner = params("owner")
|
||||
val repository = params("repository")
|
||||
val page = params("page")
|
||||
@@ -90,7 +91,7 @@ trait WikiControllerBase extends ControllerBase { self: WikiService with Reposit
|
||||
getWikiPage(owner, repository, page), getRepository(owner, repository, servletContext).get)
|
||||
})
|
||||
|
||||
post("/:owner/:repository/wiki/_edit", editForm)(usersOnly { form =>
|
||||
post("/:owner/:repository/wiki/_edit", editForm)(collaboratorsOnly { form =>
|
||||
val owner = params("owner")
|
||||
val repository = params("repository")
|
||||
|
||||
@@ -100,14 +101,14 @@ trait WikiControllerBase extends ControllerBase { self: WikiService with Reposit
|
||||
redirect("%s/%s/wiki/%s".format(owner, repository, form.pageName))
|
||||
})
|
||||
|
||||
get("/:owner/:repository/wiki/_new")(usersOnly {
|
||||
get("/:owner/:repository/wiki/_new")(collaboratorsOnly {
|
||||
val owner = params("owner")
|
||||
val repository = params("repository")
|
||||
|
||||
wiki.html.wikiedit("", None, getRepository(owner, repository, servletContext).get)
|
||||
})
|
||||
|
||||
post("/:owner/:repository/wiki/_new", newForm)(usersOnly { form =>
|
||||
post("/:owner/:repository/wiki/_new", newForm)(collaboratorsOnly { form =>
|
||||
val owner = params("owner")
|
||||
val repository = params("repository")
|
||||
|
||||
@@ -117,7 +118,7 @@ trait WikiControllerBase extends ControllerBase { self: WikiService with Reposit
|
||||
redirect("%s/%s/wiki/%s".format(owner, repository, form.pageName))
|
||||
})
|
||||
|
||||
get("/:owner/:repository/wiki/:page/_delete")(usersOnly {
|
||||
get("/:owner/:repository/wiki/:page/_delete")(collaboratorsOnly {
|
||||
val owner = params("owner")
|
||||
val repository = params("repository")
|
||||
val page = params("page")
|
||||
|
||||
96
src/main/scala/util/Authenticator.scala
Normal file
96
src/main/scala/util/Authenticator.scala
Normal file
@@ -0,0 +1,96 @@
|
||||
package util
|
||||
|
||||
import app.ControllerBase
|
||||
import service._
|
||||
|
||||
/**
|
||||
* Allows only the repository owner and administrators.
|
||||
*/
|
||||
trait OwnerOnlyAuthenticator { self: ControllerBase =>
|
||||
|
||||
protected def ownerOnly(action: => Any) = {
|
||||
{
|
||||
context.loginAccount match {
|
||||
case Some(x) if(x.userType == AccountService.Administrator) => action
|
||||
case Some(x) if(request.getRequestURI.split("/")(1) == x.userName) => action
|
||||
case _ => redirect("/signin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected def ownerOnly[T](action: T => Any) = {
|
||||
(form: T) => {
|
||||
context.loginAccount match {
|
||||
case Some(x) if(x.userType == AccountService.Administrator) => action(form)
|
||||
case Some(x) if(request.getRequestURI.split("/")(1) == x.userName) => action(form)
|
||||
case _ => redirect("/signin")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows only signed in users.
|
||||
*/
|
||||
trait UsersOnlyAuthenticator { self: ControllerBase =>
|
||||
|
||||
protected def usersOnly(action: => Any) = {
|
||||
{
|
||||
context.loginAccount match {
|
||||
case Some(x) => action
|
||||
case None => redirect("/signin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected def usersOnly[T](action: T => Any) = {
|
||||
(form: T) => {
|
||||
context.loginAccount match {
|
||||
case Some(x) => action(form)
|
||||
case None => redirect("/signin")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows only collaborators and administrators.
|
||||
*/
|
||||
trait CollaboratorsOnlyAuthenticator { self: ControllerBase with RepositoryService =>
|
||||
|
||||
protected def collaboratorsOnly(action: => Any) = {
|
||||
{
|
||||
context.loginAccount match {
|
||||
case Some(x) if(x.userType == AccountService.Administrator) => action
|
||||
case Some(x) if(request.getRequestURI.split("/")(1) == x.userName) => action
|
||||
case Some(x) => {
|
||||
val paths = request.getRequestURI.split("/")
|
||||
if(getCollaborators(paths(1), paths(2)).contains(x.userName)){
|
||||
action
|
||||
} else {
|
||||
redirect("/signin")
|
||||
}
|
||||
}
|
||||
case None => redirect("/signin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected def collaboratorsOnly[T](action: T => Any) = {
|
||||
(form: T) => {
|
||||
context.loginAccount match {
|
||||
case Some(x) if(x.userType == AccountService.Administrator) => action(form)
|
||||
case Some(x) if(request.getRequestURI.split("/")(1) == x.userName) => action(form)
|
||||
case Some(x) => {
|
||||
val paths = request.getRequestURI.split("/")
|
||||
if(getCollaborators(paths(1), paths(2)).contains(x.userName)){
|
||||
action(form)
|
||||
} else {
|
||||
redirect("/signin")
|
||||
}
|
||||
}
|
||||
case None => redirect("/signin")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user