def put(self, req, team_id, user_id): if req.user.user_id == user_id: raise serializers.ValidationError( "您不能修改自己的权限!", status=status.HTTP_400_BAD_REQUEST) serializer = CreateTeamUserReqSerializer(data=req.data) serializer.is_valid(raise_exception=True) role_ids = req.data["role_ids"].replace(" ", "").split(",") roleids = team_services.get_all_team_role_id(tenant_name=team_id, allow_owner=True) for role_id in role_ids: if int(role_id) not in roleids: raise serializers.ValidationError("角色{}不存在".format(role_id), status.HTTP_404_NOT_FOUND) try: user_services.get_user_by_tenant_id(team_id, user_id) except UserNotExistError as e: return Response({"msg": e.message}, status.HTTP_404_NOT_FOUND) team_services.change_tenant_role(user_id=user_id, tenant_name=team_id, role_id_list=role_ids) return Response(None, status.HTTP_200_OK)
def post(self, req, team_id, user_id): serializer = CreateTeamUserReqSerializer(data=req.data) serializer.is_valid(raise_exception=True) try: team = team_services.get_team_by_team_id(team_id) except Tenants.DoesNotExist: raise exceptions.NotFound() role_ids = req.data["role_ids"].replace(" ", "").split(",") roleids = team_services.get_all_team_role_id(tenant_name=team_id, allow_owner=True) for role_id in role_ids: if int(role_id) not in roleids: raise serializers.ValidationError("角色{}不存在".format(role_id), status.HTTP_404_NOT_FOUND) flag = team_services.user_is_exist_in_team(user_list=[user_id], tenant_name=team_id) if flag: user_obj = user_services.get_user_by_user_id(user_id=user_id) raise serializers.ValidationError( "用户{}已经存在".format(user_obj.nick_name), status.HTTP_400_BAD_REQUEST) team_services.add_user_role_to_team(tenant=team, user_ids=[user_id], role_ids=role_ids) return Response(None, status.HTTP_201_CREATED)
def post(self, req, team_id, user_id): serializer = CreateTeamUserReqSerializer(data=req.data) serializer.is_valid(raise_exception=True) role_ids = req.data["role_ids"].replace(" ", "").split(",") team_services.add_user_role_to_team(tenant=self.team, user_ids=[user_id], role_ids=role_ids) return Response(None, status.HTTP_201_CREATED)
def put(self, req, team_id, user_id): if req.user.user_id == user_id: raise serializers.ValidationError("您不能修改自己的权限!", status.HTTP_400_BAD_REQUEST) serializer = CreateTeamUserReqSerializer(data=req.data) serializer.is_valid(raise_exception=True) role_ids = req.data["role_ids"].replace(" ", "").split(",") user = user_repo.get_by_user_id(user_id) user_kind_role_service.update_user_roles(kind="team", kind_id=self.team.tenant_id, user=user, role_ids=role_ids) return Response(None, status.HTTP_200_OK)
class TeamUserInfoView(TeamAPIView): @swagger_auto_schema( operation_description="将用户从团队中移除", responses={ status.HTTP_404_NOT_FOUND: FailSerializer(), }, tags=['openapi-team'], ) def delete(self, req, team_id, user_id): if req.user.user_id == user_id: raise serializers.ValidationError("不能删除自己", status.HTTP_400_BAD_REQUEST) try: user_services.get_user_by_tenant_id(team_id, user_id) user_services.batch_delete_users(team_id, [user_id]) return Response(None, status.HTTP_200_OK) except UserNotExistError as e: return Response({"msg": e.message}, status.HTTP_404_NOT_FOUND) except Tenants.DoesNotExist: return Response({"msg": "团队不存在"}, status.HTTP_404_NOT_FOUND) @swagger_auto_schema( operation_description="add team user", request_body=CreateTeamUserReqSerializer(), responses={}, tags=['openapi-team'], ) def post(self, req, team_id, user_id): serializer = CreateTeamUserReqSerializer(data=req.data) serializer.is_valid(raise_exception=True) role_ids = req.data["role_ids"].replace(" ", "").split(",") team_services.add_user_role_to_team(tenant=self.team, user_ids=[user_id], role_ids=role_ids) return Response(None, status.HTTP_201_CREATED) @swagger_auto_schema( operation_description="update team user", request_body=CreateTeamUserReqSerializer(), responses={ status.HTTP_404_NOT_FOUND: FailSerializer(), }, tags=['openapi-team'], ) # TODO 修改权限控制 def put(self, req, team_id, user_id): if req.user.user_id == user_id: raise serializers.ValidationError("您不能修改自己的权限!", status.HTTP_400_BAD_REQUEST) serializer = CreateTeamUserReqSerializer(data=req.data) serializer.is_valid(raise_exception=True) role_ids = req.data["role_ids"].replace(" ", "").split(",") user = user_repo.get_by_user_id(user_id) user_kind_role_service.update_user_roles(kind="team", kind_id=self.team.tenant_id, user=user, role_ids=role_ids) return Response(None, status.HTTP_200_OK)
class TeamUserInfoView(BaseOpenAPIView): @swagger_auto_schema( operation_description="将用户从团队中移除", responses={ status.HTTP_200_OK: None, status.HTTP_404_NOT_FOUND: FailSerializer(), status.HTTP_500_INTERNAL_SERVER_ERROR: None }, tags=['openapi-team'], ) def delete(self, req, team_id, user_id): if req.user.user_id == user_id: raise serializers.ValidationError("不能删除自己", status.HTTP_400_BAD_REQUEST) try: user_services.get_user_by_tenant_id(team_id, user_id) user_services.batch_delete_users(team_id, [user_id]) return Response(None, status.HTTP_200_OK) except UserNotExistError as e: return Response({"msg": e.message}, status.HTTP_404_NOT_FOUND) except Tenants.DoesNotExist: return Response({"msg": "团队不存在"}, status.HTTP_404_NOT_FOUND) @swagger_auto_schema( operation_description="add team user", request_body=CreateTeamUserReqSerializer(), responses={ status.HTTP_201_CREATED: None, status.HTTP_500_INTERNAL_SERVER_ERROR: None, status.HTTP_400_BAD_REQUEST: None, }, tags=['openapi-team'], ) def post(self, req, team_id, user_id): serializer = CreateTeamUserReqSerializer(data=req.data) serializer.is_valid(raise_exception=True) try: team = team_services.get_team_by_team_id(team_id) except Tenants.DoesNotExist: raise exceptions.NotFound() role_ids = req.data["role_ids"].replace(" ", "").split(",") roleids = team_services.get_all_team_role_id(tenant_name=team_id, allow_owner=True) for role_id in role_ids: if int(role_id) not in roleids: raise serializers.ValidationError("角色{}不存在".format(role_id), status.HTTP_404_NOT_FOUND) flag = team_services.user_is_exist_in_team(user_list=[user_id], tenant_name=team_id) if flag: user_obj = user_services.get_user_by_user_id(user_id=user_id) raise serializers.ValidationError("用户{}已经存在".format(user_obj.nick_name), status.HTTP_400_BAD_REQUEST) team_services.add_user_role_to_team(tenant=team, user_ids=[user_id], role_ids=role_ids) return Response(None, status.HTTP_201_CREATED) @swagger_auto_schema( operation_description="update team user", request_body=CreateTeamUserReqSerializer(), responses={ status.HTTP_200_OK: None, status.HTTP_500_INTERNAL_SERVER_ERROR: None, status.HTTP_400_BAD_REQUEST: None, status.HTTP_404_NOT_FOUND: FailSerializer(), }, tags=['openapi-team'], ) def put(self, req, team_id, user_id): if req.user.user_id == user_id: raise serializers.ValidationError("您不能修改自己的权限!", status=status.HTTP_400_BAD_REQUEST) serializer = CreateTeamUserReqSerializer(data=req.data) serializer.is_valid(raise_exception=True) role_ids = req.data["role_ids"].replace(" ", "").split(",") roleids = team_services.get_all_team_role_id(tenant_name=team_id, allow_owner=True) for role_id in role_ids: if int(role_id) not in roleids: raise serializers.ValidationError("角色{}不存在".format(role_id), status.HTTP_404_NOT_FOUND) try: user_services.get_user_by_tenant_id(team_id, user_id) except UserNotExistError as e: return Response({"msg": e.message}, status.HTTP_404_NOT_FOUND) team_services.change_tenant_role(user_id=user_id, tenant_name=team_id, role_id_list=role_ids) return Response(None, status.HTTP_200_OK)