本文整理汇总了Scala中com.mohiva.play.silhouette.api.Authorization类的典型用法代码示例。如果您正苦于以下问题:Scala Authorization类的具体用法?Scala Authorization怎么用?Scala Authorization使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Authorization类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: WithRole
//设置package包名称以及导入依赖的类
package models.user
import com.mohiva.play.silhouette.api.{ Authenticator, Authorization }
import play.api.i18n._
import play.api.mvc.Request
import scala.concurrent.Future
case class WithRole(role: Role) extends Authorization[User, Authenticator] {
override def isAuthorized[B](user: User, authenticator: Authenticator)(implicit request: Request[B], messages: Messages) = {
Future.successful(user.roles match {
case list: Set[Role] => list.contains(role)
case _ => false
})
}
}
sealed trait Role extends Serializable {
def name: String
}
object Role {
def apply(role: String): Role = role match {
case Admin.name => Admin
case User.name => User
case _ => Unknown
}
def unapply(role: Role): Option[String] = Some(role.name)
object Admin extends Role {
val name = "admin"
}
object User extends Role {
val name = "user"
}
object Unknown extends Role {
val name = "-"
}
}
示例2: WithRole
//设置package包名称以及导入依赖的类
package utils
import com.mohiva.play.silhouette.api.Authorization
import com.mohiva.play.silhouette.impl.authenticators.CookieAuthenticator
import models.{Role, User}
import play.api.i18n._
import play.api.mvc.{Request, RequestHeader}
import scala.concurrent.Future
case class WithRole(role: Role) extends Authorization[User,CookieAuthenticator] {
def isAuthorized[B](user: User, authenticator: CookieAuthenticator)(implicit request : Request[B], messages: Messages) =
user.roles match {
case list: Set[Role] => Future.successful(list.contains(role))
case _ => Future.successful(false)
}
}
case class WithAlternativeRoles(role: Role*) extends Authorization[User, CookieAuthenticator] {
override def isAuthorized[B](identity: User, authenticator: CookieAuthenticator)(implicit request: Request[B], messages: Messages): Future[Boolean] =
Future.successful(role.foldLeft[Boolean](false)((contains, r) => identity.roles.contains(r) || contains))
}