当前位置: 首页>>代码示例>>Scala>>正文


Scala Authorization类代码示例

本文整理汇总了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 = "-"
  }
} 
开发者ID:laurinka,项目名称:golfmit,代码行数:43,代码来源:Roles.scala

示例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))
} 
开发者ID:Viva-con-Agua,项目名称:drops,代码行数:24,代码来源:WithRole.scala


注:本文中的com.mohiva.play.silhouette.api.Authorization类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。