casdoor/object/adapter.go

151 lines
3.1 KiB
Go
Raw Normal View History

2021-03-13 23:06:03 +08:00
// Copyright 2021 The casbin Authors. All Rights Reserved.
2020-10-20 23:14:03 +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-02-15 10:11:26 +08:00
"fmt"
2020-10-20 23:14:03 +08:00
"runtime"
"github.com/astaxie/beego"
_ "github.com/go-sql-driver/mysql" // db = mysql
2021-04-29 21:54:53 +08:00
//_ "github.com/lib/pq" // db = postgres
2020-10-20 23:14:03 +08:00
"xorm.io/xorm"
)
var adapter *Adapter
2021-03-28 21:50:00 +08:00
func InitConfig() {
err := beego.LoadAppConfig("ini", "../conf/app.conf")
if err != nil {
panic(err)
}
InitAdapter()
}
2020-10-20 23:14:03 +08:00
func InitAdapter() {
2021-05-02 00:24:34 +08:00
adapter = NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), beego.AppConfig.String("dbName"))
adapter.createTable()
2020-10-20 23:14:03 +08:00
}
// Adapter represents the MySQL adapter for policy storage.
type Adapter struct {
driverName string
dataSourceName string
2021-05-02 00:24:34 +08:00
dbName string
Engine *xorm.Engine
2020-10-20 23:14:03 +08:00
}
// finalizer is the destructor for Adapter.
func finalizer(a *Adapter) {
err := a.Engine.Close()
2020-10-20 23:14:03 +08:00
if err != nil {
panic(err)
}
}
// NewAdapter is the constructor for Adapter.
2021-05-02 00:24:34 +08:00
func NewAdapter(driverName string, dataSourceName string, dbName string) *Adapter {
2020-10-20 23:14:03 +08:00
a := &Adapter{}
a.driverName = driverName
a.dataSourceName = dataSourceName
2021-05-02 00:24:34 +08:00
a.dbName = dbName
2020-10-20 23:14:03 +08:00
// Open the DB, create it if not existed.
a.open()
// Call the destructor when the object is released.
runtime.SetFinalizer(a, finalizer)
return a
}
func (a *Adapter) createDatabase() error {
engine, err := xorm.NewEngine(a.driverName, a.dataSourceName)
if err != nil {
return err
}
defer engine.Close()
2021-05-02 00:24:34 +08:00
_, err = engine.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci", a.dbName))
2020-10-20 23:14:03 +08:00
return err
}
func (a *Adapter) open() {
2021-05-02 00:24:34 +08:00
if a.driverName != "postgres" {
if err := a.createDatabase(); err != nil {
panic(err)
}
2020-10-20 23:14:03 +08:00
}
2021-05-02 00:24:34 +08:00
engine, err := xorm.NewEngine(a.driverName, a.dataSourceName+a.dbName)
2020-10-20 23:14:03 +08:00
if err != nil {
panic(err)
}
a.Engine = engine
2020-10-20 23:14:03 +08:00
}
func (a *Adapter) close() {
2021-08-07 22:02:56 +08:00
_ = a.Engine.Close()
a.Engine = nil
2020-10-20 23:14:03 +08:00
}
func (a *Adapter) createTable() {
err := a.Engine.Sync2(new(Organization))
2020-10-20 23:14:03 +08:00
if err != nil {
panic(err)
}
2020-12-20 20:31:48 +08:00
err = a.Engine.Sync2(new(User))
2020-12-20 23:24:09 +08:00
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(Provider))
2020-12-20 23:24:09 +08:00
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(Application))
2020-12-20 20:31:48 +08:00
if err != nil {
panic(err)
}
2021-03-13 23:47:35 +08:00
2021-08-15 00:17:53 +08:00
err = a.Engine.Sync2(new(Resource))
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(Token))
2021-03-13 23:47:35 +08:00
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(VerificationRecord))
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(Records))
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(Ldap))
if err != nil {
panic(err)
}
2020-10-20 23:14:03 +08:00
}