From 23bea37ed6f766d0e2842d932d87e97f3621d959 Mon Sep 17 00:00:00 2001 From: takezoe Date: Wed, 3 Jul 2013 14:53:40 +0900 Subject: [PATCH] Authenticator pass RepositoryInfo to actions. --- src/main/scala/util/Authenticator.scala | 40 +++++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/main/scala/util/Authenticator.scala b/src/main/scala/util/Authenticator.scala index ea15a485d..334437e30 100644 --- a/src/main/scala/util/Authenticator.scala +++ b/src/main/scala/util/Authenticator.scala @@ -1,8 +1,8 @@ package util +import JGitUtil.RepositoryInfo import app.ControllerBase import service._ -import org.scalatra._ /** * Allows only the repository owner and administrators. @@ -61,18 +61,24 @@ trait AdminOnlyAuthenticator { self: ControllerBase => * Allows only collaborators and administrators. */ trait CollaboratorsAuthenticator { self: ControllerBase with RepositoryService => - protected def collaboratorsOnly(action: => Any) = { authenticate(action) } - protected def collaboratorsOnly[T](action: T => Any) = (form: T) => authenticate({action(form)}) + + protected def collaboratorsOnly(action: (RepositoryInfo) => Any) = + (repository: RepositoryInfo) => authenticate({action(repository)}) + + protected def collaboratorsOnly[T](action: (RepositoryInfo, T) => Any) = + (repository: RepositoryInfo, form: T) => authenticate({action(repository, form)}) private def authenticate(action: => Any) = { { val paths = request.getRequestURI.substring(request.getContextPath.length).split("/") - context.loginAccount match { - case Some(x) if(x.isAdmin) => action - case Some(x) if(paths(1) == x.userName) => action - case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action - case _ => Unauthorized() - } + getRepository(paths(1), paths(2), baseUrl).map { _ => + context.loginAccount match { + case Some(x) if(x.isAdmin) => action + case Some(x) if(paths(1) == x.userName) => action + case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action + case _ => Unauthorized() + } + } getOrElse NotFound() } } } @@ -81,8 +87,12 @@ trait CollaboratorsAuthenticator { self: ControllerBase with RepositoryService = * Allows only the repository owner and administrators. */ trait ReferrerAuthenticator { self: ControllerBase with RepositoryService => - protected def referrersOnly(action: => Any) = { authenticate(action) } - protected def referrersOnly[T](action: T => Any) = (form: T) => authenticate({action(form)}) + + protected def referrersOnly(action: (RepositoryInfo) => Any) = + (repository: RepositoryInfo) => authenticate({action(repository)}) + + protected def referrersOnly[T](action: (RepositoryInfo, T) => Any) = + (repository: RepositoryInfo, form: T) => authenticate({action(repository, form)}) private def authenticate(action: => Any) = { { @@ -109,8 +119,12 @@ trait ReferrerAuthenticator { self: ControllerBase with RepositoryService => * Allows only signed in users which can access the repository. */ trait ReadableUsersAuthenticator { self: ControllerBase with RepositoryService => - protected def readableUsersOnly(action: => Any) = { authenticate(action) } - protected def readableUsersOnly[T](action: T => Any) = (form: T) => authenticate({action(form)}) + + protected def readableUsersOnly(action: (RepositoryInfo) => Any) = + (repository: RepositoryInfo) => authenticate({action(repository)}) + + protected def readableUsersOnly[T](action: (RepositoryInfo, T) => Any) = + (repository: RepositoryInfo, form: T) => authenticate({action(repository, form)}) private def authenticate(action: => Any) = { {