]> git.ipfire.org Git - ipfire.org.git/blame - src/web/wiki.py
wiki: Fix authorisation handling when editing pages
[ipfire.org.git] / src / web / wiki.py
CommitLineData
181d08f3
MT
1#!/usr/bin/python3
2
c21ffadb 3import difflib
181d08f3
MT
4import tornado.web
5
6from . import auth
7from . import base
6ac7e934 8from . import ui_modules
181d08f3 9
b6e8b28f
MT
10class ActionEditHandler(auth.CacheMixin, base.BaseHandler):
11 @tornado.web.authenticated
3587f73a
MT
12 def get(self, path):
13 # Check permissions
14 if not self.backend.wiki.check_acl(path, self.current_user):
15 raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user))
16
17 # Fetch the wiki page
18 page = self.backend.wiki.get_page(path)
19
20 # Empty page if it was deleted
21 if page and page.was_deleted():
22 page = None
b6e8b28f 23
3587f73a
MT
24 # Render page
25 self.render("wiki/edit.html", page=page)
26
27 @tornado.web.authenticated
28 def post(self, path):
b6e8b28f
MT
29 # Check permissions
30 if not self.backend.wiki.check_acl(path, self.current_user):
31 raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user))
32
33 content = self.get_argument("content", None)
34 changes = self.get_argument("changes")
35
36 # Create a new page in the database
37 with self.db.transaction():
38 page = self.backend.wiki.create_page(path,
39 self.current_user, content, changes=changes, address=self.get_remote_ip())
40
d64a1e35
MT
41 # Add user as a watcher if wanted
42 watch = self.get_argument("watch", False)
43 if watch:
44 page.add_watcher(self.current_user)
45
b6e8b28f
MT
46 # Redirect back
47 if page.was_deleted():
48 self.redirect("/")
49 else:
50 self.redirect(page.url)
51
52 def on_finish(self):
53 """
54 Updates the search index after the page has been edited
55 """
56 # This is being executed in the background and after
57 # the response has been set to the client
58 with self.db.transaction():
59 self.backend.wiki.refresh()
60
61
f2cfd873
MT
62class ActionUploadHandler(auth.CacheMixin, base.BaseHandler):
63 @tornado.web.authenticated
64 def post(self):
65 path = self.get_argument("path")
66
11afe905
MT
67 # Check permissions
68 if not self.backend.wiki.check_acl(path, self.current_user):
69 raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user))
70
f2cfd873
MT
71 try:
72 filename, data, mimetype = self.get_file("file")
73
74 # XXX check valid mimetypes
75
76 with self.db.transaction():
77 file = self.backend.wiki.upload(path, filename, data,
78 mimetype=mimetype, author=self.current_user,
79 address=self.get_remote_ip())
80
81 except TypeError as e:
82 raise e
83
84 self.redirect("%s/files" % path)
85
86
d64a1e35
MT
87class ActionWatchHandler(auth.CacheMixin, base.BaseHandler):
88 @tornado.web.authenticated
89 def get(self, action, path):
90 page = self.backend.wiki.get_page(path)
91 if not page:
92 raise tornado.web.HTTPError(404, "Page does not exist: %s" % path)
93
94 with self.db.transaction():
95 if action == "watch":
96 page.add_watcher(self.current_user)
97 elif action == "unwatch":
98 page.remove_watcher(self.current_user)
99
100 # Redirect back to page
101 self.redirect(page.url)
102
103
f2cfd873
MT
104class FilesHandler(auth.CacheMixin, base.BaseHandler):
105 @tornado.web.authenticated
106 def get(self, path):
11afe905
MT
107 # Check permissions
108 if not self.backend.wiki.check_acl(path, self.current_user):
109 raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user))
110
f2cfd873
MT
111 files = self.backend.wiki.get_files(path)
112
113 self.render("wiki/files/index.html", path=path, files=files)
114
115
568b265b 116class FileHandler(base.BaseHandler):
8cb0bea4
MT
117 @property
118 def action(self):
119 return self.get_argument("action", None)
120
f2cfd873 121 def get(self, path):
11afe905
MT
122 # Check permissions
123 if not self.backend.wiki.check_acl(path, self.current_user):
124 raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user))
125
126 # Fetch the file
f2cfd873
MT
127 file = self.backend.wiki.get_file_by_path(path)
128 if not file:
129 raise tornado.web.HTTPError(404, "Could not find %s" % path)
130
8cb0bea4
MT
131 # Render detail page
132 if self.action == "detail":
531846f7
MT
133 for breadcrumb, title in self.backend.wiki.make_breadcrumbs(path):
134 page = self.backend.wiki.get_page(breadcrumb)
135 if page:
136 break
137
138 self.render("wiki/files/detail.html", page=page, file=file)
8cb0bea4
MT
139 return
140
79dd9a0f
MT
141 size = self.get_argument_int("s", None)
142
143 # Check if image should be resized
144 if file.is_image() and size:
145 blob = file.get_thumbnail(size)
146 else:
147 blob = file.blob
148
f2cfd873
MT
149 # Set headers
150 self.set_header("Content-Type", file.mimetype or "application/octet-stream")
79dd9a0f 151 self.set_header("Content-Length", len(blob))
f2cfd873 152
568b265b
MT
153 # Set expires
154 self.set_expires(3600)
155
79dd9a0f
MT
156 # Deliver content
157 self.finish(blob)
f2cfd873
MT
158
159
181d08f3 160class PageHandler(auth.CacheMixin, base.BaseHandler):
d398ca08
MT
161 @property
162 def action(self):
163 return self.get_argument("action", None)
164
a446dcb9
MT
165 def write_error(self, status_code, **kwargs):
166 # Render a custom page for 404
167 if status_code == 404:
168 self.render("wiki/404.html", **kwargs)
169 return
170
171 # Otherwise raise this to one layer above
172 super().write_error(status_code, **kwargs)
173
181d08f3 174 @tornado.web.removeslash
5bc8e262 175 def get(self, path):
11afe905 176 # Check permissions
5bc8e262
MT
177 if not self.backend.wiki.check_acl(path, self.current_user):
178 raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user))
11afe905 179
7d699684
MT
180 # Check if we are asked to render a certain revision
181 revision = self.get_argument("revision", None)
182
183 # Fetch the wiki page
5bc8e262 184 page = self.backend.wiki.get_page(path, revision=revision)
181d08f3 185
c21ffadb
MT
186 # Diff
187 if self.action == "diff":
188 # Get both revisions
189 a = self.get_argument("a")
190 b = self.get_argument("b")
191
192 # Fetch both versions of the page
5bc8e262
MT
193 a = self.backend.wiki.get_page(path, revision=a)
194 b = self.backend.wiki.get_page(path, revision=b)
c21ffadb
MT
195 if not a or not b:
196 raise tornado.web.HTTPError(404)
197
198 # Cannot render a diff for the identical page
199 if a == b:
200 raise tornado.web.HTTPError(400)
201
202 # Make sure that b is newer than a
203 if a > b:
204 a, b = b, a
205
206 self.render("wiki/diff.html", page=page, a=a, b=b)
207 return
208
7d699684
MT
209 # Revisions
210 elif self.action == "revisions":
211 self.render("wiki/revisions.html", page=page)
212 return
d398ca08 213
181d08f3
MT
214 # If the page does not exist, we send 404
215 if not page or page.was_deleted():
216 raise tornado.web.HTTPError(404)
217
218 # Fetch the latest revision
219 latest_revision = page.get_latest_revision()
220
221 # Render page
222 self.render("wiki/page.html", page=page, latest_revision=latest_revision)
223
181d08f3
MT
224
225class SearchHandler(auth.CacheMixin, base.BaseHandler):
226 @base.blacklisted
227 def get(self):
228 q = self.get_argument("q")
229
11afe905 230 pages = self.backend.wiki.search(q, account=self.current_user, limit=50)
181d08f3 231
df80be2c 232 self.render("wiki/search-results.html", q=q, pages=pages)
6ac7e934
MT
233
234
f9db574a
MT
235class RecentChangesHandler(auth.CacheMixin, base.BaseHandler):
236 def get(self):
11afe905 237 recent_changes = self.backend.wiki.get_recent_changes(self.current_user, limit=50)
f9db574a
MT
238
239 self.render("wiki/recent-changes.html", recent_changes=recent_changes)
240
241
2f23c558
MT
242class WatchlistHandler(auth.CacheMixin, base.BaseHandler):
243 @tornado.web.authenticated
244 def get(self):
245 pages = self.backend.wiki.get_watchlist(self.current_user)
246
247 self.render("wiki/watchlist.html", pages=pages)
248
249
c21ffadb
MT
250class WikiDiffModule(ui_modules.UIModule):
251 differ = difflib.Differ()
252
253 def render(self, a, b):
254 diff = self.differ.compare(
255 a.markdown.splitlines(),
256 b.markdown.splitlines(),
257 )
258
259 return self.render_string("wiki/modules/diff.html", diff=diff)
260
261
f9db574a 262class WikiListModule(ui_modules.UIModule):
27ac1524
MT
263 def render(self, pages, link_revision=False, show_breadcrumbs=True,
264 show_author=True, show_changes=False):
7d699684 265 return self.render_string("wiki/modules/list.html", link_revision=link_revision,
27ac1524
MT
266 pages=pages, show_breadcrumbs=show_breadcrumbs,
267 show_author=show_author, show_changes=show_changes)
f9db574a
MT
268
269
6ac7e934 270class WikiNavbarModule(ui_modules.UIModule):
67573803 271 def render(self, suffix=None):
f2cfd873
MT
272 _ = self.locale.translate
273
67573803
MT
274 breadcrumbs = self.backend.wiki.make_breadcrumbs(self.request.path)
275
f2cfd873
MT
276 # Don't search for a title for the file manager
277 if self.request.path.endswith("/files"):
278 title = _("Files")
279 else:
280 title = self.backend.wiki.get_page_title(self.request.path)
6ac7e934
MT
281
282 return self.render_string("wiki/modules/navbar.html",
67573803 283 breadcrumbs=breadcrumbs, page_title=title, suffix=suffix)