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/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 {
|
2021-12-25 00:05:54 +08:00
|
|
|
sql := fmt.Sprintf("select * from %s", syncer.getTable())
|
2021-12-19 22:30:54 +08:00
|
|
|
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)
|
2021-12-25 00:05:54 +08:00
|
|
|
keyString, valueString := syncer.getSqlKeyValueStringFromMap(m)
|
2021-12-24 00:36:53 +08:00
|
|
|
|
2021-12-25 00:05:54 +08:00
|
|
|
sql := fmt.Sprintf("insert into %s (%s) values (%s)", syncer.getTable(), keyString, valueString)
|
2021-12-24 00:36:53 +08:00
|
|
|
res, err := syncer.Adapter.Engine.Exec(sql)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := res.RowsAffected()
|
2021-12-18 01:08:03 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
|
|
|
|
2021-12-24 00:36:53 +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-24 00:36:53 +08:00
|
|
|
}*/
|
2021-12-19 22:30:54 +08:00
|
|
|
|
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-24 00:36:53 +08:00
|
|
|
pkValue := m[syncer.TablePrimaryKey]
|
|
|
|
delete(m, syncer.TablePrimaryKey)
|
|
|
|
setString := syncer.getSqlSetStringFromMap(m)
|
|
|
|
|
2021-12-25 00:05:54 +08:00
|
|
|
sql := fmt.Sprintf("update %s set %s where %s = %s", syncer.getTable(), setString, syncer.TablePrimaryKey, pkValue)
|
2021-12-24 00:36:53 +08:00
|
|
|
res, err := syncer.Adapter.Engine.Exec(sql)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := res.RowsAffected()
|
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())
|
2021-12-25 00:19:17 +08:00
|
|
|
oldUser := getUserById(owner, name)
|
2021-12-20 00:26:46 +08:00
|
|
|
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")
|
2021-12-25 00:19:17 +08:00
|
|
|
affected, err := adapter.Engine.ID(core.PK{oldUser.Owner, oldUser.Name}).Cols(columns...).Update(user)
|
2021-12-20 00:26:46 +08:00
|
|
|
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 {
|
2021-12-24 22:53:35 +08:00
|
|
|
var dataSourceName string
|
|
|
|
if syncer.DatabaseType == "mssql" {
|
|
|
|
dataSourceName = fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", syncer.User, syncer.Password, syncer.Host, syncer.Port, syncer.Database)
|
|
|
|
} else {
|
|
|
|
dataSourceName = fmt.Sprintf("%s:%s@tcp(%s:%d)/", syncer.User, syncer.Password, syncer.Host, syncer.Port)
|
|
|
|
}
|
|
|
|
|
2021-12-23 21:28:40 +08:00
|
|
|
if !isCloudIntranet {
|
|
|
|
dataSourceName = strings.ReplaceAll(dataSourceName, "dbi.", "db.")
|
|
|
|
}
|
2021-12-24 22:53:35 +08:00
|
|
|
|
|
|
|
syncer.Adapter = NewAdapter(syncer.DatabaseType, dataSourceName, syncer.Database)
|
2021-12-18 01:08:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunSyncUsersJob() {
|
|
|
|
syncers := GetSyncers("admin")
|
|
|
|
for _, syncer := range syncers {
|
2022-01-17 20:09:29 +08:00
|
|
|
addSyncerJob(syncer)
|
2021-12-18 01:08:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(1<<63 - 1))
|
|
|
|
}
|