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-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
|
|
|
|
2024-03-03 21:05:44 +08:00
|
|
|
"github.com/casdoor/casdoor/i18n"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/util"
|
2023-02-12 09:33:24 +08:00
|
|
|
"github.com/xorm-io/core"
|
2020-12-20 23:24:09 +08:00
|
|
|
)
|
|
|
|
|
2024-01-02 21:11:52 +08:00
|
|
|
type SigninMethod struct {
|
|
|
|
Name string `xorm:"varchar(100) notnull pk" json:"name"`
|
|
|
|
DisplayName string `xorm:"varchar(100)" json:"displayName"`
|
|
|
|
Rule string `json:"rule"`
|
|
|
|
}
|
|
|
|
|
2022-06-16 20:52:54 +08:00
|
|
|
type SignupItem struct {
|
2023-10-15 17:24:38 +08:00
|
|
|
Name string `json:"name"`
|
|
|
|
Visible bool `json:"visible"`
|
|
|
|
Required bool `json:"required"`
|
|
|
|
Prompted bool `json:"prompted"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
Placeholder string `json:"placeholder"`
|
2024-01-13 16:05:45 +08:00
|
|
|
Regex string `json:"regex"`
|
2023-10-15 17:24:38 +08:00
|
|
|
Rule string `json:"rule"`
|
2022-06-16 20:52:54 +08:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:12:40 +08:00
|
|
|
type SigninItem struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Visible bool `json:"visible"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
Placeholder string `json:"placeholder"`
|
|
|
|
Rule string `json:"rule"`
|
|
|
|
IsCustom bool `json:"isCustom"`
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:40:41 +08:00
|
|
|
type SamlItem struct {
|
|
|
|
Name string `json:"name"`
|
2024-01-13 15:19:15 +08:00
|
|
|
NameFormat string `json:"nameFormat"`
|
2023-10-17 15:40:41 +08:00
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
|
2024-02-01 17:28:56 +08:00
|
|
|
DisplayName string `xorm:"varchar(100)" json:"displayName"`
|
|
|
|
Logo string `xorm:"varchar(200)" json:"logo"`
|
|
|
|
HomepageUrl string `xorm:"varchar(100)" json:"homepageUrl"`
|
|
|
|
Description string `xorm:"varchar(100)" json:"description"`
|
|
|
|
Organization string `xorm:"varchar(100)" json:"organization"`
|
|
|
|
Cert string `xorm:"varchar(100)" json:"cert"`
|
2024-02-22 17:56:47 +08:00
|
|
|
HeaderHtml string `xorm:"mediumtext" json:"headerHtml"`
|
2024-02-01 17:28:56 +08:00
|
|
|
EnablePassword bool `json:"enablePassword"`
|
|
|
|
EnableSignUp bool `json:"enableSignUp"`
|
|
|
|
EnableSigninSession bool `json:"enableSigninSession"`
|
|
|
|
EnableAutoSignin bool `json:"enableAutoSignin"`
|
|
|
|
EnableCodeSignin bool `json:"enableCodeSignin"`
|
|
|
|
EnableSamlCompress bool `json:"enableSamlCompress"`
|
|
|
|
EnableSamlC14n10 bool `json:"enableSamlC14n10"`
|
|
|
|
EnableSamlPostBinding bool `json:"enableSamlPostBinding"`
|
|
|
|
EnableWebAuthn bool `json:"enableWebAuthn"`
|
|
|
|
EnableLinkWithEmail bool `json:"enableLinkWithEmail"`
|
|
|
|
OrgChoiceMode string `json:"orgChoiceMode"`
|
|
|
|
SamlReplyUrl string `xorm:"varchar(100)" json:"samlReplyUrl"`
|
|
|
|
Providers []*ProviderItem `xorm:"mediumtext" json:"providers"`
|
|
|
|
SigninMethods []*SigninMethod `xorm:"varchar(2000)" json:"signinMethods"`
|
|
|
|
SignupItems []*SignupItem `xorm:"varchar(2000)" json:"signupItems"`
|
2024-02-13 23:12:40 +08:00
|
|
|
SigninItems []*SigninItem `xorm:"mediumtext" json:"signinItems"`
|
2024-02-01 17:28:56 +08:00
|
|
|
GrantTypes []string `xorm:"varchar(1000)" json:"grantTypes"`
|
|
|
|
OrganizationObj *Organization `xorm:"-" json:"organizationObj"`
|
|
|
|
CertPublicKey string `xorm:"-" json:"certPublicKey"`
|
|
|
|
Tags []string `xorm:"mediumtext" json:"tags"`
|
|
|
|
SamlAttributes []*SamlItem `xorm:"varchar(1000)" json:"samlAttributes"`
|
2021-03-06 16:39:17 +08:00
|
|
|
|
2023-02-01 22:06:40 +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"`
|
2024-01-09 22:09:21 +08:00
|
|
|
TokenFields []string `xorm:"varchar(1000)" json:"tokenFields"`
|
2023-02-01 22:06:40 +08:00
|
|
|
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"`
|
|
|
|
ThemeData *ThemeData `xorm:"json" json:"themeData"`
|
2024-03-08 23:11:03 +08:00
|
|
|
FooterHtml string `xorm:"mediumtext" json:"footerHtml"`
|
2023-02-01 22:06:40 +08:00
|
|
|
FormCss string `xorm:"text" json:"formCss"`
|
2023-05-12 12:16:03 +08:00
|
|
|
FormCssMobile string `xorm:"text" json:"formCssMobile"`
|
2023-02-01 22:06:40 +08:00
|
|
|
FormOffset int `json:"formOffset"`
|
|
|
|
FormSideHtml string `xorm:"mediumtext" json:"formSideHtml"`
|
|
|
|
FormBackgroundUrl string `xorm:"varchar(200)" json:"formBackgroundUrl"`
|
2023-12-20 22:29:53 +08:00
|
|
|
|
|
|
|
FailedSigninLimit int `json:"failedSigninLimit"`
|
2024-01-13 02:12:29 +08:00
|
|
|
FailedSigninFrozenTime int `json:"failedSigninFrozenTime"`
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetApplicationCount(owner, field, value string) (int64, error) {
|
2022-01-26 19:36:36 +08:00
|
|
|
session := GetSession(owner, -1, -1, field, value, "", "")
|
2023-05-30 15:49:39 +08:00
|
|
|
return session.Count(&Application{})
|
2021-11-06 11:32:22 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetOrganizationApplicationCount(owner, Organization, field, value string) (int64, error) {
|
2022-11-21 01:17:55 +08:00
|
|
|
session := GetSession(owner, -1, -1, field, value, "", "")
|
2023-05-30 15:49:39 +08:00
|
|
|
return session.Count(&Application{Organization: Organization})
|
2022-11-21 01:17:55 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetApplications(owner string) ([]*Application, error) {
|
2020-12-20 23:24:09 +08:00
|
|
|
applications := []*Application{}
|
2023-07-29 15:07:04 +08:00
|
|
|
err := ormer.Engine.Desc("created_time").Find(&applications, &Application{Owner: owner})
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return applications, err
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return applications, nil
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetOrganizationApplications(owner string, organization string) ([]*Application, error) {
|
2021-11-06 11:32:22 +08:00
|
|
|
applications := []*Application{}
|
2023-07-29 15:07:04 +08:00
|
|
|
err := ormer.Engine.Desc("created_time").Find(&applications, &Application{Organization: organization})
|
2022-11-21 01:17:55 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return applications, err
|
2022-11-21 01:17:55 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return applications, nil
|
2022-11-21 01:17:55 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetPaginationApplications(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Application, error) {
|
2022-11-21 01:17:55 +08:00
|
|
|
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 {
|
2023-05-30 15:49:39 +08:00
|
|
|
return applications, err
|
2021-11-06 11:32:22 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return applications, nil
|
2021-11-06 11:32:22 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetPaginationOrganizationApplications(owner, organization string, offset, limit int, field, value, sortField, sortOrder string) ([]*Application, error) {
|
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)
|
2023-05-09 00:06:52 +08:00
|
|
|
err := session.Find(&applications, &Application{Organization: organization})
|
2021-12-23 01:01:23 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return applications, err
|
2021-12-23 01:01:23 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return applications, nil
|
2021-12-23 01:01:23 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func getProviderMap(owner string) (m map[string]*Provider, err error) {
|
|
|
|
providers, err := GetProviders(owner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m = map[string]*Provider{}
|
2021-03-20 11:34:04 +08:00
|
|
|
for _, provider := range providers {
|
2023-05-19 13:09:53 +08:00
|
|
|
m[provider.Name] = GetMaskedProvider(provider, true)
|
2021-03-20 11:34:04 +08:00
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
|
|
|
|
return m, err
|
2021-06-14 21:35:19 +08:00
|
|
|
}
|
2021-03-20 11:34:04 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func extendApplicationWithProviders(application *Application) (err error) {
|
|
|
|
m, err := getProviderMap(application.Organization)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
|
|
|
|
return
|
2021-03-20 11:34:04 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func extendApplicationWithOrg(application *Application) (err error) {
|
|
|
|
organization, err := getOrganization(application.Owner, application.Organization)
|
2021-04-29 21:28:24 +08:00
|
|
|
application.OrganizationObj = organization
|
2023-05-30 15:49:39 +08:00
|
|
|
return
|
2021-04-29 21:28:24 +08:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:12:40 +08:00
|
|
|
func extendApplicationWithSigninItems(application *Application) (err error) {
|
|
|
|
if len(application.SigninItems) == 0 {
|
|
|
|
signinItem := &SigninItem{
|
|
|
|
Name: "Back button",
|
|
|
|
Visible: true,
|
|
|
|
Label: "\n<style>\n .back-button {\n top: 65px;\n left: 15px;\n position: absolute;\n }\n</style>\n",
|
|
|
|
Placeholder: "",
|
|
|
|
Rule: "None",
|
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
signinItem = &SigninItem{
|
|
|
|
Name: "Languages",
|
|
|
|
Visible: true,
|
|
|
|
Label: "\n<style>\n .login-languages {\n top: 55px;\n right: 5px;\n position: absolute;\n }\n</style>\n",
|
|
|
|
Placeholder: "",
|
|
|
|
Rule: "None",
|
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
signinItem = &SigninItem{
|
|
|
|
Name: "Logo",
|
|
|
|
Visible: true,
|
2024-03-03 23:46:57 +08:00
|
|
|
Label: "\n<style>\n .login-logo-box {\n }\n</style>\n",
|
2024-02-13 23:12:40 +08:00
|
|
|
Placeholder: "",
|
2024-02-14 12:20:03 +08:00
|
|
|
Rule: "None",
|
2024-02-13 23:12:40 +08:00
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
signinItem = &SigninItem{
|
|
|
|
Name: "Signin methods",
|
|
|
|
Visible: true,
|
2024-03-03 23:46:57 +08:00
|
|
|
Label: "\n<style>\n .signin-methods {\n }\n</style>\n",
|
2024-02-13 23:12:40 +08:00
|
|
|
Placeholder: "",
|
|
|
|
Rule: "None",
|
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
signinItem = &SigninItem{
|
|
|
|
Name: "Username",
|
|
|
|
Visible: true,
|
2024-03-03 23:46:57 +08:00
|
|
|
Label: "\n<style>\n .login-username {\n }\n</style>\n",
|
2024-02-13 23:12:40 +08:00
|
|
|
Placeholder: "",
|
|
|
|
Rule: "None",
|
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
signinItem = &SigninItem{
|
|
|
|
Name: "Password",
|
|
|
|
Visible: true,
|
2024-03-03 23:46:57 +08:00
|
|
|
Label: "\n<style>\n .login-password {\n }\n</style>\n",
|
2024-02-13 23:12:40 +08:00
|
|
|
Placeholder: "",
|
|
|
|
Rule: "None",
|
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
signinItem = &SigninItem{
|
|
|
|
Name: "Agreement",
|
|
|
|
Visible: true,
|
2024-03-03 23:46:57 +08:00
|
|
|
Label: "\n<style>\n .login-agreement {\n }\n</style>\n",
|
2024-02-13 23:12:40 +08:00
|
|
|
Placeholder: "",
|
|
|
|
Rule: "None",
|
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
signinItem = &SigninItem{
|
|
|
|
Name: "Forgot password?",
|
|
|
|
Visible: true,
|
2024-03-03 23:46:57 +08:00
|
|
|
Label: "\n<style>\n .login-forget-password {\n display: inline-flex;\n justify-content: space-between;\n width: 320px;\n margin-bottom: 25px;\n }\n</style>\n",
|
2024-02-13 23:12:40 +08:00
|
|
|
Placeholder: "",
|
|
|
|
Rule: "None",
|
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
signinItem = &SigninItem{
|
|
|
|
Name: "Login button",
|
|
|
|
Visible: true,
|
2024-03-03 23:46:57 +08:00
|
|
|
Label: "\n<style>\n .login-button-box {\n margin-bottom: 5px;\n }\n .login-button {\n width: 100%;\n }\n</style>\n",
|
2024-02-13 23:12:40 +08:00
|
|
|
Placeholder: "",
|
|
|
|
Rule: "None",
|
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
signinItem = &SigninItem{
|
|
|
|
Name: "Signup link",
|
|
|
|
Visible: true,
|
2024-03-03 23:46:57 +08:00
|
|
|
Label: "\n<style>\n .login-signup-link {\n margin-bottom: 24px;\n display: flex;\n justify-content: end;\n}\n</style>\n",
|
2024-02-13 23:12:40 +08:00
|
|
|
Placeholder: "",
|
|
|
|
Rule: "None",
|
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
signinItem = &SigninItem{
|
|
|
|
Name: "Providers",
|
|
|
|
Visible: true,
|
|
|
|
Label: "\n<style>\n .provider-img {\n width: 30px;\n margin: 5px;\n }\n .provider-big-img {\n margin-bottom: 10px;\n }\n</style>\n",
|
|
|
|
Placeholder: "",
|
2024-02-14 09:44:42 +08:00
|
|
|
Rule: "None",
|
2024-02-13 23:12:40 +08:00
|
|
|
}
|
|
|
|
application.SigninItems = append(application.SigninItems, signinItem)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:11:52 +08:00
|
|
|
func extendApplicationWithSigninMethods(application *Application) (err error) {
|
|
|
|
if len(application.SigninMethods) == 0 {
|
|
|
|
if application.EnablePassword {
|
2024-01-08 21:07:34 +08:00
|
|
|
signinMethod := &SigninMethod{Name: "Password", DisplayName: "Password", Rule: "All"}
|
2024-01-02 21:11:52 +08:00
|
|
|
application.SigninMethods = append(application.SigninMethods, signinMethod)
|
|
|
|
}
|
|
|
|
if application.EnableCodeSignin {
|
|
|
|
signinMethod := &SigninMethod{Name: "Verification code", DisplayName: "Verification code", Rule: "All"}
|
|
|
|
application.SigninMethods = append(application.SigninMethods, signinMethod)
|
|
|
|
}
|
|
|
|
if application.EnableWebAuthn {
|
|
|
|
signinMethod := &SigninMethod{Name: "WebAuthn", DisplayName: "WebAuthn", Rule: "None"}
|
|
|
|
application.SigninMethods = append(application.SigninMethods, signinMethod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 22:19:43 +08:00
|
|
|
if len(application.SigninMethods) == 0 {
|
2024-01-08 21:07:34 +08:00
|
|
|
signinMethod := &SigninMethod{Name: "Password", DisplayName: "Password", Rule: "All"}
|
2024-01-03 22:19:43 +08:00
|
|
|
application.SigninMethods = append(application.SigninMethods, signinMethod)
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:11:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func getApplication(owner string, name string) (*Application, error) {
|
2021-06-21 01:01:16 +08:00
|
|
|
if owner == "" || name == "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, nil
|
2021-06-21 01:01:16 +08:00
|
|
|
}
|
|
|
|
|
2020-12-20 23:24:09 +08:00
|
|
|
application := Application{Owner: owner, Name: name}
|
2023-07-29 15:07:04 +08:00
|
|
|
existed, err := ormer.Engine.Get(&application)
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, err
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
2023-05-30 15:49:39 +08:00
|
|
|
err = extendApplicationWithProviders(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = extendApplicationWithOrg(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:11:52 +08:00
|
|
|
err = extendApplicationWithSigninMethods(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-13 23:12:40 +08:00
|
|
|
err = extendApplicationWithSigninItems(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-02 21:11:52 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return &application, nil
|
2020-12-20 23:24:09 +08:00
|
|
|
} else {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, nil
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetApplicationByOrganizationName(organization string) (*Application, error) {
|
2021-05-16 23:07:45 +08:00
|
|
|
application := Application{}
|
2023-07-29 15:07:04 +08:00
|
|
|
existed, err := ormer.Engine.Where("organization=?", organization).Get(&application)
|
2021-04-19 01:14:41 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, nil
|
2021-04-19 01:14:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
2023-05-30 15:49:39 +08:00
|
|
|
err = extendApplicationWithProviders(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = extendApplicationWithOrg(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:11:52 +08:00
|
|
|
err = extendApplicationWithSigninMethods(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:12:40 +08:00
|
|
|
err = extendApplicationWithSigninItems(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return &application, nil
|
2021-04-19 01:14:41 +08:00
|
|
|
} else {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, nil
|
2021-04-19 01:14:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetApplicationByUser(user *User) (*Application, error) {
|
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
|
|
|
}
|
|
|
|
|
2023-06-10 15:51:26 +08:00
|
|
|
func GetApplicationByUserId(userId string) (application *Application, err error) {
|
2021-09-05 01:03:29 +08:00
|
|
|
owner, name := util.GetOwnerAndNameFromId(userId)
|
|
|
|
if owner == "app" {
|
2023-05-30 15:49:39 +08:00
|
|
|
application, err = getApplication("admin", name)
|
|
|
|
return
|
2021-09-05 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 15:51:26 +08:00
|
|
|
user, err := GetUser(userId)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
2023-06-10 15:51:26 +08:00
|
|
|
return nil, err
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
application, err = GetApplicationByUser(user)
|
|
|
|
return
|
2021-09-05 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetApplicationByClientId(clientId string) (*Application, error) {
|
2021-03-14 18:18:03 +08:00
|
|
|
application := Application{}
|
2023-07-29 15:07:04 +08:00
|
|
|
existed, err := ormer.Engine.Where("client_id=?", clientId).Get(&application)
|
2021-03-14 18:18:03 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, err
|
2021-03-14 18:18:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
2023-05-30 15:49:39 +08:00
|
|
|
err = extendApplicationWithProviders(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = extendApplicationWithOrg(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:11:52 +08:00
|
|
|
err = extendApplicationWithSigninMethods(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:12:40 +08:00
|
|
|
err = extendApplicationWithSigninItems(&application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return &application, nil
|
2021-03-14 18:18:03 +08:00
|
|
|
} else {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, nil
|
2021-03-14 18:18:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetApplication(id string) (*Application, error) {
|
2021-12-29 19:12:11 +08:00
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
|
|
return getApplication(owner, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMaskedApplication(application *Application, userId string) *Application {
|
|
|
|
if application == nil {
|
2021-07-30 14:15:10 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-13 15:19:15 +08:00
|
|
|
if application.TokenFields == nil {
|
|
|
|
application.TokenFields = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if application.FailedSigninLimit == 0 {
|
|
|
|
application.FailedSigninLimit = DefaultFailedSigninLimit
|
|
|
|
}
|
|
|
|
if application.FailedSigninFrozenTime == 0 {
|
|
|
|
application.FailedSigninFrozenTime = DefaultFailedSigninFrozenTime
|
|
|
|
}
|
|
|
|
|
2024-03-06 01:06:15 +08:00
|
|
|
isOrgUser := false
|
2023-07-26 16:17:49 +07:00
|
|
|
if userId != "" {
|
|
|
|
if isUserIdGlobalAdmin(userId) {
|
|
|
|
return application
|
|
|
|
}
|
|
|
|
|
2024-03-06 01:06:15 +08:00
|
|
|
user, err := GetUser(userId)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if user != nil {
|
|
|
|
if user.IsApplicationAdmin(application) {
|
|
|
|
return application
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Owner == application.Organization {
|
|
|
|
isOrgUser = true
|
|
|
|
}
|
2023-07-26 16:17:49 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 22:01:49 +08:00
|
|
|
application.ClientSecret = "***"
|
|
|
|
application.Cert = "***"
|
|
|
|
application.EnablePassword = false
|
|
|
|
application.EnableSigninSession = false
|
|
|
|
application.EnableCodeSignin = false
|
|
|
|
application.EnableSamlCompress = false
|
|
|
|
application.EnableSamlC14n10 = false
|
|
|
|
application.EnableSamlPostBinding = false
|
|
|
|
application.EnableWebAuthn = false
|
|
|
|
application.EnableLinkWithEmail = false
|
|
|
|
application.SamlReplyUrl = "***"
|
|
|
|
|
|
|
|
providerItems := []*ProviderItem{}
|
|
|
|
for _, providerItem := range application.Providers {
|
|
|
|
if providerItem.Provider != nil && (providerItem.Provider.Category == "OAuth" || providerItem.Provider.Category == "Web3") {
|
|
|
|
providerItems = append(providerItems, providerItem)
|
|
|
|
}
|
2021-12-29 19:12:11 +08:00
|
|
|
}
|
2024-03-03 22:01:49 +08:00
|
|
|
application.Providers = providerItems
|
|
|
|
|
|
|
|
application.GrantTypes = nil
|
|
|
|
application.Tags = nil
|
|
|
|
application.RedirectUris = nil
|
|
|
|
application.TokenFormat = "***"
|
|
|
|
application.TokenFields = nil
|
|
|
|
application.ExpireInHours = -1
|
|
|
|
application.RefreshExpireInHours = -1
|
|
|
|
application.FailedSigninLimit = -1
|
|
|
|
application.FailedSigninFrozenTime = -1
|
2022-03-02 20:58:16 +08:00
|
|
|
|
|
|
|
if application.OrganizationObj != nil {
|
2024-03-03 22:01:49 +08:00
|
|
|
application.OrganizationObj.MasterPassword = "***"
|
|
|
|
application.OrganizationObj.DefaultPassword = "***"
|
|
|
|
application.OrganizationObj.MasterVerificationCode = "***"
|
|
|
|
application.OrganizationObj.PasswordType = "***"
|
|
|
|
application.OrganizationObj.PasswordSalt = "***"
|
|
|
|
application.OrganizationObj.InitScore = -1
|
|
|
|
application.OrganizationObj.EnableSoftDeletion = false
|
2024-03-06 01:06:15 +08:00
|
|
|
|
|
|
|
if !isOrgUser {
|
|
|
|
application.OrganizationObj.MfaItems = nil
|
2024-03-10 14:11:18 +08:00
|
|
|
if !application.OrganizationObj.IsProfilePublic {
|
|
|
|
application.OrganizationObj.AccountItems = nil
|
|
|
|
}
|
2024-03-06 01:06:15 +08:00
|
|
|
}
|
2022-03-02 20:58:16 +08:00
|
|
|
}
|
2023-08-24 13:42:17 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-03-03 21:05:44 +08:00
|
|
|
func GetAllowedApplications(applications []*Application, userId string, lang string) ([]*Application, error) {
|
|
|
|
if userId == "" {
|
|
|
|
return nil, fmt.Errorf(i18n.Translate(lang, "auth:Unauthorized operation"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if isUserIdGlobalAdmin(userId) {
|
2023-11-08 09:48:31 +08:00
|
|
|
return applications, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := GetUser(userId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-03 21:05:44 +08:00
|
|
|
if user == nil {
|
|
|
|
return nil, fmt.Errorf(i18n.Translate(lang, "auth:Unauthorized operation"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.IsAdmin {
|
2023-11-08 09:48:31 +08:00
|
|
|
return applications, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := []*Application{}
|
|
|
|
for _, application := range applications {
|
|
|
|
var allowed bool
|
|
|
|
allowed, err = CheckLoginPermission(userId, application)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if allowed {
|
|
|
|
res = append(res, application)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func UpdateApplication(id string, application *Application) (bool, error) {
|
2020-12-20 23:24:09 +08:00
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
2023-05-30 15:49:39 +08:00
|
|
|
oldApplication, err := getApplication(owner, name)
|
2023-01-17 17:37:20 +08:00
|
|
|
if oldApplication == nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, err
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-30 15:49:39 +08:00
|
|
|
err = applicationChangeTrigger(name, application.Name)
|
2022-11-02 00:17:38 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, err
|
2022-11-02 00:17:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
applicationByClientId, err := GetApplicationByClientId(application.ClientId)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldApplication.ClientId != application.ClientId && applicationByClientId != nil {
|
|
|
|
return false, err
|
2023-01-17 17:37:20 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 21:35:19 +08:00
|
|
|
for _, providerItem := range application.Providers {
|
|
|
|
providerItem.Provider = nil
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:07:04 +08:00
|
|
|
session := ormer.Engine.ID(core.PK{owner, name}).AllCols()
|
2022-05-10 17:37:12 +08:00
|
|
|
if application.ClientSecret == "***" {
|
|
|
|
session.Omit("client_secret")
|
|
|
|
}
|
|
|
|
affected, err := session.Update(application)
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, err
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return affected != 0, nil
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func AddApplication(application *Application) (bool, error) {
|
2023-05-16 22:17:39 +08:00
|
|
|
if application.Owner == "" {
|
|
|
|
application.Owner = "admin"
|
|
|
|
}
|
|
|
|
if application.Organization == "" {
|
|
|
|
application.Organization = "built-in"
|
|
|
|
}
|
2022-07-06 19:27:59 +08:00
|
|
|
if application.ClientId == "" {
|
|
|
|
application.ClientId = util.GenerateClientId()
|
|
|
|
}
|
|
|
|
if application.ClientSecret == "" {
|
|
|
|
application.ClientSecret = util.GenerateClientSecret()
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
|
|
|
|
app, err := GetApplicationByClientId(application.ClientId)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if app != nil {
|
|
|
|
return false, nil
|
2023-01-17 17:37:20 +08:00
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
|
2021-06-14 21:35:19 +08:00
|
|
|
for _, providerItem := range application.Providers {
|
|
|
|
providerItem.Provider = nil
|
|
|
|
}
|
2021-03-06 16:39:17 +08:00
|
|
|
|
2023-07-29 15:07:04 +08:00
|
|
|
affected, err := ormer.Engine.Insert(application)
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, nil
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return affected != 0, nil
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func DeleteApplication(application *Application) (bool, error) {
|
2021-12-23 00:40:07 +08:00
|
|
|
if application.Name == "app-built-in" {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, nil
|
2021-12-23 00:40:07 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 15:07:04 +08:00
|
|
|
affected, err := ormer.Engine.ID(core.PK{application.Owner, application.Name}).Delete(&Application{})
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, err
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return affected != 0, nil
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
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
|
|
|
|
2022-12-21 00:35:33 +08:00
|
|
|
func (application *Application) IsRedirectUriValid(redirectUri string) bool {
|
2024-03-04 18:31:56 +08:00
|
|
|
redirectUris := append([]string{"http://localhost:", "https://localhost:", "http://127.0.0.1:", "http://casdoor-app", ".chromiumapp.org"}, application.RedirectUris...)
|
2023-09-06 21:14:58 +08:00
|
|
|
for _, targetUri := range redirectUris {
|
2022-12-21 00:35:33 +08:00
|
|
|
targetUriRegex := regexp.MustCompile(targetUri)
|
|
|
|
if targetUriRegex.MatchString(redirectUri) || strings.Contains(redirectUri, targetUri) {
|
2023-09-06 21:14:58 +08:00
|
|
|
return true
|
2022-03-02 20:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-06 21:14:58 +08:00
|
|
|
return false
|
2022-03-02 20:37:31 +08:00
|
|
|
}
|
2022-06-26 01:28:33 +08:00
|
|
|
|
2024-01-02 21:11:52 +08:00
|
|
|
func (application *Application) IsPasswordEnabled() bool {
|
|
|
|
if len(application.SigninMethods) == 0 {
|
|
|
|
return application.EnablePassword
|
|
|
|
} else {
|
|
|
|
for _, signinMethod := range application.SigninMethods {
|
|
|
|
if signinMethod.Name == "Password" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 21:07:34 +08:00
|
|
|
func (application *Application) IsPasswordWithLdapEnabled() bool {
|
|
|
|
if len(application.SigninMethods) == 0 {
|
|
|
|
return application.EnablePassword
|
|
|
|
} else {
|
|
|
|
for _, signinMethod := range application.SigninMethods {
|
|
|
|
if signinMethod.Name == "Password" && signinMethod.Rule == "All" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:11:52 +08:00
|
|
|
func (application *Application) IsCodeSigninViaEmailEnabled() bool {
|
|
|
|
if len(application.SigninMethods) == 0 {
|
|
|
|
return application.EnableCodeSignin
|
|
|
|
} else {
|
|
|
|
for _, signinMethod := range application.SigninMethods {
|
|
|
|
if signinMethod.Name == "Verification code" && signinMethod.Rule != "Phone only" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (application *Application) IsCodeSigninViaSmsEnabled() bool {
|
|
|
|
if len(application.SigninMethods) == 0 {
|
|
|
|
return application.EnableCodeSignin
|
|
|
|
} else {
|
|
|
|
for _, signinMethod := range application.SigninMethods {
|
|
|
|
if signinMethod.Name == "Verification code" && signinMethod.Rule != "Email only" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 21:07:34 +08:00
|
|
|
func (application *Application) IsLdapEnabled() bool {
|
|
|
|
if len(application.SigninMethods) > 0 {
|
|
|
|
for _, signinMethod := range application.SigninMethods {
|
|
|
|
if signinMethod.Name == "LDAP" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func IsOriginAllowed(origin string) (bool, error) {
|
|
|
|
applications, err := GetApplications("")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:35:33 +08:00
|
|
|
for _, application := range applications {
|
|
|
|
if application.IsRedirectUriValid(origin) {
|
2023-05-30 15:49:39 +08:00
|
|
|
return true, nil
|
2022-06-26 01:28:33 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, nil
|
2022-06-26 01:28:33 +08:00
|
|
|
}
|
2022-08-30 00:57:27 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func getApplicationMap(organization string) (map[string]*Application, error) {
|
2022-08-30 00:57:27 +08:00
|
|
|
applicationMap := make(map[string]*Application)
|
2023-05-30 15:49:39 +08:00
|
|
|
applications, err := GetOrganizationApplications("admin", organization)
|
|
|
|
if err != nil {
|
|
|
|
return applicationMap, err
|
|
|
|
}
|
|
|
|
|
2022-08-30 00:57:27 +08:00
|
|
|
for _, application := range applications {
|
|
|
|
applicationMap[application.Name] = application
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return applicationMap, nil
|
2022-08-30 00:57:27 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func ExtendManagedAccountsWithUser(user *User) (*User, error) {
|
2022-08-30 00:57:27 +08:00
|
|
|
if user.ManagedAccounts == nil || len(user.ManagedAccounts) == 0 {
|
2023-05-30 15:49:39 +08:00
|
|
|
return user, nil
|
2022-08-30 00:57:27 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
applicationMap, err := getApplicationMap(user.Owner)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2022-08-30 00:57:27 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return user, nil
|
2022-08-30 00:57:27 +08:00
|
|
|
}
|
2022-11-02 00:17:38 +08:00
|
|
|
|
|
|
|
func applicationChangeTrigger(oldName string, newName string) error {
|
2023-07-29 15:07:04 +08:00
|
|
|
session := ormer.Engine.NewSession()
|
2022-11-02 00:17:38 +08:00
|
|
|
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
|
2023-07-29 15:07:04 +08:00
|
|
|
err = ormer.Engine.Find(&permissions)
|
2022-11-02 00:17:38 +08:00
|
|
|
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
|
2024-01-05 20:45:55 +08:00
|
|
|
_, err = session.Where("owner=?", permissions[i].Owner).Where("name=?", permissions[i].Name).Update(permissions[i])
|
2022-11-02 00:17:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.Commit()
|
|
|
|
}
|