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, request, team_name, *args, **kwargs): """ 团队中添加新用户给用户分配一个角色 --- parameters: - name: team_name description: 团队名称 required: true type: string paramType: path - name: user_ids description: 添加成员id 格式 {'user_ids':'1,2'} required: true type: string paramType: body - name: role_ids description: 选择角色 格式{"role_ids": "1,2,3"} required: true type: string paramType: body """ perm_list = team_services.get_user_perm_identitys_in_permtenant( user_id=request.user.user_id, tenant_name=team_name) # 根据用户在一个团队的角色来获取这个角色对应的所有权限操作 role_perm_tuple = team_services.get_user_perm_in_tenant( user_id=request.user.user_id, tenant_name=team_name) no_auth = ("owner" not in perm_list) and ( "admin" not in perm_list ) and "manage_team_member_permissions" not in role_perm_tuple if no_auth: code = 400 result = general_message(code, "no identity", "您没有权限做此操作") return Response(result, status=code) try: user_ids = request.data.get('user_ids', None) role_ids = request.data.get('role_ids', None) if not user_ids: raise ParamsError("用户名为空") if not role_ids: raise ParamsError("角色ID为空") try: user_ids = [int(user_id) for user_id in user_ids.split(",")] role_ids = [int(user_id) for user_id in role_ids.split(",")] except Exception as e: code = 400 logger.exception(e) result = general_message(code, "Incorrect parameter format", "参数格式不正确") return Response(result, status=code) for role_id in role_ids: if role_id not in team_services.get_all_team_role_id( tenant_name=team_name): code = 400 result = general_message(code, "The role does not exist", "该角色在团队中不存在") return Response(result, status=code) user_id = team_services.user_is_exist_in_team( user_list=user_ids, tenant_name=team_name) if user_id: user_obj = user_services.get_user_by_user_id(user_id=user_id) code = 400 result = general_message(code, "user already exist", "用户{}已经存在".format(user_obj.nick_name)) return Response(result, status=code) code = 200 team = team_services.get_tenant(tenant_name=team_name) team_services.add_user_role_to_team(request=request, tenant=team, user_ids=user_ids, role_ids=role_ids) result = general_message(code, "success", "用户添加到{}成功".format(team_name)) except ParamsError as e: logging.exception(e) code = 400 result = general_message(code, "params is empty", e.message) except UserNotExistError as e: code = 400 result = general_message(code, "user not exist", e.message) except Tenants.DoesNotExist as e: code = 400 logger.exception(e) result = general_message(code, "tenant not exist", "{}团队不存在".format(team_name)) except Exception as e: code = 500 logger.exception(e) print(str(e)) result = general_message(code, "system error", "系统异常") return Response(result, status=code)
def post(self, request, team_name, *args, **kwargs): """ 团队中添加新用户给用户分配一个角色 --- parameters: - name: team_name description: 团队名称 required: true type: string paramType: path - name: user_ids description: 添加成员id 格式 {'user_ids':'1,2'} required: true type: string paramType: body - name: role_ids description: 选择角色 格式{"role_ids": "1,2,3"} required: true type: string paramType: body """ try: user_ids = request.data.get('user_ids', None) role_ids = request.data.get('role_ids', None) if not user_ids: raise ParamsError("用户名为空") if not role_ids: raise ParamsError("角色ID为空") try: user_ids = [int(user_id) for user_id in user_ids.split(",")] role_ids = [int(user_id) for user_id in role_ids.split(",")] except Exception as e: code = 400 logger.exception(e) result = general_message(code, "Incorrect parameter format", "参数格式不正确") return Response(result, status=code) user_id = team_services.user_is_exist_in_team(user_list=user_ids, tenant_name=team_name) if user_id: user_obj = user_services.get_user_by_user_id(user_id=user_id) code = 400 result = general_message(code, "user already exist", "用户{}已经存在".format(user_obj.nick_name)) return Response(result, status=code) code = 200 team = team_services.get_tenant(tenant_name=team_name) team_services.add_user_role_to_team(tenant=team, user_ids=user_ids, role_ids=role_ids) result = general_message(code, "success", "用户添加到{}成功".format(team_name)) except ParamsError as e: logging.exception(e) code = 400 result = general_message(code, "params is empty", e.message) except UserNotExistError as e: code = 400 result = general_message(code, "user not exist", e.message) except Tenants.DoesNotExist as e: code = 400 logger.exception(e) result = general_message(code, "tenant not exist", "{}团队不存在".format(team_name)) return Response(result, status=code)