]> git.ipfire.org Git - ipfire.org.git/blob - src/web/people.py
people: Rename registrations page
[ipfire.org.git] / src / web / people.py
1 #!/usr/bin/python
2
3 import datetime
4 import ldap
5 import logging
6 import tornado.web
7
8 from . import handlers_base as base
9 from . import ui_modules
10
11 class IndexHandler(base.BaseHandler):
12 @tornado.web.authenticated
13 def get(self):
14 self.render("people/index.html")
15
16
17 class AvatarHandler(base.BaseHandler):
18 always_cache = True
19
20 def get(self, uid):
21 # Get the desired size of the avatar file
22 size = self.get_argument("size", 0)
23
24 try:
25 size = int(size)
26 except (TypeError, ValueError):
27 size = None
28
29 logging.debug("Querying for avatar of %s" % uid)
30
31 # Fetch user account
32 account = self.backend.accounts.get_by_uid(uid)
33 if not account:
34 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
35
36 # Allow downstream to cache this for 60 minutes
37 self.set_expires(3600)
38
39 # Resize avatar
40 avatar = account.get_avatar(size)
41
42 # If there is no avatar, we serve a default image
43 if not avatar:
44 logging.debug("No avatar uploaded for %s" % account)
45
46 return self.redirect(self.static_url("img/default-avatar.jpg"))
47
48 # Set headers about content
49 self.set_header("Content-Disposition", "inline; filename=\"%s.jpg\"" % account.uid)
50 self.set_header("Content-Type", "image/jpeg")
51
52 # Deliver payload
53 self.finish(avatar)
54
55
56 class CallsHandler(base.BaseHandler):
57 @tornado.web.authenticated
58 def get(self, uid, date=None):
59 account = self.backend.accounts.get_by_uid(uid)
60 if not account:
61 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
62
63 if date:
64 try:
65 date = datetime.datetime.strptime(date, "%Y-%m-%d").date()
66 except ValueError:
67 raise tornado.web.HTTPError(400, "Invalid date: %s" % date)
68 else:
69 date = datetime.date.today()
70
71 self.render("people/calls.html", account=account, date=date)
72
73
74 class CallHandler(base.BaseHandler):
75 @tornado.web.authenticated
76 def get(self, uid, uuid):
77 call = self.backend.talk.freeswitch.get_call_by_uuid(uuid)
78 if not call:
79 raise tornado.web.HTTPError(404, "Could not find call %s" % uuid)
80
81 # XXX limit
82
83 self.render("people/call.html", call=call)
84
85
86 class SearchHandler(base.BaseHandler):
87 @tornado.web.authenticated
88 def get(self):
89 q = self.get_argument("q")
90
91 # Perform the search
92 accounts = self.backend.accounts.search(q)
93
94 # Redirect when only one result was found
95 if len(accounts) == 1:
96 self.redirect("/users/%s" % accounts[0].uid)
97 return
98
99 self.render("people/search.html", q=q, accounts=accounts)
100
101
102 class SIPHandler(base.BaseHandler):
103 @tornado.web.authenticated
104 def get(self, uid):
105 account = self.backend.accounts.get_by_uid(uid)
106 if not account:
107 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
108
109 # Check for permissions
110 if not account.can_be_managed_by(self.current_user):
111 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
112
113 self.render("people/sip.html", account=account)
114
115
116 class UsersHandler(base.BaseHandler):
117 @tornado.web.authenticated
118 def get(self):
119 self.render("people/users.html")
120
121
122 class UserHandler(base.BaseHandler):
123 @tornado.web.authenticated
124 def get(self, uid):
125 account = self.backend.accounts.get_by_uid(uid)
126 if not account:
127 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
128
129 self.render("people/user.html", account=account)
130
131
132 class UserEditHandler(base.BaseHandler):
133 @tornado.web.authenticated
134 def get(self, uid):
135 account = self.backend.accounts.get_by_uid(uid)
136 if not account:
137 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
138
139 # Check for permissions
140 if not account.can_be_managed_by(self.current_user):
141 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
142
143 self.render("people/user-edit.html", account=account)
144
145 @tornado.web.authenticated
146 def post(self, uid):
147 account = self.backend.accounts.get_by_uid(uid)
148 if not account:
149 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
150
151 # Check for permissions
152 if not account.can_be_managed_by(self.current_user):
153 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
154
155 # Unfortunately this cannot be wrapped into a transaction
156 try:
157 account.first_name = self.get_argument("first_name")
158 account.last_name = self.get_argument("last_name")
159 account.address = self.get_argument("address")
160
161 # Avatar
162 try:
163 filename, data, mimetype = self.get_file("avatar")
164
165 if not mimetype.startswith("image/"):
166 raise tornado.web.HTTPError(400, "Avatar is not an image file: %s" % mimetype)
167
168 account.upload_avatar(data)
169 except TypeError:
170 pass
171
172 # Email
173 account.mail_routing_address = self.get_argument("mail_routing_address", None)
174
175 # Telephone
176 account.phone_numbers = self.get_argument("phone_numbers", "").splitlines()
177 account.sip_routing_address = self.get_argument("sip_routing_address", None)
178 except ldap.STRONG_AUTH_REQUIRED as e:
179 raise tornado.web.HTTPError(403, "%s" % e) from e
180
181 # Redirect back to user page
182 self.redirect("/users/%s" % account.uid)
183
184
185 class UserPasswdHandler(base.BaseHandler):
186 @tornado.web.authenticated
187 def get(self, uid):
188 account = self.backend.accounts.get_by_uid(uid)
189 if not account:
190 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
191
192 # Check for permissions
193 if not account.can_be_managed_by(self.current_user):
194 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
195
196 self.render("people/passwd.html", account=account)
197
198 @tornado.web.authenticated
199 def post(self, uid):
200 account = self.backend.accounts.get_by_uid(uid)
201 if not account:
202 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
203
204 # Check for permissions
205 if not account.can_be_managed_by(self.current_user):
206 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
207
208 # Get current password
209 password = self.get_argument("password")
210
211 # Get new password
212 password1 = self.get_argument("password1")
213 password2 = self.get_argument("password2")
214
215 # Passwords must match
216 if not password1 == password2:
217 raise tornado.web.HTTPError(400, "Passwords do not match")
218
219 # XXX Check password complexity
220
221 # Check if old password matches
222 if not account.check_password(password):
223 raise tornado.web.HTTPError(403, "Incorrect password for %s" % account)
224
225 # Save new password
226 account.passwd(password1)
227
228 # Redirect back to user's page
229 self.redirect("/users/%s" % account.uid)
230
231
232 class AccountsListModule(ui_modules.UIModule):
233 def render(self, accounts=None):
234 if accounts is None:
235 accounts = self.backend.accounts
236
237 return self.render_string("people/modules/accounts-list.html", accounts=accounts)
238
239
240 class CDRModule(ui_modules.UIModule):
241 def render(self, account, date=None, limit=None):
242 cdr = account.get_cdr(date=date, limit=limit)
243
244 return self.render_string("people/modules/cdr.html", account=account, cdr=cdr)
245
246
247 class ChannelsModule(ui_modules.UIModule):
248 def render(self, account):
249 return self.render_string("people/modules/channels.html",
250 account=account, channels=account.sip_channels)
251
252
253 class MOSModule(ui_modules.UIModule):
254 def render(self, call):
255 return self.render_string("people/modules/mos.html", call=call)
256
257
258 class RegistrationsModule(ui_modules.UIModule):
259 def render(self, account):
260 return self.render_string("people/modules/registrations.html", account=account)
261
262
263 class SIPStatusModule(ui_modules.UIModule):
264 def render(self, account):
265 return self.render_string("people/modules/sip-status.html", account=account)