BBS论坛(十)
10.1.客户端权限验证功能完成
(1)cms/cms_profile
显示当前用户的角色和权限
<tr> <td>角色:</td> <td> {% for role in user.roles %} {{ role.name }} {% if not loop.last %},{% endif %} {% endfor %} </td> </tr> <tr> <td>权限:</td> <td> {% for role in user.roles %} {{ role.desc }} {% if not loop.last %}/{% endif %} {% endfor %} </td> </tr>
(2)cms/hooks.py
@bp.context_processor def cms_context_pocessor(): return {'CMSPermission':CMSPermission}
(3)cms/cms_base.html
不同的角色,显示不同的内容
{% set cms_user = g.cms_user %} {% if cms_user.has_permission(CMSPermission.POSTER) %} <li class="nav-group post-manage"><a href="#">帖子管理</a></li> {% endif %} {% if cms_user.has_permission(CMSPermission.COMMENTER) %} <li class="comments-manage"><a href="#">评论管理</a></li> {% endif %} {% if cms_user.has_permission(CMSPermission.BOARDER) %} <li class="board-manage"><a href="#">板块管理</a></li> {% endif %} {% if cms_user.has_permission(CMSPermission.FRONTUSER) %} <li class="nav-group user-manage"><a href="#">前台用户管理</a></li> {% endif %} {% if cms_user.has_permission(CMSPermission.CMSUSER) %} <li class="nav-group cmsuser-manage"><a href="#">CMS用户管理</a></li> {% endif %} {% if cms_user.is_developer %} <li class="cmsrole-manage"><a href="#">CMS组管理</a></li> {% endif %}
(4)添加用户和角色
#添加访问者 python manage.py create_cms_user -u 我是访问者 -p 123456 -e 11111@qq.com python manage.py add_user_to_role -e 11111@qq.com -n 访问者 #添加运营者 python manage.py create_cms_user -u 我是运营者 -p 123456 -e 22222@qq.com python manage.py add_user_to_role -e 22222@qq.com -n 运营 #添加管理员 python manage.py create_cms_user -u 我是管理员 -p 123456 -e 33333@qq.com python manage.py add_user_to_role -e 33333@qq.com -n 管理员
不同的用户登录到后台cms显示不同的内容
开发者
访问者
10.2.服务端权限验证功能完成
(1)后台剩余的html页面
cms_posts.html
{% extends 'cms/cms_base.html' %}
{% block title %}
帖子管理
{% endblock %}
{% block head %}
{% endblock %}
{% block page_title %}
{{ self.title() }}
{% endblock %}
{% block main_content %}
帖子管理页面
{% endblock %}
其它几个一样
cms_comments.html
cms_boards.html
cms_fusers.html
cms_cusers.html
cms_croles.html
(2)cms/decorators.py
添加一个权限验证的装饰器
def permission_required(permission): def outter(func): @wraps(func) def inner(*args,**kwargs): user = g.cms_user if user.has_permission(permission): return func(*args,**kwargs) else: return redirect(url_for('cms.index')) return inner return outter
(3)cms/views.py
@bp.route('/posts/') @login_required @permission_required(CMSPermission.POSTER) def posts(): return render_template('cms/cms_posts.html') @bp.route('/comments/') @login_required @permission_required(CMSPermission.COMMENTER) def comments(): return render_template('cms/cms_comments.html') @bp.route('/boards/') @login_required @permission_required(CMSPermission.BOARDER) def boards(): return render_template('cms/cms_boards.html') @bp.route('/fusers/') @login_required @permission_required(CMSPermission.FRONTUSER) def fusers(): return render_template('cms/cms_fusers.html') @bp.route('/cusers/') @login_required @permission_required(CMSPermission.CMSUSER) def cusers(): return render_template('cms/cms_cusers.html') @bp.route('/croles/') @login_required @permission_required(CMSPermission.ALL_PERMISSION) def croles(): return render_template('cms/cms_croles.html')
(4)cms/cms_base.html
修改url链接
{% set cms_user = g.cms_user %} {% if cms_user.has_permission(CMSPermission.POSTER) %} <li class="nav-group post-manage"><a href="{{ url_for('cms.posts') }}">帖子管理</a></li> {% endif %} {% if cms_user.has_permission(CMSPermission.COMMENTER) %} <li class="comments-manage"><a href="{{ url_for('cms.comments') }}">评论管理</a></li> {% endif %} {% if cms_user.has_permission(CMSPermission.BOARDER) %} <li class="board-manage"><a href="{{ url_for('cms.boards') }}">板块管理</a></li> {% endif %} {% if cms_user.has_permission(CMSPermission.FRONTUSER) %} <li class="nav-group user-manage"><a href="{{ url_for('cms.fusers') }}">前台用户管理</a></li> {% endif %} {% if cms_user.has_permission(CMSPermission.CMSUSER) %} <li class="nav-group cmsuser-manage"><a href="{{ url_for('cms.cusers') }}">CMS用户管理</a></li> {% endif %} {% if cms_user.is_developer %} <li class="cmsrole-manage"><a href="{{ url_for('cms.croles') }}">CMS组管理</a></li> {% endif %}
posted on 2018-06-06 23:10 zhang_derek 阅读(585) 评论(0) 编辑 收藏 举报