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

Commit 9f30b66

Browse files
authored
Merge pull request #43 from nodes-vapor/develop
Gate allow leaf tag
2 parents 3bea0c7 + 7d862c4 commit 9f30b66

File tree

4 files changed

+64
-23
lines changed

4 files changed

+64
-23
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
Dashboard
99
</a>
1010
</li>
11+
12+
#allow(request, "super-admin") {
13+
<li class="list-group-heading">
14+
Super admin
15+
</li>
16+
}
17+
18+
#allow(request, "admin") {
1119
<li class="list-group-heading">
1220
Admin
1321
</li>
@@ -23,4 +31,5 @@
2331
Roles
2432
</a>
2533
</li>
34+
}
2635
</ul>

Sources/AdminPanel/Support/Gate.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import Vapor
22

33
class Gate {
44

5-
public let error = Abort.custom(status: .forbidden, message: "User does not have access to this page")
5+
public static let error = Abort.custom(status: .forbidden, message: "User does not have access to this page")
66

77
/// Check if a backend_users.role is allowed to access
88
///
99
/// - Parameters:
1010
/// - backendUserRole: role from config
1111
/// - role: role from config
1212
/// - Returns: bool
13-
public func allow(_ backendUserRole: String, _ role: String) -> Bool {
13+
public static func allow(_ backendUserRole: String, _ role: String) -> Bool {
1414
guard let roles = Configuration.shared?.roles else {
1515
print("AdminPanel.Gate missing configuration")
1616
return false
@@ -53,27 +53,27 @@ class Gate {
5353
return false
5454
}
5555

56-
public func disallow(_ backendUserRole: String, _ role: String) -> Bool {
57-
return !allow(backendUserRole, role)
56+
public static func disallow(_ backendUserRole: String, _ role: String) -> Bool {
57+
return !self.allow(backendUserRole, role)
5858
}
5959

60-
public func allowOrFail(_ backendUserRole: String, _ role: String) throws {
61-
if disallow(backendUserRole, role) {
62-
throw error
60+
public static func allowOrFail(_ backendUserRole: String, _ role: String) throws {
61+
if self.disallow(backendUserRole, role) {
62+
throw self.error
6363
}
6464
}
6565

66-
public func allow(_ backendUser: BackendUser, _ role: String) -> Bool {
67-
return allow(backendUser.role, role)
66+
public static func allow(_ backendUser: BackendUser?, _ role: String) -> Bool {
67+
return self.allow(backendUser?.role ?? "", role)
6868
}
6969

70-
public func disallow(_ backendUser: BackendUser, _ role: String) -> Bool {
71-
return !allow(backendUser.role, role)
70+
public static func disallow(_ backendUser: BackendUser?, _ role: String) -> Bool {
71+
return !self.allow(backendUser?.role ?? "", role)
7272
}
7373

74-
public func allowOrFail(_ backendUser: BackendUser, _ role: String) throws {
75-
if disallow(backendUser, role) {
76-
throw error
74+
public static func allowOrFail(_ backendUser: BackendUser?, _ role: String) throws {
75+
if self.disallow(backendUser, role) {
76+
throw self.error
7777
}
7878
}
7979
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Leaf
2+
3+
public final class Allow: Tag {
4+
public let name = "allow"
5+
6+
public enum Error: Swift.Error {
7+
case expetedTwoArguments(have: [Argument])
8+
}
9+
10+
public func run(
11+
stem: Stem,
12+
context: Context,
13+
tagTemplate: TagTemplate,
14+
arguments: [Argument]) throws -> Node? {
15+
guard arguments.count == 2 else { throw Error.expetedTwoArguments(have: arguments) }
16+
return nil
17+
}
18+
19+
public func shouldRender(
20+
stem: Stem,
21+
context: Context,
22+
tagTemplate: TagTemplate,
23+
arguments: [Argument],
24+
value: Node?) -> Bool {
25+
guard let request = arguments.first?.value else { return false }
26+
guard let backendUserRole = request["storage", "authedBackendUser", "role"]?.string else { return false }
27+
guard let role = arguments[1].value?.string else { return false }
28+
29+
return Gate.allow(backendUserRole, role)
30+
}
31+
}

Sources/AdminPanel/Support/Provider.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ public final class Provider: Vapor.Provider {
1414

1515
if let leaf = droplet.view as? LeafRenderer {
1616
// AdminPanel
17-
leaf.stem.register(Active());
18-
leaf.stem.register(FormOpen());
19-
leaf.stem.register(FormClose());
20-
leaf.stem.register(FormTextGroup());
21-
leaf.stem.register(FormEmailGroup());
22-
leaf.stem.register(FormPasswordGroup());
23-
leaf.stem.register(FormNumberGroup());
24-
leaf.stem.register(FormCheckboxGroup());
25-
leaf.stem.register(FormSelectGroup());
17+
leaf.stem.register(Active())
18+
leaf.stem.register(FormOpen())
19+
leaf.stem.register(FormClose())
20+
leaf.stem.register(FormTextGroup())
21+
leaf.stem.register(FormEmailGroup())
22+
leaf.stem.register(FormPasswordGroup())
23+
leaf.stem.register(FormNumberGroup())
24+
leaf.stem.register(FormCheckboxGroup())
25+
leaf.stem.register(FormSelectGroup())
26+
leaf.stem.register(Allow())
2627

2728
//Paginator
2829
leaf.stem.register(PaginatorTag())

0 commit comments

Comments
 (0)