mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 13:35:50 +01:00
Authenticator pass RepositoryInfo to actions.
This commit is contained in:
@@ -1,15 +1,15 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import JGitUtil.RepositoryInfo
|
|
||||||
import app.ControllerBase
|
import app.ControllerBase
|
||||||
import service._
|
import service._
|
||||||
|
import RepositoryService.RepositoryInfo
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows only the repository owner and administrators.
|
* Allows only the repository owner and administrators.
|
||||||
*/
|
*/
|
||||||
trait OwnerOnlyAuthenticator { self: ControllerBase =>
|
trait OwnerOnlyAuthenticator { self: ControllerBase =>
|
||||||
protected def ownerOnly(action: => Any) = { authenticate(action) }
|
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) = {
|
private def authenticate(action: => Any) = {
|
||||||
{
|
{
|
||||||
@@ -27,7 +27,7 @@ trait OwnerOnlyAuthenticator { self: ControllerBase =>
|
|||||||
*/
|
*/
|
||||||
trait UsersOnlyAuthenticator { self: ControllerBase =>
|
trait UsersOnlyAuthenticator { self: ControllerBase =>
|
||||||
protected def usersOnly(action: => Any) = { authenticate(action) }
|
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) = {
|
private def authenticate(action: => Any) = {
|
||||||
{
|
{
|
||||||
@@ -45,7 +45,7 @@ trait UsersOnlyAuthenticator { self: ControllerBase =>
|
|||||||
trait AdminOnlyAuthenticator { self: ControllerBase =>
|
trait AdminOnlyAuthenticator { self: ControllerBase =>
|
||||||
|
|
||||||
protected def adminOnly(action: => Any) = { authenticate(action) }
|
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) = {
|
private def authenticate(action: => Any) = {
|
||||||
{
|
{
|
||||||
@@ -61,21 +61,17 @@ trait AdminOnlyAuthenticator { self: ControllerBase =>
|
|||||||
* Allows only collaborators and administrators.
|
* Allows only collaborators and administrators.
|
||||||
*/
|
*/
|
||||||
trait CollaboratorsAuthenticator { self: ControllerBase with RepositoryService =>
|
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) =
|
private def authenticate(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("/")
|
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 {
|
context.loginAccount match {
|
||||||
case Some(x) if(x.isAdmin) => action
|
case Some(x) if(x.isAdmin) => action(repository)
|
||||||
case Some(x) if(paths(1) == x.userName) => action
|
case Some(x) if(paths(1) == x.userName) => action(repository)
|
||||||
case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action
|
case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action(repository)
|
||||||
case _ => Unauthorized()
|
case _ => Unauthorized()
|
||||||
}
|
}
|
||||||
} getOrElse NotFound()
|
} getOrElse NotFound()
|
||||||
@@ -87,30 +83,24 @@ trait CollaboratorsAuthenticator { self: ControllerBase with RepositoryService =
|
|||||||
* Allows only the repository owner and administrators.
|
* Allows only the repository owner and administrators.
|
||||||
*/
|
*/
|
||||||
trait ReferrerAuthenticator { self: ControllerBase with RepositoryService =>
|
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) =
|
private def authenticate(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) = {
|
|
||||||
{
|
{
|
||||||
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
||||||
getRepository(paths(1), paths(2), baseUrl) match {
|
getRepository(paths(1), paths(2), baseUrl).map { repository =>
|
||||||
case None => NotFound()
|
|
||||||
case Some(repository) =>
|
|
||||||
if(!repository.repository.isPrivate){
|
if(!repository.repository.isPrivate){
|
||||||
action
|
action(repository)
|
||||||
} else {
|
} else {
|
||||||
context.loginAccount match {
|
context.loginAccount match {
|
||||||
case Some(x) if(x.isAdmin) => action
|
case Some(x) if(x.isAdmin) => action(repository)
|
||||||
case Some(x) if(paths(1) == x.userName) => action
|
case Some(x) if(paths(1) == x.userName) => action(repository)
|
||||||
case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action
|
case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action(repository)
|
||||||
case _ => Unauthorized()
|
case _ => Unauthorized()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} getOrElse NotFound()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,26 +109,21 @@ trait ReferrerAuthenticator { self: ControllerBase with RepositoryService =>
|
|||||||
* Allows only signed in users which can access the repository.
|
* Allows only signed in users which can access the repository.
|
||||||
*/
|
*/
|
||||||
trait ReadableUsersAuthenticator { self: ControllerBase with RepositoryService =>
|
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) =
|
private def authenticate(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) = {
|
|
||||||
{
|
{
|
||||||
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
||||||
getRepository(paths(1), paths(2), baseUrl) match {
|
getRepository(paths(1), paths(2), baseUrl).map { repository =>
|
||||||
case None => NotFound()
|
context.loginAccount match {
|
||||||
case Some(repository) => context.loginAccount match {
|
case Some(x) if(x.isAdmin) => action(repository)
|
||||||
case Some(x) if(x.isAdmin) => action
|
case Some(x) if(!repository.repository.isPrivate) => action(repository)
|
||||||
case Some(x) if(!repository.repository.isPrivate) => action
|
case Some(x) if(paths(1) == x.userName) => action(repository)
|
||||||
case Some(x) if(paths(1) == x.userName) => action
|
case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action(repository)
|
||||||
case Some(x) if(getCollaborators(paths(1), paths(2)).contains(x.userName)) => action
|
|
||||||
case _ => Unauthorized()
|
case _ => Unauthorized()
|
||||||
}
|
}
|
||||||
}
|
} getOrElse NotFound()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user