Commit aef56afc authored by Paul Brown's avatar Paul Brown

add support for new version of flask-login and flask-security

parent f2a53614
...@@ -95,7 +95,7 @@ def init_login(): ...@@ -95,7 +95,7 @@ def init_login():
class MyModelView(sqla.ModelView): class MyModelView(sqla.ModelView):
def is_accessible(self): def is_accessible(self):
return login.current_user.is_authenticated() return login.current_user.is_authenticated
# Create customized index view class that handles login & registration # Create customized index view class that handles login & registration
...@@ -103,7 +103,7 @@ class MyAdminIndexView(admin.AdminIndexView): ...@@ -103,7 +103,7 @@ class MyAdminIndexView(admin.AdminIndexView):
@expose('/') @expose('/')
def index(self): def index(self):
if not login.current_user.is_authenticated(): if not login.current_user.is_authenticated:
return redirect(url_for('.login_view')) return redirect(url_for('.login_view'))
return super(MyAdminIndexView, self).index() return super(MyAdminIndexView, self).index()
...@@ -115,7 +115,7 @@ class MyAdminIndexView(admin.AdminIndexView): ...@@ -115,7 +115,7 @@ class MyAdminIndexView(admin.AdminIndexView):
user = form.get_user() user = form.get_user()
login.login_user(user) login.login_user(user)
if login.current_user.is_authenticated(): if login.current_user.is_authenticated:
return redirect(url_for('.index')) return redirect(url_for('.index'))
link = '<p>Don\'t have an account? <a href="' + url_for('.register_view') + '">Click here to register.</a></p>' link = '<p>Don\'t have an account? <a href="' + url_for('.register_view') + '">Click here to register.</a></p>'
self._template_args['form'] = form self._template_args['form'] = form
......
Flask Flask
Flask-Admin Flask-Admin
Flask-SQLAlchemy Flask-SQLAlchemy
Flask-Login==0.2.11 Flask-Login>=0.3.0
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<div class="row-fluid"> <div class="row-fluid">
<div> <div>
{% if current_user.is_authenticated() %} {% if current_user.is_authenticated %}
<h1>Flask-Admin example</h1> <h1>Flask-Admin example</h1>
<p class="lead"> <p class="lead">
Authentication Authentication
...@@ -36,4 +36,4 @@ ...@@ -36,4 +36,4 @@
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a> <a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a>
</div> </div>
{% endblock body %} {% endblock body %}
\ No newline at end of file
{% extends 'admin/base.html' %} {% extends 'admin/base.html' %}
{% block access_control %} {% block access_control %}
{% if current_user.is_authenticated() %} {% if current_user.is_authenticated %}
<div class="btn-group pull-right"> <div class="btn-group pull-right">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<i class="icon-user"></i> {{ current_user.login }} <span class="caret"></span> <i class="icon-user"></i> {{ current_user.login }} <span class="caret"></span>
...@@ -11,4 +11,4 @@ ...@@ -11,4 +11,4 @@
</ul> </ul>
</div> </div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -87,13 +87,13 @@ def init_login(): ...@@ -87,13 +87,13 @@ def init_login():
# Create customized model view class # Create customized model view class
class MyModelView(ModelView): class MyModelView(ModelView):
def is_accessible(self): def is_accessible(self):
return login.current_user.is_authenticated() return login.current_user.is_authenticated
# Create customized index view class # Create customized index view class
class MyAdminIndexView(admin.AdminIndexView): class MyAdminIndexView(admin.AdminIndexView):
def is_accessible(self): def is_accessible(self):
return login.current_user.is_authenticated() return login.current_user.is_authenticated
# Flask views # Flask views
......
Flask Flask
Flask-Admin Flask-Admin
flask-mongoengine flask-mongoengine
Flask-Login==0.2.11 Flask-Login>=0.3.0
<html> <html>
<body> <body>
<div> <div>
{% if user and user.is_authenticated() %} {% if user and user.is_authenticated %}
Hello {{ user.login }}! <a href="{{ url_for('logout_view') }}">Logout</a> Hello {{ user.login }}! <a href="{{ url_for('logout_view') }}">Logout</a>
{% else %} {% else %}
Welcome anonymous user! Welcome anonymous user!
......
...@@ -56,7 +56,7 @@ security = Security(app, user_datastore) ...@@ -56,7 +56,7 @@ security = Security(app, user_datastore)
class MyModelView(sqla.ModelView): class MyModelView(sqla.ModelView):
def is_accessible(self): def is_accessible(self):
if not current_user.is_active() or not current_user.is_authenticated(): if not current_user.is_active or not current_user.is_authenticated:
return False return False
if current_user.has_role('superuser'): if current_user.has_role('superuser'):
...@@ -69,7 +69,7 @@ class MyModelView(sqla.ModelView): ...@@ -69,7 +69,7 @@ class MyModelView(sqla.ModelView):
Override builtin _handle_view in order to redirect users when a view is not accessible. Override builtin _handle_view in order to redirect users when a view is not accessible.
""" """
if not self.is_accessible(): if not self.is_accessible():
if current_user.is_authenticated(): if current_user.is_authenticated:
# permission denied # permission denied
abort(403) abort(403)
else: else:
......
Flask Flask
Flask-Admin Flask-Admin
Flask-SQLAlchemy Flask-SQLAlchemy
Flask-Security==1.7.4 Flask-Security>=1.7.5
Flask-Login==0.2.11
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<p> <p>
This example shows how you can use <a href="https://pythonhosted.org/Flask-Security/index.html" target="_blank">Flask-Security</a> for authentication. This example shows how you can use <a href="https://pythonhosted.org/Flask-Security/index.html" target="_blank">Flask-Security</a> for authentication.
</p> </p>
{% if not current_user.is_authenticated() %} {% if not current_user.is_authenticated %}
<p>You can register as a regular user, or log in as a superuser with the following credentials: <p>You can register as a regular user, or log in as a superuser with the following credentials:
<ul> <ul>
<li>email: <b>admin</b></li> <li>email: <b>admin</b></li>
...@@ -27,4 +27,4 @@ ...@@ -27,4 +27,4 @@
</div> </div>
</div> </div>
</div> </div>
{% endblock body %} {% endblock body %}
\ No newline at end of file
{% extends 'admin/base.html' %} {% extends 'admin/base.html' %}
{% block access_control %} {% block access_control %}
{% if current_user.is_authenticated() %} {% if current_user.is_authenticated %}
<div class="navbar-text btn-group pull-right"> <div class="navbar-text btn-group pull-right">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<i class="glyphicon glyphicon-user"></i> <i class="glyphicon glyphicon-user"></i>
...@@ -15,4 +15,4 @@ ...@@ -15,4 +15,4 @@
</ul> </ul>
</div> </div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -19,12 +19,12 @@ class User(UserMixin): ...@@ -19,12 +19,12 @@ class User(UserMixin):
# Create menu links classes with reloaded accessible # Create menu links classes with reloaded accessible
class AuthenticatedMenuLink(MenuLink): class AuthenticatedMenuLink(MenuLink):
def is_accessible(self): def is_accessible(self):
return current_user.is_authenticated() return current_user.is_authenticated
class NotAuthenticatedMenuLink(MenuLink): class NotAuthenticatedMenuLink(MenuLink):
def is_accessible(self): def is_accessible(self):
return not current_user.is_authenticated() return not current_user.is_authenticated
# Create custom admin view for authenticated users # Create custom admin view for authenticated users
...@@ -34,7 +34,7 @@ class MyAdminView(BaseView): ...@@ -34,7 +34,7 @@ class MyAdminView(BaseView):
return self.render('authenticated-admin.html') return self.render('authenticated-admin.html')
def is_accessible(self): def is_accessible(self):
return current_user.is_authenticated() return current_user.is_authenticated
# Create flask app # Create flask app
......
Flask Flask
Flask-Admin Flask-Admin
Flask-Login==0.2.11 Flask-Login>=0.3.0
Flask Flask
Flask-Admin Flask-Admin
Flask-MongoEngine Flask-MongoEngine
Flask-Login==0.2.11 Flask-Login>=0.3.0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment