2021-03-13 23:06:03 +08:00
|
|
|
// Copyright 2021 The casbin 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-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/util"
|
2020-12-20 23:24:09 +08:00
|
|
|
"xorm.io/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
|
|
|
EnableCodeSignin bool `json:"enableCodeSignin"`
|
|
|
|
Providers []*ProviderItem `xorm:"mediumtext" json:"providers"`
|
|
|
|
SignupItems []*SignupItem `xorm:"varchar(1000)" json:"signupItems"`
|
|
|
|
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"`
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:55:10 +08:00
|
|
|
func GetApplicationCount(owner, field, value string) int {
|
|
|
|
session := adapter.Engine.Where("owner=?", owner)
|
|
|
|
if field != "" && value != "" {
|
|
|
|
session = session.And(fmt.Sprintf("%s like ?", util.SnakeString(field)), fmt.Sprintf("%%%s%%", value))
|
|
|
|
}
|
|
|
|
count, err := session.Count(&Application{})
|
2021-11-06 11:32:22 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:55:10 +08:00
|
|
|
func GetPaginationApplications(owner string, offset, limit int, field, value, sortField, sortOrder string) []*Application {
|
2021-11-06 11:32:22 +08:00
|
|
|
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-01-15 18:29:10 +08:00
|
|
|
func GetApplicationsByOrganizationName(owner string, organization string) []*Application {
|
2021-12-23 01:01:23 +08:00
|
|
|
applications := []*Application{}
|
|
|
|
err := adapter.Engine.Desc("created_time").Find(&applications, &Application{Owner: owner, Organization: organization})
|
|
|
|
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 {
|
2021-06-16 00:18:56 +08:00
|
|
|
//if provider.Category != "OAuth" {
|
|
|
|
// continue
|
|
|
|
//}
|
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) {
|
|
|
|
m := getProviderMap(application.Owner)
|
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 = "***"
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:35:19 +08:00
|
|
|
for _, providerItem := range application.Providers {
|
|
|
|
providerItem.Provider = nil
|
|
|
|
}
|
|
|
|
|
2021-05-02 10:30:12 +08:00
|
|
|
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().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 {
|
2021-03-06 16:39:17 +08:00
|
|
|
application.ClientId = util.GenerateClientId()
|
|
|
|
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)
|
|
|
|
}
|