mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Move adapter.createTable() out, make engine public.
This commit is contained in:
parent
be0cfa132e
commit
79f1c62ff7
@ -37,6 +37,7 @@ func InitConfig() {
|
||||
|
||||
func InitAdapter() {
|
||||
adapter = NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), beego.AppConfig.String("dbName"))
|
||||
adapter.createTable()
|
||||
}
|
||||
|
||||
// Adapter represents the MySQL adapter for policy storage.
|
||||
@ -44,12 +45,12 @@ type Adapter struct {
|
||||
driverName string
|
||||
dataSourceName string
|
||||
dbName string
|
||||
engine *xorm.Engine
|
||||
Engine *xorm.Engine
|
||||
}
|
||||
|
||||
// finalizer is the destructor for Adapter.
|
||||
func finalizer(a *Adapter) {
|
||||
err := a.engine.Close()
|
||||
err := a.Engine.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -94,37 +95,36 @@ func (a *Adapter) open() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
a.engine = engine
|
||||
a.createTable()
|
||||
a.Engine = engine
|
||||
}
|
||||
|
||||
func (a *Adapter) close() {
|
||||
a.engine.Close()
|
||||
a.engine = nil
|
||||
a.Engine.Close()
|
||||
a.Engine = nil
|
||||
}
|
||||
|
||||
func (a *Adapter) createTable() {
|
||||
err := a.engine.Sync2(new(Organization))
|
||||
err := a.Engine.Sync2(new(Organization))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = a.engine.Sync2(new(User))
|
||||
err = a.Engine.Sync2(new(User))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = a.engine.Sync2(new(Provider))
|
||||
err = a.Engine.Sync2(new(Provider))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = a.engine.Sync2(new(Application))
|
||||
err = a.Engine.Sync2(new(Application))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = a.engine.Sync2(new(Token))
|
||||
err = a.Engine.Sync2(new(Token))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ type Application struct {
|
||||
|
||||
func GetApplications(owner string) []*Application {
|
||||
applications := []*Application{}
|
||||
err := adapter.engine.Desc("created_time").Find(&applications, &Application{Owner: owner})
|
||||
err := adapter.Engine.Desc("created_time").Find(&applications, &Application{Owner: owner})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -73,7 +73,7 @@ func extendApplicationWithOrg(application *Application) {
|
||||
|
||||
func getApplication(owner string, name string) *Application {
|
||||
application := Application{Owner: owner, Name: name}
|
||||
existed, err := adapter.engine.Get(&application)
|
||||
existed, err := adapter.Engine.Get(&application)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -90,7 +90,7 @@ func getApplication(owner string, name string) *Application {
|
||||
func GetDefaultApplication(owner string) *Application {
|
||||
name := "app-built-in"
|
||||
application := Application{Owner: owner, Name: name}
|
||||
existed, err := adapter.engine.Get(&application)
|
||||
existed, err := adapter.Engine.Get(&application)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -106,7 +106,7 @@ func GetDefaultApplication(owner string) *Application {
|
||||
|
||||
func getApplicationByClientId(clientId string) *Application {
|
||||
application := Application{}
|
||||
existed, err := adapter.engine.Where("client_id=?", clientId).Get(&application)
|
||||
existed, err := adapter.Engine.Where("client_id=?", clientId).Get(&application)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -131,7 +131,7 @@ func UpdateApplication(id string, application *Application) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(application)
|
||||
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(application)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -143,7 +143,7 @@ func AddApplication(application *Application) bool {
|
||||
application.ClientId = util.GenerateClientId()
|
||||
application.ClientSecret = util.GenerateClientSecret()
|
||||
|
||||
affected, err := adapter.engine.Insert(application)
|
||||
affected, err := adapter.Engine.Insert(application)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -152,7 +152,7 @@ func AddApplication(application *Application) bool {
|
||||
}
|
||||
|
||||
func DeleteApplication(application *Application) bool {
|
||||
affected, err := adapter.engine.ID(core.PK{application.Owner, application.Name}).Delete(&Application{})
|
||||
affected, err := adapter.Engine.ID(core.PK{application.Owner, application.Name}).Delete(&Application{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ type Organization struct {
|
||||
|
||||
func GetOrganizations(owner string) []*Organization {
|
||||
organizations := []*Organization{}
|
||||
err := adapter.engine.Desc("created_time").Find(&organizations, &Organization{Owner: owner})
|
||||
err := adapter.Engine.Desc("created_time").Find(&organizations, &Organization{Owner: owner})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -41,7 +41,7 @@ func GetOrganizations(owner string) []*Organization {
|
||||
|
||||
func getOrganization(owner string, name string) *Organization {
|
||||
organization := Organization{Owner: owner, Name: name}
|
||||
existed, err := adapter.engine.Get(&organization)
|
||||
existed, err := adapter.Engine.Get(&organization)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -64,7 +64,7 @@ func UpdateOrganization(id string, organization *Organization) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(organization)
|
||||
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(organization)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -73,7 +73,7 @@ func UpdateOrganization(id string, organization *Organization) bool {
|
||||
}
|
||||
|
||||
func AddOrganization(organization *Organization) bool {
|
||||
affected, err := adapter.engine.Insert(organization)
|
||||
affected, err := adapter.Engine.Insert(organization)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -82,7 +82,7 @@ func AddOrganization(organization *Organization) bool {
|
||||
}
|
||||
|
||||
func DeleteOrganization(organization *Organization) bool {
|
||||
affected, err := adapter.engine.ID(core.PK{organization.Owner, organization.Name}).Delete(&Organization{})
|
||||
affected, err := adapter.Engine.ID(core.PK{organization.Owner, organization.Name}).Delete(&Organization{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ type Provider struct {
|
||||
|
||||
func GetProviders(owner string) []*Provider {
|
||||
providers := []*Provider{}
|
||||
err := adapter.engine.Desc("created_time").Find(&providers, &Provider{Owner: owner})
|
||||
err := adapter.Engine.Desc("created_time").Find(&providers, &Provider{Owner: owner})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -56,7 +56,7 @@ func GetDefaultProviders(owner string) []*Provider {
|
||||
|
||||
func getProvider(owner string, name string) *Provider {
|
||||
provider := Provider{Owner: owner, Name: name}
|
||||
existed, err := adapter.engine.Get(&provider)
|
||||
existed, err := adapter.Engine.Get(&provider)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -79,7 +79,7 @@ func UpdateProvider(id string, provider *Provider) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(provider)
|
||||
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(provider)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -88,7 +88,7 @@ func UpdateProvider(id string, provider *Provider) bool {
|
||||
}
|
||||
|
||||
func AddProvider(provider *Provider) bool {
|
||||
affected, err := adapter.engine.Insert(provider)
|
||||
affected, err := adapter.Engine.Insert(provider)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -97,7 +97,7 @@ func AddProvider(provider *Provider) bool {
|
||||
}
|
||||
|
||||
func DeleteProvider(provider *Provider) bool {
|
||||
affected, err := adapter.engine.ID(core.PK{provider.Owner, provider.Name}).Delete(&Provider{})
|
||||
affected, err := adapter.Engine.ID(core.PK{provider.Owner, provider.Name}).Delete(&Provider{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ type TokenWrapper struct {
|
||||
|
||||
func GetTokens(owner string) []*Token {
|
||||
tokens := []*Token{}
|
||||
err := adapter.engine.Desc("created_time").Find(&tokens, &Token{Owner: owner})
|
||||
err := adapter.Engine.Desc("created_time").Find(&tokens, &Token{Owner: owner})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -59,7 +59,7 @@ func GetTokens(owner string) []*Token {
|
||||
|
||||
func getToken(owner string, name string) *Token {
|
||||
token := Token{Owner: owner, Name: name}
|
||||
existed, err := adapter.engine.Get(&token)
|
||||
existed, err := adapter.Engine.Get(&token)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -73,7 +73,7 @@ func getToken(owner string, name string) *Token {
|
||||
|
||||
func getTokenByCode(code string) *Token {
|
||||
token := Token{}
|
||||
existed, err := adapter.engine.Where("code=?", code).Get(&token)
|
||||
existed, err := adapter.Engine.Where("code=?", code).Get(&token)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -96,7 +96,7 @@ func UpdateToken(id string, token *Token) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(token)
|
||||
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(token)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -105,7 +105,7 @@ func UpdateToken(id string, token *Token) bool {
|
||||
}
|
||||
|
||||
func AddToken(token *Token) bool {
|
||||
affected, err := adapter.engine.Insert(token)
|
||||
affected, err := adapter.Engine.Insert(token)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -114,7 +114,7 @@ func AddToken(token *Token) bool {
|
||||
}
|
||||
|
||||
func DeleteToken(token *Token) bool {
|
||||
affected, err := adapter.engine.ID(core.PK{token.Owner, token.Name}).Delete(&Token{})
|
||||
affected, err := adapter.Engine.ID(core.PK{token.Owner, token.Name}).Delete(&Token{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ type User struct {
|
||||
|
||||
func GetGlobalUsers() []*User {
|
||||
users := []*User{}
|
||||
err := adapter.engine.Desc("created_time").Find(&users)
|
||||
err := adapter.Engine.Desc("created_time").Find(&users)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -59,7 +59,7 @@ func GetGlobalUsers() []*User {
|
||||
|
||||
func GetUsers(owner string) []*User {
|
||||
users := []*User{}
|
||||
err := adapter.engine.Desc("created_time").Find(&users, &User{Owner: owner})
|
||||
err := adapter.Engine.Desc("created_time").Find(&users, &User{Owner: owner})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -69,7 +69,7 @@ func GetUsers(owner string) []*User {
|
||||
|
||||
func getUser(owner string, name string) *User {
|
||||
user := User{Owner: owner, Name: name}
|
||||
existed, err := adapter.engine.Get(&user)
|
||||
existed, err := adapter.Engine.Get(&user)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -101,7 +101,7 @@ func UpdateUser(id string, user *User) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(user)
|
||||
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(user)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -111,7 +111,7 @@ func UpdateUser(id string, user *User) bool {
|
||||
|
||||
func AddUser(user *User) bool {
|
||||
user.Id = util.GenerateId()
|
||||
affected, err := adapter.engine.Insert(user)
|
||||
affected, err := adapter.Engine.Insert(user)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -120,7 +120,7 @@ func AddUser(user *User) bool {
|
||||
}
|
||||
|
||||
func DeleteUser(user *User) bool {
|
||||
affected, err := adapter.engine.ID(core.PK{user.Owner, user.Name}).Delete(&User{})
|
||||
affected, err := adapter.Engine.ID(core.PK{user.Owner, user.Name}).Delete(&User{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -130,7 +130,7 @@ func DeleteUser(user *User) bool {
|
||||
|
||||
func GetUserByField(organizationName string, field string, value string) *User {
|
||||
user := User{Owner: organizationName}
|
||||
existed, err := adapter.engine.Where(fmt.Sprintf("%s=?", field), value).Get(&user)
|
||||
existed, err := adapter.Engine.Where(fmt.Sprintf("%s=?", field), value).Get(&user)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -169,7 +169,7 @@ func GetUserByFields(organization string, field string) *User {
|
||||
}
|
||||
|
||||
func SetUserField(user *User, field string, value string) bool {
|
||||
affected, err := adapter.engine.Table(user).ID(core.PK{user.Owner, user.Name}).Update(map[string]interface{}{field: value})
|
||||
affected, err := adapter.Engine.Table(user).ID(core.PK{user.Owner, user.Name}).Update(map[string]interface{}{field: value})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func updateUserColumn(column string, user *User) bool {
|
||||
affected, err := adapter.engine.ID(core.PK{user.Owner, user.Name}).Cols(column).Update(user)
|
||||
affected, err := adapter.Engine.ID(core.PK{user.Owner, user.Name}).Cols(column).Update(user)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user