From e0d2bc3dc90c8109dc38331f4afcd4be1a70ca8a Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Sat, 10 Jun 2023 15:51:26 +0800 Subject: [PATCH] Return error in GetProviderFromContext() --- controllers/resource.go | 10 ++++++---- controllers/service.go | 13 +++++++------ controllers/util.go | 27 +++++++++++++-------------- object/application.go | 6 +++--- routers/auto_signin_filter.go | 2 +- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/controllers/resource.go b/controllers/resource.go index 4fd001d1..8f18c477 100644 --- a/controllers/resource.go +++ b/controllers/resource.go @@ -139,8 +139,9 @@ func (c *ApiController) DeleteResource() { return } - provider, _, ok := c.GetProviderFromContext("Storage") - if !ok { + provider, err := c.GetProviderFromContext("Storage") + if err != nil { + c.ResponseError(err.Error()) return } @@ -187,8 +188,9 @@ func (c *ApiController) UploadResource() { return } - provider, _, ok := c.GetProviderFromContext("Storage") - if !ok { + provider, err := c.GetProviderFromContext("Storage") + if err != nil { + c.ResponseError(err.Error()) return } diff --git a/controllers/service.go b/controllers/service.go index cae6c627..038feac4 100644 --- a/controllers/service.go +++ b/controllers/service.go @@ -69,9 +69,9 @@ func (c *ApiController) SendEmail() { } else { // called by Casdoor SDK via Client ID & Client Secret, so the used Email provider will be the application' Email provider or the default Email provider - var ok bool - provider, _, ok = c.GetProviderFromContext("Email") - if !ok { + provider, err = c.GetProviderFromContext("Email") + if err != nil { + c.ResponseError(err.Error()) return } } @@ -127,13 +127,14 @@ func (c *ApiController) SendEmail() { // @Success 200 {object} Response object // @router /api/send-sms [post] func (c *ApiController) SendSms() { - provider, _, ok := c.GetProviderFromContext("SMS") - if !ok { + provider, err := c.GetProviderFromContext("SMS") + if err != nil { + c.ResponseError(err.Error()) return } var smsForm SmsForm - err := json.Unmarshal(c.Ctx.Input.RequestBody, &smsForm) + err = json.Unmarshal(c.Ctx.Input.RequestBody, &smsForm) if err != nil { c.ResponseError(err.Error()) return diff --git a/controllers/util.go b/controllers/util.go index 036b5303..eb481b1b 100644 --- a/controllers/util.go +++ b/controllers/util.go @@ -139,47 +139,46 @@ func (c *ApiController) IsMaskedEnabled() (bool, bool) { return true, isMaskEnabled } -func (c *ApiController) GetProviderFromContext(category string) (*object.Provider, *object.User, bool) { +func (c *ApiController) GetProviderFromContext(category string) (*object.Provider, error) { providerName := c.Input().Get("provider") if providerName != "" { provider, err := object.GetProvider(util.GetId("admin", providerName)) if err != nil { - panic(err) + return nil, err } if provider == nil { - c.ResponseError(fmt.Sprintf(c.T("util:The provider: %s is not found"), providerName)) - return nil, nil, false + err = fmt.Errorf(c.T("util:The provider: %s is not found"), providerName) + return nil, err } - return provider, nil, true + + return provider, nil } userId, ok := c.RequireSignedIn() if !ok { - return nil, nil, false + return nil, fmt.Errorf(c.T("general:Please login first")) } - application, user, err := object.GetApplicationByUserId(userId) + application, err := object.GetApplicationByUserId(userId) if err != nil { - panic(err) + return nil, err } if application == nil { - c.ResponseError(fmt.Sprintf(c.T("util:No application is found for userId: %s"), userId)) - return nil, nil, false + return nil, fmt.Errorf(c.T("util:No application is found for userId: %s"), userId) } provider, err := application.GetProviderByCategory(category) if err != nil { - panic(err) + return nil, err } if provider == nil { - c.ResponseError(fmt.Sprintf(c.T("util:No provider for category: %s is found for application: %s"), category, application.Name)) - return nil, nil, false + return nil, fmt.Errorf(c.T("util:No provider for category: %s is found for application: %s"), category, application.Name) } - return provider, user, true + return provider, nil } func checkQuotaForApplication(count int) error { diff --git a/object/application.go b/object/application.go index ec45add2..b7a96076 100644 --- a/object/application.go +++ b/object/application.go @@ -235,16 +235,16 @@ func GetApplicationByUser(user *User) (*Application, error) { } } -func GetApplicationByUserId(userId string) (application *Application, user *User, err error) { +func GetApplicationByUserId(userId string) (application *Application, err error) { owner, name := util.GetOwnerAndNameFromId(userId) if owner == "app" { application, err = getApplication("admin", name) return } - user, err = GetUser(userId) + user, err := GetUser(userId) if err != nil { - return nil, nil, err + return nil, err } application, err = GetApplicationByUser(user) return diff --git a/routers/auto_signin_filter.go b/routers/auto_signin_filter.go index fea0d6b0..345a8583 100644 --- a/routers/auto_signin_filter.go +++ b/routers/auto_signin_filter.go @@ -49,7 +49,7 @@ func AutoSigninFilter(ctx *context.Context) { } userId := util.GetId(token.Organization, token.User) - application, _, err := object.GetApplicationByUserId(fmt.Sprintf("app/%s", token.Application)) + application, err := object.GetApplicationByUserId(fmt.Sprintf("app/%s", token.Application)) if err != nil { panic(err) }