]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/people.py
9 from . import handlers_base
as base
10 from . import ui_modules
12 class IndexHandler(base
.BaseHandler
):
13 @tornado.web
.authenticated
15 self
.render("people/index.html")
18 class AvatarHandler(base
.BaseHandler
):
22 # Get the desired size of the avatar file
23 size
= self
.get_argument("size", 0)
27 except (TypeError, ValueError):
30 logging
.debug("Querying for avatar of %s" % uid
)
33 account
= self
.backend
.accounts
.get_by_uid(uid
)
35 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
37 # Allow downstream to cache this for 60 minutes
38 self
.set_expires(3600)
41 avatar
= account
.get_avatar(size
)
43 # If there is no avatar, we serve a default image
45 logging
.debug("No avatar uploaded for %s" % account
)
47 return self
.redirect(self
.static_url("img/default-avatar.jpg"))
49 # Set headers about content
50 self
.set_header("Content-Disposition", "inline; filename=\"%s.jpg\"" % account
.uid
)
51 self
.set_header("Content-Type", "image/jpeg")
57 class CallsHandler(base
.BaseHandler
):
58 @tornado.web
.authenticated
59 def get(self
, uid
, date
=None):
60 account
= self
.backend
.accounts
.get_by_uid(uid
)
62 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
66 date
= datetime
.datetime
.strptime(date
, "%Y-%m-%d").date()
68 raise tornado
.web
.HTTPError(400, "Invalid date: %s" % date
)
70 date
= datetime
.date
.today()
72 self
.render("people/calls.html", account
=account
, date
=date
)
75 class CallHandler(base
.BaseHandler
):
76 @tornado.web
.authenticated
77 def get(self
, uid
, uuid
):
78 account
= self
.backend
.accounts
.get_by_uid(uid
)
80 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
82 call
= self
.backend
.talk
.freeswitch
.get_call_by_uuid(uuid
)
84 raise tornado
.web
.HTTPError(404, "Could not find call %s" % uuid
)
88 self
.render("people/call.html", account
=account
, call
=call
)
91 class ConferencesHandler(base
.BaseHandler
):
92 @tornado.web
.authenticated
94 self
.render("people/conferences.html", conferences
=self
.backend
.talk
.conferences
)
97 class SearchHandler(base
.BaseHandler
):
98 @tornado.web
.authenticated
100 q
= self
.get_argument("q")
103 accounts
= self
.backend
.accounts
.search(q
)
105 # Redirect when only one result was found
106 if len(accounts
) == 1:
107 self
.redirect("/users/%s" % accounts
[0].uid
)
110 self
.render("people/search.html", q
=q
, accounts
=accounts
)
113 class SSHKeysIndexHandler(base
.BaseHandler
):
114 @tornado.web
.authenticated
116 account
= self
.backend
.accounts
.get_by_uid(uid
)
118 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
120 self
.render("people/ssh-keys/index.html", account
=account
)
123 class SSHKeysDownloadHandler(base
.BaseHandler
):
124 @tornado.web
.authenticated
125 def get(self
, uid
, hash_sha256
):
126 account
= self
.backend
.accounts
.get_by_uid(uid
)
128 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
131 key
= account
.get_ssh_key_by_hash_sha256(hash_sha256
)
133 raise tornado
.web
.HTTPError(404, "Could not find key: %s" % hash_sha256
)
136 self
.add_header("Content-Type", "text/plain")
138 self
.finish(key
.keydata
)
141 class SSHKeysUploadHandler(base
.BaseHandler
):
142 @tornado.web
.authenticated
144 account
= self
.backend
.accounts
.get_by_uid(uid
)
146 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
148 # Check for permissions
149 if not account
.can_be_managed_by(self
.current_user
):
150 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
152 self
.render("people/ssh-keys/upload.html", account
=account
)
154 @tornado.web
.authenticated
156 account
= self
.backend
.accounts
.get_by_uid(uid
)
158 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
160 # Check for permissions
161 if not account
.can_be_managed_by(self
.current_user
):
162 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
164 key
= self
.get_argument("key")
167 password
= self
.get_argument("password")
168 if not account
.check_password(password
):
169 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
171 # Try to add new SSH key
173 account
.add_ssh_key(key
)
175 except sshpubkeys
.InvalidKeyException
as e
:
176 self
.render("people/ssh-keys/error-invalid-key.html", account
=account
, exception
=e
)
179 self
.redirect("/users/%s/ssh-keys" % account
.uid
)
182 class SSHKeysDeleteHandler(base
.BaseHandler
):
183 @tornado.web
.authenticated
184 def get(self
, uid
, hash_sha256
):
185 account
= self
.backend
.accounts
.get_by_uid(uid
)
187 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
190 key
= account
.get_ssh_key_by_hash_sha256(hash_sha256
)
192 raise tornado
.web
.HTTPError(404, "Could not find key: %s" % hash_sha256
)
194 self
.render("people/ssh-keys/delete.html", account
=account
, key
=key
)
196 @tornado.web
.authenticated
197 def post(self
, uid
, hash_sha256
):
198 account
= self
.backend
.accounts
.get_by_uid(uid
)
200 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
203 key
= account
.get_ssh_key_by_hash_sha256(hash_sha256
)
205 raise tornado
.web
.HTTPError(404, "Could not find key: %s" % hash_sha256
)
208 password
= self
.get_argument("password")
209 if not account
.check_password(password
):
210 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
213 account
.delete_ssh_key(key
.keydata
)
215 self
.redirect("/users/%s/ssh-keys" % account
.uid
)
218 class SIPHandler(base
.BaseHandler
):
219 @tornado.web
.authenticated
221 account
= self
.backend
.accounts
.get_by_uid(uid
)
223 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
225 # Check for permissions
226 if not account
.can_be_managed_by(self
.current_user
):
227 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
229 self
.render("people/sip.html", account
=account
)
232 class UsersHandler(base
.BaseHandler
):
233 @tornado.web
.authenticated
235 self
.render("people/users.html")
238 class UserHandler(base
.BaseHandler
):
239 @tornado.web
.authenticated
241 account
= self
.backend
.accounts
.get_by_uid(uid
)
243 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
245 self
.render("people/user.html", account
=account
)
248 class UserEditHandler(base
.BaseHandler
):
249 @tornado.web
.authenticated
251 account
= self
.backend
.accounts
.get_by_uid(uid
)
253 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
255 # Check for permissions
256 if not account
.can_be_managed_by(self
.current_user
):
257 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
259 self
.render("people/user-edit.html", account
=account
)
261 @tornado.web
.authenticated
263 account
= self
.backend
.accounts
.get_by_uid(uid
)
265 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
267 # Check for permissions
268 if not account
.can_be_managed_by(self
.current_user
):
269 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
271 # Unfortunately this cannot be wrapped into a transaction
273 account
.first_name
= self
.get_argument("first_name")
274 account
.last_name
= self
.get_argument("last_name")
275 account
.address
= self
.get_argument("address")
279 filename
, data
, mimetype
= self
.get_file("avatar")
281 if not mimetype
.startswith("image/"):
282 raise tornado
.web
.HTTPError(400, "Avatar is not an image file: %s" % mimetype
)
284 account
.upload_avatar(data
)
289 account
.mail_routing_address
= self
.get_argument("mail_routing_address", None)
292 account
.phone_numbers
= self
.get_argument("phone_numbers", "").splitlines()
293 account
.sip_routing_address
= self
.get_argument("sip_routing_address", None)
294 except ldap
.STRONG_AUTH_REQUIRED
as e
:
295 raise tornado
.web
.HTTPError(403, "%s" % e
) from e
297 # Redirect back to user page
298 self
.redirect("/users/%s" % account
.uid
)
301 class UserPasswdHandler(base
.BaseHandler
):
302 @tornado.web
.authenticated
304 account
= self
.backend
.accounts
.get_by_uid(uid
)
306 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
308 # Check for permissions
309 if not account
.can_be_managed_by(self
.current_user
):
310 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
312 self
.render("people/passwd.html", account
=account
)
314 @tornado.web
.authenticated
316 account
= self
.backend
.accounts
.get_by_uid(uid
)
318 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
320 # Check for permissions
321 if not account
.can_be_managed_by(self
.current_user
):
322 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
324 # Get current password
325 password
= self
.get_argument("password")
328 password1
= self
.get_argument("password1")
329 password2
= self
.get_argument("password2")
331 # Passwords must match
332 if not password1
== password2
:
333 raise tornado
.web
.HTTPError(400, "Passwords do not match")
335 # XXX Check password complexity
337 # Check if old password matches
338 if not account
.check_password(password
):
339 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
342 account
.passwd(password1
)
344 # Redirect back to user's page
345 self
.redirect("/users/%s" % account
.uid
)
348 class AccountsListModule(ui_modules
.UIModule
):
349 def render(self
, accounts
=None):
351 accounts
= self
.backend
.accounts
353 return self
.render_string("people/modules/accounts-list.html", accounts
=accounts
)
356 class CDRModule(ui_modules
.UIModule
):
357 def render(self
, account
, date
=None, limit
=None):
358 cdr
= account
.get_cdr(date
=date
, limit
=limit
)
360 return self
.render_string("people/modules/cdr.html", account
=account
, cdr
=cdr
)
363 class ChannelsModule(ui_modules
.UIModule
):
364 def render(self
, account
):
365 return self
.render_string("people/modules/channels.html",
366 account
=account
, channels
=account
.sip_channels
)
369 class MOSModule(ui_modules
.UIModule
):
370 def render(self
, call
):
371 return self
.render_string("people/modules/mos.html", call
=call
)
374 class PasswordModule(ui_modules
.UIModule
):
375 def render(self
, account
):
376 return self
.render_string("people/modules/password.html", account
=account
)
378 def javascript_files(self
):
379 return "js/zxcvbn.js"
381 def embedded_javascript(self
):
382 return self
.render_string("people/modules/password.js")
385 class RegistrationsModule(ui_modules
.UIModule
):
386 def render(self
, account
):
387 return self
.render_string("people/modules/registrations.html", account
=account
)
390 class SIPStatusModule(ui_modules
.UIModule
):
391 def render(self
, account
):
392 return self
.render_string("people/modules/sip-status.html", account
=account
)