Add TestSyncUsers().

This commit is contained in:
Yang Luo 2021-05-02 23:06:08 +08:00
parent 1407ba5af7
commit d8aa6a1c95
6 changed files with 147 additions and 2 deletions

View File

@ -120,6 +120,40 @@ func AddUser(user *User) bool {
return affected != 0
}
func AddUsers(users []*User) bool {
affected, err := adapter.Engine.Insert(users)
if err != nil {
panic(err)
}
return affected != 0
}
func AddUsersSafe(users []*User) bool {
batchSize := 1000
if len(users) == 0 {
return false
}
affected := false
for i := 0; i < (len(users)-1)/batchSize+1; i++ {
start := i * batchSize
end := (i + 1) * batchSize
if end > len(users) {
end = len(users)
}
tmp := users[start:end]
fmt.Printf("Add users: [%d - %d].\n", start, end)
if AddUsers(tmp) {
affected = true
}
}
return affected
}
func DeleteUser(user *User) bool {
affected, err := adapter.Engine.ID(core.PK{user.Owner, user.Name}).Delete(&User{})
if err != nil {

View File

@ -16,3 +16,6 @@ package original
var dbName = "dbName"
var tableName = "tableName"
var avatarBaseUrl = "https://cdn.example.com/"
var orgName = "orgName"

61
original/sync.go Normal file
View File

@ -0,0 +1,61 @@
// 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 original
import (
"fmt"
"strconv"
"github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/util"
)
func createUserFromOriginalUser(originalUser *User) *object.User {
user := &object.User{
Owner: orgName,
Name: strconv.Itoa(originalUser.Id),
CreatedTime: util.GetCurrentTime(),
Id: strconv.Itoa(originalUser.Id),
Type: "normal-user",
Password: originalUser.Password,
PasswordType: "salt",
DisplayName: originalUser.Name,
Avatar: fmt.Sprintf("%s%s", avatarBaseUrl, originalUser.Avatar),
Email: "",
PhonePrefix: "86",
Phone: originalUser.Cellphone,
Affiliation: "",
IsAdmin: false,
IsGlobalAdmin: false,
IsForbidden: originalUser.Deleted != 0,
}
return user
}
func syncUsers() {
userMap := getUserMap()
oUsers := getUsersOriginal()
newUsers := []*object.User{}
for _, oUser := range oUsers {
id := strconv.Itoa(oUser.Id)
if _, ok := userMap[id]; !ok {
user := createUserFromOriginalUser(oUser)
fmt.Printf("New user: %v\n", user)
newUsers = append(newUsers, user)
}
}
object.AddUsersSafe(newUsers)
}

27
original/user.go Normal file
View File

@ -0,0 +1,27 @@
// 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 original
import "github.com/casdoor/casdoor/object"
func getUserMap() map[string]*object.User {
users := object.GetUsers(orgName)
m := map[string]*object.User{}
for _, user := range users {
m[user.Name] = user
}
return m
}

View File

@ -27,7 +27,7 @@ func (User) TableName() string {
return tableName
}
func getUsers() []*User {
func getUsersOriginal() []*User {
users := []*User{}
err := adapter.Engine.Asc("id").Find(&users)
if err != nil {
@ -36,3 +36,13 @@ func getUsers() []*User {
return users
}
//func getUserMapOriginal() map[string]*User {
// users := getUsersOriginal()
//
// m := map[string]*User{}
// for _, user := range users {
// m[strconv.Itoa(user.Id)] = user
// }
// return m
//}

View File

@ -17,14 +17,24 @@ package original
import (
"fmt"
"testing"
"github.com/casdoor/casdoor/object"
)
func TestGetUsers(t *testing.T) {
initConfig()
initAdapter()
users := getUsers()
users := getUsersOriginal()
for _, user := range users {
fmt.Printf("%v\n", user)
}
}
func TestSyncUsers(t *testing.T) {
initConfig()
initAdapter()
object.InitAdapter()
syncUsers()
}