Skip to content

Commit

Permalink
#52 Add ability to provide multiple roles on controller helper
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Feb 19, 2018
1 parent e0bfd1f commit 7d391e4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/org/elastic4play/Errors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ case class AttributeCheckingError(
sealed trait AttributeError extends Throwable {
def withName(name: String): AttributeError
val name: String
override def getMessage: String = toString
}

case class InvalidFormatAttributeError(name: String, format: String, value: InputValue) extends AttributeError {
Expand Down
5 changes: 2 additions & 3 deletions app/org/elastic4play/controllers/Authenticated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.util.Date
import javax.inject.{ Inject, Singleton }
import javax.naming.ldap.LdapName

import scala.collection.JavaConverters._
import scala.concurrent.duration.{ DurationLong, FiniteDuration }
import scala.concurrent.{ ExecutionContext, Future }
import scala.collection.JavaConverters._
Expand Down Expand Up @@ -226,15 +225,15 @@ class Authenticated(
* If user has sufficient right (have required role) action is executed
* otherwise, action returns a not authorized error
*/
def apply(requiredRole: Role): ActionBuilder[AuthenticatedRequest, AnyContent] =
def apply(requiredRole: Role*): ActionBuilder[AuthenticatedRequest, AnyContent] =
new ActionBuilder[AuthenticatedRequest, AnyContent] {
val executionContext: ExecutionContext = ec

def parser: BodyParser[AnyContent] = defaultParser

def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) Future[Result]): Future[Result] = {
getContext(request).flatMap { authContext
if (authContext.roles.contains(requiredRole))
if (requiredRole.isEmpty || requiredRole.toSet.intersect(authContext.roles.toSet).nonEmpty)
block(new AuthenticatedRequest(authContext, request))
.map(result setSessingUser(result, authContext)(request))
else
Expand Down

0 comments on commit 7d391e4

Please sign in to comment.