/
web.d
executable file
·216 lines (169 loc) · 5.9 KB
/
web.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
module pierun.interfaces.web;
import std.conv, std.format, std.string, std.typecons;
import vibe.d;
import vibe.web.auth;
import hibernated.core;
import pierun.core;
import pierun.utils.dbcache, pierun.utils.misc;
import pierun.interfaces.common,
pierun.interfaces.admin,
pierun.interfaces.rss;
@requiresAuth
class WebInterface
{
private {
DataSource dataSource;
DBSession sessionMember;
DBCache dbCacheMember;
AdminWebInterface adminWebInterface;
RSSWebInterface rssWebInterace;
}
@property AdminWebInterface admin() { return adminWebInterface; }
@property RSSWebInterface rss() { return rssWebInterace; }
@noRoute @property DBCache dbCache() { return dbCacheMember; }
@noRoute @property DBSession session() { return sessionMember; }
@noRoute
AuthInfo authenticate(HTTPServerRequest req, HTTPServerResponse res){
if (!req.session || !req.session.isKeySet("auth"))
throw new HTTPStatusException(HTTPStatus.forbidden, "Not authorized to perform this action!");
return req.session.get!AuthInfo("auth");
}
this(DataSource ds, DBSession s)
{
this.dataSource = ds;
this.sessionMember = s;
this.dbCacheMember = new DBCache(s);
this.adminWebInterface = new AdminWebInterface(this);
this.rssWebInterace = new RSSWebInterface(this);
}
@noAuth
void index(HTTPServerRequest req, HTTPServerResponse res)
{
Post[] posts = session
.createQuery("SELECT P FROM Post AS P " ~
"WHERE status = 0 " ~
"ORDER BY P.published DESC")
.list!Post;
render!("index.dt", posts);
}
@noAuth
void getLogin(HTTPServerRequest req, string _error = null)
{
string page_title = "login";
render!("login.dt", _error);
}
@noAuth @errorDisplay!getLogin
void postLogin(string username, string password,
scope HTTPServerRequest req, scope HTTPServerResponse res)
{
auto user = session.createQuery("FROM User WHERE name=:Name")
.setParameter("Name", username)
.uniqueResult!User;
enforceHTTP(user !is null,
HTTPStatus.forbidden, "Username or password incorrect");
import botan.passhash.bcrypt;
enforceHTTP(checkBcrypt(password ~ user.salt, user.hashedPassword),
HTTPStatus.forbidden, "Username or password incorrect");
AuthInfo ai;
ai.userName = user.name;
ai.admin = true;
req.session = res.startSession;
req.session.set("auth", ai);
redirect("/");
}
@path("/post/:id/*") @noAuth @errorDisplay!error
void getPostIdName(scope HTTPServerRequest req, scope HTTPServerResponse res)
{
auto id = req.params["id"].to!int;
Post p = dbCache.getPost(id);
enforceHTTP(p !is null, HTTPStatus.notFound,
"Post %d not found!".format(id));
if(p.status == Post.Status.Private) {
auto auth = req.getAuth;
enforceHTTP(!auth.isNull && auth.isAdmin, HTTPStatus.notFound,
"Post %d not found!".format(id));
}
render!("post.dt", p);
}
@path("/post/:id") @noAuth @errorDisplay!error
void getPostId(scope HTTPServerRequest req, scope HTTPServerResponse res)
{
getPostIdName(req, res);
}
@auth(Role.admin)
void getAddPost(HTTPServerRequest req, HTTPServerResponse res)
{
postAddPost(req, res);
}
@auth(Role.admin)
void postAddPost(HTTPServerRequest req, HTTPServerResponse res,
string markdown = "", string excerpt = "", string title = "",
string language = "EN", string tags = "", string _error = null)
{
render!("add_post.dt", markdown, excerpt,
title, language, tags, _error);
}
@auth(Role.admin) @errorDisplay!postAddPost
void postSendPost(HTTPServerRequest req, HTTPServerResponse res,
AuthInfo ai, string markdown, string excerpt, string title,
string language, string tags)
{
enforceHTTP(language.length == 2, HTTPStatus.badRequest,
"Language must be two characters long");
import std.array, std.algorithm, std.regex, std.traits;
auto getOrMakeTag = delegate Tag(const string name) {
import std.stdio;
writefln("working on tag: %s", name);
auto t = dbCache.getTag(name);
if(t is null) {
t = new Tag;
t.name = name;
t.slugName = name.toSlugForm;
session.save(t);
}
return t;
};
auto splitTags = tags
.split(ctRegex!`,\s+`)
.map!(identity!getOrMakeTag)
.array;
User u = session.createQuery("FROM User WHERE name=:Name")
.setParameter("Name", ai.userName)
.uniqueResult!User;
Post p = new Post;
PostData pd = new PostData;
p.author = u;
p.edits = [pd];
p.published = cast(DateTime)Clock.currTime;
pd.title = title;
pd.markdown = markdown;
pd.excerpt = excerpt;
pd.timestamp = p.published;
pd.post = p;
pd.tags = splitTags;
u.posts ~= p;
session.update(u);
session.save(p);
session.save(pd);
redirect("/");
}
@noRoute @noAuth
void error(HTTPServerRequest req, string _error)
{
render!("error.dt", _error);
}
mixin PrivateAccessProxy;
}
Nullable!AuthInfo getAuth(ref HTTPServerRequest req) {
Nullable!AuthInfo auth;
if (req.session && req.session.isKeySet("auth"))
auth = req.session.get!AuthInfo("auth");
return auth;
}
Nullable!string getTime(ref HTTPServerRequest req) {
Nullable!string ret;
import std.conv;
auto diff = Clock.currTime - req.timeCreated;
ret = diff.to!string;
return ret;
}