1 module vibeauth.router.management.views;
2 
3 import std..string;
4 import std.conv;
5 
6 import vibeauth.mvc.view;
7 import vibeauth.configuration;
8 import vibeauth.users;
9 
10 import vibe.data.json;
11 import vibe.http.router;
12 
13 class UserManagementListView : View {
14   private {
15     const ServiceConfiguration configuration;
16     UserCollection userCollection;
17   }
18 
19   this(const ServiceConfiguration configuration, UserCollection userCollection) {
20     this.configuration = configuration;
21     this.userCollection = userCollection;
22 
23     super(configuration.templates.userManagement.listTemplate, configuration.serializeToJson);
24   }
25 
26   override string generateBody() {
27     string listPage = `<table class="table"><tbody>`;
28 
29     foreach(user; userCollection) {
30       listPage ~= `<tr>` ~ 
31       `<th>` ~ user.username ~ `</th>` ~ 
32       `<td>` ~ user.email ~ `</td>` ~
33        `<td><a href="` ~ configuration.paths.userManagement.profile.replace(":id", user.id) ~ `">Edit</a></td>`~ 
34        `</tr>`;
35     }
36 
37     listPage ~= `</tbody></table>`;
38 
39     return listPage;
40   }
41 }
42 
43 alias UserView(string T) = BasicView!("templates.userManagement.userTemplate", T);
44 
45 alias ProfileView  = UserView!"templates.userManagement.profileForm";
46 alias AccountView  = UserView!"templates.userManagement.accountForm";
47 alias SecurityView = UserView!"templates.userManagement.securityForm";
48 alias QuestionView = UserView!"templates.userManagement.question";
49 
50 class RedirectView {
51   private {
52     HTTPServerRequest req;
53     HTTPServerResponse res;
54     string path;
55   }
56 
57   this(HTTPServerRequest req, HTTPServerResponse res, string path) {
58     this.req = req;
59     this.res = res;
60     this.path = path;
61   }
62 
63   string destinationPath() {
64     auto requestPath = req.fullURL;
65     auto destinationPath = path.replace(":id", req.context["userId"].to!string);
66 
67     return requestPath.schema ~ "://" ~ requestPath.host ~ ":" ~ requestPath.port.to!string ~ destinationPath;
68   }
69 
70   void respondError(string value) {
71     string message = `?error=` ~ value.replace(" ", "%20");
72 
73     res.redirect(destinationPath ~ message, 302);
74   }
75 
76   void respondMessage(string value) {
77     string message = `?message=` ~ value.replace(" ", "%20");
78 
79     res.redirect(destinationPath ~ message, 302);
80   }
81 }