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

Commit ffbd149

Browse files
Merge pull request #105 from nodes-vapor/feature/role-allows-tag
added tag for boolean evaluation of user role
2 parents 335d5ab + 634bc5c commit ffbd149

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Sources/AdminPanel/Providers/AdminPanelProvider.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ public final class AdminPanelProvider<U: AdminPanelUserType>: Provider {
137137
"adminpanel:sidebar:heading": SidebarHeadingTag(),
138138
"adminpanel:sidebar:menuitem": SidebarMenuItemTag(),
139139
"adminpanel:user": CurrentUserTag<U>(),
140-
"adminpanel:user:requireRole": RequireRoleTag<U>()
140+
"adminpanel:user:requireRole": RequireRoleTag<U>(),
141+
"adminpanel:user:roleAllows": RoleAllowsTag<U>()
141142
])
142143

143144
return .done(on: container)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Leaf
2+
import Authentication
3+
import Sugar
4+
5+
public final class RoleAllowsTag<U: AdminPanelUserType>: TagRenderer {
6+
public func render(tag: TagContext) throws -> Future<TemplateData> {
7+
try tag.requireParameterCount(1)
8+
9+
let container = try tag.container.make(
10+
CurrentUserContainer<U>.self
11+
)
12+
13+
guard
14+
let roleString: String = tag.parameters[0].string,
15+
let requiredRole = U.Role.init(roleString)
16+
else {
17+
throw tag.error(reason: "Invalid role requirement")
18+
}
19+
20+
guard
21+
let userRole = container.user?.role
22+
else {
23+
return tag.future(.bool(false))
24+
}
25+
26+
return tag.future(.bool(userRole >= requiredRole))
27+
}
28+
}

0 commit comments

Comments
 (0)