Play-silhouette:如何在飞行中创建角色?

时间:2017-10-31 18:29:12

标签: scala playframework silhouette

我希望能够动态创建和删除一些Silhouette角色,例如添加一个非硬编码的角色管理器。

我看到的唯一解决方案是将这些角色存储在数据库中。

Silhouette需要我实现这个:

case class WithRole(role: String*) extends Authorization[User, CookieAuthenticator] {
  def isAuthorized[B](user: User, authenticator: CookieAuthenticator)(
      implicit request: Request[B]
  ): Future[Boolean] = {
    myChekingIfAuthorizedFunction()
}

问题是我无法将我的数据库注入案例类。所以我找到的唯一解决方法是:

myChekingIfAuthorizedFunction(): Future[Boolean] = {
    val currentApp: Application = Play.current
    val injector: Injector = currentApp.injector
    lazy val dbConfProvider: DatabaseConfigProvider = injector.instanceOf[DatabaseConfigProvider]
    lazy val ec: ExecutionContextExecutor = scala.concurrent.ExecutionContext.Implicits.global
    lazy val isInRole = new IsInRole(dbConfProvider, ec)
    isInRole.isInRole(1, user)
}

class IsInRole @Inject()(
    protected val dbConfigProvider: DatabaseConfigProvider,
    implicit val ec: ExecutionContext
) extends HasDatabaseConfigProvider[MyPostgresDriver]
    with RoleTableDefinitions {

  def isInRole(roleId: Long, user: User): Future[Boolean] =
    db.run(userRoles.filter(_.roleId === roleId).result) map { relations =>
      relations.map(_.userId).contains(user.uuid)
    }
}

此解决方案的问题是调用Play.current

那么,你有什么想法可以避免这种情况吗?或者是否有一个我没有找到提供此功能的github项目?

0 个答案:

没有答案