diff --git a/src/main/scala/app/ControllerBase.scala b/src/main/scala/app/ControllerBase.scala index d92a2b7..4b89776 100644 --- a/src/main/scala/app/ControllerBase.scala +++ b/src/main/scala/app/ControllerBase.scala @@ -26,101 +26,6 @@ } } - /** - * 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]) \ No newline at end of file diff --git a/src/main/scala/app/CreateRepositoryController.scala b/src/main/scala/app/CreateRepositoryController.scala index 595f5cb..ac815b5 100644 --- a/src/main/scala/app/CreateRepositoryController.scala +++ b/src/main/scala/app/CreateRepositoryController.scala @@ -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 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? diff --git a/src/main/scala/app/SettingsController.scala b/src/main/scala/app/SettingsController.scala index c6f1f52..288d068 100644 --- a/src/main/scala/app/SettingsController.scala +++ b/src/main/scala/app/SettingsController.scala @@ -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) diff --git a/src/main/scala/app/WikiController.scala b/src/main/scala/app/WikiController.scala index c8483a9..4c5ebe5 100644 --- a/src/main/scala/app/WikiController.scala +++ b/src/main/scala/app/WikiController.scala @@ -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 @@ } } - 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 @@ 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 @@ 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 @@ 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") diff --git a/src/main/scala/util/Authenticator.scala b/src/main/scala/util/Authenticator.scala new file mode 100644 index 0000000..e2749bf --- /dev/null +++ b/src/main/scala/util/Authenticator.scala @@ -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") + } + } + } +} \ No newline at end of file