2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2020-12-20 20:31:48 +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 (
|
2022-07-30 18:17:13 +08:00
|
|
|
"fmt"
|
2023-05-19 02:34:25 +08:00
|
|
|
"strconv"
|
2022-07-30 18:17:13 +08:00
|
|
|
|
2023-05-19 02:34:25 +08:00
|
|
|
"github.com/casdoor/casdoor/conf"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/cred"
|
2022-10-23 15:16:24 +08:00
|
|
|
"github.com/casdoor/casdoor/i18n"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/util"
|
2023-06-14 23:27:46 +08:00
|
|
|
"github.com/xorm-io/builder"
|
2023-02-12 09:33:24 +08:00
|
|
|
"github.com/xorm-io/core"
|
2020-12-20 20:31:48 +08:00
|
|
|
)
|
|
|
|
|
2022-06-18 01:41:21 +08:00
|
|
|
type AccountItem struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Visible bool `json:"visible"`
|
|
|
|
ViewRule string `json:"viewRule"`
|
|
|
|
ModifyRule string `json:"modifyRule"`
|
|
|
|
}
|
|
|
|
|
2023-02-01 22:06:40 +08:00
|
|
|
type ThemeData struct {
|
|
|
|
ThemeType string `xorm:"varchar(30)" json:"themeType"`
|
|
|
|
ColorPrimary string `xorm:"varchar(10)" json:"colorPrimary"`
|
|
|
|
BorderRadius int `xorm:"int" json:"borderRadius"`
|
|
|
|
IsCompact bool `xorm:"bool" json:"isCompact"`
|
|
|
|
IsEnabled bool `xorm:"bool" json:"isEnabled"`
|
|
|
|
}
|
|
|
|
|
2023-05-17 01:13:13 +08:00
|
|
|
type MfaItem struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Rule string `json:"rule"`
|
|
|
|
}
|
|
|
|
|
2020-12-20 20:31:48 +08:00
|
|
|
type Organization 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"`
|
|
|
|
|
2023-02-01 22:06:40 +08:00
|
|
|
DisplayName string `xorm:"varchar(100)" json:"displayName"`
|
|
|
|
WebsiteUrl string `xorm:"varchar(100)" json:"websiteUrl"`
|
|
|
|
Favicon string `xorm:"varchar(100)" json:"favicon"`
|
|
|
|
PasswordType string `xorm:"varchar(100)" json:"passwordType"`
|
|
|
|
PasswordSalt string `xorm:"varchar(100)" json:"passwordSalt"`
|
2023-06-17 00:01:20 +08:00
|
|
|
PasswordOptions []string `xorm:"varchar(100)" json:"passwordOptions"`
|
2023-02-16 22:53:28 +08:00
|
|
|
CountryCodes []string `xorm:"varchar(200)" json:"countryCodes"`
|
2023-04-29 08:33:04 +08:00
|
|
|
DefaultAvatar string `xorm:"varchar(200)" json:"defaultAvatar"`
|
2023-02-01 22:06:40 +08:00
|
|
|
DefaultApplication string `xorm:"varchar(100)" json:"defaultApplication"`
|
|
|
|
Tags []string `xorm:"mediumtext" json:"tags"`
|
|
|
|
Languages []string `xorm:"varchar(255)" json:"languages"`
|
|
|
|
ThemeData *ThemeData `xorm:"json" json:"themeData"`
|
|
|
|
MasterPassword string `xorm:"varchar(100)" json:"masterPassword"`
|
|
|
|
InitScore int `json:"initScore"`
|
|
|
|
EnableSoftDeletion bool `json:"enableSoftDeletion"`
|
|
|
|
IsProfilePublic bool `json:"isProfilePublic"`
|
2022-06-18 01:41:21 +08:00
|
|
|
|
2023-05-17 01:13:13 +08:00
|
|
|
MfaItems []*MfaItem `xorm:"varchar(300)" json:"mfaItems"`
|
2023-07-07 00:13:05 +08:00
|
|
|
AccountItems []*AccountItem `xorm:"varchar(5000)" json:"accountItems"`
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetOrganizationCount(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(&Organization{})
|
2021-11-06 11:32:22 +08:00
|
|
|
}
|
|
|
|
|
2023-06-14 23:27:46 +08:00
|
|
|
func GetOrganizations(owner string, name ...string) ([]*Organization, error) {
|
2020-12-20 20:31:48 +08:00
|
|
|
organizations := []*Organization{}
|
2023-06-14 23:27:46 +08:00
|
|
|
if name != nil && len(name) > 0 {
|
|
|
|
err := adapter.Engine.Desc("created_time").Where(builder.In("name", name)).Find(&organizations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := adapter.Engine.Desc("created_time").Find(&organizations, &Organization{Owner: owner})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return organizations, nil
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetOrganizationsByFields(owner string, fields ...string) ([]*Organization, error) {
|
2023-05-27 19:02:54 +08:00
|
|
|
organizations := []*Organization{}
|
|
|
|
err := adapter.Engine.Desc("created_time").Cols(fields...).Find(&organizations, &Organization{Owner: owner})
|
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, err
|
2023-05-27 19:02:54 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return organizations, nil
|
2023-05-27 19:02:54 +08:00
|
|
|
}
|
|
|
|
|
2023-06-29 20:32:34 +03:00
|
|
|
func GetPaginationOrganizations(owner string, name string, offset, limit int, field, value, sortField, sortOrder string) ([]*Organization, error) {
|
2021-11-06 11:32:22 +08:00
|
|
|
organizations := []*Organization{}
|
2021-12-25 10:55:10 +08:00
|
|
|
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
|
2023-06-29 20:32:34 +03:00
|
|
|
var err error
|
|
|
|
if name != "" {
|
|
|
|
err = session.Find(&organizations, &Organization{Name: name})
|
|
|
|
} else {
|
|
|
|
err = session.Find(&organizations)
|
|
|
|
}
|
2021-11-06 11:32:22 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, err
|
2021-11-06 11:32:22 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return organizations, nil
|
2021-11-06 11:32:22 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func getOrganization(owner string, name string) (*Organization, 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 20:31:48 +08:00
|
|
|
organization := Organization{Owner: owner, Name: name}
|
2021-05-02 10:30:12 +08:00
|
|
|
existed, err := adapter.Engine.Get(&organization)
|
2020-12-20 20:31:48 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, err
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
2023-05-30 15:49:39 +08:00
|
|
|
return &organization, nil
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
2021-11-06 11:32:22 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, nil
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetOrganization(id string) (*Organization, error) {
|
2020-12-20 20:31:48 +08:00
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
|
|
return getOrganization(owner, name)
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetMaskedOrganization(organization *Organization, errs ...error) (*Organization, error) {
|
|
|
|
if len(errs) > 0 && errs[0] != nil {
|
|
|
|
return nil, errs[0]
|
|
|
|
}
|
|
|
|
|
2021-11-06 21:14:53 +08:00
|
|
|
if organization == nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, nil
|
2021-11-06 21:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if organization.MasterPassword != "" {
|
|
|
|
organization.MasterPassword = "***"
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
return organization, nil
|
2021-11-06 21:14:53 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetMaskedOrganizations(organizations []*Organization, errs ...error) ([]*Organization, error) {
|
|
|
|
if len(errs) > 0 && errs[0] != nil {
|
|
|
|
return nil, errs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2021-11-06 21:14:53 +08:00
|
|
|
for _, organization := range organizations {
|
2023-05-30 15:49:39 +08:00
|
|
|
organization, err = GetMaskedOrganization(organization)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-06 21:14:53 +08:00
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
|
|
|
|
return organizations, nil
|
2021-11-06 21:14:53 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func UpdateOrganization(id string, organization *Organization) (bool, error) {
|
2020-12-20 20:31:48 +08:00
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
2023-05-30 15:49:39 +08:00
|
|
|
if org, err := getOrganization(owner, name); err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if org == nil {
|
|
|
|
return false, nil
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2021-12-23 00:40:07 +08:00
|
|
|
if name == "built-in" {
|
|
|
|
organization.Name = name
|
|
|
|
}
|
|
|
|
|
2021-12-23 01:01:23 +08:00
|
|
|
if name != organization.Name {
|
2022-11-02 00:17:38 +08:00
|
|
|
err := organizationChangeTrigger(name, organization.Name)
|
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, nil
|
2022-11-02 00:17:38 +08:00
|
|
|
}
|
2021-12-23 01:01:23 +08:00
|
|
|
}
|
|
|
|
|
2022-05-10 17:37:12 +08:00
|
|
|
if organization.MasterPassword != "" && organization.MasterPassword != "***" {
|
2021-12-22 20:56:22 +08:00
|
|
|
credManager := cred.GetCredManager(organization.PasswordType)
|
|
|
|
if credManager != nil {
|
|
|
|
hashedPassword := credManager.GetHashedPassword(organization.MasterPassword, "", organization.PasswordSalt)
|
|
|
|
organization.MasterPassword = hashedPassword
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 17:37:12 +08:00
|
|
|
session := adapter.Engine.ID(core.PK{owner, name}).AllCols()
|
|
|
|
if organization.MasterPassword == "***" {
|
|
|
|
session.Omit("master_password")
|
|
|
|
}
|
|
|
|
affected, err := session.Update(organization)
|
2020-12-20 20:31:48 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, err
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return affected != 0, nil
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func AddOrganization(organization *Organization) (bool, error) {
|
2021-05-02 10:30:12 +08:00
|
|
|
affected, err := adapter.Engine.Insert(organization)
|
2020-12-20 20:31:48 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, err
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return affected != 0, nil
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func DeleteOrganization(organization *Organization) (bool, error) {
|
2021-12-23 00:40:07 +08:00
|
|
|
if organization.Name == "built-in" {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, nil
|
2021-12-23 00:40:07 +08:00
|
|
|
}
|
|
|
|
|
2021-05-02 10:30:12 +08:00
|
|
|
affected, err := adapter.Engine.ID(core.PK{organization.Owner, organization.Name}).Delete(&Organization{})
|
2020-12-20 20:31:48 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return false, err
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return affected != 0, nil
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
2021-05-13 09:39:07 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func GetOrganizationByUser(user *User) (*Organization, error) {
|
2023-07-03 14:56:14 +08:00
|
|
|
if user == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-05-16 21:04:26 +08:00
|
|
|
return getOrganization("admin", user.Owner)
|
|
|
|
}
|
2022-07-30 18:17:13 +08:00
|
|
|
|
|
|
|
func GetAccountItemByName(name string, organization *Organization) *AccountItem {
|
|
|
|
if organization == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, accountItem := range organization.AccountItems {
|
|
|
|
if accountItem.Name == name {
|
|
|
|
return accountItem
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
func CheckAccountItemModifyRule(accountItem *AccountItem, isAdmin bool, lang string) (bool, string) {
|
2023-02-22 12:16:05 +08:00
|
|
|
if accountItem == nil {
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
|
2022-07-30 18:17:13 +08:00
|
|
|
switch accountItem.ModifyRule {
|
|
|
|
case "Admin":
|
2023-04-26 16:21:58 +08:00
|
|
|
if !isAdmin {
|
2022-12-07 13:13:23 +08:00
|
|
|
return false, fmt.Sprintf(i18n.Translate(lang, "organization:Only admin can modify the %s."), accountItem.Name)
|
2022-07-30 18:17:13 +08:00
|
|
|
}
|
|
|
|
case "Immutable":
|
2022-12-07 13:13:23 +08:00
|
|
|
return false, fmt.Sprintf(i18n.Translate(lang, "organization:The %s is immutable."), accountItem.Name)
|
2022-07-30 18:17:13 +08:00
|
|
|
case "Self":
|
|
|
|
break
|
|
|
|
default:
|
2022-12-07 13:13:23 +08:00
|
|
|
return false, fmt.Sprintf(i18n.Translate(lang, "organization:Unknown modify rule %s."), accountItem.ModifyRule)
|
2022-07-30 18:17:13 +08:00
|
|
|
}
|
|
|
|
return true, ""
|
|
|
|
}
|
2022-09-10 20:41:45 +08:00
|
|
|
|
2022-10-09 10:39:33 +08:00
|
|
|
func GetDefaultApplication(id string) (*Application, error) {
|
2023-05-30 15:49:39 +08:00
|
|
|
organization, err := GetOrganization(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:41:45 +08:00
|
|
|
if organization == nil {
|
2022-10-09 10:39:33 +08:00
|
|
|
return nil, fmt.Errorf("The organization: %s does not exist", id)
|
2022-09-10 20:41:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if organization.DefaultApplication != "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
defaultApplication, err := getApplication("admin", organization.DefaultApplication)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-16 00:39:05 +08:00
|
|
|
if defaultApplication == nil {
|
|
|
|
return nil, fmt.Errorf("The default application: %s does not exist", organization.DefaultApplication)
|
|
|
|
} else {
|
|
|
|
return defaultApplication, nil
|
|
|
|
}
|
2022-09-10 20:41:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
applications := []*Application{}
|
2023-05-30 15:49:39 +08:00
|
|
|
err = adapter.Engine.Asc("created_time").Find(&applications, &Application{Organization: organization.Name})
|
2022-09-10 20:41:45 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, err
|
2022-09-10 20:41:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(applications) == 0 {
|
2022-10-09 10:39:33 +08:00
|
|
|
return nil, fmt.Errorf("The application does not exist")
|
2022-09-10 20:41:45 +08:00
|
|
|
}
|
|
|
|
|
2022-09-13 22:54:05 +08:00
|
|
|
defaultApplication := applications[0]
|
2022-09-10 20:41:45 +08:00
|
|
|
for _, application := range applications {
|
|
|
|
if application.EnableSignUp {
|
2022-09-13 22:54:05 +08:00
|
|
|
defaultApplication = application
|
|
|
|
break
|
2022-09-10 20:41:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
err = extendApplicationWithProviders(defaultApplication)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = extendApplicationWithOrg(defaultApplication)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-13 22:54:05 +08:00
|
|
|
|
2022-10-09 10:39:33 +08:00
|
|
|
return defaultApplication, nil
|
2022-09-10 20:41:45 +08:00
|
|
|
}
|
2022-11-02 00:17:38 +08:00
|
|
|
|
|
|
|
func organizationChangeTrigger(oldName string, newName string) error {
|
|
|
|
session := adapter.Engine.NewSession()
|
|
|
|
defer session.Close()
|
|
|
|
|
|
|
|
err := session.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
application := new(Application)
|
|
|
|
application.Organization = newName
|
|
|
|
_, err = session.Where("organization=?", oldName).Update(application)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
user := new(User)
|
|
|
|
user.Owner = newName
|
|
|
|
_, err = session.Where("owner=?", oldName).Update(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-12 09:27:16 +08:00
|
|
|
group := new(Group)
|
|
|
|
group.Owner = newName
|
|
|
|
_, err = session.Where("owner=?", oldName).Update(group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-02 00:17:38 +08:00
|
|
|
role := new(Role)
|
|
|
|
_, err = adapter.Engine.Where("owner=?", oldName).Get(role)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i, u := range role.Users {
|
|
|
|
// u = organization/username
|
2023-04-22 21:15:06 +08:00
|
|
|
owner, name := util.GetOwnerAndNameFromId(u)
|
|
|
|
if name == oldName {
|
|
|
|
role.Users[i] = util.GetId(owner, newName)
|
2022-11-02 00:17:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, u := range role.Roles {
|
|
|
|
// u = organization/username
|
2023-04-22 21:15:06 +08:00
|
|
|
owner, name := util.GetOwnerAndNameFromId(u)
|
|
|
|
if name == oldName {
|
|
|
|
role.Roles[i] = util.GetId(owner, newName)
|
2022-11-02 00:17:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
role.Owner = newName
|
|
|
|
_, err = session.Where("owner=?", oldName).Update(role)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
permission := new(Permission)
|
|
|
|
_, err = adapter.Engine.Where("owner=?", oldName).Get(permission)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i, u := range permission.Users {
|
|
|
|
// u = organization/username
|
2023-04-22 21:15:06 +08:00
|
|
|
owner, name := util.GetOwnerAndNameFromId(u)
|
|
|
|
if name == oldName {
|
|
|
|
permission.Users[i] = util.GetId(owner, newName)
|
2022-11-02 00:17:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, u := range permission.Roles {
|
|
|
|
// u = organization/username
|
2023-04-22 21:15:06 +08:00
|
|
|
owner, name := util.GetOwnerAndNameFromId(u)
|
|
|
|
if name == oldName {
|
|
|
|
permission.Roles[i] = util.GetId(owner, newName)
|
2022-11-02 00:17:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
permission.Owner = newName
|
|
|
|
_, err = session.Where("owner=?", oldName).Update(permission)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
casbinAdapter := new(CasbinAdapter)
|
|
|
|
casbinAdapter.Owner = newName
|
|
|
|
_, err = session.Where("owner=?", oldName).Update(casbinAdapter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ldap := new(Ldap)
|
|
|
|
ldap.Owner = newName
|
|
|
|
_, err = session.Where("owner=?", oldName).Update(ldap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
model := new(Model)
|
|
|
|
model.Owner = newName
|
|
|
|
_, err = session.Where("owner=?", oldName).Update(model)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
payment := new(Payment)
|
|
|
|
payment.Organization = newName
|
|
|
|
_, err = session.Where("organization=?", oldName).Update(payment)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
record := new(Record)
|
|
|
|
record.Owner = newName
|
|
|
|
record.Organization = newName
|
|
|
|
_, err = session.Where("organization=?", oldName).Update(record)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resource := new(Resource)
|
|
|
|
resource.Owner = newName
|
|
|
|
_, err = session.Where("owner=?", oldName).Update(resource)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
syncer := new(Syncer)
|
|
|
|
syncer.Organization = newName
|
|
|
|
_, err = session.Where("organization=?", oldName).Update(syncer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
token := new(Token)
|
|
|
|
token.Organization = newName
|
|
|
|
_, err = session.Where("organization=?", oldName).Update(token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
webhook := new(Webhook)
|
|
|
|
webhook.Organization = newName
|
|
|
|
_, err = session.Where("organization=?", oldName).Update(webhook)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.Commit()
|
|
|
|
}
|
2023-05-17 01:13:13 +08:00
|
|
|
|
|
|
|
func (org *Organization) HasRequiredMfa() bool {
|
|
|
|
for _, item := range org.MfaItems {
|
|
|
|
if item.Rule == "Required" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-05-19 02:34:25 +08:00
|
|
|
|
|
|
|
func (org *Organization) GetInitScore() (int, error) {
|
|
|
|
if org != nil {
|
|
|
|
return org.InitScore, nil
|
|
|
|
} else {
|
|
|
|
return strconv.Atoi(conf.GetConfigString("initScore"))
|
|
|
|
}
|
|
|
|
}
|