Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit e751370

Browse files
authored
Merge pull request #44 from nodes-vapor/develop
Gate system integrated into controllers and remove roles table
2 parents 9f30b66 + b070d9b commit e751370

File tree

12 files changed

+68
-251
lines changed

12 files changed

+68
-251
lines changed

Sources/AdminPanel/Controllers/BackendUserRolesController.swift

Lines changed: 0 additions & 71 deletions
This file was deleted.

Sources/AdminPanel/Controllers/BackendUsersController.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public final class BackendUsersController {
3131
* - return: View
3232
*/
3333
public func index(request: Request) throws -> ResponseRepresentable {
34+
try Gate.allowOrFail(request, "super-admin")
3435

3536
let query = try BackendUser.query()
3637
if let search: String = request.query?["search"]?.string {
@@ -51,10 +52,12 @@ public final class BackendUsersController {
5152
* - return: View
5253
*/
5354
public func create(request: Request) throws -> ResponseRepresentable {
55+
try Gate.allowOrFail(request, "super-admin")
56+
5457
return try drop.view.make("BackendUsers/edit", [
55-
"roles": BackendUserRole.options().makeNode(),
58+
"roles": Configuration.shared?.roleOptions.makeNode() ?? [],
5659
"fieldset": BackendUserForm.getFieldset(request),
57-
"defaultRole": BackendUserRole.defaultRole()
60+
"defaultRole": Configuration.shared?.defaultRole ?? "admin"
5861
], for: request)
5962
}
6063

@@ -65,6 +68,8 @@ public final class BackendUsersController {
6568
* - return: View
6669
*/
6770
public func store(request: Request) throws -> ResponseRepresentable {
71+
try Gate.allowOrFail(request, "super-admin")
72+
6873
do {
6974
// Validate
7075
let backendUserForm = try BackendUserForm(validating: request.data)
@@ -95,12 +100,13 @@ public final class BackendUsersController {
95100
* - return: View
96101
*/
97102
public func edit(request: Request, user: BackendUser) throws -> ResponseRepresentable {
103+
try Gate.allowOrFail(request, "super-admin")
98104

99105
return try drop.view.make("BackendUsers/edit", [
100106
"fieldset": BackendUserForm.getFieldset(request),
101107
"backendUser": try user.makeNode(),
102-
"roles": BackendUserRole.options().makeNode(),
103-
"defaultRole": BackendUserRole.defaultRole()
108+
"roles": Configuration.shared?.roleOptions.makeNode() ?? [],
109+
"defaultRole": Configuration.shared?.defaultRole ?? "admin"
104110
], for: request)
105111
}
106112

@@ -112,6 +118,8 @@ public final class BackendUsersController {
112118
* - return: View
113119
*/
114120
public func update(request: Request) throws -> ResponseRepresentable {
121+
try Gate.allowOrFail(request, "super-admin")
122+
115123
guard let id = request.data["id"]?.int, var backendUser = try BackendUser.query().filter("id", id).first() else {
116124
throw Abort.notFound
117125
}
@@ -140,6 +148,8 @@ public final class BackendUsersController {
140148
* - return: View
141149
*/
142150
public func destroy(request: Request, user: BackendUser) throws -> ResponseRepresentable {
151+
try Gate.allowOrFail(request, "super-admin")
152+
143153
do {
144154
try user.delete()
145155
return Response(redirect: "/admin/backend_users").flash(.success, "Deleted user")

Sources/AdminPanel/Models/BackendUsers/BackendUser.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public final class BackendUser: Auth.User, Model {
114114
table.timestamps()
115115
}
116116

117-
try database.driver.raw(Database.foreign(parentTable: "backend_user_roles", parentPrimaryKey: "slug", childTable: "backend_users", childForeignKey: "role"))
118117
try database.index(table: "backend_users", column: "email")
119118
}
120119

Sources/AdminPanel/Models/BackendUsers/BackendUserRoles/BackendUserRole.swift

Lines changed: 0 additions & 98 deletions
This file was deleted.

Sources/AdminPanel/Resources/Views/Layout/Partials/Navigation/navigation.leaf

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,18 @@
1313
<li class="list-group-heading">
1414
Super admin
1515
</li>
16-
}
17-
18-
#allow(request, "admin") {
19-
<li class="list-group-heading">
20-
Admin
21-
</li>
2216
<li class="list-group-item #active(request, "/admin/backend_users", "/admin/backend_users/create", "/admin/backend_users/edit*")">
2317
<a href="/admin/backend_users">
2418
<i class="fa fa-street-view"></i>
2519
Backend users
2620
</a>
2721
</li>
28-
<li class="list-group-item #active(request, "/admin/backend_users/roles*")">
29-
<a href="/admin/backend_users/roles">
30-
<i class="fa fa-graduation-cap"></i>
31-
Roles
32-
</a>
22+
}
23+
24+
#allow(request, "admin") {
25+
<li class="list-group-heading">
26+
Admin
3327
</li>
28+
3429
}
3530
</ul>

Sources/AdminPanel/Routes/BackendUserRolesRoutes.swift

Lines changed: 0 additions & 24 deletions
This file was deleted.

Sources/AdminPanel/Support/Configuration.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ public struct Configuration {
4141
public var ssoProvider: SSOProtocol?
4242
public let ssoCallbackPath: String?
4343
public let roles: [Role]
44+
public var roleOptions: [String: String] {
45+
46+
var options: [String: String] = [:]
47+
48+
for role in roles {
49+
options[role.slug] = role.title
50+
}
51+
52+
return options
53+
}
54+
55+
public var defaultRole: String {
56+
return "admin"
57+
}
4458

4559
public init(drop: Droplet) throws {
4660
try self.init(config: drop.config)

Sources/AdminPanel/Support/Gate.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vapor
2+
import HTTP
23

34
class Gate {
45

@@ -63,17 +64,40 @@ class Gate {
6364
}
6465
}
6566

67+
// MARK : BackendUser
6668
public static func allow(_ backendUser: BackendUser?, _ role: String) -> Bool {
6769
return self.allow(backendUser?.role ?? "", role)
6870
}
6971

7072
public static func disallow(_ backendUser: BackendUser?, _ role: String) -> Bool {
71-
return !self.allow(backendUser?.role ?? "", role)
73+
return !self.allow(backendUser, role)
7274
}
7375

7476
public static func allowOrFail(_ backendUser: BackendUser?, _ role: String) throws {
7577
if self.disallow(backendUser, role) {
7678
throw self.error
7779
}
7880
}
81+
82+
// MARK: User
83+
public static func allow(_ request: Request, _ role: String) -> Bool {
84+
do {
85+
guard let backendUser = try request.auth.user() as? BackendUser else {
86+
return false
87+
}
88+
return self.allow(backendUser.role, role)
89+
} catch {
90+
return false
91+
}
92+
}
93+
94+
public static func disallow(_ request: Request, _ role: String) -> Bool {
95+
return !self.allow(request, role)
96+
}
97+
98+
public static func allowOrFail(_ request: Request, _ role: String) throws {
99+
if self.disallow(request, role) {
100+
throw self.error
101+
}
102+
}
79103
}

Sources/AdminPanel/Support/LeafTags/FormSelectGroup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public class FormSelectGroup: BasicTag {
104104
let inputValues = arguments[1].value?.nodeObject,
105105
let fieldsetNode = arguments[2].value?.nodeObject
106106
else {
107-
throw Abort.custom(status: .internalServerError, message: "FormSelectGroup parse error, expecting: #form:selectgroup(\"name\", \"default\", fieldset)")
107+
throw Abort.custom(status: .internalServerError, message: "FormSelectGroup parse error, expecting: #form:selectgroup(\"name\", \"values\", fieldset), \"default\"")
108108
}
109109

110110
let fieldset = fieldsetNode[inputName]

Sources/AdminPanel/Support/Provider.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public final class Provider: Vapor.Provider {
3333
Configuration.shared = config
3434

3535
droplet.preparations.append(BackendUserResetPasswordTokens.self)
36-
droplet.preparations.append(BackendUserRole.self)
3736
droplet.preparations.append(BackendUser.self)
3837

3938
droplet.commands.append(Seeder(dropet: droplet))
@@ -61,7 +60,6 @@ public final class Provider: Vapor.Provider {
6160
droplet.group(collection: Middlewares.secured) { secured in
6261
secured.grouped("/admin/dashboard").collection(DashboardRoutes(droplet: droplet))
6362
secured.grouped("/admin/backend_users").collection(BackendUsersRoutes(droplet: droplet))
64-
secured.grouped("/admin/backend_users/roles").collection(BackendUserRolesRoutes(droplet: droplet))
6563
}
6664
}
6765
}

0 commit comments

Comments
 (0)