Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook in Model{,Token}Authenticatable to load associated information #700

Open
tholo opened this issue Aug 5, 2020 · 1 comment
Open
Labels
enhancement New feature or request
Projects

Comments

@tholo
Copy link

tholo commented Aug 5, 2020

It would often be very helpful if it would be possibly to hook into ModelAuthenticatable and ModelTokenAuthenticatable to cause related data to be eager loaded.

One area where this would be very useful would be if a User has a relationship to e.g. Role, which is then used to validate actual access to resources, for instance:

final class User: Model {
...
@Siblings(through: UserRole.self, from: \.$user, to: \.$role)
public var roles: [Role]
...
}

final class UserRole: Model { ... }

final class Role: Model {
...
@Siblings(through: UserRole.self, from: \.$role, to: \.$user)
public var users: [User]
...
}

With such a setup one would rather often want to be able to access User.roles to validate access to some resource, but with the current implementation of ModelAuthenticatable and ModelTokenAuthenticatable one has to load User.$roles in each and every instance one wants to check them.

@Magiguigui
Copy link

You can override the static query(on: Database) method of Model protocol directly in your User model :

    public static func query(on database: Database) -> QueryBuilder<User> {
        return QueryBuilder(database: database)
            .with(\.$roles)
    }

@tanner0101 tanner0101 added the enhancement New feature or request label Aug 6, 2020
@tanner0101 tanner0101 added this to To Do in Vapor 4 via automation Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Vapor 4
  
To Do
Development

No branches or pull requests

3 participants