diff --git a/controllers/resource.go b/controllers/resource.go index b9281595..04f4a5ad 100644 --- a/controllers/resource.go +++ b/controllers/resource.go @@ -64,31 +64,6 @@ func (c *ApiController) AddResource() { c.ServeJSON() } -func (c *ApiController) GetProviderParam() (*object.Provider, *object.User, bool) { - providerName := c.Input().Get("provider") - if providerName != "" { - provider := object.GetProvider(util.GetId(providerName)) - if provider == nil { - c.ResponseError(fmt.Sprintf("The provider: %s is not found", providerName)) - return nil, nil, false - } - return provider, nil, true - } - - userId, ok := c.RequireSignedIn() - if !ok { - return nil, nil, false - } - - application, user := object.GetApplicationByUserId(userId) - provider := application.GetStorageProvider() - if provider == nil { - c.ResponseError(fmt.Sprintf("No storage provider is found for application: %s", application.Name)) - return nil, nil, false - } - return provider, user, true -} - func (c *ApiController) DeleteResource() { var resource object.Resource err := json.Unmarshal(c.Ctx.Input.RequestBody, &resource) @@ -96,7 +71,7 @@ func (c *ApiController) DeleteResource() { panic(err) } - provider, _, ok := c.GetProviderParam() + provider, _, ok := c.GetProviderFromContext("Storage") if !ok { return } @@ -132,7 +107,7 @@ func (c *ApiController) UploadResource() { return } - provider, user, ok := c.GetProviderParam() + provider, user, ok := c.GetProviderFromContext("Storage") if !ok { return } diff --git a/controllers/util.go b/controllers/util.go index 7b2b03a3..a7447f5e 100644 --- a/controllers/util.go +++ b/controllers/util.go @@ -15,9 +15,12 @@ package controllers import ( + "fmt" "strconv" "github.com/astaxie/beego" + "github.com/casbin/casdoor/object" + "github.com/casbin/casdoor/util" ) // ResponseOk ... @@ -66,3 +69,28 @@ func getInitScore() int { return score } + +func (c *ApiController) GetProviderFromContext(category string) (*object.Provider, *object.User, bool) { + providerName := c.Input().Get("provider") + if providerName != "" { + provider := object.GetProvider(util.GetId(providerName)) + if provider == nil { + c.ResponseError(fmt.Sprintf("The provider: %s is not found", providerName)) + return nil, nil, false + } + return provider, nil, true + } + + userId, ok := c.RequireSignedIn() + if !ok { + return nil, nil, false + } + + application, user := object.GetApplicationByUserId(userId) + provider := application.GetProviderByCategory(category) + if provider == nil { + c.ResponseError(fmt.Sprintf("No provider for category: \"%s\" is found for application: %s", category, application.Name)) + return nil, nil, false + } + return provider, user, true +} diff --git a/object/application_item.go b/object/application_item.go index 9b5c1e70..d186ef72 100644 --- a/object/application_item.go +++ b/object/application_item.go @@ -14,7 +14,7 @@ package object -func (application *Application) getProviderByCategory(category string) *Provider { +func (application *Application) GetProviderByCategory(category string) *Provider { providers := GetProviders(application.Owner) m := map[string]*Provider{} for _, provider := range providers { @@ -35,15 +35,15 @@ func (application *Application) getProviderByCategory(category string) *Provider } func (application *Application) GetEmailProvider() *Provider { - return application.getProviderByCategory("Email") + return application.GetProviderByCategory("Email") } func (application *Application) GetSmsProvider() *Provider { - return application.getProviderByCategory("SMS") + return application.GetProviderByCategory("SMS") } func (application *Application) GetStorageProvider() *Provider { - return application.getProviderByCategory("Storage") + return application.GetProviderByCategory("Storage") } func (application *Application) getSignupItem(itemName string) *SignupItem { diff --git a/object/email.go b/object/email.go index 21608b1c..f5c39ea6 100644 --- a/object/email.go +++ b/object/email.go @@ -18,7 +18,7 @@ package object import "github.com/go-gomail/gomail" -func SendEmail(provider *Provider, title, content, dest, sender string) error { +func SendEmail(provider *Provider, title string, content string, dest string, sender string) error { dialer := gomail.NewDialer(provider.Host, provider.Port, provider.ClientId, provider.ClientSecret) message := gomail.NewMessage()