Use c.ResponseError() for all places.

This commit is contained in:
Yang Luo
2021-08-08 11:06:45 +08:00
parent 6aeadfa3bd
commit d16569d461
5 changed files with 24 additions and 66 deletions

View File

@ -83,7 +83,7 @@ func (c *ApiController) Signup() {
var resp Response var resp Response
if c.GetSessionUsername() != "" { if c.GetSessionUsername() != "" {
c.ResponseErrorWithData("Please sign out first before signing up", c.GetSessionUsername()) c.ResponseError("Please sign out first before signing up", c.GetSessionUsername())
return return
} }
@ -214,9 +214,7 @@ func (c *ApiController) GetAccount() {
user := object.GetUser(userId) user := object.GetUser(userId)
if user == nil { if user == nil {
resp := Response{Status: "error", Msg: fmt.Sprintf("The user: %s doesn't exist", userId)} c.ResponseError(fmt.Sprintf("The user: %s doesn't exist", userId))
c.Data["json"] = resp
c.ServeJSON()
return return
} }
@ -253,18 +251,14 @@ func (c *ApiController) UploadAvatar() {
avatarBase64 := c.Ctx.Request.Form.Get("avatarfile") avatarBase64 := c.Ctx.Request.Form.Get("avatarfile")
index := strings.Index(avatarBase64, ",") index := strings.Index(avatarBase64, ",")
if index < 0 || avatarBase64[0:index] != "data:image/png;base64" { if index < 0 || avatarBase64[0:index] != "data:image/png;base64" {
resp = Response{Status: "error", Msg: "File encoding error"} c.ResponseError("File encoding error")
c.Data["json"] = resp
c.ServeJSON()
return return
} }
dist, _ := base64.StdEncoding.DecodeString(avatarBase64[index+1:]) dist, _ := base64.StdEncoding.DecodeString(avatarBase64[index+1:])
msg := object.UploadAvatar(provider, user.GetId(), dist) msg := object.UploadAvatar(provider, user.GetId(), dist)
if msg != "" { if msg != "" {
resp = Response{Status: "error", Msg: msg} c.ResponseError(msg)
c.Data["json"] = resp
c.ServeJSON()
return return
} }

View File

@ -121,18 +121,14 @@ func (c *ApiController) Login() {
var form RequestForm var form RequestForm
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form) err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
if err != nil { if err != nil {
resp = &Response{Status: "error", Msg: err.Error()} c.ResponseError(err.Error())
c.Data["json"] = resp
c.ServeJSON()
return return
} }
if form.Username != "" { if form.Username != "" {
if form.Type == ResponseTypeLogin { if form.Type == ResponseTypeLogin {
if c.GetSessionUsername() != "" { if c.GetSessionUsername() != "" {
resp = &Response{Status: "error", Msg: "Please log out first before signing in", Data: c.GetSessionUsername()} c.ResponseError("Please sign out first before signing in", c.GetSessionUsername())
c.Data["json"] = resp
c.ServeJSON()
return return
} }
} }
@ -196,7 +192,7 @@ func (c *ApiController) Login() {
} }
if msg != "" { if msg != "" {
resp = &Response{Status: "error", Msg: msg, Data: ""} resp = &Response{Status: "error", Msg: msg}
} else { } else {
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application)) application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
resp = c.HandleLoggedIn(application, user, &form) resp = c.HandleLoggedIn(application, user, &form)
@ -213,50 +209,38 @@ func (c *ApiController) Login() {
provider := object.GetProvider(fmt.Sprintf("admin/%s", form.Provider)) provider := object.GetProvider(fmt.Sprintf("admin/%s", form.Provider))
providerItem := application.GetProviderItem(provider.Name) providerItem := application.GetProviderItem(provider.Name)
if !providerItem.IsProviderVisible() { if !providerItem.IsProviderVisible() {
resp = &Response{Status: "error", Msg: fmt.Sprintf("The provider: %s is not enabled for the application", provider.Name)} c.ResponseError(fmt.Sprintf("The provider: %s is not enabled for the application", provider.Name))
c.Data["json"] = resp
c.ServeJSON()
return return
} }
idProvider := idp.GetIdProvider(provider.Type, provider.ClientId, provider.ClientSecret, form.RedirectUri) idProvider := idp.GetIdProvider(provider.Type, provider.ClientId, provider.ClientSecret, form.RedirectUri)
if idProvider == nil { if idProvider == nil {
resp = &Response{Status: "error", Msg: fmt.Sprintf("The provider type: %s is not supported", provider.Type)} c.ResponseError(fmt.Sprintf("The provider type: %s is not supported", provider.Type))
c.Data["json"] = resp
c.ServeJSON()
return return
} }
setHttpClient(idProvider, provider.Type) setHttpClient(idProvider, provider.Type)
if form.State != beego.AppConfig.String("authState") && form.State != application.Name { if form.State != beego.AppConfig.String("authState") && form.State != application.Name {
resp = &Response{Status: "error", Msg: fmt.Sprintf("state expected: \"%s\", but got: \"%s\"", beego.AppConfig.String("authState"), form.State)} c.ResponseError(fmt.Sprintf("state expected: \"%s\", but got: \"%s\"", beego.AppConfig.String("authState"), form.State))
c.Data["json"] = resp
c.ServeJSON()
return return
} }
// https://github.com/golang/oauth2/issues/123#issuecomment-103715338 // https://github.com/golang/oauth2/issues/123#issuecomment-103715338
token, err := idProvider.GetToken(form.Code) token, err := idProvider.GetToken(form.Code)
if err != nil { if err != nil {
resp = &Response{Status: "error", Msg: err.Error()} c.ResponseError(err.Error())
c.Data["json"] = resp
c.ServeJSON()
return return
} }
if !token.Valid() { if !token.Valid() {
resp = &Response{Status: "error", Msg: "Invalid token"} c.ResponseError("Invalid token")
c.Data["json"] = resp
c.ServeJSON()
return return
} }
userInfo, err := idProvider.GetUserInfo(token) userInfo, err := idProvider.GetUserInfo(token)
if err != nil { if err != nil {
resp = &Response{Status: "error", Msg: fmt.Sprintf("Failed to login in: %s", err.Error())} c.ResponseError(fmt.Sprintf("Failed to login in: %s", err.Error()))
c.Data["json"] = resp
c.ServeJSON()
return return
} }
@ -292,16 +276,12 @@ func (c *ApiController) Login() {
} else { } else {
// Sign up via OAuth // Sign up via OAuth
if !application.EnableSignUp { if !application.EnableSignUp {
resp = &Response{Status: "error", Msg: fmt.Sprintf("The account for provider: %s and username: %s (%s) does not exist and is not allowed to sign up as new account, please contact your IT support", provider.Type, userInfo.Username, userInfo.DisplayName)} c.ResponseError(fmt.Sprintf("The account for provider: %s and username: %s (%s) does not exist and is not allowed to sign up as new account, please contact your IT support", provider.Type, userInfo.Username, userInfo.DisplayName))
c.Data["json"] = resp
c.ServeJSON()
return return
} }
if !providerItem.CanSignUp { if !providerItem.CanSignUp {
resp = &Response{Status: "error", Msg: fmt.Sprintf("The account for provider: %s and username: %s (%s) does not exist and is not allowed to sign up as new account via %s, please use another way to sign up", provider.Type, userInfo.Username, userInfo.DisplayName, provider.Type)} c.ResponseError(fmt.Sprintf("The account for provider: %s and username: %s (%s) does not exist and is not allowed to sign up as new account via %s, please use another way to sign up", provider.Type, userInfo.Username, userInfo.DisplayName, provider.Type))
c.Data["json"] = resp
c.ServeJSON()
return return
} }
@ -348,9 +328,7 @@ func (c *ApiController) Login() {
} else { // form.Method != "signup" } else { // form.Method != "signup"
userId := c.GetSessionUsername() userId := c.GetSessionUsername()
if userId == "" { if userId == "" {
resp = &Response{Status: "error", Msg: "The account does not exist", Data: userInfo} c.ResponseError("The account does not exist", userInfo)
c.Data["json"] = resp
c.ServeJSON()
return return
} }
@ -359,9 +337,7 @@ func (c *ApiController) Login() {
oldUser = object.GetUserByField(application.Organization, provider.Type, userInfo.Username) oldUser = object.GetUserByField(application.Organization, provider.Type, userInfo.Username)
} }
if oldUser != nil { if oldUser != nil {
resp = &Response{Status: "error", Msg: fmt.Sprintf("The account for provider: %s and username: %s (%s) is already linked to another account: %s (%s)", provider.Type, userInfo.Username, userInfo.DisplayName, oldUser.Name, oldUser.DisplayName)} c.ResponseError(fmt.Sprintf("The account for provider: %s and username: %s (%s) is already linked to another account: %s (%s)", provider.Type, userInfo.Username, userInfo.DisplayName, oldUser.Name, oldUser.DisplayName))
c.Data["json"] = resp
c.ServeJSON()
return return
} }
@ -382,7 +358,8 @@ func (c *ApiController) Login() {
//} //}
} }
} else { } else {
panic("unknown authentication type (not password or provider), form = " + util.StructToJson(form)) c.ResponseError(fmt.Sprintf("unknown authentication type (not password or provider), form = %s", util.StructToJson(form)))
return
} }
c.Data["json"] = resp c.Data["json"] = resp

View File

@ -55,15 +55,13 @@ func (c *ApiController) GetLdapUser() {
conn, err := object.GetLdapConn(ldapServer.Host, ldapServer.Port, ldapServer.Admin, ldapServer.Passwd) conn, err := object.GetLdapConn(ldapServer.Host, ldapServer.Port, ldapServer.Admin, ldapServer.Passwd)
if err != nil { if err != nil {
c.Data["json"] = Response{Status: "error", Msg: err.Error()} c.ResponseError(err.Error())
c.ServeJSON()
return return
} }
//groupsMap, err := conn.GetLdapGroups(ldapServer.BaseDn) //groupsMap, err := conn.GetLdapGroups(ldapServer.BaseDn)
//if err != nil { //if err != nil {
// c.Data["json"] = Response{Status: "error", Msg: err.Error()} // c.ResponseError(err.Error())
// c.ServeJSON()
// return // return
//} //}
@ -76,8 +74,7 @@ func (c *ApiController) GetLdapUser() {
users, err := conn.GetLdapUsers(ldapServer.BaseDn) users, err := conn.GetLdapUsers(ldapServer.BaseDn)
if err != nil { if err != nil {
c.Data["json"] = Response{Status: "error", Msg: err.Error()} c.ResponseError(err.Error())
c.ServeJSON()
return return
} }

View File

@ -44,9 +44,7 @@ func (c *ApiController) Unlink() {
value := object.GetUserField(user, providerType) value := object.GetUserField(user, providerType)
if value == "" { if value == "" {
resp = Response{Status: "error", Msg: "Please link first", Data: value} c.ResponseError("Please link first", value)
c.Data["json"] = resp
c.ServeJSON()
return return
} }

View File

@ -69,19 +69,11 @@ func (c *ApiController) ResponseError(error string, data ...interface{}) {
c.ServeJSON() c.ServeJSON()
} }
// ResponseErrorWithData ...
func (c *ApiController) ResponseErrorWithData(error string, data interface{}) {
c.Data["json"] = Response{Status: "error", Msg: error, Data: data}
c.ServeJSON()
}
// RequireSignedIn ... // RequireSignedIn ...
func (c *ApiController) RequireSignedIn() (string, bool) { func (c *ApiController) RequireSignedIn() (string, bool) {
userId := c.GetSessionUsername() userId := c.GetSessionUsername()
if userId == "" { if userId == "" {
resp := Response{Status: "error", Msg: "Please sign in first"} c.ResponseError("Please sign in first")
c.Data["json"] = resp
c.ServeJSON()
return "", false return "", false
} }
return userId, true return userId, true