Return error in GetProviderFromContext()

This commit is contained in:
Yang Luo 2023-06-10 15:51:26 +08:00
parent 0bda29f143
commit e0d2bc3dc9
5 changed files with 30 additions and 28 deletions

View File

@ -139,8 +139,9 @@ func (c *ApiController) DeleteResource() {
return return
} }
provider, _, ok := c.GetProviderFromContext("Storage") provider, err := c.GetProviderFromContext("Storage")
if !ok { if err != nil {
c.ResponseError(err.Error())
return return
} }
@ -187,8 +188,9 @@ func (c *ApiController) UploadResource() {
return return
} }
provider, _, ok := c.GetProviderFromContext("Storage") provider, err := c.GetProviderFromContext("Storage")
if !ok { if err != nil {
c.ResponseError(err.Error())
return return
} }

View File

@ -69,9 +69,9 @@ func (c *ApiController) SendEmail() {
} else { } 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 // 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, err = c.GetProviderFromContext("Email")
provider, _, ok = c.GetProviderFromContext("Email") if err != nil {
if !ok { c.ResponseError(err.Error())
return return
} }
} }
@ -127,13 +127,14 @@ func (c *ApiController) SendEmail() {
// @Success 200 {object} Response object // @Success 200 {object} Response object
// @router /api/send-sms [post] // @router /api/send-sms [post]
func (c *ApiController) SendSms() { func (c *ApiController) SendSms() {
provider, _, ok := c.GetProviderFromContext("SMS") provider, err := c.GetProviderFromContext("SMS")
if !ok { if err != nil {
c.ResponseError(err.Error())
return return
} }
var smsForm SmsForm var smsForm SmsForm
err := json.Unmarshal(c.Ctx.Input.RequestBody, &smsForm) err = json.Unmarshal(c.Ctx.Input.RequestBody, &smsForm)
if err != nil { if err != nil {
c.ResponseError(err.Error()) c.ResponseError(err.Error())
return return

View File

@ -139,47 +139,46 @@ func (c *ApiController) IsMaskedEnabled() (bool, bool) {
return true, isMaskEnabled 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") providerName := c.Input().Get("provider")
if providerName != "" { if providerName != "" {
provider, err := object.GetProvider(util.GetId("admin", providerName)) provider, err := object.GetProvider(util.GetId("admin", providerName))
if err != nil { if err != nil {
panic(err) return nil, err
} }
if provider == nil { if provider == nil {
c.ResponseError(fmt.Sprintf(c.T("util:The provider: %s is not found"), providerName)) err = fmt.Errorf(c.T("util:The provider: %s is not found"), providerName)
return nil, nil, false return nil, err
} }
return provider, nil, true
return provider, nil
} }
userId, ok := c.RequireSignedIn() userId, ok := c.RequireSignedIn()
if !ok { 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 { if err != nil {
panic(err) return nil, err
} }
if application == nil { if application == nil {
c.ResponseError(fmt.Sprintf(c.T("util:No application is found for userId: %s"), userId)) return nil, fmt.Errorf(c.T("util:No application is found for userId: %s"), userId)
return nil, nil, false
} }
provider, err := application.GetProviderByCategory(category) provider, err := application.GetProviderByCategory(category)
if err != nil { if err != nil {
panic(err) return nil, err
} }
if provider == nil { 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, fmt.Errorf(c.T("util:No provider for category: %s is found for application: %s"), category, application.Name)
return nil, nil, false
} }
return provider, user, true return provider, nil
} }
func checkQuotaForApplication(count int) error { func checkQuotaForApplication(count int) error {

View File

@ -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) owner, name := util.GetOwnerAndNameFromId(userId)
if owner == "app" { if owner == "app" {
application, err = getApplication("admin", name) application, err = getApplication("admin", name)
return return
} }
user, err = GetUser(userId) user, err := GetUser(userId)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
application, err = GetApplicationByUser(user) application, err = GetApplicationByUser(user)
return return

View File

@ -49,7 +49,7 @@ func AutoSigninFilter(ctx *context.Context) {
} }
userId := util.GetId(token.Organization, token.User) 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 { if err != nil {
panic(err) panic(err)
} }