]> git.ipfire.org Git - ipfire.org.git/blob - src/web/people.py
3502704ff341021acfcf810f891799b38bf6ba26
[ipfire.org.git] / src / web / people.py
1 #!/usr/bin/python
2
3 import datetime
4 import ldap
5 import tornado.web
6
7 from .. import countries
8
9 from . import base
10 from . import ui_modules
11
12 class IndexHandler(base.BaseHandler):
13 @tornado.web.authenticated
14 def get(self):
15 self.render("people/index.html")
16
17
18 class CallsHandler(base.BaseHandler):
19 @tornado.web.authenticated
20 def get(self, uid, date=None):
21 account = self.backend.accounts.get_by_uid(uid)
22 if not account:
23 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
24
25 # Check for permissions
26 if not account.can_be_managed_by(self.current_user):
27 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
28
29 if date:
30 try:
31 date = datetime.datetime.strptime(date, "%Y-%m-%d").date()
32 except ValueError:
33 raise tornado.web.HTTPError(400, "Invalid date: %s" % date)
34 else:
35 date = datetime.date.today()
36
37 self.render("people/calls.html", account=account, date=date)
38
39
40 class CallHandler(base.BaseHandler):
41 @tornado.web.authenticated
42 def get(self, uid, uuid):
43 account = self.backend.accounts.get_by_uid(uid)
44 if not account:
45 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
46
47 # Check for permissions
48 if not account.can_be_managed_by(self.current_user):
49 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
50
51 call = self.backend.talk.freeswitch.get_call_by_uuid(uuid)
52 if not call:
53 raise tornado.web.HTTPError(404, "Could not find call %s" % uuid)
54
55 # XXX limit
56
57 self.render("people/call.html", account=account, call=call)
58
59
60 class ConferencesHandler(base.BaseHandler):
61 @tornado.web.authenticated
62 def get(self):
63 self.render("people/conferences.html", conferences=self.backend.talk.conferences)
64
65
66 class SubscribeHandler(base.BaseHandler):
67 @tornado.web.authenticated
68 def post(self):
69 # Give consent
70 self.current_user.consents_to_promotional_emails = True
71
72 self.render("people/subscribed.html")
73
74
75 class UnsubscribeHandler(base.BaseHandler):
76 @tornado.web.authenticated
77 def get(self):
78 if self.current_user.consents_to_promotional_emails:
79 return self.render("people/unsubscribe.html")
80
81 self.render("people/unsubscribed.html")
82
83 @tornado.web.authenticated
84 def post(self):
85 # Withdraw consent
86 self.current_user.consents_to_promotional_emails = False
87
88 self.render("people/unsubscribed.html")
89
90
91 class SIPHandler(base.BaseHandler):
92 @tornado.web.authenticated
93 def get(self, uid):
94 account = self.backend.accounts.get_by_uid(uid)
95 if not account:
96 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
97
98 # Check for permissions
99 if not account.can_be_managed_by(self.current_user):
100 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
101
102 self.render("people/sip.html", account=account)
103
104
105 class UserEditHandler(base.BaseHandler):
106 @tornado.web.authenticated
107 def get(self, uid):
108 account = self.backend.accounts.get_by_uid(uid)
109 if not account:
110 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
111
112 # Check for permissions
113 if not account.can_be_managed_by(self.current_user):
114 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
115
116 self.render("people/user-edit.html", account=account, countries=countries.get_all())
117
118 @tornado.web.authenticated
119 def post(self, uid):
120 account = self.backend.accounts.get_by_uid(uid)
121 if not account:
122 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
123
124 # Check for permissions
125 if not account.can_be_managed_by(self.current_user):
126 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
127
128 # Unfortunately this cannot be wrapped into a transaction
129 try:
130 account.first_name = self.get_argument("first_name")
131 account.last_name = self.get_argument("last_name")
132 account.nickname = self.get_argument("nickname", None)
133 account.street = self.get_argument("street", None)
134 account.city = self.get_argument("city", None)
135 account.postal_code = self.get_argument("postal_code", None)
136 account.country_code = self.get_argument("country_code", None)
137 account.description = self.get_argument("description", None)
138
139 # Avatar
140 try:
141 filename, data, mimetype = self.get_file("avatar")
142
143 if not mimetype.startswith("image/"):
144 raise tornado.web.HTTPError(400, "Avatar is not an image file: %s" % mimetype)
145
146 account.upload_avatar(data)
147 except TypeError:
148 pass
149
150 # Email
151 account.mail_routing_address = self.get_argument("mail_routing_address", None)
152
153 # Telephone
154 account.phone_numbers = self.get_argument("phone_numbers", "").splitlines()
155 account.sip_routing_address = self.get_argument("sip_routing_address", None)
156 except ldap.STRONG_AUTH_REQUIRED as e:
157 raise tornado.web.HTTPError(403, "%s" % e) from e
158
159 # Redirect back to user page
160 self.redirect("/users/%s" % account.uid)
161
162
163 class UserPasswdHandler(base.BaseHandler):
164 @tornado.web.authenticated
165 def get(self, uid):
166 account = self.backend.accounts.get_by_uid(uid)
167 if not account:
168 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
169
170 # Check for permissions
171 if not account.can_be_managed_by(self.current_user):
172 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
173
174 self.render("people/passwd.html", account=account)
175
176 @tornado.web.authenticated
177 def post(self, uid):
178 account = self.backend.accounts.get_by_uid(uid)
179 if not account:
180 raise tornado.web.HTTPError(404, "Could not find account %s" % uid)
181
182 # Check for permissions
183 if not account.can_be_managed_by(self.current_user):
184 raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account))
185
186 # Get current password
187 password = self.get_argument("password")
188
189 # Get new password
190 password1 = self.get_argument("password1")
191 password2 = self.get_argument("password2")
192
193 # Passwords must match
194 if not password1 == password2:
195 raise tornado.web.HTTPError(400, "Passwords do not match")
196
197 # XXX Check password complexity
198
199 # Check if old password matches
200 if not account.check_password(password):
201 raise tornado.web.HTTPError(403, "Incorrect password for %s" % account)
202
203 # Save new password
204 account.passwd(password1)
205
206 # Redirect back to user's page
207 self.redirect("/users/%s" % account.uid)
208
209
210 class AgentModule(ui_modules.UIModule):
211 def render(self, account):
212 return self.render_string("people/modules/agent.html", account=account)
213
214
215 class CDRModule(ui_modules.UIModule):
216 def render(self, account, date=None, limit=None):
217 cdr = account.get_cdr(date=date, limit=limit)
218
219 return self.render_string("people/modules/cdr.html",
220 account=account, cdr=list(cdr))
221
222
223 class ChannelsModule(ui_modules.UIModule):
224 def render(self, account):
225 return self.render_string("people/modules/channels.html",
226 account=account, channels=account.sip_channels)
227
228
229 class MOSModule(ui_modules.UIModule):
230 def render(self, call):
231 return self.render_string("people/modules/mos.html", call=call)
232
233
234 class PasswordModule(ui_modules.UIModule):
235 def render(self, account=None):
236 return self.render_string("people/modules/password.html", account=account)
237
238 def javascript_files(self):
239 return "js/zxcvbn.js"
240
241 def embedded_javascript(self):
242 return self.render_string("people/modules/password.js")
243
244
245 class RegistrationsModule(ui_modules.UIModule):
246 def render(self, account):
247 return self.render_string("people/modules/registrations.html", account=account)