Esempio n. 1
0
    def post(self, request, team_name, *args, **kwargs):
        """
        新建一个角色
        ------
        parameters:
            - name: team_name
              description: 团队名称
              required: true
              type: string
              paramType: path
            - name: role_name
              description: 角色名称 格式 {'role_name':'DBA'}
              required: true
              type: string
              paramType: body
            - name: options_id_list
              description: 权限id列表 格式 {'options_id_list':'1,2,3,4'}
              required: true
              type: string
              paramType: body
        """

        try:
            role_name = request.data.get("role_name", None)
            options_id_list = request.data.get("options_id_list", None)

            if not role_name:
                raise ParamsError("角色名为空")
            if not options_id_list:
                raise ParamsError("权限选项为空")
            try:
                premission_id_list = options_id_list.split(",")
                premission_id_list = map(lambda x: int(x), premission_id_list)
            except Exception as e:
                logging.exception(e)
                code = 400
                result = general_message(code, "Incorrect parameter format",
                                         "参数格式不正确")
                return Response(result, status=code)

            r = re.compile(u'^[a-zA-Z0-9_\\-\u4e00-\u9fa5]+$')
            if not r.match(role_name.decode("utf-8")) or len(role_name) > 30:
                code = 400
                result = general_message(code, "failed",
                                         "角色名称只能是30个字符内任意数字,字母,中文字符,下划线的组合")
                return Response(result, status=code)
            if role_name in role_repo.get_default_role():
                code = 400
                result = general_message(code, "failed", "角色名称不能与系统默认相同")
                return Response(result, status=code)
            if role_repo.team_role_is_exist_by_role_name_team_id_2(
                    role_name=role_name, tenant_name=team_name):
                code = 400
                result = general_message(code, "failed", "该角色已经存在")
                return Response(result, status=code)

            select_perm_list = role_perm_repo.get_select_perm_list()
            for i in premission_id_list:
                if i not in select_perm_list:
                    result = general_message(400, "failed", "权限列表中有权限不可选")
                    return Response(result, status=400)

            role_obj = team_services.add_role_by_team_name_perm_list(
                role_name=role_name,
                tenant_name=team_name,
                perm_id_list=premission_id_list)
            if role_obj:
                code = 200
                role_info = {
                    "role_id": role_obj.pk,
                    "role_name": role_obj.role_name,
                    "is_default": role_obj.is_default
                }
                result = general_message(code,
                                         "success",
                                         "创建角色成功",
                                         bean=role_info)
            else:
                code = 400
                result = general_message(code, "failed", "创建角色失败")
        except ParamsError as e:
            logging.exception(e)
            code = 400
            result = general_message(code, "params is empty", e.message)
        except Tenants.DoesNotExist as e:
            code = 200
            logger.exception(e)
            print(str(e))
            result = generate_result(code, "tenant not exist", "团队不存在")
        except Exception as e:
            code = 500
            logger.exception(e)
            print(str(e))
            result = general_message(code, "system error", "系统异常")
        return Response(result, status=code)
Esempio n. 2
0
    def post(self, request, team_name, *args, **kwargs):
        """
        修改角色名称及对应的权限
        ---
        parameters:
            - name: team_name
              description: 团队名
              required: true
              type: string
              paramType: path
            - name: role_id
              description: 角色ID {'role_id':'1'}
              required: true
              type: string
              paramType: body
            - name: new_role_name
              description: 新的的角色名称
              required: true
              type: string
              paramType: body
            - name: new_options_id_list
              description: 新的权限ID列表 格式 {'new_options_id_list':'1,2,3,4'}
              required: true
              type: string
              paramType: body
        """
        try:
            role_id = request.data.get("role_id", None)
            new_role_name = request.data.get("new_role_name", None)
            new_options_id_list = request.data.get("new_options_id_list", None)

            if not role_id:
                raise ParamsError("原角色ID为空")
            if not new_role_name:
                raise ParamsError("新角色名为空")
            if not new_options_id_list:
                raise ParamsError("权限选项为空")

            try:
                role_id = int(role_id)
                perm_id_list = [
                    int(perm_id) for perm_id in new_options_id_list.split(",")
                ]
            except Exception as e:
                logging.exception(e)
                code = 400
                result = general_message(code, "Incorrect parameter format",
                                         "参数格式不正确")
                return Response(result, status=code)

            r = re.compile(u'^[a-zA-Z0-9_\\-\u4e00-\u9fa5]+$')
            if not r.match(
                    new_role_name.decode("utf-8")) or len(new_role_name) > 30:
                code = 400
                result = general_message(code, "failed",
                                         "角色名称只能是30个字符内任意数字,字母,中文字符,下划线的组合")
                return Response(result, status=code)

            if new_role_name in role_repo.get_default_role():
                code = 400
                result = general_message(code, "failed", "角色名称不能与系统默认相同")
                return Response(result, status=code)
            if not role_repo.team_role_is_exist_by_role_name_team_id(
                    tenant_name=team_name, role_id=role_id):
                code = 400
                result = general_message(code, "failed", "原角色不存在")
                return Response(result, status=code)

            select_perm_list = role_perm_repo.get_select_perm_list()
            for i in perm_id_list:
                if i not in select_perm_list:
                    result = general_message(400, "failed", "权限列表中有权限不可选")
                    return Response(result, status=400)

            try:
                role_obj = team_services.update_role_by_team_name_role_name_perm_list(
                    new_role_name=new_role_name,
                    role_id=role_id,
                    tenant_name=team_name,
                    perm_id_list=perm_id_list)
                if role_obj:
                    code = 200
                    role_info = {
                        "role_id": role_obj.pk,
                        "role_name": role_obj.role_name,
                        "is_default": role_obj.is_default
                    }
                    result = general_message(code,
                                             "success",
                                             "更新角色权限成功",
                                             bean=role_info)
                else:
                    code = 400
                    result = general_message(code, "failed", "更新角色权限失败")
            except Exception as e:
                logging.exception(e)
                code = 400
                print(str(e))
                result = general_message(code, "failed", "更新角色权限失败")
                return Response(result, status=code)
        except ParamsError as e:
            logging.exception(e)
            code = 400
            result = general_message(code, "params is empty", e.message)
        except Tenants.DoesNotExist as e:
            code = 200
            logger.exception(e)
            print(str(e))
            result = generate_result(code, "tenant not exist", "团队不存在")
        except Exception as e:
            code = 500
            logger.exception(e)
            print(str(e))
            result = general_message(code, "system error", "系统异常")
        return Response(result, status=code)