mirror of
https://github.com/wassname/flask-security.git
synced 2026-07-23 12:50:33 +08:00
Refactor templates and added url_for_security utility. Can also configure the blueprint name.
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
{% endif -%}
|
||||
<li>
|
||||
{%- if current_user.is_authenticated() -%}
|
||||
<a href="{{ url_for('flask_security.logout') }}">Log out</a>
|
||||
<a href="{{ url_for('security.logout') }}">Log out</a>
|
||||
{%- else -%}
|
||||
<a href="{{ url_for('login') }}">Log in</a>
|
||||
{%- endif -%}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{% include "_messages.html" %}
|
||||
{% include "_nav.html" %}
|
||||
<form action="{{ url_for('flask_security.authenticate') }}" method="POST" name="login_form">
|
||||
<form action="{{ url_for_security('authenticate') }}" method="POST" name="login_form">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.email.label }} {{ form.email }}<br/>
|
||||
{{ form.password.label }} {{ form.password }}<br/>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{% include "_messages.html" %}
|
||||
{% include "_nav.html" %}
|
||||
<form action="{{ url_for('flask_security.send_login') }}" method="POST" name="login_form">
|
||||
<form action="{{ url_for_security('send_login') }}" method="POST" name="login_form">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.email.label }} {{ form.email }}<br/>
|
||||
{{ form.next }}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{% include "_messages.html" %}
|
||||
{% include "_nav.html" %}
|
||||
<h1>Register</h1>
|
||||
<form action="{{ url_for('flask_security.register') }}" method="POST" name="register_form">
|
||||
<form action="{{ url_for_security('register') }}" method="POST" name="register_form">
|
||||
{{ register_user_form.hidden_tag() }}
|
||||
{{ register_user_form.email.label }} {{ register_user_form.email }}<br/>
|
||||
{{ register_user_form.password.label }} {{ register_user_form.password }}<br/>
|
||||
|
||||
@@ -22,4 +22,4 @@ from .forms import ForgotPasswordForm, LoginForm, RegisterForm, \
|
||||
from .signals import confirm_instructions_sent, password_reset, \
|
||||
password_reset_requested, reset_instructions_sent, user_confirmed, \
|
||||
user_registered
|
||||
from .utils import login_user, logout_user
|
||||
from .utils import login_user, logout_user, url_for_security
|
||||
|
||||
@@ -16,7 +16,7 @@ from flask import current_app as app, request, url_for
|
||||
from werkzeug.local import LocalProxy
|
||||
|
||||
from .exceptions import ConfirmationError
|
||||
from .utils import send_mail, get_max_age, md5, get_message
|
||||
from .utils import send_mail, get_max_age, md5, get_message, url_for_security
|
||||
from .signals import user_confirmed, confirm_instructions_sent
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ def send_confirmation_instructions(user, token):
|
||||
:param user: The user to send the instructions to
|
||||
:param token: The confirmation token
|
||||
"""
|
||||
url = url_for('flask_security.confirm_email', token=token)
|
||||
url = url_for_security('confirm_email', token=token)
|
||||
|
||||
confirmation_link = request.url_root[:-1] + url
|
||||
|
||||
|
||||
+11
-5
@@ -9,7 +9,7 @@
|
||||
:license: MIT, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
from itsdangerous import URLSafeTimedSerializer, BadSignature
|
||||
from itsdangerous import URLSafeTimedSerializer
|
||||
from flask import current_app
|
||||
from flask.ext.login import AnonymousUser as AnonymousUserBase, \
|
||||
UserMixin as BaseUserMixin, LoginManager, current_user
|
||||
@@ -21,7 +21,8 @@ from werkzeug.local import LocalProxy
|
||||
|
||||
from . import views, exceptions
|
||||
from .confirmable import requires_confirmation
|
||||
from .utils import config_value as cv, get_config, verify_password, md5
|
||||
from .utils import config_value as cv, get_config, verify_password, md5, \
|
||||
url_for_security
|
||||
|
||||
# Convenient references
|
||||
_security = LocalProxy(lambda: current_app.extensions['security'])
|
||||
@@ -29,6 +30,7 @@ _security = LocalProxy(lambda: current_app.extensions['security'])
|
||||
|
||||
#: Default Flask-Security configuration
|
||||
_default_config = {
|
||||
'BLUEPRINT_NAME': 'security',
|
||||
'URL_PREFIX': None,
|
||||
'FLASH_MESSAGES': True,
|
||||
'PASSWORD_HASH': 'plaintext',
|
||||
@@ -246,11 +248,15 @@ class Security(object):
|
||||
identity_loaded.connect_via(app)(_on_identity_loaded)
|
||||
|
||||
if register_blueprint:
|
||||
bp = views.create_blueprint(app, 'flask_security', __name__,
|
||||
template_folder='templates',
|
||||
url_prefix=cv('URL_PREFIX', app=app))
|
||||
name = cv('BLUEPRINT_NAME', app=app)
|
||||
url_prefix = cv('URL_PREFIX', app=app)
|
||||
bp = views.create_blueprint(app, name, __name__,
|
||||
url_prefix=url_prefix,
|
||||
template_folder='templates')
|
||||
app.register_blueprint(bp)
|
||||
|
||||
app.context_processor(lambda: dict(url_for_security=url_for_security))
|
||||
|
||||
state = self._get_state(app, datastore or self.datastore)
|
||||
|
||||
app.extensions['security'] = state
|
||||
|
||||
@@ -9,13 +9,14 @@
|
||||
:license: MIT, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
from flask import url_for, request, current_app as app
|
||||
from flask import request, current_app as app
|
||||
from itsdangerous import SignatureExpired, BadSignature
|
||||
from werkzeug.local import LocalProxy
|
||||
|
||||
from .exceptions import PasswordlessLoginError
|
||||
from .signals import login_instructions_sent
|
||||
from .utils import send_mail, md5, get_max_age, login_user, get_message
|
||||
from .utils import send_mail, md5, get_max_age, login_user, get_message, \
|
||||
url_for_security
|
||||
|
||||
|
||||
# Convenient references
|
||||
@@ -32,7 +33,7 @@ def send_login_instructions(user, next):
|
||||
"""
|
||||
token = generate_login_token(user, next)
|
||||
|
||||
url = url_for('flask_security.token_login', token=token)
|
||||
url = url_for_security('token_login', token=token)
|
||||
|
||||
login_link = request.url_root[:-1] + url
|
||||
|
||||
|
||||
@@ -10,13 +10,14 @@
|
||||
"""
|
||||
|
||||
from itsdangerous import BadSignature, SignatureExpired
|
||||
from flask import current_app as app, request, url_for
|
||||
from flask import current_app as app, request
|
||||
from werkzeug.local import LocalProxy
|
||||
|
||||
from .exceptions import ResetPasswordError, UserNotFoundError
|
||||
from .signals import password_reset, password_reset_requested, \
|
||||
reset_instructions_sent
|
||||
from .utils import send_mail, get_max_age, md5, get_message, encrypt_password
|
||||
from .utils import send_mail, get_max_age, md5, get_message, encrypt_password, \
|
||||
url_for_security
|
||||
|
||||
|
||||
# Convenient references
|
||||
@@ -30,8 +31,7 @@ def send_reset_password_instructions(user, reset_token):
|
||||
|
||||
:param user: The user to send the instructions to
|
||||
"""
|
||||
url = url_for('flask_security.reset_password',
|
||||
token=reset_token)
|
||||
url = url_for_security('reset_password', token=reset_token)
|
||||
|
||||
reset_link = request.url_root[:-1] + url
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
{% from "security/macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/messages.html" %}
|
||||
<h1>Resend confirmation instructions</h1>
|
||||
<form action="{{ url_for('flask_security.send_confirmation') }}" method="POST" name="reset_confirmation_form">
|
||||
{{ reset_confirmation_form.hidden_tag() }}
|
||||
{{ render_field_with_errors(reset_confirmation_form.email) }}
|
||||
{{ render_field(reset_confirmation_form.submit) }}
|
||||
</form>
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
{% from "security/macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/messages.html" %}
|
||||
{% from "security/_macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/_messages.html" %}
|
||||
<h1>Edit Account</h1>
|
||||
<form action="{{ url_for('flask_security.update_user') }}" method="POST" name="edit_user_form">
|
||||
<form action="{{ url_for_security('edit_user') }}" method="POST" name="edit_user_form">
|
||||
{{ edit_user_form.hidden_tag() }}
|
||||
{{ render_field_with_errors(edit_user_form.email) }}
|
||||
{{ render_field_with_errors(edit_user_form.password) }}
|
||||
@@ -0,0 +1,8 @@
|
||||
{% from "security/_macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/_messages.html" %}
|
||||
<h1>Send reset password instructions</h1>
|
||||
<form action="{{ url_for_security('forgot_password') }}" method="POST" name="forgot_password_form">
|
||||
{{ forgot_password_form.hidden_tag() }}
|
||||
{{ render_field_with_errors(forgot_password_form.email) }}
|
||||
{{ render_field(forgot_password_form.submit) }}
|
||||
</form>
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
{% from "security/macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/messages.html" %}
|
||||
{% from "security/_macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/_messages.html" %}
|
||||
<h1>Login</h1>
|
||||
<form action="{{ url_for('flask_security.authenticate') }}" method="POST" name="login_form">
|
||||
<form action="{{ url_for_security('authenticate') }}" method="POST" name="login_form">
|
||||
{{ login_form.hidden_tag() }}
|
||||
{{ render_field_with_errors(login_form.email) }}
|
||||
{{ render_field_with_errors(login_form.password) }}
|
||||
@@ -1,9 +0,0 @@
|
||||
{% from "security/macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/messages.html" %}
|
||||
<h1>Login</h1>
|
||||
<form action="{{ url_for('flask_security.send_login') }}" method="POST" name="login_form">
|
||||
{{ login_form.hidden_tag() }}
|
||||
{{ render_field_with_errors(login_form.email) }}
|
||||
{{ render_field(login_form.next) }}
|
||||
{{ render_field(login_form.submit) }}
|
||||
</form>
|
||||
@@ -1,8 +0,0 @@
|
||||
{% from "security/macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/messages.html" %}
|
||||
<h1>Send reset password instructions</h1>
|
||||
<form action="{{ url_for('flask_security.forgot_password') }}" method="POST" name="forgot_password_form">
|
||||
{{ forgot_password_form.hidden_tag() }}
|
||||
{{ render_field_with_errors(forgot_password_form.email) }}
|
||||
{{ render_field(forgot_password_form.submit) }}
|
||||
</form>
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
{% from "security/macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/messages.html" %}
|
||||
{% from "security/_macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/_messages.html" %}
|
||||
<h1>Register</h1>
|
||||
<form action="{{ url_for('flask_security.register') }}" method="POST" name="register_user_form">
|
||||
<form action="{{ url_for_security('register') }}" method="POST" name="register_user_form">
|
||||
{{ register_user_form.hidden_tag() }}
|
||||
{{ render_field_with_errors(register_user_form.email) }}
|
||||
{{ render_field_with_errors(register_user_form.password) }}
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
{% from "security/macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/messages.html" %}
|
||||
{% from "security/_macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/_messages.html" %}
|
||||
<h1>Change password</h1>
|
||||
<form action="{{ url_for('flask_security.reset_password', token=password_reset_token) }}" method="POST" name="reset_password_form">
|
||||
<form action="{{ url_for_security('reset_password', token=password_reset_token) }}" method="POST" name="reset_password_form">
|
||||
{{ reset_password_form.hidden_tag() }}
|
||||
{{ render_field_with_errors(reset_password_form.password) }}
|
||||
{{ render_field_with_errors(reset_password_form.password_confirm) }}
|
||||
@@ -0,0 +1,8 @@
|
||||
{% from "security/_macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/_messages.html" %}
|
||||
<h1>Resend confirmation instructions</h1>
|
||||
<form action="{{ url_for_security('send_confirmation') }}" method="POST" name="reset_confirmation_form">
|
||||
{{ reset_confirmation_form.hidden_tag() }}
|
||||
{{ render_field_with_errors(reset_confirmation_form.email) }}
|
||||
{{ render_field(reset_confirmation_form.submit) }}
|
||||
</form>
|
||||
@@ -0,0 +1,9 @@
|
||||
{% from "security/_macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/_messages.html" %}
|
||||
<h1>Login</h1>
|
||||
<form action="{{ url_for_security('send_login') }}" method="POST" name="login_form">
|
||||
{{ login_form.hidden_tag() }}
|
||||
{{ render_field_with_errors(login_form.email) }}
|
||||
{{ render_field(login_form.next) }}
|
||||
{{ render_field(login_form.submit) }}
|
||||
</form>
|
||||
@@ -115,6 +115,21 @@ def get_url(endpoint_or_url):
|
||||
return endpoint_or_url
|
||||
|
||||
|
||||
def url_for_security(endpoint, **values):
|
||||
"""Return a URL for the security blueprint
|
||||
|
||||
:param endpoint: the endpoint of the URL (name of the function)
|
||||
:param values: the variable arguments of the URL rule
|
||||
:param _external: if set to `True`, an absolute URL is generated. Server
|
||||
address can be changed via `SERVER_NAME` configuration variable which
|
||||
defaults to `localhost`.
|
||||
:param _anchor: if provided this is added as anchor to the URL.
|
||||
:param _method: if provided this explicitly specifies an HTTP method.
|
||||
"""
|
||||
endpoint = '%s.%s' % (_security.blueprint_name, endpoint)
|
||||
return url_for(endpoint, **values)
|
||||
|
||||
|
||||
def get_post_login_redirect():
|
||||
"""Returns the URL to redirect to after a user logs in successfully."""
|
||||
return (get_url(request.args.get('next')) or
|
||||
|
||||
@@ -128,7 +128,7 @@ def register_user():
|
||||
return redirect(_security.post_register_view or
|
||||
_security.post_login_view)
|
||||
|
||||
return render_template('security/registrations/new.html',
|
||||
return render_template('security/register_user.html',
|
||||
register_user_form=form)
|
||||
|
||||
|
||||
@@ -143,7 +143,7 @@ def send_login():
|
||||
else:
|
||||
do_flash(*get_message('DISABLED_ACCOUNT'))
|
||||
|
||||
return render_template('security/logins/passwordless.html', login_form=form)
|
||||
return render_template('security/send_login.html', login_form=form)
|
||||
|
||||
|
||||
def token_login(token):
|
||||
@@ -182,7 +182,7 @@ def send_confirmation():
|
||||
|
||||
do_flash(msg, cat)
|
||||
|
||||
return render_template('security/confirmations/new.html',
|
||||
return render_template('security/send_confirmation.html',
|
||||
reset_confirmation_form=form)
|
||||
|
||||
|
||||
@@ -231,7 +231,7 @@ def forgot_password():
|
||||
for key, value in form.errors.items():
|
||||
do_flash(value[0], 'error')
|
||||
|
||||
return render_template('security/passwords/new.html',
|
||||
return render_template('security/forgot_password.html',
|
||||
forgot_password_form=form)
|
||||
|
||||
|
||||
@@ -267,7 +267,7 @@ def reset_password(token):
|
||||
|
||||
do_flash(msg, cat)
|
||||
|
||||
return render_template('security/passwords/edit.html',
|
||||
return render_template('security/reset_password.html',
|
||||
reset_password_form=form,
|
||||
password_reset_token=token)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user