如何将 flask-login 添加到 flask-admin
how to add flask-login to flask-admin
登录在我的标准视图上运行良好,管理员运行良好,但我似乎无法将登录添加到我的 admin/yikes!!!
看起来很简单flask admin docs但是当我添加这个部分时
class MyView(BaseView):
def is_accessible(self):
return login.current_user.is_authenticated()
def _handle_view(self, name, **kwargs):
if not self.is_accessible():
return redirect(url_for('login', next=request.url))
我没有被发送到未经身份验证的用户的登录页面。
知道我还需要做什么吗?
这在我的 init 文件中看起来正确吗?
from flask.ext.admin import BaseView
class MyView(BaseView):
def is_accessible(self):
return login.current_user.is_authenticated()
def _handle_view(self, name, **kwargs):
if not self.is_accessible():
return redirect(url_for('login', next=request.url))
app = Flask(__name__)
app.config.from_object('config')
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
Bootstrap(app)
from app import views, models
admin = Admin(app)
admin.add_view(ModelView(models.User, db.session))
admin.add_view(ModelView(models.rutable, db.session))
admin.add_view(ModelView(models.rustage, db.session))
admin.add_view(ModelView(models.TOC, db.session))
admin.add_view(ModelView(models.Request, db.session))
admin.add_view(ModelView(models.Staff, db.session))
admin.add_view(ModelView(models.Status, db.session))
admin.add_view(ModelView(models.Challenge, db.session))
这是我连接到 ldap auth 的登录视图
from flask.ext.admin import BaseView
def logged_in(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get('logged_in') is not None:
return f(*args, **kwargs)
else:
flash('Please log in first...', 'error')
next_url = request.url
login_url = '%s?next=%s' % (url_for('login'), next_url)
return redirect(login_url)
return decorated_function
@login_manager.unauthorized_handler
def unauthorized():
print 'unauthorized'
flash("You must be logged in.")
return redirect(url_for("login"))
@login_manager.user_loader
def user_loader(user_id):
"""Given *user_id*, return the associated User object.
:param unicode user_id: user_id (email) user to retrieve
"""
g.user=current_user
return models.User.query.get(user_id)
@app.route("/logout")
# @logged_in
def logout():
logout_user()
session.pop('logged_in', None)
flash("Logged Out.")
return redirect(url_for("login"))
@app.before_request
def before_request():
g.user = current_user
@app.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
if app.config['ENVIRONMENT']=='dev':
try:
print "Authentification Successful"
namedb=models.User.query.filter_by(name=unicode(form.username.data)).first()
email=models.User.query.first().email
login_user(user_loader(unicode(email)),remember=form.remember_me.data)
flash("Logged in successfully.")
g.email=email
session['logged_in'] = True
return redirect( request.values.get('next') or url_for("main"))
except Exception as e:
flash("Invalid Credentials.")
return render_template("login.html", form=form)
else:
try:
if '@' in form.username.data:
form.username.data=re.sub(' /d+','', (re.sub("\d+",'', form.username.data.split('@')[0]))[1:]+(re.sub("\d+",'', form.username.data.split('@')[0]))[0:1])
l = ldap.initialize("ldap://10.129.18.101")
l.simple_bind_s("program\%s" % form.username.data,form.password.data)
print "Authentification Successful"
r=l.search_s('cn=Users,dc=BHCS,dc=Internal',ldap.SCOPE_SUBTREE,'(sAMAccountName=*%s*)' % form.username.data,['mail','objectGUID','displayName'])
email=r[0][1]['mail'][0]
GUID=r[0][1]['objectGUID'][0]
FullName=r[0][1]['displayName'][0]
import uuid
guid = uuid.UUID(bytes=GUID)
if not models.User.query.filter_by(email=unicode(email)).first():
p=models.User(name=FullName,email=email)
db.session.add(p)
db.session.commit()
login_user(user_loader(unicode(email)),remember=form.remember_me.data)
flash("Logged in successfully.")
g.email=email
session['logged_in'] = True
return redirect( request.values.get('next') or url_for("main"))
except Exception as e:
flash("Invalid Credentials.")
return render_template("login.html", form=form)
return render_template("login.html", form=form)
根据要求,这是我的用户 class 来自我的模型
class User(db.Model):
"""An admin user capable of viewing reports.
:param str email: email address of user
:param str password: encrypted password for the user
"""
__tablename__ = 'user'
name=db.Column(db.String)
email = db.Column(db.String, primary_key=True)
authenticated = db.Column(db.Boolean, default=True)
admin = db.Column(db.Boolean, default=False)
def is_active(self):
"""True, as all users are active."""
return True
def get_id(self):
"""Return the email address to satisfy Flask-Login's requirements."""
return self.email
def is_authenticated(self):
"""Return True if the user is authenticated."""
return self.authenticated
def is_anonymous(self):
"""False, as anonymous users aren't supported."""
return False
经过大量的反复试验(更多错误),我终于从示例(部分理解)中复制了足够的内容以使其正常工作。看来我需要向 MyAdminIndexView() 添加一个 super 才能使其正常工作。
那是我将我的“MyView”函数移出了 init 并添加了这一行
admin = Admin(app, 'example',index_view=views.MyAdminIndexView())
根据示例显着改变了我的观点
class MyModelView(sqla.ModelView):
def is_accessible(self):
return g.user.is_authenticated()
class MyAdminIndexView(admin.AdminIndexView):
@expose('/')
def index(self):
if not g.user.is_authenticated():
flash('Please log in first...', 'error')
next_url = request.url
login_url = '%s?next=%s' % (url_for('login'), next_url)
return redirect(login_url)
# import pdb;pdb.set_trace()
if g.user.admin == True:
return super(MyAdminIndexView,self).index()
else:
return redirect(url_for("main"))
def logged_in(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get('logged_in') is not None:
return f(*args, **kwargs)
else:
flash('Please log in first...', 'error')
next_url = request.url
login_url = '%s?next=%s' % (url_for('login'), next_url)
return redirect(login_url)
return decorated_function
@login_manager.unauthorized_handler
def unauthorized():
print 'unauthorized'
flash("You must be logged in.")
return redirect(url_for("login"))
@login_manager.user_loader
def user_loader(user_id):
"""Given *user_id*, return the associated User object.
:param unicode user_id: user_id (email) user to retrieve
"""
g.user=current_user
return models.User.query.get(user_id)
@app.route("/logout")
# @logged_in
def logout():
logout_user()
session.pop('logged_in', None)
flash("Logged Out.")
return redirect(url_for("login"))
@app.before_request
def before_request():
g.user = current_user
登录在我的标准视图上运行良好,管理员运行良好,但我似乎无法将登录添加到我的 admin/yikes!!!
看起来很简单flask admin docs但是当我添加这个部分时
class MyView(BaseView):
def is_accessible(self):
return login.current_user.is_authenticated()
def _handle_view(self, name, **kwargs):
if not self.is_accessible():
return redirect(url_for('login', next=request.url))
我没有被发送到未经身份验证的用户的登录页面。
知道我还需要做什么吗?
这在我的 init 文件中看起来正确吗?
from flask.ext.admin import BaseView
class MyView(BaseView):
def is_accessible(self):
return login.current_user.is_authenticated()
def _handle_view(self, name, **kwargs):
if not self.is_accessible():
return redirect(url_for('login', next=request.url))
app = Flask(__name__)
app.config.from_object('config')
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
Bootstrap(app)
from app import views, models
admin = Admin(app)
admin.add_view(ModelView(models.User, db.session))
admin.add_view(ModelView(models.rutable, db.session))
admin.add_view(ModelView(models.rustage, db.session))
admin.add_view(ModelView(models.TOC, db.session))
admin.add_view(ModelView(models.Request, db.session))
admin.add_view(ModelView(models.Staff, db.session))
admin.add_view(ModelView(models.Status, db.session))
admin.add_view(ModelView(models.Challenge, db.session))
这是我连接到 ldap auth 的登录视图
from flask.ext.admin import BaseView
def logged_in(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get('logged_in') is not None:
return f(*args, **kwargs)
else:
flash('Please log in first...', 'error')
next_url = request.url
login_url = '%s?next=%s' % (url_for('login'), next_url)
return redirect(login_url)
return decorated_function
@login_manager.unauthorized_handler
def unauthorized():
print 'unauthorized'
flash("You must be logged in.")
return redirect(url_for("login"))
@login_manager.user_loader
def user_loader(user_id):
"""Given *user_id*, return the associated User object.
:param unicode user_id: user_id (email) user to retrieve
"""
g.user=current_user
return models.User.query.get(user_id)
@app.route("/logout")
# @logged_in
def logout():
logout_user()
session.pop('logged_in', None)
flash("Logged Out.")
return redirect(url_for("login"))
@app.before_request
def before_request():
g.user = current_user
@app.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
if app.config['ENVIRONMENT']=='dev':
try:
print "Authentification Successful"
namedb=models.User.query.filter_by(name=unicode(form.username.data)).first()
email=models.User.query.first().email
login_user(user_loader(unicode(email)),remember=form.remember_me.data)
flash("Logged in successfully.")
g.email=email
session['logged_in'] = True
return redirect( request.values.get('next') or url_for("main"))
except Exception as e:
flash("Invalid Credentials.")
return render_template("login.html", form=form)
else:
try:
if '@' in form.username.data:
form.username.data=re.sub(' /d+','', (re.sub("\d+",'', form.username.data.split('@')[0]))[1:]+(re.sub("\d+",'', form.username.data.split('@')[0]))[0:1])
l = ldap.initialize("ldap://10.129.18.101")
l.simple_bind_s("program\%s" % form.username.data,form.password.data)
print "Authentification Successful"
r=l.search_s('cn=Users,dc=BHCS,dc=Internal',ldap.SCOPE_SUBTREE,'(sAMAccountName=*%s*)' % form.username.data,['mail','objectGUID','displayName'])
email=r[0][1]['mail'][0]
GUID=r[0][1]['objectGUID'][0]
FullName=r[0][1]['displayName'][0]
import uuid
guid = uuid.UUID(bytes=GUID)
if not models.User.query.filter_by(email=unicode(email)).first():
p=models.User(name=FullName,email=email)
db.session.add(p)
db.session.commit()
login_user(user_loader(unicode(email)),remember=form.remember_me.data)
flash("Logged in successfully.")
g.email=email
session['logged_in'] = True
return redirect( request.values.get('next') or url_for("main"))
except Exception as e:
flash("Invalid Credentials.")
return render_template("login.html", form=form)
return render_template("login.html", form=form)
根据要求,这是我的用户 class 来自我的模型
class User(db.Model):
"""An admin user capable of viewing reports.
:param str email: email address of user
:param str password: encrypted password for the user
"""
__tablename__ = 'user'
name=db.Column(db.String)
email = db.Column(db.String, primary_key=True)
authenticated = db.Column(db.Boolean, default=True)
admin = db.Column(db.Boolean, default=False)
def is_active(self):
"""True, as all users are active."""
return True
def get_id(self):
"""Return the email address to satisfy Flask-Login's requirements."""
return self.email
def is_authenticated(self):
"""Return True if the user is authenticated."""
return self.authenticated
def is_anonymous(self):
"""False, as anonymous users aren't supported."""
return False
经过大量的反复试验(更多错误),我终于从示例(部分理解)中复制了足够的内容以使其正常工作。看来我需要向 MyAdminIndexView() 添加一个 super 才能使其正常工作。
那是我将我的“MyView”函数移出了 init 并添加了这一行
admin = Admin(app, 'example',index_view=views.MyAdminIndexView())
根据示例显着改变了我的观点
class MyModelView(sqla.ModelView):
def is_accessible(self):
return g.user.is_authenticated()
class MyAdminIndexView(admin.AdminIndexView):
@expose('/')
def index(self):
if not g.user.is_authenticated():
flash('Please log in first...', 'error')
next_url = request.url
login_url = '%s?next=%s' % (url_for('login'), next_url)
return redirect(login_url)
# import pdb;pdb.set_trace()
if g.user.admin == True:
return super(MyAdminIndexView,self).index()
else:
return redirect(url_for("main"))
def logged_in(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get('logged_in') is not None:
return f(*args, **kwargs)
else:
flash('Please log in first...', 'error')
next_url = request.url
login_url = '%s?next=%s' % (url_for('login'), next_url)
return redirect(login_url)
return decorated_function
@login_manager.unauthorized_handler
def unauthorized():
print 'unauthorized'
flash("You must be logged in.")
return redirect(url_for("login"))
@login_manager.user_loader
def user_loader(user_id):
"""Given *user_id*, return the associated User object.
:param unicode user_id: user_id (email) user to retrieve
"""
g.user=current_user
return models.User.query.get(user_id)
@app.route("/logout")
# @logged_in
def logout():
logout_user()
session.pop('logged_in', None)
flash("Logged Out.")
return redirect(url_for("login"))
@app.before_request
def before_request():
g.user = current_user