def group_roles():
        """
        用户组拥有角色列表
        :return:
        """
        params = request.values.get('params')
        conditions = json.loads(params)
        group_id = conditions['group_id']
        role_ids = PermissionsRelService.ids_in_groupids(
            model=TPermissionsRoleGroupRel(),
            gids=[group_id],
            select_column_name='rid')
        if not role_ids:
            data = {"rows": [], "total": 0}
            return make_response(
                json.dumps(ResponseCode.response(data=data),
                           ensure_ascii=False))
        conditions['group_id'] = None
        conditions['id'] = role_ids
        conditions['rule']['id'] = Constants.QUERY_CONDITION_RULE_IN
        model = TPermissionsRole()
        rows = CRUDHelper.execute_select(model, conditions)
        total = CRUDHelper.execute_select_count(model, conditions)

        data = {"rows": rows, "total": total}
        return make_response(
            json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
Esempio n. 2
0
    def get_user_role():
        """
        获取当前用户可以关联的角色
        :return: 
        """
        params = request.values.get("params")
        params_data = json.loads(params)
        user_id = params_data['user_id']
        conditions = params_data['condition']

        data = {"total": 0, "rows": []}
        # 根据用户id获取所在的用户组
        group_ids = PermissionsRelService.ids_by_userid(
            TPermissionsUserGroupRel(), user_id, 'gid')
        if not group_ids or len(group_ids) < 1:
            return make_response(
                json.dumps(ResponseCode.response(data=data),
                           ensure_ascii=False))
        # 根据用户组获取角色id
        role_ids = PermissionsRelService.ids_in_groupids(
            TPermissionsRoleGroupRel(), group_ids, "rid")
        if not role_ids or len(role_ids) < 1:
            return make_response(
                json.dumps(ResponseCode.response(data=data),
                           ensure_ascii=False))
        # 条件查找
        conditions['id'] = role_ids
        conditions['rule']['id'] = Constants.QUERY_CONDITION_RULE_IN
        model = TPermissionsRole()
        rows = CRUDHelper.execute_select(model, conditions)
        total = CRUDHelper.execute_select_count(model, conditions)
        data['rows'] = rows
        data['total'] = total
        return make_response(
            json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
Esempio n. 3
0
 def del_in_uids(model, uids):
     """
     删除给定的用户id列表相关的关联数据
     :param model: 表模型
     :param uids: 用户ids
     :return: void
     """
     # 用户和角色的关联关系
     # 用户和用户组的关联关系
     CRUDHelper.execute_delete_by_field_in(model, uids, field_name='uid')
Esempio n. 4
0
 def del_in_groupids(model, groupids):
     """
     删除给定的用户组id列表相关的关联数据
     :param model: 表模型
     :param groupids: 用户组ids
     :return: void
     """
     # 用户组和角色的关联关系
     # 用户和用户组的关联关系
     # 用户组和资源的关联关系
     CRUDHelper.execute_delete_by_field_in(model, groupids, field_name='gid')
 def delete_resource():
     """
     删除权限资源
     :return:
     """
     params_data = json.loads(request.args.get('ids'))
     if len(params_data) > 0:
         # 删除资源和用户组的关联关系
         PermissionsRelService.del_in_resids(TPermissionsResGroupRel(), params_data)
         # 删除角色和资源的关联关系
         PermissionsRelService.del_in_resids(TPermissionsResRoleRel(), params_data)
         CRUDHelper.execute_delete_by_field_in(TPermissionsResource(), params_data, 'id')
     return make_response(json.dumps(ResponseCode.response(), ensure_ascii=False))
Esempio n. 6
0
 def del_in_resids(model, rids):
     """
     删除给定的资源id列表相关的关联数据
     :param model: 表模型
     :param rids: 资源ids
     :return:
     """
     id_name = 'rid'
     if isinstance(model, TPermissionsResRoleRel):
         id_name = 'res_id'
     # 资源和用户组的关联关系
     # 资源和角色的关联关系
     CRUDHelper.execute_delete_by_field_in(model, rids, field_name=id_name)
Esempio n. 7
0
 def del_in_roleids(model, rids):
     """
     删除给定的角色id列表相关的关联数据
     :param model: 表模型
     :param rids: 角色ids
     :return: void
     """
     role_id_name = 'rid'
     if isinstance(model, TPermissionsResRoleRel):
         role_id_name = 'role_id'
     # 角色和用户的关联关系
     # 角色和资源的关联关系
     # 角色和用户组的关联关系
     CRUDHelper.execute_delete_by_field_in(model, rids, field_name=role_id_name)
Esempio n. 8
0
    def get_example():
        """
        example 查询
        :return: json
        """
        params = request.values.get('params')
        conditions = json.loads(params)
        example_model = TExample()

        rows = CRUDHelper.execute_select(example_model, conditions)
        total = CRUDHelper.execute_select_count(example_model, conditions)
        data = {"total": total, "rows": rows}
        return make_response(
            json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
Esempio n. 9
0
 def get_user():
     """
     获取用户列表
     :return:
     """
     params = request.values.get('params')
     conditions = json.loads(params)
     model = TPermissionsUser()
     rows = CRUDHelper.execute_select(model, conditions)
     total = CRUDHelper.execute_select_count(model, conditions)
     DbHelper.get_db().session.close()
     data = {"total": total, "rows": rows}
     return make_response(
         json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
Esempio n. 10
0
 def get_role_resource():
     """
     根据角色获取资源
     可以关联的资源    已经关联的资源
     :return:
     """
     params = request.values.get('params')
     params_data = json.loads(params)
     role_id = params_data['role_id']
     if not role_id:
         raise Exception('param role_id is blank')
     response_data = {'all_can_rel_rows': [], 'aleary_rel_rows': []}
     group_ids = PermissionsRelService.ids_in_roleids(
         TPermissionsRoleGroupRel(), [role_id], 'gid')
     if not group_ids or len(group_ids) < 1:
         return make_response(
             json.dumps(ResponseCode.response(data=response_data),
                        ensure_ascii=False))
     all_can_rel_resource_ids = PermissionsRelService.ids_in_groupids(
         TPermissionsResGroupRel(), group_ids, 'rid')
     if not all_can_rel_resource_ids or len(all_can_rel_resource_ids) < 1:
         return make_response(
             json.dumps(ResponseCode.response(data=response_data),
                        ensure_ascii=False))
     all_aleary_rel_res_ids = PermissionsRelService.ids_in_roleids(
         TPermissionsResRoleRel(), [role_id], 'res_id')
     response_data['all_can_rel_rows'] = PermissionsResourceService. \
         gen_resource_tree(CRUDHelper.execute_select(TPermissionsResource(), {'id': all_can_rel_resource_ids, 'rule': {
          'id': Constants.QUERY_CONDITION_RULE_IN}}))
     response_data['aleary_rel_rows'] = all_aleary_rel_res_ids
     return make_response(
         json.dumps(ResponseCode.response(data=response_data),
                    ensure_ascii=False))
Esempio n. 11
0
 def delete_user():
     """
     删除用户
     :return:
     """
     params_data = json.loads(request.args.get('ids'))
     if not params_data or len(params_data) < 1:
         raise Exception("参数 ids is blank")
     # 删除用户和用户组的关联关系
     PermissionsRelService.del_in_uids(TPermissionsUserGroupRel(),
                                       params_data)
     # 删除用户和角色的关联关系
     PermissionsRelService.del_in_uids(TPermissionsUserRoleRel(),
                                       params_data)
     CRUDHelper.execute_delete_by_field_in(TPermissionsUser(), params_data,
                                           'id')
     return make_response(
         json.dumps(ResponseCode.response(), ensure_ascii=False))
 def get_resource():
     """
     获取权限资源列表
     :return:
     """
     model = TPermissionsResource()
     rows = CRUDHelper.execute_select(model, {})
     data = {"rows": PermissionsResourceService.gen_resource_tree(rows)}
     return make_response(json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
 def resource_check_uniq():
     """
     检查唯一性约束
     :return: 结果对象
     """
     params = request.values.get('params')
     conditions = json.loads(params)
     model = TPermissionsResource()
     rows = CRUDHelper.execute_select(model=model, conditions=conditions)
     return make_response(json.dumps(ResponseCode.response(data=rows), ensure_ascii=False))
 def get_group():
     """
     获取用户组列表
     :return:
     """
     model = TPermissionsGroup()
     rows = CRUDHelper.execute_select(model, {})
     DbHelper.get_db().session.close()
     data = {"rows": PermissionsGroupService.gen_group_tree(rows)}
     return make_response(
         json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
 def delete_group_resources():
     """
     删除用户组和资源的关联关系
     :return:
     """
     params_data = json.loads(request.args.get('params'))
     if params_data['gid'] == Constants.ADMIN_ID:
         """
         过滤系统菜单关联关系
         """
         sys_rel = [1, 2, 3, 4, 5, 6]
         for i, v in enumerate(sys_rel):
             if params_data['rid'] == str(v):
                 return make_response(
                     json.dumps(ResponseCode.response(),
                                ensure_ascii=False))
     model = TPermissionsResGroupRel()
     CRUDHelper.execute_delete_by_conditions(model, params_data)
     return make_response(
         json.dumps(ResponseCode.response(), ensure_ascii=False))
 def delete_group():
     """
     删除用户组
     :return:
     """
     params_data = json.loads(request.args.get('ids'))
     if len(params_data) > 0:
         # 删除用户和用户组的关联关系
         PermissionsRelService.del_in_groupids(TPermissionsUserGroupRel(),
                                               params_data)
         # 删除角色和用户组的关联关系
         PermissionsRelService.del_in_groupids(TPermissionsRoleGroupRel(),
                                               params_data)
         # 删除资源和用户组的关联关系
         PermissionsRelService.del_in_groupids(TPermissionsResGroupRel(),
                                               params_data)
         CRUDHelper.execute_upd(TPermissionsGroup(),
                                {'valid_status': Constants.VALID_STATUS_N},
                                params_data)
     return make_response(
         json.dumps(ResponseCode.response(), ensure_ascii=False))
Esempio n. 17
0
 def ids_by_userid(model, uid, select_column_name, valid_status=Constants.VALID_STATUS_Y):
     """
     通过用户id获取角色、组id列表
     :param select_column_name:
     :param valid_status: 数据有效状态
     :param uid: 用户id
     :param model: 表模型
     :return: 角色、组id列表
     """
     conditions = {"uid": uid, "valid_status": valid_status}
     rows = CRUDHelper.execute_select(model, conditions)
     return PermissionsRelService.__filter_ids(rows, select_column_name)
Esempio n. 18
0
 def ids_in_groupids(model, gids, select_column_name, valid_status=Constants.VALID_STATUS_Y):
     """
     通过用户组ids查资源/角色id列表
     :param select_column_name:
     :param valid_status: 数据有效状态
     :param model: 表模型
     :param gids: 组id列表
     :return: 资源/角色id列表
     """
     conditions = {"gid": gids, "valid_status": valid_status, "rule": {"gid": "in"}}
     rows = CRUDHelper.execute_select(model, conditions)
     return PermissionsRelService.__filter_ids(rows, select_column_name)
Esempio n. 19
0
 def ids_in_roleids(model, rids, select_column_name, valid_status=Constants.VALID_STATUS_Y):
     """
     通过角色ids获取资源、组id列表
     :param select_column_name:
     :param valid_status: 数据有效状态
     :param rids: 角色ids
     :param model: 表模型
     :return: 资源、组id列表
     """
     role_id_name = 'rid'
     if isinstance(model, TPermissionsResRoleRel):
         role_id_name = 'role_id'
     conditions = {role_id_name: rids, "valid_status": valid_status, "rule": {role_id_name: "in"}}
     rows = CRUDHelper.execute_select(model, conditions)
     return PermissionsRelService.__filter_ids(rows, select_column_name)
    def resources_by_user(uid, res_type, pid=Constants.ROOT_PID, valid_status=Constants.VALID_STATUS_Y):
        """
        通过用户id、用户类型、资源类型查资源列表(导航菜单,前台uri,后台uri)
        :param valid_status:
        :param pid: 父级id
        :param res_type: 资源类型
        :param uid: 用户id
        :return:
        """
        # 根据用户查所属的用户组
        group_ids = PermissionsRelService.ids_by_userid(TPermissionsUserGroupRel(), uid, "gid")
        if not group_ids or len(group_ids) < 1:
            return []
        # 获取用户所在的所有用户组的权限数据
        group_res_ids = PermissionsRelService.ids_in_groupids(TPermissionsResGroupRel(), group_ids, "rid")
        if not group_res_ids or len(group_res_ids) < 1:
            return []
        # 根据用户查角色ids
        role_ids = PermissionsRelService.ids_by_userid(model=TPermissionsUserRoleRel(),
                                                             uid=uid, select_column_name='rid') if uid else []
        if not role_ids or len(role_ids) < 1:
            return []
        # 根据角色查资源ids
        role_res_ids = PermissionsRelService.ids_in_roleids(TPermissionsResRoleRel(), role_ids, 'res_id')
        if not role_res_ids or len(role_res_ids) < 1:
            return []

        # 计算用户组资源集合和角色资源集合的交集得到最终用户的所有资源id
        user_res_ids = list(set(group_res_ids).intersection(set(role_res_ids)))

        # 创建查找条件
        conditions = {
            "id": user_res_ids,
            "res_type": res_type,
            "valid_status": valid_status
        }
        # 菜单资源时才有根据父查子数据
        if pid and res_type == Constants.RES_TYPE_MENU:
            conditions['pid'] = pid

        conditions['rule'] = {"id": Constants.QUERY_CONDITION_RULE_IN}
        rows = CRUDHelper.execute_select(TPermissionsResource(), conditions)
        return rows
    def get_resources_by_groupid():
        """
        通过用户组id获取资源列表
        :return:
        """
        params = request.values.get('params')
        params = json.loads(params)
        group_id = params['group_id']
        res_ids = PermissionsRelService.ids_in_groupids(
            model=TPermissionsResGroupRel(),
            gids=[group_id],
            select_column_name='rid')

        model = TPermissionsResource()
        conditions = dict(id=None, rule={})
        current_resources = None
        if res_ids:
            conditions['id'] = res_ids
            conditions['rule']['id'] = Constants.QUERY_CONDITION_RULE_IN
            current_resources = CRUDHelper.execute_select(model, conditions)
        return make_response(
            json.dumps(ResponseCode.response(data={"rows": current_resources}),
                       ensure_ascii=False))
Esempio n. 22
0
 def upd_in_ids(model, kv, ids):
     CRUDHelper.execute_upd(model, kv, ids)