2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2020-12-20 23:24:09 +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
|
|
|
|
|
|
|
|
import (
|
2021-12-23 01:01:23 +08:00
|
|
|
"fmt"
|
2022-06-26 01:28:33 +08:00
|
|
|
"net/url"
|
2022-09-10 13:12:36 +08:00
|
|
|
"regexp"
|
2022-03-02 20:37:31 +08:00
|
|
|
"strings"
|
2021-12-23 01:01:23 +08:00
|
|
|
|
2022-11-13 15:05:15 +08:00
|
|
|
"github.com/casdoor/casdoor/idp"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/util"
|
2020-12-20 23:24:09 +08:00
|
|
|
"xorm.io/core"
|
|
|
|
)
|
|
|
|
|
2022-06-16 20:52:54 +08:00
|
|
|
type SignupItem struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Visible bool `json:"visible"`
|
|
|
|
Required bool `json:"required"`
|
|
|
|
Prompted bool `json:"prompted"`
|
|
|
|
Rule string `json:"rule"`
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:24:09 +08:00
|
|
|
type Application struct {
|
|
|
|
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
|
|
|
|
Name string `xorm:"varchar(100) notnull pk" json:"name"`
|
|
|
|
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
|
|
|
|
2021-12-28 17:13:45 +08:00
|
|
|
DisplayName string `xorm:"varchar(100)" json:"displayName"`
|
|
|
|
Logo string `xorm:"varchar(100)" json:"logo"`
|
|
|
|
HomepageUrl string `xorm:"varchar(100)" json:"homepageUrl"`
|
|
|
|
Description string `xorm:"varchar(100)" json:"description"`
|
|
|
|
Organization string `xorm:"varchar(100)" json:"organization"`
|
2021-12-31 09:36:48 +08:00
|
|
|
Cert string `xorm:"varchar(100)" json:"cert"`
|
2021-12-28 17:13:45 +08:00
|
|
|
EnablePassword bool `json:"enablePassword"`
|
|
|
|
EnableSignUp bool `json:"enableSignUp"`
|
|
|
|
EnableSigninSession bool `json:"enableSigninSession"`
|
2022-09-27 20:06:46 +08:00
|
|
|
EnableAutoSignin bool `json:"enableAutoSignin"`
|
2021-12-28 17:13:45 +08:00
|
|
|
EnableCodeSignin bool `json:"enableCodeSignin"`
|
2022-06-28 22:05:02 +08:00
|
|
|
EnableSamlCompress bool `json:"enableSamlCompress"`
|
2022-07-12 20:06:01 +08:00
|
|
|
EnableWebAuthn bool `json:"enableWebAuthn"`
|
2022-12-13 22:32:45 +08:00
|
|
|
SamlReplyUrl string `xorm:"varchar(100)" json:"samlReplyUrl"`
|
2021-12-28 17:13:45 +08:00
|
|
|
Providers []*ProviderItem `xorm:"mediumtext" json:"providers"`
|
|
|
|
SignupItems []*SignupItem `xorm:"varchar(1000)" json:"signupItems"`
|
2022-02-27 14:05:07 +08:00
|
|
|
GrantTypes []string `xorm:"varchar(1000)" json:"grantTypes"`
|
2021-12-28 17:13:45 +08:00
|
|
|
OrganizationObj *Organization `xorm:"-" json:"organizationObj"`
|
2021-03-06 16:39:17 +08:00
|
|
|
|
2021-12-18 18:49:38 +08:00
|
|
|
ClientId string `xorm:"varchar(100)" json:"clientId"`
|
|
|
|
ClientSecret string `xorm:"varchar(100)" json:"clientSecret"`
|
|
|
|
RedirectUris []string `xorm:"varchar(1000)" json:"redirectUris"`
|
|
|
|
TokenFormat string `xorm:"varchar(100)" json:"tokenFormat"`
|
|
|
|
ExpireInHours int `json:"expireInHours"`
|
|
|
|
RefreshExpireInHours int `json:"refreshExpireInHours"`
|
|
|
|
SignupUrl string `xorm:"varchar(200)" json:"signupUrl"`
|
|
|
|
SigninUrl string `xorm:"varchar(200)" json:"signinUrl"`
|
|
|
|
ForgetUrl string `xorm:"varchar(200)" json:"forgetUrl"`
|
|
|
|
AffiliationUrl string `xorm:"varchar(100)" json:"affiliationUrl"`
|
|
|
|
TermsOfUse string `xorm:"varchar(100)" json:"termsOfUse"`
|
|
|
|
SignupHtml string `xorm:"mediumtext" json:"signupHtml"`
|
|
|
|
SigninHtml string `xorm:"mediumtext" json:"signinHtml"`
|
2022-09-10 00:56:37 +08:00
|
|
|
FormCss string `xorm:"text" json:"formCss"`
|
|
|
|
FormOffset int `json:"formOffset"`
|
2022-10-22 21:43:41 +08:00
|
|
|
FormSideHtml string `xorm:"mediumtext" json:"formSideHtml"`
|
2022-09-10 00:56:37 +08:00
|
|
|
FormBackgroundUrl string `xorm:"varchar(200)" json:"formBackgroundUrl"`
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:55:10 +08:00
|
|
|
func GetApplicationCount(owner, field, value string) int {
|
2022-01-26 19:36:36 +08:00
|
|
|
session := GetSession(owner, -1, -1, field, value, "", "")
|
2021-12-25 10:55:10 +08:00
|
|
|
count, err := session.Count(&Application{})
|
2021-11-06 11:32:22 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(count)
|
|
|
|
}
|
|
|
|
|
2022-11-21 01:17:55 +08:00
|
|
|
func GetOrganizationApplicationCount(owner, Organization, field, value string) int {
|
|
|
|
session := GetSession(owner, -1, -1, field, value, "", "")
|
|
|
|
count, err := session.Count(&Application{Organization: Organization})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(count)
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:24:09 +08:00
|
|
|
func GetApplications(owner string) []*Application {
|
|
|
|
applications := []*Application{}
|
2021-05-02 10:30:12 +08:00
|
|
|
err := adapter.Engine.Desc("created_time").Find(&applications, &Application{Owner: owner})
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return applications
|
|
|
|
}
|
|
|
|
|
2022-11-21 01:17:55 +08:00
|
|
|
func GetOrganizationApplications(owner string, organization string) []*Application {
|
2021-11-06 11:32:22 +08:00
|
|
|
applications := []*Application{}
|
2022-11-21 01:17:55 +08:00
|
|
|
err := adapter.Engine.Desc("created_time").Find(&applications, &Application{Owner: owner, Organization: organization})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return applications
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPaginationApplications(owner string, offset, limit int, field, value, sortField, sortOrder string) []*Application {
|
|
|
|
var applications []*Application
|
2021-12-25 10:55:10 +08:00
|
|
|
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
|
|
|
|
err := session.Find(&applications)
|
2021-11-06 11:32:22 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return applications
|
|
|
|
}
|
|
|
|
|
2022-11-21 01:17:55 +08:00
|
|
|
func GetPaginationOrganizationApplications(owner, organization string, offset, limit int, field, value, sortField, sortOrder string) []*Application {
|
2021-12-23 01:01:23 +08:00
|
|
|
applications := []*Application{}
|
2022-11-21 01:17:55 +08:00
|
|
|
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
|
|
|
|
err := session.Find(&applications, &Application{Owner: owner, Organization: organization})
|
2021-12-23 01:01:23 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return applications
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:35:19 +08:00
|
|
|
func getProviderMap(owner string) map[string]*Provider {
|
|
|
|
providers := GetProviders(owner)
|
2021-03-20 11:34:04 +08:00
|
|
|
m := map[string]*Provider{}
|
|
|
|
for _, provider := range providers {
|
2022-11-13 15:05:15 +08:00
|
|
|
// Get QRCode only once
|
|
|
|
if provider.Type == "WeChat" && provider.DisableSsl == true && provider.Content == "" {
|
|
|
|
provider.Content, _ = idp.GetWechatOfficialAccountQRCode(provider.ClientId2, provider.ClientSecret2)
|
|
|
|
UpdateProvider(provider.Owner+"/"+provider.Name, provider)
|
|
|
|
}
|
2021-05-23 23:38:38 +08:00
|
|
|
|
2021-12-20 23:46:38 +08:00
|
|
|
m[provider.Name] = GetMaskedProvider(provider)
|
2021-03-20 11:34:04 +08:00
|
|
|
}
|
2021-06-14 21:35:19 +08:00
|
|
|
return m
|
|
|
|
}
|
2021-03-20 11:34:04 +08:00
|
|
|
|
2021-06-14 21:35:19 +08:00
|
|
|
func extendApplicationWithProviders(application *Application) {
|
2022-11-21 01:17:55 +08:00
|
|
|
m := getProviderMap(application.Organization)
|
2021-06-14 19:09:04 +08:00
|
|
|
for _, providerItem := range application.Providers {
|
|
|
|
if provider, ok := m[providerItem.Name]; ok {
|
2021-06-14 21:35:19 +08:00
|
|
|
providerItem.Provider = provider
|
2021-05-24 01:02:38 +08:00
|
|
|
}
|
2021-03-20 11:34:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 21:28:24 +08:00
|
|
|
func extendApplicationWithOrg(application *Application) {
|
|
|
|
organization := getOrganization(application.Owner, application.Organization)
|
|
|
|
application.OrganizationObj = organization
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:24:09 +08:00
|
|
|
func getApplication(owner string, name string) *Application {
|
2021-06-21 01:01:16 +08:00
|
|
|
if owner == "" || name == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:24:09 +08:00
|
|
|
application := Application{Owner: owner, Name: name}
|
2021-05-02 10:30:12 +08:00
|
|
|
existed, err := adapter.Engine.Get(&application)
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
2021-04-29 21:28:24 +08:00
|
|
|
extendApplicationWithProviders(&application)
|
|
|
|
extendApplicationWithOrg(&application)
|
2020-12-20 23:24:09 +08:00
|
|
|
return &application
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 01:02:38 +08:00
|
|
|
func GetApplicationByOrganizationName(organization string) *Application {
|
2021-05-16 23:07:45 +08:00
|
|
|
application := Application{}
|
|
|
|
existed, err := adapter.Engine.Where("organization=?", organization).Get(&application)
|
2021-04-19 01:14:41 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
2021-04-29 21:28:24 +08:00
|
|
|
extendApplicationWithProviders(&application)
|
|
|
|
extendApplicationWithOrg(&application)
|
2021-04-19 01:14:41 +08:00
|
|
|
return &application
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 23:07:45 +08:00
|
|
|
func GetApplicationByUser(user *User) *Application {
|
2021-07-18 17:50:38 +08:00
|
|
|
if user.SignupApplication != "" {
|
|
|
|
return getApplication("admin", user.SignupApplication)
|
|
|
|
} else {
|
|
|
|
return GetApplicationByOrganizationName(user.Owner)
|
|
|
|
}
|
2021-05-16 23:07:45 +08:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:03:29 +08:00
|
|
|
func GetApplicationByUserId(userId string) (*Application, *User) {
|
|
|
|
var application *Application
|
|
|
|
|
|
|
|
owner, name := util.GetOwnerAndNameFromId(userId)
|
|
|
|
if owner == "app" {
|
|
|
|
application = getApplication("admin", name)
|
|
|
|
return application, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
user := GetUser(userId)
|
|
|
|
application = GetApplicationByUser(user)
|
|
|
|
|
|
|
|
return application, user
|
|
|
|
}
|
|
|
|
|
2021-06-06 17:27:03 +08:00
|
|
|
func GetApplicationByClientId(clientId string) *Application {
|
2021-03-14 18:18:03 +08:00
|
|
|
application := Application{}
|
2021-05-02 10:30:12 +08:00
|
|
|
existed, err := adapter.Engine.Where("client_id=?", clientId).Get(&application)
|
2021-03-14 18:18:03 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
2021-04-29 21:28:24 +08:00
|
|
|
extendApplicationWithProviders(&application)
|
|
|
|
extendApplicationWithOrg(&application)
|
2021-03-14 18:18:03 +08:00
|
|
|
return &application
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:12:11 +08:00
|
|
|
func GetApplication(id string) *Application {
|
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
|
|
return getApplication(owner, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMaskedApplication(application *Application, userId string) *Application {
|
|
|
|
if isUserIdGlobalAdmin(userId) {
|
|
|
|
return application
|
2021-07-30 14:15:10 +08:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:12:11 +08:00
|
|
|
if application == nil {
|
2021-07-30 14:15:10 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:12:11 +08:00
|
|
|
if application.ClientSecret != "" {
|
|
|
|
application.ClientSecret = "***"
|
|
|
|
}
|
2022-03-02 20:58:16 +08:00
|
|
|
|
|
|
|
if application.OrganizationObj != nil {
|
|
|
|
if application.OrganizationObj.MasterPassword != "" {
|
|
|
|
application.OrganizationObj.MasterPassword = "***"
|
|
|
|
}
|
|
|
|
if application.OrganizationObj.PasswordType != "" {
|
|
|
|
application.OrganizationObj.PasswordType = "***"
|
|
|
|
}
|
|
|
|
if application.OrganizationObj.PasswordSalt != "" {
|
|
|
|
application.OrganizationObj.PasswordSalt = "***"
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 23:21:09 +08:00
|
|
|
return application
|
2021-07-30 14:15:10 +08:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:12:11 +08:00
|
|
|
func GetMaskedApplications(applications []*Application, userId string) []*Application {
|
|
|
|
if isUserIdGlobalAdmin(userId) {
|
|
|
|
return applications
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, application := range applications {
|
|
|
|
application = GetMaskedApplication(application, userId)
|
|
|
|
}
|
|
|
|
return applications
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateApplication(id string, application *Application) bool {
|
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
|
|
if getApplication(owner, name) == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-23 00:40:07 +08:00
|
|
|
if name == "app-built-in" {
|
|
|
|
application.Name = name
|
|
|
|
}
|
|
|
|
|
2022-11-02 00:17:38 +08:00
|
|
|
if name != application.Name {
|
|
|
|
err := applicationChangeTrigger(name, application.Name)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:35:19 +08:00
|
|
|
for _, providerItem := range application.Providers {
|
|
|
|
providerItem.Provider = nil
|
|
|
|
}
|
|
|
|
|
2022-05-10 17:37:12 +08:00
|
|
|
session := adapter.Engine.ID(core.PK{owner, name}).AllCols()
|
|
|
|
if application.ClientSecret == "***" {
|
|
|
|
session.Omit("client_secret")
|
|
|
|
}
|
|
|
|
affected, err := session.Update(application)
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-03-28 00:48:34 +08:00
|
|
|
return affected != 0
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func AddApplication(application *Application) bool {
|
2022-07-06 19:27:59 +08:00
|
|
|
if application.ClientId == "" {
|
|
|
|
application.ClientId = util.GenerateClientId()
|
|
|
|
}
|
|
|
|
if application.ClientSecret == "" {
|
|
|
|
application.ClientSecret = util.GenerateClientSecret()
|
|
|
|
}
|
2021-06-14 21:35:19 +08:00
|
|
|
for _, providerItem := range application.Providers {
|
|
|
|
providerItem.Provider = nil
|
|
|
|
}
|
2021-03-06 16:39:17 +08:00
|
|
|
|
2021-05-02 10:30:12 +08:00
|
|
|
affected, err := adapter.Engine.Insert(application)
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteApplication(application *Application) bool {
|
2021-12-23 00:40:07 +08:00
|
|
|
if application.Name == "app-built-in" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-02 10:30:12 +08:00
|
|
|
affected, err := adapter.Engine.ID(core.PK{application.Owner, application.Name}).Delete(&Application{})
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
2021-12-23 01:01:23 +08:00
|
|
|
|
|
|
|
func (application *Application) GetId() string {
|
|
|
|
return fmt.Sprintf("%s/%s", application.Owner, application.Name)
|
|
|
|
}
|
2022-03-02 20:37:31 +08:00
|
|
|
|
|
|
|
func CheckRedirectUriValid(application *Application, redirectUri string) bool {
|
2022-08-07 12:26:14 +08:00
|
|
|
validUri := false
|
2022-03-02 20:37:31 +08:00
|
|
|
for _, tmpUri := range application.RedirectUris {
|
2022-09-10 13:12:36 +08:00
|
|
|
tmpUriRegex := regexp.MustCompile(tmpUri)
|
|
|
|
if tmpUriRegex.MatchString(redirectUri) || strings.Contains(redirectUri, tmpUri) {
|
2022-03-02 20:37:31 +08:00
|
|
|
validUri = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return validUri
|
|
|
|
}
|
2022-06-26 01:28:33 +08:00
|
|
|
|
|
|
|
func IsAllowOrigin(origin string) bool {
|
|
|
|
allowOrigin := false
|
|
|
|
originUrl, err := url.Parse(origin)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := adapter.Engine.Cols("redirect_uris").Rows(&Application{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
application := Application{}
|
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&application)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, tmpRedirectUri := range application.RedirectUris {
|
|
|
|
u1, err := url.Parse(tmpRedirectUri)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if u1.Scheme == originUrl.Scheme && u1.Host == originUrl.Host {
|
|
|
|
allowOrigin = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if allowOrigin {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allowOrigin
|
|
|
|
}
|
2022-08-30 00:57:27 +08:00
|
|
|
|
|
|
|
func getApplicationMap(organization string) map[string]*Application {
|
2022-11-21 01:17:55 +08:00
|
|
|
applications := GetOrganizationApplications("admin", organization)
|
2022-08-30 00:57:27 +08:00
|
|
|
|
|
|
|
applicationMap := make(map[string]*Application)
|
|
|
|
for _, application := range applications {
|
|
|
|
applicationMap[application.Name] = application
|
|
|
|
}
|
|
|
|
|
|
|
|
return applicationMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExtendManagedAccountsWithUser(user *User) *User {
|
|
|
|
if user.ManagedAccounts == nil || len(user.ManagedAccounts) == 0 {
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
applicationMap := getApplicationMap(user.Owner)
|
|
|
|
|
|
|
|
var managedAccounts []ManagedAccount
|
|
|
|
for _, managedAccount := range user.ManagedAccounts {
|
|
|
|
application := applicationMap[managedAccount.Application]
|
|
|
|
if application != nil {
|
|
|
|
managedAccount.SigninUrl = application.SigninUrl
|
|
|
|
managedAccounts = append(managedAccounts, managedAccount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.ManagedAccounts = managedAccounts
|
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
2022-11-02 00:17:38 +08:00
|
|
|
|
|
|
|
func applicationChangeTrigger(oldName string, newName string) error {
|
|
|
|
session := adapter.Engine.NewSession()
|
|
|
|
defer session.Close()
|
|
|
|
|
|
|
|
err := session.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
organization := new(Organization)
|
|
|
|
organization.DefaultApplication = newName
|
|
|
|
_, err = session.Where("default_application=?", oldName).Update(organization)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
user := new(User)
|
|
|
|
user.SignupApplication = newName
|
|
|
|
_, err = session.Where("signup_application=?", oldName).Update(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resource := new(Resource)
|
|
|
|
resource.Application = newName
|
|
|
|
_, err = session.Where("application=?", oldName).Update(resource)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var permissions []*Permission
|
|
|
|
err = adapter.Engine.Find(&permissions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := 0; i < len(permissions); i++ {
|
|
|
|
permissionResoureces := permissions[i].Resources
|
|
|
|
for j := 0; j < len(permissionResoureces); j++ {
|
|
|
|
if permissionResoureces[j] == oldName {
|
|
|
|
permissionResoureces[j] = newName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
permissions[i].Resources = permissionResoureces
|
|
|
|
_, err = session.Where("name=?", permissions[i].Name).Update(permissions[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.Commit()
|
|
|
|
}
|