2021-03-13 23:06:03 +08:00
|
|
|
// Copyright 2021 The casbin 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 (
|
|
|
|
"github.com/casdoor/casdoor/util"
|
|
|
|
"xorm.io/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
|
|
|
|
2021-05-05 23:32:21 +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"`
|
2021-05-05 23:40:18 +08:00
|
|
|
PasswordSalt string `xorm:"varchar(100)" json:"passwordSalt"`
|
2021-05-13 09:39:07 +08:00
|
|
|
PhonePrefix string `xorm:"varchar(10)" json:"phonePrefix"`
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetOrganizations(owner string) []*Organization {
|
|
|
|
organizations := []*Organization{}
|
2021-05-02 10:30:12 +08:00
|
|
|
err := adapter.Engine.Desc("created_time").Find(&organizations, &Organization{Owner: owner})
|
2020-12-20 20:31:48 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return organizations
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOrganization(owner string, name string) *Organization {
|
|
|
|
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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
|
|
|
return &organization
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetOrganization(id string) *Organization {
|
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
|
|
return getOrganization(owner, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateOrganization(id string, organization *Organization) bool {
|
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
|
|
if getOrganization(owner, name) == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-02 10:30:12 +08:00
|
|
|
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(organization)
|
2020-12-20 20:31:48 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-03-28 00:48:34 +08:00
|
|
|
return affected != 0
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func AddOrganization(organization *Organization) bool {
|
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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteOrganization(organization *Organization) bool {
|
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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
2021-05-13 09:39:07 +08:00
|
|
|
|
|
|
|
func GetOrganizationByName(name string) *Organization {
|
|
|
|
var ret Organization
|
|
|
|
ret.Name = name
|
|
|
|
has, err := adapter.Engine.Get(&ret)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &ret
|
|
|
|
}
|