casdoor/object/syncer_user.go

155 lines
3.9 KiB
Go
Raw Normal View History

2021-12-18 01:08:03 +08:00
// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// 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 (
"fmt"
"strings"
"time"
"github.com/astaxie/beego"
"github.com/casbin/casdoor/util"
2021-12-20 00:26:46 +08:00
"xorm.io/core"
2021-12-18 01:08:03 +08:00
)
2021-12-19 22:30:54 +08:00
type OriginalUser = User
2021-12-18 01:08:03 +08:00
2021-12-19 22:30:54 +08:00
func (syncer *Syncer) getOriginalUsers() []*OriginalUser {
sql := fmt.Sprintf("select * from %s", syncer.Table)
results, err := syncer.Adapter.Engine.QueryString(sql)
2021-12-18 01:08:03 +08:00
if err != nil {
panic(err)
}
2021-12-19 22:30:54 +08:00
return syncer.getOriginalUsersFromMap(results)
2021-12-18 01:08:03 +08:00
}
2021-12-19 22:30:54 +08:00
func (syncer *Syncer) getOriginalUserMap() ([]*OriginalUser, map[string]*OriginalUser) {
users := syncer.getOriginalUsers()
2021-12-18 01:08:03 +08:00
2021-12-19 22:30:54 +08:00
m := map[string]*OriginalUser{}
2021-12-18 01:08:03 +08:00
for _, user := range users {
2021-12-19 22:30:54 +08:00
m[user.Id] = user
2021-12-18 01:08:03 +08:00
}
return users, m
}
2021-12-19 22:30:54 +08:00
func (syncer *Syncer) addUser(user *OriginalUser) bool {
m := syncer.getMapFromOriginalUser(user)
affected, err := syncer.Adapter.Engine.Table(syncer.Table).Insert(m)
2021-12-18 01:08:03 +08:00
if err != nil {
panic(err)
}
return affected != 0
}
2021-12-20 00:26:46 +08:00
func (syncer *Syncer) getOriginalColumns() []string {
2021-12-19 22:30:54 +08:00
res := []string{}
for _, tableColumn := range syncer.TableColumns {
if tableColumn.CasdoorName != "Id" {
res = append(res, tableColumn.Name)
}
}
return res
}
2021-12-20 00:26:46 +08:00
func (syncer *Syncer) getCasdoorColumns() []string {
res := []string{}
for _, tableColumn := range syncer.TableColumns {
if tableColumn.CasdoorName != "Id" {
v := util.CamelToSnakeCase(tableColumn.CasdoorName)
res = append(res, v)
}
}
return res
}
2021-12-19 22:30:54 +08:00
func (syncer *Syncer) updateUser(user *OriginalUser) bool {
m := syncer.getMapFromOriginalUser(user)
2021-12-20 00:26:46 +08:00
columns := syncer.getOriginalColumns()
2021-12-19 23:33:22 +08:00
affected, err := syncer.Adapter.Engine.Table(syncer.Table).ID(syncer.TablePrimaryKey).Cols(columns...).Update(m)
2021-12-18 01:08:03 +08:00
if err != nil {
panic(err)
}
return affected != 0
}
2021-12-20 00:26:46 +08:00
func (syncer *Syncer) updateUserForOriginalFields(user *User) bool {
owner, name := util.GetOwnerAndNameFromId(user.GetId())
oldUser := getUser(owner, name)
if oldUser == nil {
return false
}
if user.Avatar != oldUser.Avatar && user.Avatar != "" {
user.PermanentAvatar = getPermanentAvatarUrl(user.Owner, user.Name, user.Avatar)
}
columns := syncer.getCasdoorColumns()
columns = append(columns, "affiliation", "hash", "pre_hash")
affected, err := adapter.Engine.ID(core.PK{user.Owner, user.Name}).Cols(columns...).Update(user)
if err != nil {
panic(err)
}
return affected != 0
}
2021-12-19 22:30:54 +08:00
func (syncer *Syncer) calculateHash(user *OriginalUser) string {
values := []string{}
m := syncer.getMapFromOriginalUser(user)
for _, tableColumn := range syncer.TableColumns {
2021-12-19 23:32:42 +08:00
if tableColumn.IsHashed {
2021-12-20 00:26:46 +08:00
values = append(values, m[tableColumn.Name])
2021-12-19 23:32:42 +08:00
}
2021-12-19 22:30:54 +08:00
}
s := strings.Join(values, "|")
2021-12-18 01:08:03 +08:00
return util.GetMd5Hash(s)
}
func (syncer *Syncer) initAdapter() {
if syncer.Adapter == nil {
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/", syncer.User, syncer.Password, syncer.Host, syncer.Port)
syncer.Adapter = NewAdapter(beego.AppConfig.String("driverName"), dataSourceName, syncer.Database)
}
}
func RunSyncUsersJob() {
syncers := GetSyncers("admin")
for _, syncer := range syncers {
if !syncer.IsEnabled {
continue
}
syncer.initAdapter()
syncer.syncUsers()
// run at every minute
//schedule := fmt.Sprintf("* * * * %d", syncer.SyncInterval)
schedule := "* * * * *"
ctab := getCrontab(syncer.Name)
err := ctab.AddJob(schedule, syncer.syncUsers)
if err != nil {
panic(err)
}
}
time.Sleep(time.Duration(1<<63 - 1))
}