mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-22 18:25:47 +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/casbin/casdoor/authz"
|
||||
"github.com/casbin/casdoor/object"
|
||||
"github.com/casbin/casdoor/original"
|
||||
"github.com/casbin/casdoor/proxy"
|
||||
"github.com/casbin/casdoor/routers"
|
||||
|
||||
@ -34,6 +35,10 @@ func main() {
|
||||
proxy.InitHttpClient()
|
||||
authz.InitAuthz()
|
||||
|
||||
if original.InitAdapter() {
|
||||
go original.RunSyncUsersJob()
|
||||
}
|
||||
|
||||
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
|
||||
AllowOrigins: []string{"*"},
|
||||
AllowMethods: []string{"GET", "PUT", "PATCH"},
|
||||
|
@ -28,18 +28,17 @@ func initConfig() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
initAdapter()
|
||||
}
|
||||
|
||||
func initAdapter() {
|
||||
func InitAdapter() bool {
|
||||
if dbName == "dbName" {
|
||||
adapter = nil
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
adapter = object.NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), dbName)
|
||||
createTable(adapter)
|
||||
return true
|
||||
}
|
||||
|
||||
func createTable(a *object.Adapter) {
|
||||
|
@ -22,7 +22,7 @@ import (
|
||||
|
||||
func isEnabled() bool {
|
||||
if adapter == nil {
|
||||
initAdapter()
|
||||
InitAdapter()
|
||||
if adapter == nil {
|
||||
return false
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ package original
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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)}, "|")
|
||||
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 (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/casbin/casdoor/object"
|
||||
)
|
||||
|
||||
func TestGetUsers(t *testing.T) {
|
||||
initConfig()
|
||||
initAdapter()
|
||||
InitAdapter()
|
||||
|
||||
users := getUsersOriginal()
|
||||
for _, user := range users {
|
||||
@ -34,16 +33,8 @@ func TestGetUsers(t *testing.T) {
|
||||
|
||||
func TestSyncUsers(t *testing.T) {
|
||||
initConfig()
|
||||
initAdapter()
|
||||
InitAdapter()
|
||||
object.InitAdapter()
|
||||
|
||||
syncUsers()
|
||||
|
||||
// run at every minute
|
||||
schedule := "* * * * *"
|
||||
err := ctab.AddJob(schedule, syncUsers)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
time.Sleep(time.Duration(1<<63 - 1))
|
||||
RunSyncUsersJob()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user