func (p Resource) initServices() { services.GetTenantService() services.GetTokenService() services.GetUserService() services.GetRoleService() services.GetAuthService() }
// UserUpdateHandler parses the http request and updata a exist user. // Usage : // PUT /v1/user/{ParamID} // Params : // ParamID : storage identifier of user // If successful,response code will be set to 201. func (p *Resource) UserUpdateHandler(req *restful.Request, resp *restful.Response) { logrus.Infof("UserUpdateHanlder is called!") token := req.HeaderParameter("X-Auth-Token") id := req.PathParameter(ParamID) if len(id) <= 0 { logrus.Warnln("user id should not be null for update operation") response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("user id should not be null for update operation"), resp) return } newuser := entity.User{} // Populate the user data err := json.NewDecoder(req.Request.Body).Decode(&newuser) if err != nil { logrus.Errorf("convert body to user failed, error is %v", err) response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, err, resp) return } created, id, errorCode, err := services.GetUserService().UserUpdate(token, newuser, id) if err != nil { response.WriteStatusError(errorCode, err, resp) return } p.successUpdate(id, created, req, resp) }
// UserLoginHandler parses the http request and login with an exist user. // Usage : // POST v1/user/login // If successful,response code will be set to 201. func (p *Resource) UserLoginHandler(req *restful.Request, resp *restful.Response) { logrus.Infof("UserLoginHandler is called!") doc := bson.M{} decoder := json.NewDecoder(req.Request.Body) err := decoder.Decode(&doc) if err != nil { logrus.Errorf("decode user err is %v", err) response.WriteStatusError(services.USER_ERROR_LOGIN, err, resp) return } username, password, paraErr := userLoginParamCheck(doc) if paraErr != nil { response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, paraErr, resp) return } if len(username) == 0 || len(password) == 0 { logrus.Errorf("username and password can not be null!") response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("Username or password can not be null"), resp) return } errorCode, loginRes, err := services.GetUserService().UserLogin(username, password) if err != nil { response.WriteStatusError(errorCode, err, resp) return } response.WriteResponse(loginRes, resp) return }
// UserListHandler parses the http request and return the user items. // Usage : // GET /v1/user // GET /v1/user/{ParamID} // Params : // ParamID : storage identifier of user // If successful,response code will be set to 201. func (p *Resource) UserListHandler(req *restful.Request, resp *restful.Response) { logrus.Infof("UserListHandler is called!") token := req.HeaderParameter("X-Auth-Token") limitnum := queryIntParam(req, "limit", 10) skipnum := queryIntParam(req, "skip", 0) sort := req.QueryParameter("sort") ret, count, errorCode, err := services.GetUserService().UserList(token, limitnum, skipnum, sort) if err != nil { response.WriteStatusError(errorCode, err, resp) return } p.successList(ret, limitnum, count, req, resp) }
// UserDeleteHandler parses the http request and delete a user. // Usage : // DELETE /v1/user/{ParamID} // Params : // ParamID : storage identifier of user // If successful,response code will be set to 201. func (p *Resource) UserDeleteHandler(req *restful.Request, resp *restful.Response) { logrus.Infof("UserDeleteHandler is called!") token := req.HeaderParameter("X-Auth-Token") id := req.PathParameter(ParamID) if len(id) <= 0 { logrus.Warnln("user id should not be null for delete operation") response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("user id should not be null for delete operation"), resp) return } errorCode, err := services.GetUserService().UserDelete(token, id) if err != nil { response.WriteStatusError(errorCode, err, resp) return } response.WriteSuccess(resp) }
// UserChangePasswdHandler parses the http request and change // password of an exist user. // Usage : // PUT v1/user/changepassword/{ParamID} // Params : // ParamID : storage identifier of user // If successful,response code will be set to 201. func (p *Resource) UserChangePasswdHandler(req *restful.Request, resp *restful.Response) { logrus.Infof("UserChangePasswdHandler is called!") token := req.HeaderParameter("X-Auth-Token") id := req.PathParameter(ParamID) if len(id) <= 0 { logrus.Warnln("user id should not be null for change password operation") response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("user id should not be null for update operation"), resp) return } document := bson.M{} decoder := json.NewDecoder(req.Request.Body) err := decoder.Decode(&document) if err != nil { logrus.Errorf("decode change password object err is %v", err) response.WriteStatusError(services.COMMON_ERROR_INTERNAL, err, resp) return } document, err = mejson.Unmarshal(document) if err != nil { logrus.Errorf("unmarshal change password obejct err is %v", err) response.WriteStatusError(services.COMMON_ERROR_INTERNAL, err, resp) return } password := document["password"] newpwd1 := document["newpassword"] newpwd2 := document["confirm_newpassword"] if password == nil || newpwd1 == nil || newpwd2 == nil { logrus.Errorln("invalid parameter! password and newpassword field should not be null") response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("invalid parameter!password, newpassword and confirm_newpassword should not be null!"), resp) return } created, errorCode, err := services.GetUserService().UserChangePassword(token, id, password.(string), newpwd1.(string), newpwd2.(string)) if err != nil { response.WriteStatusError(errorCode, err, resp) return } p.successUpdate(id, created, req, resp) }
func (p *Resource) UserValidateHandler(req *restful.Request, resp *restful.Response) { logrus.Infof("UserValidateHandler is called!") token := req.HeaderParameter("X-Auth-Token") username := req.QueryParameter("username") if len(username) == 0 { logrus.Warnln("username should not be null") response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("username should not be null"), resp) return } logrus.Infof("username is %v", username) logrus.Infof("start to test username") errorCode, _, err := services.GetUserService().Validate(username, token) if err != nil { response.WriteStatusError(errorCode, err, resp) return } response.WriteSuccess(resp) }
// CheckAndGenerateToken parses the http request and registry a new user. // Usage : // POST /v1/user/registry // If successful,response code will be set to 201. func (p *Resource) UserCreateUserHandler(req *restful.Request, resp *restful.Response) { logrus.Infof("UserCreateUserHandler is called!") token := req.HeaderParameter("X-Auth-Token") doc := bson.M{} decoder := json.NewDecoder(req.Request.Body) err := decoder.Decode(&doc) if err != nil { logrus.Errorf("decode user err is %v", err) response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, err, resp) return } username, email, password, company, paraErr := userRegistryParamCheck(doc) if paraErr != nil { response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, paraErr, resp) return } if len(email) == 0 || len(password) == 0 || len(username) == 0 { logrus.Errorln("parameter can not be null!") response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("Invalid parameter"), resp) return } userParam := services.UserParam{ UserName: username, Email: email, Password: password, Company: company} errorCode, userId, err := services.GetUserService().Create(userParam, token) if err != nil { response.WriteStatusError(errorCode, err, resp) return } p.successUpdate(userId, true, req, resp) }