]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/people.py
fe167763e7eba8a3bb95a6eaf4c7a4edbfc544fe
10 from .. import countries
14 from . import ui_modules
16 class IndexHandler(auth
.CacheMixin
, base
.BaseHandler
):
17 @tornado.web
.authenticated
21 # Suggest uploading an avatar if this user does not have one
22 if not self
.current_user
.has_avatar():
23 hints
.append("avatar")
25 self
.render("people/index.html", hints
=hints
)
28 class AvatarHandler(base
.BaseHandler
):
30 # Get the desired size of the avatar file
31 size
= self
.get_argument("size", None)
35 except (TypeError, ValueError):
38 logging
.debug("Querying for avatar of %s" % uid
)
41 account
= self
.backend
.accounts
.get_by_uid(uid
)
43 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
45 # Allow downstream to cache this for 60 minutes
46 self
.set_expires(3600)
49 avatar
= account
.get_avatar(size
)
51 # If there is no avatar, we serve a default image
53 logging
.debug("No avatar uploaded for %s" % account
)
55 return self
.redirect(self
.static_url("img/default-avatar.jpg"))
58 type = imghdr
.what(None, avatar
)
60 # Set headers about content
61 self
.set_header("Content-Disposition", "inline; filename=\"%s.%s\"" % (account
.uid
, type))
62 self
.set_header("Content-Type", "image/%s" % type)
68 class CallsHandler(auth
.CacheMixin
, base
.BaseHandler
):
69 @tornado.web
.authenticated
70 def get(self
, uid
, date
=None):
71 account
= self
.backend
.accounts
.get_by_uid(uid
)
73 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
75 # Check for permissions
76 if not account
.can_be_managed_by(self
.current_user
):
77 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
81 date
= datetime
.datetime
.strptime(date
, "%Y-%m-%d").date()
83 raise tornado
.web
.HTTPError(400, "Invalid date: %s" % date
)
85 date
= datetime
.date
.today()
87 self
.render("people/calls.html", account
=account
, date
=date
)
90 class CallHandler(auth
.CacheMixin
, base
.BaseHandler
):
91 @tornado.web
.authenticated
92 def get(self
, uid
, uuid
):
93 account
= self
.backend
.accounts
.get_by_uid(uid
)
95 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
97 # Check for permissions
98 if not account
.can_be_managed_by(self
.current_user
):
99 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
101 call
= self
.backend
.talk
.freeswitch
.get_call_by_uuid(uuid
)
103 raise tornado
.web
.HTTPError(404, "Could not find call %s" % uuid
)
107 self
.render("people/call.html", account
=account
, call
=call
)
110 class ConferencesHandler(auth
.CacheMixin
, base
.BaseHandler
):
111 @tornado.web
.authenticated
113 self
.render("people/conferences.html", conferences
=self
.backend
.talk
.conferences
)
116 class GroupsHandler(auth
.CacheMixin
, base
.BaseHandler
):
117 @tornado.web
.authenticated
119 # Only staff can see other groups
120 if not self
.current_user
.is_staff():
121 raise tornado
.web
.HTTPError(403)
123 self
.render("people/groups.html")
126 class GroupHandler(auth
.CacheMixin
, base
.BaseHandler
):
127 @tornado.web
.authenticated
129 # Only staff can see other groups
130 if not self
.current_user
.is_staff():
131 raise tornado
.web
.HTTPError(403)
134 group
= self
.backend
.groups
.get_by_gid(gid
)
136 raise tornado
.web
.HTTPError(404, "Could not find group %s" % gid
)
138 self
.render("people/group.html", group
=group
)
141 class SearchHandler(auth
.CacheMixin
, base
.BaseHandler
):
142 @tornado.web
.authenticated
144 q
= self
.get_argument("q")
147 accounts
= self
.backend
.accounts
.search(q
)
149 # Redirect when only one result was found
150 if len(accounts
) == 1:
151 self
.redirect("/users/%s" % accounts
[0].uid
)
154 self
.render("people/search.html", q
=q
, accounts
=accounts
)
157 class SIPHandler(auth
.CacheMixin
, base
.BaseHandler
):
158 @tornado.web
.authenticated
160 account
= self
.backend
.accounts
.get_by_uid(uid
)
162 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
164 # Check for permissions
165 if not account
.can_be_managed_by(self
.current_user
):
166 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
168 self
.render("people/sip.html", account
=account
)
171 class UsersHandler(auth
.CacheMixin
, base
.BaseHandler
):
172 @tornado.web
.authenticated
174 # Only staff can see other users
175 if not self
.current_user
.is_staff():
176 raise tornado
.web
.HTTPError(403)
178 self
.render("people/users.html")
181 class UserHandler(auth
.CacheMixin
, base
.BaseHandler
):
182 @tornado.web
.authenticated
184 account
= self
.backend
.accounts
.get_by_uid(uid
)
186 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
188 self
.render("people/user.html", account
=account
)
191 class UserEditHandler(auth
.CacheMixin
, base
.BaseHandler
):
192 @tornado.web
.authenticated
194 account
= self
.backend
.accounts
.get_by_uid(uid
)
196 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
198 # Check for permissions
199 if not account
.can_be_managed_by(self
.current_user
):
200 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
202 self
.render("people/user-edit.html", account
=account
, countries
=countries
.get_all())
204 @tornado.web
.authenticated
206 account
= self
.backend
.accounts
.get_by_uid(uid
)
208 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
210 # Check for permissions
211 if not account
.can_be_managed_by(self
.current_user
):
212 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
214 # Unfortunately this cannot be wrapped into a transaction
216 account
.first_name
= self
.get_argument("first_name")
217 account
.last_name
= self
.get_argument("last_name")
218 account
.nickname
= self
.get_argument("nickname", None)
219 account
.street
= self
.get_argument("street", None)
220 account
.city
= self
.get_argument("city", None)
221 account
.postal_code
= self
.get_argument("postal_code", None)
222 account
.country_code
= self
.get_argument("country_code", None)
226 filename
, data
, mimetype
= self
.get_file("avatar")
228 if not mimetype
.startswith("image/"):
229 raise tornado
.web
.HTTPError(400, "Avatar is not an image file: %s" % mimetype
)
231 account
.upload_avatar(data
)
236 account
.mail_routing_address
= self
.get_argument("mail_routing_address", None)
239 account
.phone_numbers
= self
.get_argument("phone_numbers", "").splitlines()
240 account
.sip_routing_address
= self
.get_argument("sip_routing_address", None)
241 except ldap
.STRONG_AUTH_REQUIRED
as e
:
242 raise tornado
.web
.HTTPError(403, "%s" % e
) from e
244 # Redirect back to user page
245 self
.redirect("/users/%s" % account
.uid
)
248 class UserPasswdHandler(auth
.CacheMixin
, 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/passwd.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 # Get current password
272 password
= self
.get_argument("password")
275 password1
= self
.get_argument("password1")
276 password2
= self
.get_argument("password2")
278 # Passwords must match
279 if not password1
== password2
:
280 raise tornado
.web
.HTTPError(400, "Passwords do not match")
282 # XXX Check password complexity
284 # Check if old password matches
285 if not account
.check_password(password
):
286 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
289 account
.passwd(password1
)
291 # Redirect back to user's page
292 self
.redirect("/users/%s" % account
.uid
)
295 class SSODiscourse(auth
.CacheMixin
, base
.BaseHandler
):
296 def _get_discourse_params(self
):
297 # Fetch Discourse's parameters
298 sso
= self
.get_argument("sso")
299 sig
= self
.get_argument("sig")
303 return self
.accounts
.decode_discourse_payload(sso
, sig
)
305 # Raise bad request if the signature is invalid
307 raise tornado
.web
.HTTPError(400)
309 def _redirect_user_to_discourse(self
, account
, nonce
, return_sso_url
):
311 Redirects the user back to Discourse passing some
312 attributes of the user account to Discourse
316 "external_id" : account
.uid
,
319 "email" : account
.email
,
320 "require_activation" : "false",
322 # More details about the user
323 "username" : account
.uid
,
324 "name" : "%s" % account
,
327 "avatar_url" : account
.avatar_url(),
328 "avatar_force_update" : "true",
330 # Send a welcome message
331 "suppress_welcome_message" : "false",
334 "admin" : "true" if account
.is_admin() else "false",
335 "moderator" : "true" if account
.is_moderator() else "false",
338 # Format payload and sign it
339 payload
= self
.accounts
.encode_discourse_payload(**args
)
340 signature
= self
.accounts
.sign_discourse_payload(payload
)
342 qs
= urllib
.parse
.urlencode({
348 self
.redirect("%s?%s" % (return_sso_url
, qs
))
350 @base.ratelimit(minutes
=24*60, requests
=100)
352 params
= self
._get
_discourse
_params
()
354 # Redirect back if user is already logged in
355 if self
.current_user
:
356 return self
._redirect
_user
_to
_discourse
(self
.current_user
, **params
)
358 # Otherwise the user needs to authenticate
359 self
.render("auth/login.html", next
=None)
361 @base.ratelimit(minutes
=24*60, requests
=100)
363 params
= self
._get
_discourse
_params
()
366 username
= self
.get_argument("username")
367 password
= self
.get_argument("password")
370 account
= self
.accounts
.auth(username
, password
)
372 raise tornado
.web
.HTTPError(401, "Unknown user or invalid password: %s" % username
)
374 # If the user has been authenticated, we will redirect to Discourse
375 self
._redirect
_user
_to
_discourse
(account
, **params
)
378 class NewAccountsModule(ui_modules
.UIModule
):
379 def render(self
, days
=14):
380 t
= datetime
.datetime
.utcnow() - datetime
.timedelta(days
=days
)
382 # Fetch all accounts created after t
383 accounts
= self
.backend
.accounts
.get_created_after(t
)
385 accounts
.sort(key
=lambda a
: a
.created_at
, reverse
=True)
387 return self
.render_string("people/modules/accounts-new.html",
388 accounts
=accounts
, t
=t
)
391 class AccountsListModule(ui_modules
.UIModule
):
392 def render(self
, accounts
=None):
394 accounts
= self
.backend
.accounts
396 return self
.render_string("people/modules/accounts-list.html", accounts
=accounts
)
399 class CDRModule(ui_modules
.UIModule
):
400 def render(self
, account
, date
=None, limit
=None):
401 cdr
= account
.get_cdr(date
=date
, limit
=limit
)
403 return self
.render_string("people/modules/cdr.html",
404 account
=account
, cdr
=list(cdr
))
407 class ChannelsModule(ui_modules
.UIModule
):
408 def render(self
, account
):
409 return self
.render_string("people/modules/channels.html",
410 account
=account
, channels
=account
.sip_channels
)
413 class MOSModule(ui_modules
.UIModule
):
414 def render(self
, call
):
415 return self
.render_string("people/modules/mos.html", call
=call
)
418 class PasswordModule(ui_modules
.UIModule
):
419 def render(self
, account
=None):
420 return self
.render_string("people/modules/password.html", account
=account
)
422 def javascript_files(self
):
423 return "js/zxcvbn.js"
425 def embedded_javascript(self
):
426 return self
.render_string("people/modules/password.js")
429 class RegistrationsModule(ui_modules
.UIModule
):
430 def render(self
, account
):
431 return self
.render_string("people/modules/registrations.html", account
=account
)
434 class SIPStatusModule(ui_modules
.UIModule
):
435 def render(self
, account
):
436 return self
.render_string("people/modules/sip-status.html", account
=account
)