Authenticator pass RepositoryInfo to actions.

This commit is contained in:
takezoe
2013-07-03 15:13:25 +09:00
parent 23bea37ed6
commit d26a07e28e

View File

@@ -1,15 +1,15 @@
package util
import JGitUtil.RepositoryInfo
import app.ControllerBase
import service._
import RepositoryService.RepositoryInfo
/**
* Allows only the repository owner and administrators.
*/
trait OwnerOnlyAuthenticator { self: ControllerBase =>
protected def ownerOnly(action: => Any) = { authenticate(action) }
protected def ownerOnly[T](action: T => Any) = (form: T) => authenticate({action(form)})
protected def ownerOnly[T](action: T => Any) = (form: T) => { authenticate(action(form)) }
private def authenticate(action: => Any) = {
{
@@ -27,7 +27,7 @@ trait OwnerOnlyAuthenticator { self: ControllerBase =>
*/
trait UsersOnlyAuthenticator { self: ControllerBase =>
protected def usersOnly(action: => Any) = { authenticate(action) }
protected def usersOnly[T](action: T => Any) = (form: T) => authenticate({action(form)})
protected def usersOnly[T](action: T => Any) = (form: T) => { authenticate(action(form)) }
private def authenticate(action: => Any) = {
{
@@ -45,7 +45,7 @@ trait UsersOnlyAuthenticator { self: ControllerBase =>
trait AdminOnlyAuthenticator { self: ControllerBase =>
protected def adminOnly(action: => Any) = { authenticate(action) }
protected def adminOnly[T](action: T => Any) = (form: T) => authenticate({action(form)})
protected def adminOnly[T](action: T => Any) = (form: T) => { authenticate(action(form)) }
private def authenticate(action: => Any) = {
{
@@ -61,21 +61,17 @@ trait AdminOnlyAuthenticator { self: ControllerBase =>
* Allows only collaborators and administrators.
*/
trait CollaboratorsAuthenticator { self: ControllerBase with RepositoryService =>
protected def collaboratorsOnly(action: (RepositoryInfo) => Any) = { authenticate(action) }
protected def collaboratorsOnly[T](action: (T, RepositoryInfo) => 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) = {
private def authenticate(action: (RepositoryInfo) => Any) = {
{
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
getRepository(paths(1), paths(2), baseUrl).map { _ =>
getRepository(paths(1), paths(2), baseUrl).map { repository =>
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 Some(x) if(x.isAdmin) => action(repository)
case Some(x) if(paths(1) == x.userName) => action(repository)
case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action(repository)
case _ => Unauthorized()
}
} getOrElse NotFound()
@@ -87,30 +83,24 @@ trait CollaboratorsAuthenticator { self: ControllerBase with RepositoryService =
* Allows only the repository owner and administrators.
*/
trait ReferrerAuthenticator { self: ControllerBase with RepositoryService =>
protected def referrersOnly(action: (RepositoryInfo) => Any) = { authenticate(action) }
protected def referrersOnly[T](action: (T, RepositoryInfo) => 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) = {
private def authenticate(action: (RepositoryInfo) => Any) = {
{
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
getRepository(paths(1), paths(2), baseUrl) match {
case None => NotFound()
case Some(repository) =>
if(!repository.repository.isPrivate){
action
} else {
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 { repository =>
if(!repository.repository.isPrivate){
action(repository)
} else {
context.loginAccount match {
case Some(x) if(x.isAdmin) => action(repository)
case Some(x) if(paths(1) == x.userName) => action(repository)
case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action(repository)
case _ => Unauthorized()
}
}
}
} getOrElse NotFound()
}
}
}
@@ -119,26 +109,21 @@ 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: (RepositoryInfo) => Any) = { authenticate(action) }
protected def readableUsersOnly[T](action: (T, RepositoryInfo) => 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) = {
private def authenticate(action: (RepositoryInfo) => Any) = {
{
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
getRepository(paths(1), paths(2), baseUrl) match {
case None => NotFound()
case Some(repository) => context.loginAccount match {
case Some(x) if(x.isAdmin) => action
case Some(x) if(!repository.repository.isPrivate) => action
case Some(x) if(paths(1) == x.userName) => action
case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action
getRepository(paths(1), paths(2), baseUrl).map { repository =>
context.loginAccount match {
case Some(x) if(x.isAdmin) => action(repository)
case Some(x) if(!repository.repository.isPrivate) => action(repository)
case Some(x) if(paths(1) == x.userName) => action(repository)
case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action(repository)
case _ => Unauthorized()
}
}
} getOrElse NotFound()
}
}
}