]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/people.py
9 from .. import countries
13 from . import ui_modules
15 class IndexHandler(auth
.CacheMixin
, base
.BaseHandler
):
16 @tornado.web
.authenticated
18 self
.render("people/index.html")
21 class AvatarHandler(base
.BaseHandler
):
23 # Get the desired size of the avatar file
24 size
= self
.get_argument("size", 0)
28 except (TypeError, ValueError):
31 logging
.debug("Querying for avatar of %s" % uid
)
34 account
= self
.backend
.accounts
.get_by_uid(uid
)
36 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
38 # Allow downstream to cache this for 60 minutes
39 self
.set_expires(3600)
42 avatar
= account
.get_avatar(size
)
44 # If there is no avatar, we serve a default image
46 logging
.debug("No avatar uploaded for %s" % account
)
48 return self
.redirect(self
.static_url("img/default-avatar.jpg"))
50 # Set headers about content
51 self
.set_header("Content-Disposition", "inline; filename=\"%s.jpg\"" % account
.uid
)
52 self
.set_header("Content-Type", "image/jpeg")
58 class CallsHandler(auth
.CacheMixin
, base
.BaseHandler
):
59 @tornado.web
.authenticated
60 def get(self
, uid
, date
=None):
61 account
= self
.backend
.accounts
.get_by_uid(uid
)
63 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
67 date
= datetime
.datetime
.strptime(date
, "%Y-%m-%d").date()
69 raise tornado
.web
.HTTPError(400, "Invalid date: %s" % date
)
71 date
= datetime
.date
.today()
73 self
.render("people/calls.html", account
=account
, date
=date
)
76 class CallHandler(auth
.CacheMixin
, base
.BaseHandler
):
77 @tornado.web
.authenticated
78 def get(self
, uid
, uuid
):
79 account
= self
.backend
.accounts
.get_by_uid(uid
)
81 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
83 call
= self
.backend
.talk
.freeswitch
.get_call_by_uuid(uuid
)
85 raise tornado
.web
.HTTPError(404, "Could not find call %s" % uuid
)
89 self
.render("people/call.html", account
=account
, call
=call
)
92 class ConferencesHandler(auth
.CacheMixin
, base
.BaseHandler
):
93 @tornado.web
.authenticated
95 self
.render("people/conferences.html", conferences
=self
.backend
.talk
.conferences
)
98 class SearchHandler(auth
.CacheMixin
, base
.BaseHandler
):
99 @tornado.web
.authenticated
101 q
= self
.get_argument("q")
104 accounts
= self
.backend
.accounts
.search(q
)
106 # Redirect when only one result was found
107 if len(accounts
) == 1:
108 self
.redirect("/users/%s" % accounts
[0].uid
)
111 self
.render("people/search.html", q
=q
, accounts
=accounts
)
114 class SSHKeysIndexHandler(auth
.CacheMixin
, base
.BaseHandler
):
115 @tornado.web
.authenticated
117 account
= self
.backend
.accounts
.get_by_uid(uid
)
119 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
121 self
.render("people/ssh-keys/index.html", account
=account
)
124 class SSHKeysDownloadHandler(auth
.CacheMixin
, base
.BaseHandler
):
125 @tornado.web
.authenticated
126 def get(self
, uid
, hash_sha256
):
127 account
= self
.backend
.accounts
.get_by_uid(uid
)
129 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
132 key
= account
.get_ssh_key_by_hash_sha256(hash_sha256
)
134 raise tornado
.web
.HTTPError(404, "Could not find key: %s" % hash_sha256
)
137 self
.add_header("Content-Type", "text/plain")
139 self
.finish(key
.keydata
)
142 class SSHKeysUploadHandler(auth
.CacheMixin
, base
.BaseHandler
):
143 @tornado.web
.authenticated
145 account
= self
.backend
.accounts
.get_by_uid(uid
)
147 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
149 # Check for permissions
150 if not account
.can_be_managed_by(self
.current_user
):
151 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
153 self
.render("people/ssh-keys/upload.html", account
=account
)
155 @tornado.web
.authenticated
157 account
= self
.backend
.accounts
.get_by_uid(uid
)
159 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
161 # Check for permissions
162 if not account
.can_be_managed_by(self
.current_user
):
163 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
165 key
= self
.get_argument("key")
168 password
= self
.get_argument("password")
169 if not account
.check_password(password
):
170 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
172 # Try to add new SSH key
174 account
.add_ssh_key(key
)
176 except sshpubkeys
.InvalidKeyException
as e
:
177 self
.render("people/ssh-keys/error-invalid-key.html", account
=account
, exception
=e
)
180 self
.redirect("/users/%s/ssh-keys" % account
.uid
)
183 class SSHKeysDeleteHandler(auth
.CacheMixin
, base
.BaseHandler
):
184 @tornado.web
.authenticated
185 def get(self
, uid
, hash_sha256
):
186 account
= self
.backend
.accounts
.get_by_uid(uid
)
188 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
191 key
= account
.get_ssh_key_by_hash_sha256(hash_sha256
)
193 raise tornado
.web
.HTTPError(404, "Could not find key: %s" % hash_sha256
)
195 self
.render("people/ssh-keys/delete.html", account
=account
, key
=key
)
197 @tornado.web
.authenticated
198 def post(self
, uid
, hash_sha256
):
199 account
= self
.backend
.accounts
.get_by_uid(uid
)
201 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
204 key
= account
.get_ssh_key_by_hash_sha256(hash_sha256
)
206 raise tornado
.web
.HTTPError(404, "Could not find key: %s" % hash_sha256
)
209 password
= self
.get_argument("password")
210 if not account
.check_password(password
):
211 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
214 account
.delete_ssh_key(key
.keydata
)
216 self
.redirect("/users/%s/ssh-keys" % account
.uid
)
219 class SIPHandler(auth
.CacheMixin
, base
.BaseHandler
):
220 @tornado.web
.authenticated
222 account
= self
.backend
.accounts
.get_by_uid(uid
)
224 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
226 # Check for permissions
227 if not account
.can_be_managed_by(self
.current_user
):
228 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
230 self
.render("people/sip.html", account
=account
)
233 class UsersHandler(auth
.CacheMixin
, base
.BaseHandler
):
234 @tornado.web
.authenticated
236 # Only staff can see other users
237 if not self
.current_user
.is_staff():
238 raise tornado
.web
.HTTPError(403)
240 self
.render("people/users.html")
243 class UserHandler(auth
.CacheMixin
, base
.BaseHandler
):
244 @tornado.web
.authenticated
246 account
= self
.backend
.accounts
.get_by_uid(uid
)
248 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
250 self
.render("people/user.html", account
=account
)
253 class UserEditHandler(auth
.CacheMixin
, base
.BaseHandler
):
254 @tornado.web
.authenticated
256 account
= self
.backend
.accounts
.get_by_uid(uid
)
258 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
260 # Check for permissions
261 if not account
.can_be_managed_by(self
.current_user
):
262 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
264 self
.render("people/user-edit.html", account
=account
, countries
=countries
.get_all())
266 @tornado.web
.authenticated
268 account
= self
.backend
.accounts
.get_by_uid(uid
)
270 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
272 # Check for permissions
273 if not account
.can_be_managed_by(self
.current_user
):
274 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
276 # Unfortunately this cannot be wrapped into a transaction
278 account
.first_name
= self
.get_argument("first_name")
279 account
.last_name
= self
.get_argument("last_name")
280 account
.street
= self
.get_argument("street", None)
281 account
.city
= self
.get_argument("city", None)
282 account
.postal_code
= self
.get_argument("postal_code", None)
283 account
.country_code
= self
.get_argument("country_code", None)
287 filename
, data
, mimetype
= self
.get_file("avatar")
289 if not mimetype
.startswith("image/"):
290 raise tornado
.web
.HTTPError(400, "Avatar is not an image file: %s" % mimetype
)
292 account
.upload_avatar(data
)
297 account
.mail_routing_address
= self
.get_argument("mail_routing_address", None)
300 account
.phone_numbers
= self
.get_argument("phone_numbers", "").splitlines()
301 account
.sip_routing_address
= self
.get_argument("sip_routing_address", None)
302 except ldap
.STRONG_AUTH_REQUIRED
as e
:
303 raise tornado
.web
.HTTPError(403, "%s" % e
) from e
305 # Redirect back to user page
306 self
.redirect("/users/%s" % account
.uid
)
309 class UserPasswdHandler(auth
.CacheMixin
, base
.BaseHandler
):
310 @tornado.web
.authenticated
312 account
= self
.backend
.accounts
.get_by_uid(uid
)
314 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
316 # Check for permissions
317 if not account
.can_be_managed_by(self
.current_user
):
318 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
320 self
.render("people/passwd.html", account
=account
)
322 @tornado.web
.authenticated
324 account
= self
.backend
.accounts
.get_by_uid(uid
)
326 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
328 # Check for permissions
329 if not account
.can_be_managed_by(self
.current_user
):
330 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
332 # Get current password
333 password
= self
.get_argument("password")
336 password1
= self
.get_argument("password1")
337 password2
= self
.get_argument("password2")
339 # Passwords must match
340 if not password1
== password2
:
341 raise tornado
.web
.HTTPError(400, "Passwords do not match")
343 # XXX Check password complexity
345 # Check if old password matches
346 if not account
.check_password(password
):
347 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
350 account
.passwd(password1
)
352 # Redirect back to user's page
353 self
.redirect("/users/%s" % account
.uid
)
356 class AccountsListModule(ui_modules
.UIModule
):
357 def render(self
, accounts
=None):
359 accounts
= self
.backend
.accounts
361 return self
.render_string("people/modules/accounts-list.html", accounts
=accounts
)
364 class CDRModule(ui_modules
.UIModule
):
365 def render(self
, account
, date
=None, limit
=None):
366 cdr
= account
.get_cdr(date
=date
, limit
=limit
)
368 return self
.render_string("people/modules/cdr.html",
369 account
=account
, cdr
=list(cdr
))
372 class ChannelsModule(ui_modules
.UIModule
):
373 def render(self
, account
):
374 return self
.render_string("people/modules/channels.html",
375 account
=account
, channels
=account
.sip_channels
)
378 class MOSModule(ui_modules
.UIModule
):
379 def render(self
, call
):
380 return self
.render_string("people/modules/mos.html", call
=call
)
383 class PasswordModule(ui_modules
.UIModule
):
384 def render(self
, account
):
385 return self
.render_string("people/modules/password.html", account
=account
)
387 def javascript_files(self
):
388 return "js/zxcvbn.js"
390 def embedded_javascript(self
):
391 return self
.render_string("people/modules/password.js")
394 class RegistrationsModule(ui_modules
.UIModule
):
395 def render(self
, account
):
396 return self
.render_string("people/modules/registrations.html", account
=account
)
399 class SIPStatusModule(ui_modules
.UIModule
):
400 def render(self
, account
):
401 return self
.render_string("people/modules/sip-status.html", account
=account
)