mkurz / deadbolt-2-scala

Idiomatic Scala API for Deadbolt 2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to make a Subject's #getPermissions return a Future[List[Permission]]?

queirozfcom opened this issue · comments

This is what the signature for the getPermissions method in Subject.java looks like right now:

List<? extends Permission> getPermissions();

But in my case (and, I imagine, for other people as well) I need to fetch permissions for a given user from the database, which shouldn't block my app. This is how I've implemented this interface in my actual User class (note that I have to block the thread at the end because I need to return a List[java.List] of it doesn't match the interface signature.

  def getPermissions: java.util.List[Permission] = {

    val q = for {
      u <- users if u.id === this.id
      uxug <- users_x_user_groups if uxug.userId === u.id
      ug <- user_groups if ug.id === uxug.userGroupId
      pug <- permissions_x_user_groups if (pug.userGroupId === ug.id || pug.userGroupId === ug.parentGroupId)
      p <- permissions if p.id === pug.permissionId
    } yield (p)

    val f = db.run(q.result)

    Await.result(f, 10.seconds).toList.asJava

  }

Is there some way to be able to return a Future without needing to fork the repo and changing the signature in Subject.java ? I'm not a very experienced Scala dev so it's possible I missed some simple solution.

That's a great idea. I'll make the API changes tomorrow.

"Tomorrow" might have been a bit premature, because I'm releasing the Java 2.4.0 final version today, and this is a non-trivial change. I'll slate this for v2.4.1.

+1 for future, now I have to write a simple DynamicResourceHandler for use. Hope for the future implement.

I've been thinking about this, and chatted to a few other users, and it seems to make more sense to retrieve roles and permissions at the point the subject is retrieved.

Any thoughts on this?

That's great! Actually when the action access the request it will fetch the role and permissions first, why not do it in one space? And I think we must keep the implement to fetch the subject only.
By the way, it's import to fetch the role and permission in other class but not the subject class. It's better to keep the subject, role, permission more pojo.