Пример #1
0
def role_store():
    form = RoleCreateForm(request.form)
    if form.validate():
        role = Role()
        role.create(data=form.data)
        return Success(message="操作成功!")
    return Fail(message=form.first_error)
Пример #2
0
 def init_role(resource: Resource):
     """
     Create roles and associate it with a resource
     :param resource: Resource object
     :return: Edit Role for a resource
     """
     role: Role = Role.create(f'Read {resource.name}')
     Setup.init_role_resource(role, resource, ActionType.READ)
     role: Role = Role.create(f'Edit {resource.name}')
     Setup.init_role_resource(role, resource, ActionType.WRITE)
     Setup.init_role_resource(role, resource, ActionType.DELETE)
     return role
Пример #3
0
 def post(self, *args, **kwargs):
     arg_dict = self.prepare_arg_dict()
     name = self.get_argument('name')
     pinyin_name = name_to_pinyin(name)
     arg_dict['pinyin_name'] = pinyin_name
     arg_dict['avatar'] = upload_image(self.request,
                                       'avatar',
                                       prefix='role/avatar/',
                                       default='role/avatar/default.jpg')
     arg_dict['imagery'] = upload_image(self.request,
                                        'imagery',
                                        prefix='role/imagery/',
                                        default='role/imagery/default.jpg')
     arg_dict['back_img'] = upload_image(self.request,
                                         'back',
                                         prefix='role/back/',
                                         default='role/back/default.jpg')
     arg_dict['banner'] = upload_image(self.request,
                                       'banner',
                                       prefix='role/banner/',
                                       default='role/banner/default.jpg')
     arg_dict['full_imagery'] = upload_image(self.request,
                                             'full_imagery',
                                             prefix='role/full_imagery/')
     arg_dict['create_time'] = current_time_string()
     arg_dict['update_time'] = current_time_string()
     arg_dict['profession'] = ''
     try:
         yield Role.create(arg_dict)
         self.write({'status': 1})
     except Exception, e:
         print e
         self.write({'status': 0})
Пример #4
0
 def create(self, params: dict) -> RoleModel:
     data = self._fill(params, exclude=[])
     return RoleModel.create(**data)
Пример #5
0
def role_save():
    """保存角色"""
    g.title = u'保存角色'
    g.page_type = ''
    form = request.form

    desc               = form.get('desc', '')
    role_name          = form.get('role_name', '')
    role_id            = toint(form.get('role_id', '0'))
    permission_id_list = form.getlist('permission_id')
    permission_id_list = map(lambda permission_id:toint(permission_id), permission_id_list) # 现在获取的权限id列表
    errmsg = {}

    required_param_list = ['desc', 'role_name']
    for param in required_param_list:
        val = form.get(param, '')
        val = val.strip()
        if not val:
            errmsg[param] = u'必填项'

    if errmsg:
        g.errmsg = errmsg
        log_debug('errmsg:%s'%g.errmsg)
        return render_template('power/role_admin.html.j2', f=form,role_id=role_id)

    # 之前的权限id列表
    role_permission_id_list = []
    if role_id > 0:
        role = Role.query.get(role_id)
        role_permission_id_list = db.session.query(RolePermission.permission_id).\
                            filter(RolePermission.menu_type == 0).\
                            filter(RolePermission.role_id == role.role_id).all()
        # 角色权限id列表
        role_permission_id_list = map(lambda role_permission_id:toint(role_permission_id.permission_id), role_permission_id_list)
    else:
        role = Role.create(commit=True)

    if role.role_name == role_name and role.role_id != role_id:
        errmsg['role_name'] = u'角色名称不能重复'
        g.errmsg = errmsg
        log_debug('errmsg:%s'%g.errmsg)
        return render_template('power/role_admin.html.j2', f=form,role_id=role_id)

    # permission_id_list现在获取的权限id列表
    parent_p = None
    for permission_id in permission_id_list:
        p = Permission.query.get(permission_id)
        if not p:
            continue
        if p.parent_id > 0:
            parent_p = Permission.query.filter(Permission.permission_id == p.parent_id).first()

        q = RolePermission.query.filter(RolePermission.role_id == role.role_id)
        # 处理一级菜单的权限
        if parent_p:
            p_rp = q.filter(RolePermission.permission_id == parent_p.permission_id).first()

        if not p_rp:
            # 获取权限表permission_id为0的数据
            parent_rp_0 = RolePermission.query.filter(RolePermission.permission_id == 0).first()
            if parent_rp_0:
                p_rp = parent_rp_0.update(permission_id=parent_p.permission_id, role_id=role.role_id, menu_type=1)
            else:
                p_rp = RolePermission.create(permission_id=parent_p.permission_id, role_id=role.role_id, menu_type=1, endpoint_list='')

        # 处理二级菜单的权限
        rp = q.filter(RolePermission.permission_id == permission_id).first()
        if rp:
            continue

        # 获取权限表permission_id为0的数据
        rp_0 = RolePermission.query.filter(RolePermission.permission_id == 0).first()

        if not rp_0:
            rp_0 = RolePermission.create(endpoint_list='')
        rp_0.update(permission_id=permission_id, role_id=role.role_id)

    # 取消的权限id列表
    cancel_permission_id_list = [permission_id for permission_id in role_permission_id_list if permission_id not in permission_id_list]
    for permission_id in cancel_permission_id_list:
        p = Permission.query.get(permission_id)
        if not p:
            continue

        # 如果没有权限则去掉主菜单
        if p.parent_id > 0:
            parent_p = Permission.query.filter(Permission.permission_id == p.parent_id).first()
            parent_p_list = Permission.query.filter(Permission.parent_id == parent_p.permission_id).all()
            parent_p_id_list = [ parent_p.permission_id for parent_p in parent_p_list ]
            rp_list = RolePermission.query.filter(RolePermission.permission_id.in_(parent_p_id_list)).all()
            if len(rp_list) <= 1:
                parent_rp = RolePermission.query.filter(RolePermission.permission_id == p.parent_id).first()
                if parent_rp:
                    parent_rp.update(permission_id=0, role_id=0)

        rp = RolePermission.query.filter(RolePermission.role_id == role.role_id).\
                        filter(RolePermission.permission_id == permission_id).first()

        if rp:
            # 如果角色权限id列表没有在from表单获取的permission_id列表里,就把permission_id更新为0
            rp.update(permission_id=0, role_id=0)

    role = role.update(desc=desc, role_name=role_name, commit=True)

    return redirect(url_for('power.role_admin'))