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 (
|
|
|
|
"github.com/casdoor/casdoor/util"
|
|
|
|
"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-02-14 10:56:37 +08:00
|
|
|
DisplayName string `xorm:"varchar(100)" json:"displayName"`
|
|
|
|
Logo string `xorm:"varchar(100)" json:"logo"`
|
2021-03-06 21:50:48 +08:00
|
|
|
HomepageUrl string `xorm:"varchar(100)" json:"homepageUrl"`
|
|
|
|
Description string `xorm:"varchar(100)" json:"description"`
|
2021-02-14 10:56:37 +08:00
|
|
|
Organization string `xorm:"varchar(100)" json:"organization"`
|
|
|
|
EnablePassword bool `json:"enablePassword"`
|
|
|
|
Providers []string `xorm:"varchar(100)" json:"providers"`
|
|
|
|
ProviderObjs []*Provider `xorm:"-" json:"providerObjs"`
|
2021-03-06 16:39:17 +08:00
|
|
|
|
2021-03-14 22:48:09 +08:00
|
|
|
ClientId string `xorm:"varchar(100)" json:"clientId"`
|
|
|
|
ClientSecret string `xorm:"varchar(100)" json:"clientSecret"`
|
2021-03-15 00:01:21 +08:00
|
|
|
RedirectUris []string `xorm:"varchar(1000)" json:"redirectUris"`
|
2021-03-14 22:48:09 +08:00
|
|
|
ExpireInHours int `json:"expireInHours"`
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetApplications(owner string) []*Application {
|
|
|
|
applications := []*Application{}
|
|
|
|
err := adapter.engine.Desc("created_time").Find(&applications, &Application{Owner: owner})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return applications
|
|
|
|
}
|
|
|
|
|
|
|
|
func getApplication(owner string, name string) *Application {
|
|
|
|
application := Application{Owner: owner, Name: name}
|
|
|
|
existed, err := adapter.engine.Get(&application)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
2021-02-13 23:00:43 +08:00
|
|
|
providers := GetProviders(owner)
|
|
|
|
m := map[string]*Provider{}
|
|
|
|
for _, provider := range providers {
|
|
|
|
provider.ClientSecret = ""
|
|
|
|
provider.ProviderUrl = ""
|
|
|
|
m[provider.Name] = provider
|
|
|
|
}
|
|
|
|
|
|
|
|
application.ProviderObjs = []*Provider{}
|
|
|
|
for _, providerName := range application.Providers {
|
|
|
|
application.ProviderObjs = append(application.ProviderObjs, m[providerName])
|
|
|
|
}
|
2020-12-20 23:24:09 +08:00
|
|
|
return &application
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 18:18:03 +08:00
|
|
|
func getApplicationByClientId(clientId string) *Application {
|
|
|
|
application := Application{}
|
|
|
|
existed, err := adapter.engine.Where("client_id=?", clientId).Get(&application)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
|
|
|
return &application
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:24:09 +08:00
|
|
|
func GetApplication(id string) *Application {
|
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
|
|
return getApplication(owner, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateApplication(id string, application *Application) bool {
|
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
|
|
if getApplication(owner, name) == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-28 20:23:50 +08:00
|
|
|
_, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(application)
|
2020-12-20 23:24:09 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//return affected != 0
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddApplication(application *Application) bool {
|
2021-03-06 16:39:17 +08:00
|
|
|
application.ClientId = util.GenerateClientId()
|
|
|
|
application.ClientSecret = util.GenerateClientSecret()
|
|
|
|
|
2020-12-20 23:24:09 +08:00
|
|
|
affected, err := adapter.engine.Insert(application)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteApplication(application *Application) bool {
|
2021-02-28 20:23:50 +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
|
|
|
|
}
|