mirror of
https://github.com/wassname/flask-security.git
synced 2026-08-11 11:18:40 +08:00
First code commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
# a little trick so you can run:
|
||||
# $ python example/app.py
|
||||
# from the root of the security project
|
||||
import sys, os
|
||||
sys.path.pop(0)
|
||||
sys.path.insert(0, os.getcwd())
|
||||
|
||||
from flask import Flask, render_template
|
||||
from flask.ext.sqlalchemy import SQLAlchemy
|
||||
from flask.ext.security import (Security, LoginForm, user_datastore,
|
||||
login_required, roles_required, roles_accepted)
|
||||
from flask.ext.security.datastore.sqlalchemy import SQLAlchemyUserDatastore
|
||||
|
||||
def create_app(auth_config=None):
|
||||
|
||||
app = Flask(__name__)
|
||||
app.debug = True
|
||||
app.config['SECRET_KEY'] = 'secret'
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
|
||||
app.config['AUTH'] = auth_config or {}
|
||||
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
Security(app, SQLAlchemyUserDatastore(db))
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html', content='Home Page')
|
||||
|
||||
@app.route('/login')
|
||||
def login():
|
||||
return render_template('login.html', content='Login Page', form=LoginForm())
|
||||
|
||||
@app.route('/custom_login')
|
||||
def custom_login():
|
||||
return render_template('login.html', content='Custom Login Page', form=LoginForm())
|
||||
|
||||
@app.route('/profile')
|
||||
@login_required
|
||||
def profile():
|
||||
return render_template('index.html', content='Profile Page')
|
||||
|
||||
@app.route('/post_login')
|
||||
@login_required
|
||||
def post_login():
|
||||
return render_template('index.html', content='Post Login')
|
||||
|
||||
@app.route('/post_logout')
|
||||
def post_logout():
|
||||
return render_template('index.html', content='Post Logout')
|
||||
|
||||
@app.route('/admin')
|
||||
@roles_required('admin')
|
||||
def admin():
|
||||
return render_template('index.html', content='Admin Page')
|
||||
|
||||
@app.route('/admin_or_editor')
|
||||
@roles_accepted('admin', 'editor')
|
||||
def admin_or_editor():
|
||||
return render_template('index.html', content='Admin or Editor Page')
|
||||
|
||||
@app.before_first_request
|
||||
def before_first_request():
|
||||
db.create_all()
|
||||
|
||||
user_datastore.create_user(username='matt', email='matt@lp.com',
|
||||
password='password',
|
||||
roles=['admin'])
|
||||
|
||||
user_datastore.create_user(username='joe', email='joe@lp.com',
|
||||
password='password',
|
||||
roles=['editor'])
|
||||
|
||||
user_datastore.create_user(username='jill', email='jill@lp.com',
|
||||
password='password',
|
||||
roles=['author'])
|
||||
|
||||
user_datastore.create_user(username='tiya', email='tiya@lp.com',
|
||||
password='password', active=False)
|
||||
|
||||
return app
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = create_app()
|
||||
app.run()
|
||||
@@ -0,0 +1,9 @@
|
||||
{%- with messages = get_flashed_messages(with_categories=true) -%}
|
||||
{% if messages %}
|
||||
<ul class=flashes>
|
||||
{% for category, message in messages %}
|
||||
<li class="{{ category }}">{{ message }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
{%- endwith %}
|
||||
@@ -0,0 +1,20 @@
|
||||
{%- if current_user.is_authenticated() -%}
|
||||
<p>Hello {{ current_user.email }}</p>
|
||||
{%- endif %}
|
||||
<ul>
|
||||
<li><a href="{{ url_for('index') }}">Index</a></li>
|
||||
<li><a href="{{ url_for('profile') }}">Profile</a></li>
|
||||
{% if current_user.has_role('admin') -%}
|
||||
<li><a href="{{ url_for('admin') }}">Admin</a></li>
|
||||
{% endif -%}
|
||||
{% if current_user.has_role('admin') or current_user.has_role('editor') -%}
|
||||
<li><a href="{{ url_for('admin_or_editor') }}">Admin or Editor</a></li>
|
||||
{% endif -%}
|
||||
<li>
|
||||
{%- if current_user.is_authenticated() -%}
|
||||
<a href="{{ url_for('auth.logout') }}">Log out</a>
|
||||
{%- else -%}
|
||||
<a href="{{ url_for('login') }}">Log in</a>
|
||||
{%- endif -%}
|
||||
</li>
|
||||
</ul>
|
||||
@@ -0,0 +1,3 @@
|
||||
{% include "_messages.html" %}
|
||||
{% include "_nav.html" %}
|
||||
<p>{{ content }}</p>
|
||||
@@ -0,0 +1,10 @@
|
||||
{% include "_messages.html" %}
|
||||
{% include "_nav.html" %}
|
||||
<form action="{{ url_for('auth.authenticate') }}" method="POST" name="login_form">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.username.label }} {{ form.username }}<br/>
|
||||
{{ form.password.label }} {{ form.password }}<br/>
|
||||
{{ form.remember.label }} {{ form.remember }}<br/>
|
||||
{{ form.submit }}
|
||||
</form>
|
||||
<p>{{ content }}</p>
|
||||
Reference in New Issue
Block a user