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 post(self, request, *args, **kwargs): nojoin_user_ids = [] team_name = request.data.get("team_name") team = team_services.get_enterprise_tenant_by_tenant_name(self.user.enterprise_id, team_name) if not team: raise ServiceHandleException(msg="no found team", msg_show="团队不存在", status_code=404) users = team_services.get_team_users(team) if users: nojoin_user_ids = users.values_list("user_id", flat=True) if self.user.user_id not in nojoin_user_ids: team_services.add_user_role_to_team(tenant=team, user_ids=[self.user.user_id], role_ids=[]) result = general_message(200, "success", None) return Response(result, status=200)
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)
def post(self, request, *args, **kwargs): """ parameters: - name: tenant_name description: 租户名称 required: true type: string paramType: path - name: user_name description: 用户名 required: true type: string paramType: form - name: phone description: 手机号 required: true type: string paramType: form - name: email description: 邮件地址 required: true type: string paramType: form - name: password description: 密码 required: true type: string paramType: form - name: re_password description: 重复密码 required: true type: string paramType: form - name: identity description: 用户在租户的身份 required: true type: string paramType: form """ tenant_name = request.data.get("tenant_name", None) user_name = request.data.get("user_name", None) email = request.data.get("email", None) password = request.data.get("password", None) re_password = request.data.get("re_password", None) role_ids = request.data.get("role_ids", None) if len(password) < 8: result = general_message(400, "len error", "密码长度最少为8位") return Response(result) if not tenant_name: result = general_message(400, "not tenant", "团队不能为空") return Response(result) if role_ids and tenant_name: team = team_services.get_tenant_by_tenant_name(tenant_name) if not team: raise ServiceHandleException(msg_show="团队不存在", msg="no found team", status_code=404) # 校验用户信息 user_services.check_params(user_name, email, password, re_password, self.user.enterprise_id) client_ip = user_services.get_client_ip(request) enterprise = console_enterprise_service.get_enterprise_by_enterprise_id( self.user.enterprise_id) # 创建用户 user = user_services.create_user_set_password( user_name, email, password, "admin add", enterprise, client_ip) # 创建用户团队关系表 team_services.add_user_role_to_team(tenant=team, user_ids=[user.user_id], role_ids=role_ids) user.is_active = True user.save() result = general_message(200, "success", "添加用户成功") else: result = general_message(400, "not role", "创建用户时角色不能为空") return Response(result)