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

Commit 335d5ab

Browse files
Merge pull request #104 from nodes-vapor/feature/generic-current-user-tag
Make current user tag generic
2 parents 1b2ae3c + 32dd509 commit 335d5ab

File tree

4 files changed

+26
-39
lines changed

4 files changed

+26
-39
lines changed

Sources/AdminPanel/Models/AdminPanelUser+AdminPanelUserType.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ extension AdminPanelUser: AdminPanelUserType {
9191

9292
return Future.transform(to: (), on: req)
9393
}
94+
95+
// MARK: TemplateDataRepresentable
96+
97+
public func convertToTemplateData() throws -> TemplateData {
98+
return .dictionary([
99+
"id": id.map(TemplateData.int) ?? .null,
100+
"email": .string(email),
101+
"name": .string(name),
102+
"title": title.map(TemplateData.string) ?? .null,
103+
"avatarUrl": avatarUrl.map(TemplateData.string) ?? .null,
104+
"role": role.map { .string($0.description) } ?? .null
105+
])
106+
}
94107
}
95108

96109
// MARK: Roles

Sources/AdminPanel/Models/AdminPanelUserType.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public protocol AdminPanelUserType:
1111
PasswordResettable,
1212
SessionAuthenticatable,
1313
Submittable,
14-
UserType
14+
UserType,
15+
TemplateDataRepresentable
1516
where
1617
ID: LosslessStringConvertible,
1718
Self.ResolvedParameter == Future<Self>

Sources/AdminPanel/Providers/AdminPanelProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public final class AdminPanelProvider<U: AdminPanelUserType>: Provider {
136136
"adminpanel:config": AdminPanelConfigTag<U>(),
137137
"adminpanel:sidebar:heading": SidebarHeadingTag(),
138138
"adminpanel:sidebar:menuitem": SidebarMenuItemTag(),
139-
"adminpanel:user": UserTag(),
139+
"adminpanel:user": CurrentUserTag<U>(),
140140
"adminpanel:user:requireRole": RequireRoleTag<U>()
141141
])
142142

Sources/AdminPanel/Tags/CurrentUserTag.swift

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,20 @@ import Leaf
33
import Sugar
44
import TemplateKit
55

6-
// TODO: make this compatible with generic user
7-
public final class UserTag: TagRenderer {
6+
public final class CurrentUserTag<U: AdminPanelUserType>: TagRenderer {
87
public func render(tag: TagContext) throws -> Future<TemplateData> {
98
try tag.requireParameterCount(1)
10-
let container = try tag.container.make(CurrentUserContainer<AdminPanelUser>.self)
9+
let container = try tag.container.make(CurrentUserContainer<U>.self)
1110

12-
return Future.map(on: tag) {
13-
try container.user?.viewData(for: tag.parameters[0], tag: tag) ?? .null
11+
guard
12+
let user = container.user,
13+
let data = try user.convertToTemplateData().dictionary,
14+
let key = tag.parameters[0].string,
15+
let value = data[key]
16+
else {
17+
throw tag.error(reason: "No user is logged in or the key doesn't exist.")
1418
}
15-
}
16-
}
1719

18-
private extension AdminPanelUser {
19-
enum Keys: String {
20-
case id
21-
case email
22-
case name
23-
case title
24-
case avatarUrl
25-
}
26-
27-
func viewData(for data: TemplateData, tag: TagContext) throws -> TemplateData {
28-
guard let key = data.string else {
29-
throw tag.error(reason: "Wrong type given (expected a string): \(type(of: data))")
30-
}
31-
32-
guard let parsedKey = Keys(rawValue: key) else {
33-
throw tag.error(reason: "Wrong argument given: \(key)")
34-
}
35-
36-
switch parsedKey {
37-
case .id:
38-
return id.map(TemplateData.int) ?? .null
39-
case .email:
40-
return .string(email)
41-
case .name:
42-
return .string(name)
43-
case .title:
44-
return title.map(TemplateData.string) ?? .null
45-
case .avatarUrl:
46-
return avatarUrl.map(TemplateData.string) ?? .null
47-
}
20+
return tag.future(value)
4821
}
4922
}

0 commit comments

Comments
 (0)