mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Run sync user job if configured.
This commit is contained in:
parent
f5fdf0af6a
commit
37829062ad
5
main.go
5
main.go
@ -21,6 +21,7 @@ import (
|
|||||||
_ "github.com/astaxie/beego/session/redis"
|
_ "github.com/astaxie/beego/session/redis"
|
||||||
"github.com/casbin/casdoor/authz"
|
"github.com/casbin/casdoor/authz"
|
||||||
"github.com/casbin/casdoor/object"
|
"github.com/casbin/casdoor/object"
|
||||||
|
"github.com/casbin/casdoor/original"
|
||||||
"github.com/casbin/casdoor/proxy"
|
"github.com/casbin/casdoor/proxy"
|
||||||
"github.com/casbin/casdoor/routers"
|
"github.com/casbin/casdoor/routers"
|
||||||
|
|
||||||
@ -34,6 +35,10 @@ func main() {
|
|||||||
proxy.InitHttpClient()
|
proxy.InitHttpClient()
|
||||||
authz.InitAuthz()
|
authz.InitAuthz()
|
||||||
|
|
||||||
|
if original.InitAdapter() {
|
||||||
|
go original.RunSyncUsersJob()
|
||||||
|
}
|
||||||
|
|
||||||
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
|
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
|
||||||
AllowOrigins: []string{"*"},
|
AllowOrigins: []string{"*"},
|
||||||
AllowMethods: []string{"GET", "PUT", "PATCH"},
|
AllowMethods: []string{"GET", "PUT", "PATCH"},
|
||||||
|
@ -28,18 +28,17 @@ func initConfig() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
initAdapter()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func initAdapter() {
|
func InitAdapter() bool {
|
||||||
if dbName == "dbName" {
|
if dbName == "dbName" {
|
||||||
adapter = nil
|
adapter = nil
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
adapter = object.NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), dbName)
|
adapter = object.NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), dbName)
|
||||||
createTable(adapter)
|
createTable(adapter)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTable(a *object.Adapter) {
|
func createTable(a *object.Adapter) {
|
||||||
|
@ -22,7 +22,7 @@ import (
|
|||||||
|
|
||||||
func isEnabled() bool {
|
func isEnabled() bool {
|
||||||
if adapter == nil {
|
if adapter == nil {
|
||||||
initAdapter()
|
InitAdapter()
|
||||||
if adapter == nil {
|
if adapter == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ package original
|
|||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/casbin/casdoor/util"
|
"github.com/casbin/casdoor/util"
|
||||||
)
|
)
|
||||||
@ -77,3 +78,15 @@ func calculateHash(user *User) string {
|
|||||||
s := strings.Join([]string{strconv.Itoa(user.Id), user.Password, user.Name, getFullAvatarUrl(user.Avatar), user.Cellphone, strconv.Itoa(user.SchoolId)}, "|")
|
s := strings.Join([]string{strconv.Itoa(user.Id), user.Password, user.Name, getFullAvatarUrl(user.Avatar), user.Cellphone, strconv.Itoa(user.SchoolId)}, "|")
|
||||||
return util.GetMd5Hash(s)
|
return util.GetMd5Hash(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RunSyncUsersJob() {
|
||||||
|
syncUsers()
|
||||||
|
|
||||||
|
// run at every minute
|
||||||
|
schedule := "* * * * *"
|
||||||
|
err := ctab.AddJob(schedule, syncUsers)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
time.Sleep(time.Duration(1<<63 - 1))
|
||||||
|
}
|
||||||
|
@ -17,14 +17,13 @@ package original
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/casbin/casdoor/object"
|
"github.com/casbin/casdoor/object"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetUsers(t *testing.T) {
|
func TestGetUsers(t *testing.T) {
|
||||||
initConfig()
|
initConfig()
|
||||||
initAdapter()
|
InitAdapter()
|
||||||
|
|
||||||
users := getUsersOriginal()
|
users := getUsersOriginal()
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
@ -34,16 +33,8 @@ func TestGetUsers(t *testing.T) {
|
|||||||
|
|
||||||
func TestSyncUsers(t *testing.T) {
|
func TestSyncUsers(t *testing.T) {
|
||||||
initConfig()
|
initConfig()
|
||||||
initAdapter()
|
InitAdapter()
|
||||||
object.InitAdapter()
|
object.InitAdapter()
|
||||||
|
|
||||||
syncUsers()
|
RunSyncUsersJob()
|
||||||
|
|
||||||
// run at every minute
|
|
||||||
schedule := "* * * * *"
|
|
||||||
err := ctab.AddJob(schedule, syncUsers)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
time.Sleep(time.Duration(1<<63 - 1))
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user