mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-02 03:00:18 +08:00
feat: add group xlsx upload button (#3885)
This commit is contained in:
@ -181,6 +181,41 @@ func AddGroups(groups []*Group) (bool, error) {
|
||||
return affected != 0, nil
|
||||
}
|
||||
|
||||
func AddGroupsInBatch(groups []*Group) (bool, error) {
|
||||
if len(groups) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
session := ormer.Engine.NewSession()
|
||||
defer session.Close()
|
||||
err := session.Begin()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, group := range groups {
|
||||
err = checkGroupName(group.Name)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
affected, err := session.Insert(group)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if affected == 0 {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
err = session.Commit()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func deleteGroup(group *Group) (bool, error) {
|
||||
affected, err := ormer.Engine.ID(core.PK{group.Owner, group.Name}).Delete(&Group{})
|
||||
if err != nil {
|
||||
|
61
object/group_upload.go
Normal file
61
object/group_upload.go
Normal file
@ -0,0 +1,61 @@
|
||||
// Copyright 2025 The Casdoor 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 (
|
||||
"github.com/casdoor/casdoor/xlsx"
|
||||
)
|
||||
|
||||
func getGroupMap(owner string) (map[string]*Group, error) {
|
||||
m := map[string]*Group{}
|
||||
|
||||
groups, err := GetGroups(owner)
|
||||
if err != nil {
|
||||
return m, err
|
||||
}
|
||||
|
||||
for _, group := range groups {
|
||||
m[group.GetId()] = group
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func UploadGroups(owner string, path string) (bool, error) {
|
||||
table := xlsx.ReadXlsxFile(path)
|
||||
|
||||
oldGroupMap, err := getGroupMap(owner)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
transGroups, err := StringArrayToStruct[Group](table)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
newGroups := []*Group{}
|
||||
for _, group := range transGroups {
|
||||
if _, ok := oldGroupMap[group.GetId()]; !ok {
|
||||
newGroups = append(newGroups, group)
|
||||
}
|
||||
}
|
||||
|
||||
if len(newGroups) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return AddGroupsInBatch(newGroups)
|
||||
}
|
@ -81,7 +81,7 @@ func UploadUsers(owner string, path string) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
transUsers, err := StringArrayToUser(table)
|
||||
transUsers, err := StringArrayToStruct[User](table)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -724,14 +724,14 @@ func setReflectAttr[T any](fieldValue *reflect.Value, fieldString string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func StringArrayToUser(stringArray [][]string) ([]*User, error) {
|
||||
func StringArrayToStruct[T any](stringArray [][]string) ([]*T, error) {
|
||||
fieldNames := stringArray[0]
|
||||
excelMap := []map[string]string{}
|
||||
userFieldMap := map[string]int{}
|
||||
structFieldMap := map[string]int{}
|
||||
|
||||
reflectedUser := reflect.TypeOf(User{})
|
||||
for i := 0; i < reflectedUser.NumField(); i++ {
|
||||
userFieldMap[strings.ToLower(reflectedUser.Field(i).Name)] = i
|
||||
reflectedStruct := reflect.TypeOf(*new(T))
|
||||
for i := 0; i < reflectedStruct.NumField(); i++ {
|
||||
structFieldMap[strings.ToLower(reflectedStruct.Field(i).Name)] = i
|
||||
}
|
||||
|
||||
for idx, field := range stringArray {
|
||||
@ -746,22 +746,23 @@ func StringArrayToUser(stringArray [][]string) ([]*User, error) {
|
||||
excelMap = append(excelMap, tempMap)
|
||||
}
|
||||
|
||||
users := []*User{}
|
||||
instances := []*T{}
|
||||
var err error
|
||||
|
||||
for _, u := range excelMap {
|
||||
user := User{}
|
||||
reflectedUser := reflect.ValueOf(&user).Elem()
|
||||
for k, v := range u {
|
||||
for _, m := range excelMap {
|
||||
instance := new(T)
|
||||
reflectedInstance := reflect.ValueOf(instance).Elem()
|
||||
|
||||
for k, v := range m {
|
||||
if v == "" || v == "null" || v == "[]" || v == "{}" {
|
||||
continue
|
||||
}
|
||||
fName := strings.ToLower(strings.ReplaceAll(k, "_", ""))
|
||||
fieldIdx, ok := userFieldMap[fName]
|
||||
fieldIdx, ok := structFieldMap[fName]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
fv := reflectedUser.Field(fieldIdx)
|
||||
fv := reflectedInstance.Field(fieldIdx)
|
||||
if !fv.IsValid() {
|
||||
continue
|
||||
}
|
||||
@ -806,8 +807,8 @@ func StringArrayToUser(stringArray [][]string) ([]*User, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
users = append(users, &user)
|
||||
instances = append(instances, instance)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
return instances, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user