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