चूंकि यह "फ्लास्क-सुरक्षा सुरक्षित व्यवस्थापक" Google खोज के लिए पहला परिणाम है, और अभी तक कोई आउट ऑफ़ द बॉक्स समाधान नहीं है, मुझे लगता है कि मैं योगदान कर सकता हूं।
फ्लास्क-एडमिन प्रोजेक्ट Issue List पर एक समान प्रश्न पूछा गया था और फ्लास्क-लॉगिन और मोगोडब का उपयोग करके एक साधारण उदाहरण here प्रदान किया गया है।
मैंने एसक्लाइट डेटाबेस और फ्लास्क-सुरक्षा के लिए एसक्यूएलकेमी का उपयोग करके एक उदाहरण बनाया। देखें नीचे दिए गए नमूना कुप्पी एप्लिकेशन:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import os.path as op
from flask import Flask, render_template, url_for, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.event import listens_for
from flask.ext.security import current_user, login_required, RoleMixin, Security, SQLAlchemyUserDatastore, UserMixin
from flask_admin import Admin, AdminIndexView
from flask_admin.contrib import sqla
# Create application
app = Flask(__name__)
# Create dummy secrety key so we can use sessions
app.config['SECRET_KEY'] = '123456790'
# Create in-memory database
app.config['DATABASE_FILE'] = 'sample_db.sqlite'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE']
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
# Create directory for file fields to use
file_path = op.join(op.dirname(__file__), 'static/files')
# flask-security models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
# Create Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
# Only needed on first execution to create first user
#@app.before_first_request
#def create_user():
# db.create_all()
# user_datastore.create_user(email='[email protected]', password='pass')
# db.session.commit()
class AnyModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(64))
def __unicode__(self):
return self.name
class MyAdminIndexView(AdminIndexView):
def is_accessible(self):
return current_user.is_authenticated() # This does the trick rendering the view only if the user is authenticated
# Create admin. In this block you pass your custom admin index view to your admin area
admin = Admin(app, 'Admin Area', template_mode='bootstrap3', index_view=MyAdminIndexView())
# Add views
admin.add_view(sqla.ModelView(AnyModel, db.session))
# To acess the logout just type the route /logout on browser. That redirects you to the index
@login_required
@app.route('/login')
def login():
return redirect('/admin')
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
# Build sample db on the fly, if one does not exist yet.
db.create_all()
app.run(debug=True)
कृपया how to customize the login page जानने के लिए कुप्पी के सुरक्षा दस्तावेज़ देखें।
उम्मीद है कि इससे मदद मिलती है।
फ्लास्क-सुरक्षा की प्रमाणीकरण तंत्र (फ्लास्क-लॉगिन के माध्यम से) 'current_user.is_authenticated()' के वापसी मान को नियंत्रित करता है। आपके 'is_accessible' कार्यान्वयन में यह संभवतः (किसी प्रकार की भूमिका/अनुमति जांच के साथ संयुक्त) को लौटने से आपको फ्लास्क-एडमिनिस्ट्रेशन के भीतर फ्लास्क-सुरक्षा की सुरक्षा का उपयोग करने की क्षमता मिलनी चाहिए। – jonafato