Flask principal 权限管理组件
发布日期:2021-08-26 09:44:27 浏览次数:3 分类:技术文章

本文共 3601 字,大约阅读时间需要 12 分钟。

hot3.png

# -*- coding: utf-8 -*-from flask import (    abort,    flash,    Flask,    g,    redirect,    render_template,    request,    session,    url_for)from flask_principal import (    ActionNeed,    AnonymousIdentity,    Identity,    identity_changed,    identity_loaded,    Permission,    Principal,    RoleNeed)app = Flask(__name__)app.config.update(    DEBUG=True,    SECRET_KEY='secret_xxx')principals = Principal(app, skip_static=True)# Needsbe_admin = RoleNeed('admin')be_editor = RoleNeed('editor')to_sign_in = ActionNeed('sign in')# Permissionsuser = Permission(to_sign_in)user.description = "User's permissions"editor = Permission(be_editor)editor.description = "Editor's permissions"admin = Permission(be_admin)admin.description = "Admin's permissions"apps_needs = [be_admin, be_editor, to_sign_in]apps_permissions = [user, editor, admin]def authenticate(email, password):    if password == email + "user":        return "the_only_user"    elif password == email + "admin":        return "the_only_admin"    elif password == email + "editor":        return "the_only_editor"    else:        return Nonedef current_privileges():    return (('{method} : {value}').format(method=n.method, value=n.value)            for n in apps_needs if n in g.identity.provides)@app.route('/')#@user.require(http_exception=403)def index():    return 'index'    #return render_template('index.html')@app.route('/login', methods=['GET', 'POST'])def login():    # user_id = authenticate(request.form['email'],    #                        request.form['password'])    user_id = authenticate(request.args['email'], request.args['password'])    print user_id    if user_id:        identity = Identity(user_id)        identity_changed.send(app, identity=identity)        return redirect(url_for('index'))    else:        return abort(401)    return 'login '    # return render_template('login.html')@app.route('/admin')@admin.require(http_exception=403)def admin():    return 'admin '    # return render_template('admin.html')@app.route('/edit')@editor.require(http_exception=403)def editor():    return 'editor '    # return render_template('editor.html')@app.route('/about')def about():    return 'about '    #return render_template('about.html')@app.route("/logout")def logout():    for key in ['identity.id', 'identity.auth_type']:        session.pop(key, None)    identity_changed.send(app, identity=AnonymousIdentity())    return 'logout '    # return render_template('logout.html')@app.errorhandler(401)def authentication_failed(e):    flash('Authenticated failed.')    print 'Authenticated failed.'    return redirect(url_for('login'))@app.errorhandler(403)def authorisation_failed(e):    flash(('Your current identity is {id}. You need special privileges to'           ' access this page').format(id=g.identity.id))    return render_template('privileges.html', priv=current_privileges())#当用户登陆的时候,对用户的identity进行判断@identity_loaded.connect_via(app)def on_identity_loaded(sender, identity):    needs = []    if identity.id in ('the_only_user', 'the_only_editor', 'the_only_admin'):        needs.append(to_sign_in)    if identity.id in ('the_only_editor', 'the_only_admin'):        needs.append(be_editor)    if identity.id == 'the_only_admin':        needs.append(be_admin)    for n in needs:        identity.provides.add(n)    # If the authenticated identity is :    # - 'the_only user' she can sign in    # - "the_only_editor" she can sign in and edit    # - "the_only_admin" she can sign in , edit and administrateif __name__ == "__main__":    app.run()

运行测试成功。

转载于:https://my.oschina.net/dacoolbaby/blog/417645

转载地址:https://blog.csdn.net/weixin_33881753/article/details/92445628 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:前嗅FireSpider教程:采集表格/列表页中的数据(翻页)
下一篇:Spark Sql 安装

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年03月25日 23时57分59秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

php tracy,admin.php 2019-04-21
php访问父类的所有属性,php – 在父类中使用$this仅在子类中显示父类属性 2019-04-21
oracle比较强大的函数,SQL和ORACLE函数比较 2019-04-21
oracle12c order by,oracle 数据库中order by 的一些高级用法 2019-04-21
oracle8i substr,Oracle中的INSTR,NVL和SUBSTR函数的用法详解 2019-04-21
导出oracle11g的空表,轻松解决oracle11g 空表不能 exp 导出 的问题。 2019-04-21
php把整数拆分成数组,数组拆分处理(整数时的处理),该怎么处理 2019-04-21
oracle numlist,oracle sql str2numlist numtabletype 2019-04-21
php红包平均分配,红包平均分配算法 2019-04-21
linux磁盘的命令是,linux磁盘相关的命令 2019-04-21
linux 停用用户,linux – 如何禁用用户的网络访问? 2019-04-21
linux服务器 缓存,Linux服务器内存使用分析及内存缓存 2019-04-21
linux查进程内存问题,关于linux 查看服务进程内存,cpu,内存占用的一些基础命令... 2019-04-21
linux英文包安装教程视频,Linux源码包安装过程讲解 2019-04-21
linux 关闭rsync服务器,linux下配置rsync服务器和实时同步 2019-04-21
linux初始化TCP服务失败,深入Linux系统追踪TCP初始化 2019-04-21
arch Linux添加源,在Arch Linux系统中使用Archlinuxcn源(清华源)的方法 2019-04-21
私人linux远程连接,Linux远程连接 - osc_5g1gl9wp的个人空间 - OSCHINA - 中文开源技术交流社区... 2019-04-21
windows文件迁移到linux,从Windows到Linux迁移之文件服务器(Samba和AD完美结合) 2019-04-21
linux下vi编辑器的命令大全,linux下VI编辑器命令大全(超级完整版) 2019-04-21