From e3c40feacc6395340bf01f479e8663814ba669cc Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Tue, 19 Jun 2018 08:09:34 -0500 Subject: [PATCH 1/2] Run User model migration before Attribute --- Sources/App/Configuration/configure.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/App/Configuration/configure.swift b/Sources/App/Configuration/configure.swift index b3f1512..e9e1d17 100644 --- a/Sources/App/Configuration/configure.swift +++ b/Sources/App/Configuration/configure.swift @@ -84,8 +84,8 @@ public func configure( /// Configure migrations var migrations = MigrationConfig() - migrations.add(model: Attribute.self, database: .mysql) migrations.add(model: User.self, database: .mysql) + migrations.add(model: Attribute.self, database: .mysql) services.register(migrations) let jwt = JWTDataConfig() From 70ade0a6c69cd1b7c98b38b5aae6076d8cd85a9d Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Mon, 9 Jul 2018 12:34:20 -0600 Subject: [PATCH 2/2] Replaced use of RouteRestrictionMiddleware with PermissionsMiddleware --- Sources/App/Configuration/configure.swift | 3 +-- Sources/App/Controllers/AdminController.swift | 9 +-------- Sources/App/Controllers/AuthController.swift | 12 ++++-------- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/Sources/App/Configuration/configure.swift b/Sources/App/Configuration/configure.swift index e9e1d17..fccf88d 100644 --- a/Sources/App/Configuration/configure.swift +++ b/Sources/App/Configuration/configure.swift @@ -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( diff --git a/Sources/App/Controllers/AdminController.swift b/Sources/App/Controllers/AdminController.swift index f41b50a..557fcff 100644 --- a/Sources/App/Controllers/AdminController.swift +++ b/Sources/App/Controllers/AdminController.swift @@ -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( - 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(allowed: [.admin]), JWTVerificationMiddleware() ) diff --git a/Sources/App/Controllers/AuthController.swift b/Sources/App/Controllers/AuthController.swift index 00ec95f..502a6a1 100644 --- a/Sources/App/Controllers/AuthController.swift +++ b/Sources/App/Controllers/AuthController.swift @@ -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(.POST, at: any, "users", "register", allowed: [.admin])] - let auth = router.grouped(any, "users").grouped( - RouteRestrictionMiddleware( - restrictions: restrictions, - parameters: [User.routingSlug: User.resolveParameter] - ) - ) + let auth = router.grouped(any, "users") + let restricted = auth.grouped(PermissionsMiddleware(allowed: [.admin])) let protected = auth.grouped(JWTAuthenticatableMiddleware()) - 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)