add option to suppress platform admin temporarily

so that platform admins (us) can view pages as regular users do easily.
Simply adds a flag in the session cookie that overrides the actual
platform admin flag on the user model if set. This way it's safe, since
this only downgrades existing functionality, so if someone managed to
alter it they could only get less permissions, not more.

You can change this value from the user profile page if either:

* you're a platform admin
* the flag is set (to any value) on the cookie.

This slightly weird check means that we don't check the underlying
`user._platform_admin` flag anywhere in the code, even when toggling
the suppression.
This commit is contained in:
Leo Hemsted
2019-06-13 19:00:17 +01:00
parent c7325576d6
commit 7b02cb72c6
7 changed files with 147 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
import json
from flask import current_app, redirect, render_template, session, url_for
from flask import current_app, redirect, render_template, session, url_for, abort
from flask_login import current_user, login_required
from notifications_utils.url_safe_token import check_token
@@ -12,6 +12,7 @@ from app.main.forms import (
ChangeNameForm,
ChangePasswordForm,
ConfirmPasswordForm,
ServiceOnOffSettingForm,
TwoFactorForm,
)
from app.models.user import User
@@ -192,3 +193,26 @@ def user_profile_password():
'views/user-profile/change-password.html',
form=form
)
@main.route("/user-profile/suppress-platform-admin", methods=['GET', 'POST'])
@login_required
def user_profile_suppress_platform_admin():
if not current_user.platform_admin and not session.get('suppress_platform_admin'):
abort(403)
form = ServiceOnOffSettingForm(
name="This setting will be cleared if you sign out and sign in again",
enabled=session.get('suppress_platform_admin', False),
truthy='Yes (view as regular user)',
falsey='No (view as platform admin)',
)
if form.validate_on_submit():
session['suppress_platform_admin'] = form.enabled.data
return redirect(url_for('.user_profile'))
return render_template(
'views/user-profile/suppress-platform-admin.html',
form=form
)