]>
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 SearchHandler(base
.BaseHandler
):
92 @tornado.web
.authenticated
94 q
= self
.get_argument("q")
97 accounts
= self
.backend
.accounts
.search(q
)
99 # Redirect when only one result was found
100 if len(accounts
) == 1:
101 self
.redirect("/users/%s" % accounts
[0].uid
)
104 self
.render("people/search.html", q
=q
, accounts
=accounts
)
107 class SSHKeysIndexHandler(base
.BaseHandler
):
108 @tornado.web
.authenticated
110 account
= self
.backend
.accounts
.get_by_uid(uid
)
112 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
114 self
.render("people/ssh-keys/index.html", account
=account
)
117 class SSHKeysDownloadHandler(base
.BaseHandler
):
118 @tornado.web
.authenticated
119 def get(self
, uid
, hash_sha256
):
120 account
= self
.backend
.accounts
.get_by_uid(uid
)
122 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
125 key
= account
.get_ssh_key_by_hash_sha256(hash_sha256
)
127 raise tornado
.web
.HTTPError(404, "Could not find key: %s" % hash_sha256
)
130 self
.add_header("Content-Type", "text/plain")
132 self
.finish(key
.keydata
)
135 class SSHKeysUploadHandler(base
.BaseHandler
):
136 @tornado.web
.authenticated
138 account
= self
.backend
.accounts
.get_by_uid(uid
)
140 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
142 # Check for permissions
143 if not account
.can_be_managed_by(self
.current_user
):
144 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
146 self
.render("people/ssh-keys/upload.html", account
=account
)
148 @tornado.web
.authenticated
150 account
= self
.backend
.accounts
.get_by_uid(uid
)
152 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
154 # Check for permissions
155 if not account
.can_be_managed_by(self
.current_user
):
156 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
158 key
= self
.get_argument("key")
161 password
= self
.get_argument("password")
162 if not account
.check_password(password
):
163 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
165 # Try to add new SSH key
167 account
.add_ssh_key(key
)
169 except sshpubkeys
.InvalidKeyException
as e
:
170 self
.render("people/ssh-keys/error-invalid-key.html", account
=account
, exception
=e
)
173 self
.redirect("/users/%s/ssh-keys" % account
.uid
)
176 class SSHKeysDeleteHandler(base
.BaseHandler
):
177 @tornado.web
.authenticated
178 def get(self
, uid
, hash_sha256
):
179 account
= self
.backend
.accounts
.get_by_uid(uid
)
181 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
184 key
= account
.get_ssh_key_by_hash_sha256(hash_sha256
)
186 raise tornado
.web
.HTTPError(404, "Could not find key: %s" % hash_sha256
)
188 self
.render("people/ssh-keys/delete.html", account
=account
, key
=key
)
190 @tornado.web
.authenticated
191 def post(self
, uid
, hash_sha256
):
192 account
= self
.backend
.accounts
.get_by_uid(uid
)
194 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
197 key
= account
.get_ssh_key_by_hash_sha256(hash_sha256
)
199 raise tornado
.web
.HTTPError(404, "Could not find key: %s" % hash_sha256
)
202 password
= self
.get_argument("password")
203 if not account
.check_password(password
):
204 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
207 account
.delete_ssh_key(key
.keydata
)
209 self
.redirect("/users/%s/ssh-keys" % account
.uid
)
212 class SIPHandler(base
.BaseHandler
):
213 @tornado.web
.authenticated
215 account
= self
.backend
.accounts
.get_by_uid(uid
)
217 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
219 # Check for permissions
220 if not account
.can_be_managed_by(self
.current_user
):
221 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
223 self
.render("people/sip.html", account
=account
)
226 class UsersHandler(base
.BaseHandler
):
227 @tornado.web
.authenticated
229 self
.render("people/users.html")
232 class UserHandler(base
.BaseHandler
):
233 @tornado.web
.authenticated
235 account
= self
.backend
.accounts
.get_by_uid(uid
)
237 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
239 self
.render("people/user.html", account
=account
)
242 class UserEditHandler(base
.BaseHandler
):
243 @tornado.web
.authenticated
245 account
= self
.backend
.accounts
.get_by_uid(uid
)
247 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
249 # Check for permissions
250 if not account
.can_be_managed_by(self
.current_user
):
251 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
253 self
.render("people/user-edit.html", account
=account
)
255 @tornado.web
.authenticated
257 account
= self
.backend
.accounts
.get_by_uid(uid
)
259 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
261 # Check for permissions
262 if not account
.can_be_managed_by(self
.current_user
):
263 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
265 # Unfortunately this cannot be wrapped into a transaction
267 account
.first_name
= self
.get_argument("first_name")
268 account
.last_name
= self
.get_argument("last_name")
269 account
.address
= self
.get_argument("address")
273 filename
, data
, mimetype
= self
.get_file("avatar")
275 if not mimetype
.startswith("image/"):
276 raise tornado
.web
.HTTPError(400, "Avatar is not an image file: %s" % mimetype
)
278 account
.upload_avatar(data
)
283 account
.mail_routing_address
= self
.get_argument("mail_routing_address", None)
286 account
.phone_numbers
= self
.get_argument("phone_numbers", "").splitlines()
287 account
.sip_routing_address
= self
.get_argument("sip_routing_address", None)
288 except ldap
.STRONG_AUTH_REQUIRED
as e
:
289 raise tornado
.web
.HTTPError(403, "%s" % e
) from e
291 # Redirect back to user page
292 self
.redirect("/users/%s" % account
.uid
)
295 class UserPasswdHandler(base
.BaseHandler
):
296 @tornado.web
.authenticated
298 account
= self
.backend
.accounts
.get_by_uid(uid
)
300 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
302 # Check for permissions
303 if not account
.can_be_managed_by(self
.current_user
):
304 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
306 self
.render("people/passwd.html", account
=account
)
308 @tornado.web
.authenticated
310 account
= self
.backend
.accounts
.get_by_uid(uid
)
312 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
314 # Check for permissions
315 if not account
.can_be_managed_by(self
.current_user
):
316 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
318 # Get current password
319 password
= self
.get_argument("password")
322 password1
= self
.get_argument("password1")
323 password2
= self
.get_argument("password2")
325 # Passwords must match
326 if not password1
== password2
:
327 raise tornado
.web
.HTTPError(400, "Passwords do not match")
329 # XXX Check password complexity
331 # Check if old password matches
332 if not account
.check_password(password
):
333 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
336 account
.passwd(password1
)
338 # Redirect back to user's page
339 self
.redirect("/users/%s" % account
.uid
)
342 class AccountsListModule(ui_modules
.UIModule
):
343 def render(self
, accounts
=None):
345 accounts
= self
.backend
.accounts
347 return self
.render_string("people/modules/accounts-list.html", accounts
=accounts
)
350 class CDRModule(ui_modules
.UIModule
):
351 def render(self
, account
, date
=None, limit
=None):
352 cdr
= account
.get_cdr(date
=date
, limit
=limit
)
354 return self
.render_string("people/modules/cdr.html", account
=account
, cdr
=cdr
)
357 class ChannelsModule(ui_modules
.UIModule
):
358 def render(self
, account
):
359 return self
.render_string("people/modules/channels.html",
360 account
=account
, channels
=account
.sip_channels
)
363 class MOSModule(ui_modules
.UIModule
):
364 def render(self
, call
):
365 return self
.render_string("people/modules/mos.html", call
=call
)
368 class RegistrationsModule(ui_modules
.UIModule
):
369 def render(self
, account
):
370 return self
.render_string("people/modules/registrations.html", account
=account
)
373 class SIPStatusModule(ui_modules
.UIModule
):
374 def render(self
, account
):
375 return self
.render_string("people/modules/sip-status.html", account
=account
)