def post(self, request): try: post_data = json.loads(request.body) except Exception: return ApiV2FailJsonResponse( _("请求参数格式错误,必须为json格式"), code=ApiErrorCodeEnumV2.PARAM_NOT_VALID) # 非ESB的请求需要验证登录态 bk_token if not is_request_from_esb(request): # 验证Token参数 is_valid, user, message = validate_bk_token(post_data) if not is_valid: return ApiV2FailJsonResponse( message, code=ApiErrorCodeEnumV2.PARAM_NOT_VALID) username_list = post_data.get('bk_username_list') if not username_list: return ApiV2FailJsonResponse( _("缺少参数:bk_username_list"), code=ApiErrorCodeEnumV2.PARAM_NOT_VALID) # 获取用户信息 data = BkUser.objects.get_batch_users_with_dict_v2(username_list) return ApiV2OKJsonResponse(_("用户信息获取成功"), data=data)
def get(self, request): # 验证Token参数 is_valid, user, message = validate_bk_token(request.GET) if not is_valid: return ApiV2FailJsonResponse( message, code=ApiErrorCodeEnumV2.PARAM_NOT_VALID) return ApiV2OKJsonResponse(_("用户验证成功"), data={'bk_username': user.username})
def get(self, request): # 非ESB的请求需要验证登录态 bk_token if not is_request_from_esb(request): # 验证Token参数 is_valid, user, message = validate_bk_token(request.GET) if not is_valid: return ApiV2FailJsonResponse( message, code=ApiErrorCodeEnumV2.PARAM_NOT_VALID) # 获取用户角色参数,空为所有用户 role = request.GET.get('bk_role', '') # 获取用户信息 data = BkUser.objects.get_all_users_v2(role) return ApiV2OKJsonResponse(_("用户信息获取成功"), data=data)
def get(self, request): # 验证Token参数 is_valid, user, message = validate_bk_token(request.GET) if not is_valid: # 如果是ESB的请求,可以直接从参数中获取用户名 is_from_esb = is_request_from_esb(request) username = request.GET.get('bk_username') if not is_from_esb or not username: return ApiV2FailJsonResponse( message, code=ApiErrorCodeEnumV2.PARAM_NOT_VALID) else: username = user.username # 获取用户数据 result, data, message = BkUser.objects.get_user_info_v2(username) if not result: return ApiV2FailJsonResponse( message, code=ApiErrorCodeEnumV2.USER_NOT_EXISTS2) return ApiV2OKJsonResponse(_("用户信息获取成功"), data=data)