casdoor/object/application_item.go

180 lines
4.6 KiB
Go
Raw Permalink Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-06-20 09:46:06 +08:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
func (application *Application) GetProviderByCategory(category string) (*Provider, error) {
providers, err := GetProviders(application.Organization)
if err != nil {
return nil, err
}
2021-06-20 09:46:06 +08:00
m := map[string]*Provider{}
for _, provider := range providers {
if provider.Category != category {
continue
}
m[provider.Name] = provider
}
for _, providerItem := range application.Providers {
if provider, ok := m[providerItem.Name]; ok {
return provider, nil
2021-06-20 09:46:06 +08:00
}
}
return nil, nil
2021-06-20 09:46:06 +08:00
}
func isProviderItemCountryCodeMatched(providerItem *ProviderItem, countryCode string) bool {
2024-05-01 09:47:44 +08:00
if len(providerItem.CountryCodes) == 0 {
return true
}
2024-05-01 09:47:44 +08:00
for _, countryCode2 := range providerItem.CountryCodes {
if countryCode2 == "" || countryCode2 == "All" || countryCode2 == "all" || countryCode2 == countryCode {
return true
}
}
return false
}
func (application *Application) GetProviderByCategoryAndRule(category string, method string, countryCode string) (*Provider, error) {
providers, err := GetProviders(application.Organization)
if err != nil {
return nil, err
}
m := map[string]*Provider{}
for _, provider := range providers {
if provider.Category != category {
continue
}
m[provider.Name] = provider
}
for _, providerItem := range application.Providers {
if providerItem.Provider != nil && providerItem.Provider.Category == "SMS" {
if !isProviderItemCountryCodeMatched(providerItem, countryCode) {
continue
}
}
if providerItem.Rule == method || providerItem.Rule == "" || providerItem.Rule == "All" || providerItem.Rule == "all" || providerItem.Rule == "None" {
if provider, ok := m[providerItem.Name]; ok {
return provider, nil
}
}
}
return nil, nil
}
func (application *Application) GetEmailProvider(method string) (*Provider, error) {
return application.GetProviderByCategoryAndRule("Email", method, "All")
2021-06-20 09:46:06 +08:00
}
func (application *Application) GetSmsProvider(method string, countryCode string) (*Provider, error) {
return application.GetProviderByCategoryAndRule("SMS", method, countryCode)
2021-06-20 09:46:06 +08:00
}
func (application *Application) GetStorageProvider() (*Provider, error) {
2021-09-05 09:44:15 +08:00
return application.GetProviderByCategory("Storage")
}
2021-06-20 09:46:06 +08:00
func (application *Application) getSignupItem(itemName string) *SignupItem {
for _, signupItem := range application.SignupItems {
if signupItem.Name == itemName {
return signupItem
}
}
return nil
}
func (application *Application) IsSignupItemVisible(itemName string) bool {
signupItem := application.getSignupItem(itemName)
if signupItem == nil {
return false
}
return signupItem.Visible
}
func (application *Application) IsSignupItemRequired(itemName string) bool {
signupItem := application.getSignupItem(itemName)
if signupItem == nil {
return false
}
return signupItem.Required
}
2022-08-21 19:46:59 +08:00
func (si *SignupItem) isSignupItemPrompted() bool {
return si.Visible && si.Prompted
}
2021-06-20 09:46:06 +08:00
func (application *Application) GetSignupItemRule(itemName string) string {
signupItem := application.getSignupItem(itemName)
if signupItem == nil {
return ""
}
return signupItem.Rule
}
func (application *Application) getAllPromptedProviderItems() []*ProviderItem {
res := []*ProviderItem{}
for _, providerItem := range application.Providers {
if providerItem.isProviderPrompted() {
res = append(res, providerItem)
}
}
return res
}
2022-08-21 19:46:59 +08:00
func (application *Application) getAllPromptedSignupItems() []*SignupItem {
res := []*SignupItem{}
for _, signupItem := range application.SignupItems {
if signupItem.isSignupItemPrompted() {
res = append(res, signupItem)
}
}
return res
}
2021-06-20 09:46:06 +08:00
func (application *Application) isAffiliationPrompted() bool {
signupItem := application.getSignupItem("Affiliation")
if signupItem == nil {
return false
}
return signupItem.Prompted
}
func (application *Application) HasPromptPage() bool {
providerItems := application.getAllPromptedProviderItems()
if len(providerItems) != 0 {
return true
}
2022-08-21 19:46:59 +08:00
signupItems := application.getAllPromptedSignupItems()
if len(signupItems) != 0 {
return true
}
2021-06-20 09:46:06 +08:00
return application.isAffiliationPrompted()
}