Skip to content

Commit

Permalink
Replaced use of RouteRestrictionMiddleware with PermissionsMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
calebkleveter committed Jul 9, 2018
1 parent e3c40fe commit 70ade0a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
3 changes: 1 addition & 2 deletions Sources/App/Configuration/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public func configure(
else {
throw MySQLError(
identifier: "missingEnvVars",
reason: "One or more expected environment variables are missing: DATABASE_HOSTNAME, DATABASE_USER, DATABASE_DB",
source: .capture()
reason: "One or more expected environment variables are missing: DATABASE_HOSTNAME, DATABASE_USER, DATABASE_DB"
)
}
let config = MySQLDatabaseConfig(
Expand Down
9 changes: 1 addition & 8 deletions Sources/App/Controllers/AdminController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ final class AdminController: RouteCollection {
// Create a route-group that only allows
// admin users to access the endpoint.
let admin = router.grouped(
RouteRestrictionMiddleware<UserStatus, Payload, User>(
restrictions: [
RouteRestriction.init(.GET, at: "users", allowed: [.admin]),
RouteRestriction.init(at: "users", User.parameter, allowed: [.admin]),
RouteRestriction.init(.PATCH, at: "attributes", Attribute.parameter, allowed: [.admin])
],
parameters: [User.routingSlug: User.resolveParameter, Attribute.routingSlug: Attribute.resolveParameter]
),
PermissionsMiddleware<UserStatus, Payload>(allowed: [.admin]),
JWTVerificationMiddleware()
)

Expand Down
12 changes: 4 additions & 8 deletions Sources/App/Controllers/AuthController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@ import JWT
/// A route controller that handles user authentication with JWT.
final class AuthController: RouteCollection {
func boot(router: Router) throws {
let restrictions = openRegistration ? [] : [RouteRestriction<UserStatus>(.POST, at: any, "users", "register", allowed: [.admin])]

let auth = router.grouped(any, "users").grouped(
RouteRestrictionMiddleware<UserStatus, Payload, User>(
restrictions: restrictions,
parameters: [User.routingSlug: User.resolveParameter]
)
)
let auth = router.grouped(any, "users")
let restricted = auth.grouped(PermissionsMiddleware<UserStatus, Payload>(allowed: [.admin]))
let protected = auth.grouped(JWTAuthenticatableMiddleware<User>())

auth.post(User.self, at: "register", use: register)
auth.post("newPassword", use: newPassword)
auth.post("accessToken", use: refreshAccessToken)

restricted.post(User.self, at: "register", use: register)

protected.post("login", use: login)
protected.get("status", use: status)

Expand Down

0 comments on commit 70ade0a

Please sign in to comment.