-
Notifications
You must be signed in to change notification settings - Fork 16
/
account.py
363 lines (289 loc) · 15 KB
/
account.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import uuid, json
from flask import Blueprint, request, url_for, flash, redirect, make_response
from flask import render_template, abort
from flask_login import login_user, logout_user, current_user, login_required
from wtforms import StringField, HiddenField, PasswordField, validators, Form
from portality import util
from portality import constants
from portality.core import app
from portality.decorators import ssl_required, write_required
from portality.models import Account, Event
from portality.forms.validate import DataOptional, EmailAvailable, ReservedUsernames, IdAvailable, IgnoreUnchanged
from portality.bll import DOAJ
blueprint = Blueprint('account', __name__)
@blueprint.route('/')
@login_required
@ssl_required
def index():
if not current_user.has_role("list_users"):
abort(401)
return render_template("account/users.html")
class UserEditForm(Form):
# Let's not allow anyone to change IDs - there lies madness and destruction (referential integrity)
# id = StringField('ID', [IgnoreUnchanged(), ReservedUsernames(), IdAvailable()])
name = StringField('Account name', [DataOptional(), validators.Length(min=3, max=64)])
email = StringField('Email address', [
IgnoreUnchanged(),
validators.Length(min=3, max=254),
validators.Email(message='Must be a valid email address'),
EmailAvailable(),
validators.EqualTo('email_confirm', message='Email confirmation must match'),
])
email_confirm = StringField('Confirm email address')
roles = StringField('User roles')
password_change = PasswordField('Change password', [
validators.EqualTo('password_confirm', message='Passwords must match'),
])
password_confirm = PasswordField('Confirm password')
@blueprint.route('/<username>', methods=['GET', 'POST', 'DELETE'])
@login_required
@ssl_required
@write_required()
def username(username):
acc = Account.pull(username)
if acc is None:
abort(404)
if (request.method == 'DELETE' or
(request.method == 'POST' and request.values.get('submit', False) == 'Delete')):
if current_user.id != acc.id and not current_user.is_super:
abort(401)
else:
conf = request.values.get("delete_confirm")
if conf is None or conf != "delete_confirm":
flash('Check the box to confirm you really mean it!', "error")
return render_template('account/view.html', account=acc, form=UserEditForm(obj=acc))
acc.delete()
flash('Account ' + acc.id + ' deleted')
return redirect(url_for('.index'))
elif request.method == 'POST':
if current_user.id != acc.id and not current_user.is_super:
abort(401)
form = UserEditForm(obj=acc, formdata=request.form)
if not form.validate():
return render_template('account/view.html', account=acc, form=form)
newdata = request.json if request.json else request.values
if request.values.get('submit', False) == 'Generate a new API Key':
acc.generate_api_key()
# if 'id' in newdata and len(newdata['id']) > 0:
# if newdata['id'] != current_user.id == acc.id:
# flash('You may not edit the ID of your own account', 'error')
# return render_template('account/view.html', account=acc, form=form)
# else:
# acc.delete() # request for the old record to be deleted from ES
# acc.set_id(newdata['id'])
if 'name' in newdata:
acc.set_name(newdata['name'])
if 'password_change' in newdata and len(newdata['password_change']) > 0 and not newdata['password_change'].startswith('sha1'):
acc.set_password(newdata['password_change'])
# only super users can re-write roles
if "roles" in newdata and current_user.is_super:
new_roles = [r.strip() for r in newdata.get("roles").split(",")]
acc.set_role(new_roles)
if "marketing_consent" in newdata:
acc.set_marketing_consent(newdata["marketing_consent"] == "true")
if 'email' in newdata and len(newdata['email']) > 0 and newdata['email'] != acc.email:
acc.set_email(newdata['email'])
# If the user updated their own email address, invalidate the password and require verification again.
if current_user.id == acc.id:
acc.clear_password()
reset_token = uuid.uuid4().hex
acc.set_reset_token(reset_token, app.config.get("PASSWORD_RESET_TIMEOUT", 86400))
acc.save()
events_svc = DOAJ.eventsService()
events_svc.trigger(Event(constants.EVENT_ACCOUNT_PASSWORD_RESET, acc.id, context={"account" : acc.data}))
flash("Email address updated. You have been logged out for email address verification.")
logout_user()
if app.config.get('DEBUG', False):
reset_url = url_for('account.reset', reset_token=acc.reset_token)
util.flash_with_url('Debug mode - url for reset is <a href={0}>{0}</a>'.format(reset_url))
return redirect(url_for('doaj.home'))
acc.save()
flash("Record updated")
return render_template('account/view.html', account=acc, form=form)
else: # GET
if util.request_wants_json():
resp = make_response(
json.dumps(acc.data, sort_keys=True, indent=4))
resp.mimetype = "application/json"
return resp
else:
form = UserEditForm(obj=acc)
return render_template('account/view.html', account=acc, form=form)
def get_redirect_target(form=None, acc=None):
form_target = ''
if form and hasattr(form, 'next') and getattr(form, 'next'):
form_target = form.next.data
for target in form_target, request.args.get('next', []):
if not target:
continue
if target == util.is_safe_url(target):
return target
if acc is None:
return ""
destinations = app.config.get("ROLE_LOGIN_DESTINATIONS")
for role, dest in destinations:
if acc.has_role(role):
return url_for(dest)
return url_for(app.config.get("DEFAULT_LOGIN_DESTINATION"))
class RedirectForm(Form):
next = HiddenField()
def __init__(self, *args, **kwargs):
Form.__init__(self, *args, **kwargs)
if not self.next.data:
self.next.data = get_redirect_target() or ''
def redirect(self, endpoint='index', **values):
if self.next.data == util.is_safe_url(self.next.data):
return redirect(self.next.data)
target = get_redirect_target()
return redirect(target or url_for(endpoint, **values))
class LoginForm(RedirectForm):
user = StringField('Email address or username', [validators.DataRequired()])
password = PasswordField('Password', [validators.DataRequired()])
@blueprint.route('/login', methods=['GET', 'POST'])
@ssl_required
def login():
current_info = {'next': request.args.get('next', '')}
form = LoginForm(request.form, csrf_enabled=False, **current_info)
if request.method == 'POST' and form.validate():
password = form.password.data
username = form.user.data
# If our settings allow, try getting the user account by ID first, then by email address
if app.config.get('LOGIN_VIA_ACCOUNT_ID', False):
user = Account.pull(username) or Account.pull_by_email(username)
else:
user = Account.pull_by_email(username)
# If we have a verified user account, proceed to attempt login
try:
if user is not None:
if user.check_password(password):
login_user(user, remember=True)
flash('Welcome back.', 'success')
return redirect(get_redirect_target(form=form, acc=user))
else:
form.password.errors.append('The password you entered is incorrect. Try again or <a href="{0}">reset your password</a>.'.format(url_for(".forgot")))
else:
form.user.errors.append('Account not recognised. If you entered an email address, try your username instead.')
except KeyError:
# Account has no password set, the user needs to reset or use an existing valid reset link
FORGOT_INSTR = '<a href="{url}"><click here></a> to send a new reset link.'.format(url=url_for('.forgot'))
util.flash_with_url('Account verification is incomplete. Check your emails for the link or ' + FORGOT_INSTR,
'error')
return redirect(url_for('doaj.home'))
if request.args.get("redirected") == "apply":
form['next'].data = url_for("apply.public_application")
return render_template('account/login_to_apply.html', form=form)
return render_template('account/login.html', form=form)
@blueprint.route('/forgot', methods=['GET', 'POST'])
@ssl_required
@write_required()
def forgot():
CONTACT_INSTR = ' Please <a href="{url}">contact us.</a>'.format(url=url_for('doaj.contact'))
if request.method == 'POST':
# get hold of the user account
un = request.form.get('un', "")
if app.config.get('LOGIN_VIA_ACCOUNT_ID', False):
account = Account.pull(un) or Account.pull_by_email(un)
else:
account = Account.pull_by_email(un)
if account is None:
util.flash_with_url('Error - your account username / email address is not recognised.' + CONTACT_INSTR,
'error')
return render_template('account/forgot.html')
if not account.data.get('email'):
util.flash_with_url('Error - your account does not have an associated email address.' + CONTACT_INSTR,
'error')
return render_template('account/forgot.html')
# if we get to here, we have a user account to reset
reset_token = uuid.uuid4().hex
account.set_reset_token(reset_token, app.config.get("PASSWORD_RESET_TIMEOUT", 86400))
account.save()
events_svc = DOAJ.eventsService()
events_svc.trigger(Event(constants.EVENT_ACCOUNT_PASSWORD_RESET, account.id, context={"account": account.data}))
flash('Instructions to reset your password have been sent to you. Please check your emails.')
if app.config.get('DEBUG', False):
util.flash_with_url('Debug mode - url for reset is <a href={0}>{0}</a>'.format(
url_for('account.reset', reset_token=account.reset_token)))
return render_template('account/forgot.html')
class ResetForm(Form):
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Passwords must match')
])
confirm = PasswordField('Repeat Password')
@blueprint.route("/reset/<reset_token>", methods=["GET", "POST"])
@ssl_required
@write_required()
def reset(reset_token):
form = ResetForm(request.form, csrf_enabled=False)
account = Account.get_by_reset_token(reset_token)
if account is None:
abort(404)
if request.method == "POST" and form.validate():
# check that the passwords match, and bounce if not
pw = request.values.get("password")
conf = request.values.get("confirm")
if pw != conf:
flash("Passwords do not match - please try again", "error")
return render_template("account/reset.html", account=account, form=form)
# update the user's account
account.set_password(pw)
account.remove_reset_token()
account.save()
flash("New password has been set and you're now logged in.", "success")
# log the user in
login_user(account, remember=True)
return redirect(url_for('doaj.home'))
return render_template("account/reset.html", account=account, form=form)
@blueprint.route('/logout')
@ssl_required
def logout():
logout_user()
flash('You are now logged out', 'success')
return redirect('/')
class RegisterForm(RedirectForm):
identifier = StringField('ID', [ReservedUsernames(), IdAvailable()])
name = StringField('Name', [validators.Optional(), validators.Length(min=3, max=64)])
email = StringField('Email address', [
validators.DataRequired(),
validators.Length(min=3, max=254),
validators.Email(message='Must be a valid email address'),
EmailAvailable(message="That email address is already in use. Please <a href='/account/forgot'>reset your password</a>. If you still cannot login, <a href='/contact'>contact us</a>.")
])
roles = StringField('Roles')
recaptcha_value = HiddenField()
@blueprint.route('/register', methods=['GET', 'POST'])
@ssl_required
@write_required()
def register():
# 3rd-party registration only for those with create_user role, only allow public registration when configured
if current_user.is_authenticated and not current_user.has_role("create_user") \
or current_user.is_anonymous and app.config.get('PUBLIC_REGISTER', False) is False:
abort(401) # todo: we may need a template to explain this since it's linked from the application form
form = RegisterForm(request.form, csrf_enabled=False, roles='api,publisher', identifier=Account.new_short_uuid())
if request.method == 'POST' and form.validate():
if app.config.get("RECAPTCHA_ENABLE"):
recap_data = util.verify_recaptcha(form.recaptcha_value.data)
else:
recap_data = {"success": True}
if recap_data["success"]:
account = Account.make_account(email=form.email.data, username=form.identifier.data, name=form.name.data,
roles=[r.strip() for r in form.roles.data.split(',')])
account.save()
event_svc = DOAJ.eventsService()
event_svc.trigger(Event(constants.EVENT_ACCOUNT_CREATED, account.id, context={"account" : account.data}))
# send_account_created_email(account)
if app.config.get('DEBUG', False):
util.flash_with_url('Debug mode - url for verify is <a href={0}>{0}</a>'.format(url_for('account.reset', reset_token=account.reset_token)))
if current_user.is_authenticated:
util.flash_with_url('Account created for {0}. View Account: <a href={1}>{1}</a>'.format(account.email, url_for('.username', username=account.id)))
return redirect(url_for('.index'))
else:
flash('Thank you, please verify email address ' + form.email.data + ' to set your password and login.',
'success')
# We must redirect home because the user now needs to verify their email address.
return redirect(url_for('doaj.home'))
else: # recaptcha fail
util.flash("reCAPTCHA failed, please retry.")
if request.method == 'POST' and not form.validate():
flash('Please correct the errors', 'error')
return render_template('account/register.html', form=form)