Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sources/AdminPanel/Providers/AdminPanelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ public final class AdminPanelProvider<U: AdminPanelUserType>: Provider {
"adminpanel:sidebar:heading": SidebarHeadingTag(),
"adminpanel:sidebar:menuitem": SidebarMenuItemTag(),
"adminpanel:user": CurrentUserTag<U>(),
"adminpanel:user:requireRole": RequireRoleTag<U>()
"adminpanel:user:requireRole": RequireRoleTag<U>(),
"adminpanel:user:roleAllows": RoleAllowsTag<U>()
])

return .done(on: container)
Expand Down
28 changes: 28 additions & 0 deletions Sources/AdminPanel/Tags/RoleAllowsTag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Leaf
import Authentication
import Sugar

public final class RoleAllowsTag<U: AdminPanelUserType>: TagRenderer {
public func render(tag: TagContext) throws -> Future<TemplateData> {
try tag.requireParameterCount(1)

let container = try tag.container.make(
CurrentUserContainer<U>.self
)

guard
let roleString: String = tag.parameters[0].string,
let requiredRole = U.Role.init(roleString)
else {
throw tag.error(reason: "Invalid role requirement")
}

guard
let userRole = container.user?.role
else {
return tag.future(.bool(false))
}

return tag.future(.bool(userRole >= requiredRole))
}
}