]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/people.py
2d265cfa3f6dc8aa6497543f976e80c760dddae0
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 SearchHandler(auth
.CacheMixin
, base
.BaseHandler
):
127 @tornado.web
.authenticated
129 q
= self
.get_argument("q")
132 accounts
= self
.backend
.accounts
.search(q
)
134 # Redirect when only one result was found
135 if len(accounts
) == 1:
136 self
.redirect("/users/%s" % accounts
[0].uid
)
139 self
.render("people/search.html", q
=q
, accounts
=accounts
)
142 class SIPHandler(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/sip.html", account
=account
)
156 class UsersHandler(auth
.CacheMixin
, base
.BaseHandler
):
157 @tornado.web
.authenticated
159 # Only staff can see other users
160 if not self
.current_user
.is_staff():
161 raise tornado
.web
.HTTPError(403)
163 self
.render("people/users.html")
166 class UserHandler(auth
.CacheMixin
, base
.BaseHandler
):
167 @tornado.web
.authenticated
169 account
= self
.backend
.accounts
.get_by_uid(uid
)
171 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
173 self
.render("people/user.html", account
=account
)
176 class UserEditHandler(auth
.CacheMixin
, base
.BaseHandler
):
177 @tornado.web
.authenticated
179 account
= self
.backend
.accounts
.get_by_uid(uid
)
181 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
183 # Check for permissions
184 if not account
.can_be_managed_by(self
.current_user
):
185 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
187 self
.render("people/user-edit.html", account
=account
, countries
=countries
.get_all())
189 @tornado.web
.authenticated
191 account
= self
.backend
.accounts
.get_by_uid(uid
)
193 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
195 # Check for permissions
196 if not account
.can_be_managed_by(self
.current_user
):
197 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
199 # Unfortunately this cannot be wrapped into a transaction
201 account
.first_name
= self
.get_argument("first_name")
202 account
.last_name
= self
.get_argument("last_name")
203 account
.nickname
= self
.get_argument("nickname", None)
204 account
.street
= self
.get_argument("street", None)
205 account
.city
= self
.get_argument("city", None)
206 account
.postal_code
= self
.get_argument("postal_code", None)
207 account
.country_code
= self
.get_argument("country_code", None)
211 filename
, data
, mimetype
= self
.get_file("avatar")
213 if not mimetype
.startswith("image/"):
214 raise tornado
.web
.HTTPError(400, "Avatar is not an image file: %s" % mimetype
)
216 account
.upload_avatar(data
)
221 account
.mail_routing_address
= self
.get_argument("mail_routing_address", None)
224 account
.phone_numbers
= self
.get_argument("phone_numbers", "").splitlines()
225 account
.sip_routing_address
= self
.get_argument("sip_routing_address", None)
226 except ldap
.STRONG_AUTH_REQUIRED
as e
:
227 raise tornado
.web
.HTTPError(403, "%s" % e
) from e
229 # Redirect back to user page
230 self
.redirect("/users/%s" % account
.uid
)
233 class UserPasswdHandler(auth
.CacheMixin
, base
.BaseHandler
):
234 @tornado.web
.authenticated
236 account
= self
.backend
.accounts
.get_by_uid(uid
)
238 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
240 # Check for permissions
241 if not account
.can_be_managed_by(self
.current_user
):
242 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
244 self
.render("people/passwd.html", account
=account
)
246 @tornado.web
.authenticated
248 account
= self
.backend
.accounts
.get_by_uid(uid
)
250 raise tornado
.web
.HTTPError(404, "Could not find account %s" % uid
)
252 # Check for permissions
253 if not account
.can_be_managed_by(self
.current_user
):
254 raise tornado
.web
.HTTPError(403, "%s cannot manage %s" % (self
.current_user
, account
))
256 # Get current password
257 password
= self
.get_argument("password")
260 password1
= self
.get_argument("password1")
261 password2
= self
.get_argument("password2")
263 # Passwords must match
264 if not password1
== password2
:
265 raise tornado
.web
.HTTPError(400, "Passwords do not match")
267 # XXX Check password complexity
269 # Check if old password matches
270 if not account
.check_password(password
):
271 raise tornado
.web
.HTTPError(403, "Incorrect password for %s" % account
)
274 account
.passwd(password1
)
276 # Redirect back to user's page
277 self
.redirect("/users/%s" % account
.uid
)
280 class SSODiscourse(auth
.CacheMixin
, base
.BaseHandler
):
281 def _get_discourse_params(self
):
282 # Fetch Discourse's parameters
283 sso
= self
.get_argument("sso")
284 sig
= self
.get_argument("sig")
288 return self
.accounts
.decode_discourse_payload(sso
, sig
)
290 # Raise bad request if the signature is invalid
292 raise tornado
.web
.HTTPError(400)
294 def _redirect_user_to_discourse(self
, account
, nonce
, return_sso_url
):
296 Redirects the user back to Discourse passing some
297 attributes of the user account to Discourse
301 "external_id" : account
.uid
,
304 "email" : account
.email
,
305 "require_activation" : "false",
307 # More details about the user
308 "username" : account
.uid
,
309 "name" : "%s" % account
,
312 "avatar_url" : account
.avatar_url(),
313 "avatar_force_update" : "true",
315 # Send a welcome message
316 "suppress_welcome_message" : "false",
319 "admin" : "true" if account
.is_admin() else "false",
320 "moderator" : "true" if account
.is_moderator() else "false",
323 # Format payload and sign it
324 payload
= self
.accounts
.encode_discourse_payload(**args
)
325 signature
= self
.accounts
.sign_discourse_payload(payload
)
327 qs
= urllib
.parse
.urlencode({
333 self
.redirect("%s?%s" % (return_sso_url
, qs
))
335 @base.ratelimit(minutes
=24*60, requests
=100)
337 params
= self
._get
_discourse
_params
()
339 # Redirect back if user is already logged in
340 if self
.current_user
:
341 return self
._redirect
_user
_to
_discourse
(self
.current_user
, **params
)
343 # Otherwise the user needs to authenticate
344 self
.render("auth/login.html", next
=None)
346 @base.ratelimit(minutes
=24*60, requests
=100)
348 params
= self
._get
_discourse
_params
()
351 username
= self
.get_argument("username")
352 password
= self
.get_argument("password")
355 account
= self
.accounts
.auth(username
, password
)
357 raise tornado
.web
.HTTPError(401, "Unknown user or invalid password: %s" % username
)
359 # If the user has been authenticated, we will redirect to Discourse
360 self
._redirect
_user
_to
_discourse
(account
, **params
)
363 class NewAccountsModule(ui_modules
.UIModule
):
364 def render(self
, days
=14):
365 t
= datetime
.datetime
.utcnow() - datetime
.timedelta(days
=days
)
367 # Fetch all accounts created after t
368 accounts
= self
.backend
.accounts
.get_created_after(t
)
370 accounts
.sort(key
=lambda a
: a
.created_at
, reverse
=True)
372 return self
.render_string("people/modules/accounts-new.html",
373 accounts
=accounts
, t
=t
)
376 class AccountsListModule(ui_modules
.UIModule
):
377 def render(self
, accounts
=None):
379 accounts
= self
.backend
.accounts
381 return self
.render_string("people/modules/accounts-list.html", accounts
=accounts
)
384 class CDRModule(ui_modules
.UIModule
):
385 def render(self
, account
, date
=None, limit
=None):
386 cdr
= account
.get_cdr(date
=date
, limit
=limit
)
388 return self
.render_string("people/modules/cdr.html",
389 account
=account
, cdr
=list(cdr
))
392 class ChannelsModule(ui_modules
.UIModule
):
393 def render(self
, account
):
394 return self
.render_string("people/modules/channels.html",
395 account
=account
, channels
=account
.sip_channels
)
398 class MOSModule(ui_modules
.UIModule
):
399 def render(self
, call
):
400 return self
.render_string("people/modules/mos.html", call
=call
)
403 class PasswordModule(ui_modules
.UIModule
):
404 def render(self
, account
=None):
405 return self
.render_string("people/modules/password.html", account
=account
)
407 def javascript_files(self
):
408 return "js/zxcvbn.js"
410 def embedded_javascript(self
):
411 return self
.render_string("people/modules/password.js")
414 class RegistrationsModule(ui_modules
.UIModule
):
415 def render(self
, account
):
416 return self
.render_string("people/modules/registrations.html", account
=account
)
419 class SIPStatusModule(ui_modules
.UIModule
):
420 def render(self
, account
):
421 return self
.render_string("people/modules/sip-status.html", account
=account
)