mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 12:30:19 +08:00
feat: Support uploading roles and permssions via xlsx files. (#1899)
* Support uploading roles and permissions via xlsx file. * Template xlsx file for uploading users and permissions. * reformat according to gofumpt. * fix typo.
This commit is contained in:
@ -16,6 +16,9 @@ package object
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/casdoor/casdoor/conf"
|
||||
|
||||
"github.com/casdoor/casdoor/util"
|
||||
"github.com/xorm-io/core"
|
||||
@ -160,6 +163,45 @@ func AddRole(role *Role) bool {
|
||||
return affected != 0
|
||||
}
|
||||
|
||||
func AddRoles(roles []*Role) bool {
|
||||
if len(roles) == 0 {
|
||||
return false
|
||||
}
|
||||
affected, err := adapter.Engine.Insert(roles)
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "Duplicate entry") {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return affected != 0
|
||||
}
|
||||
|
||||
func AddRolesInBatch(roles []*Role) bool {
|
||||
batchSize := conf.GetConfigBatchSize()
|
||||
|
||||
if len(roles) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
affected := false
|
||||
for i := 0; i < (len(roles)-1)/batchSize+1; i++ {
|
||||
start := i * batchSize
|
||||
end := (i + 1) * batchSize
|
||||
if end > len(roles) {
|
||||
end = len(roles)
|
||||
}
|
||||
|
||||
tmp := roles[start:end]
|
||||
// TODO: save to log instead of standard output
|
||||
// fmt.Printf("Add users: [%d - %d].\n", start, end)
|
||||
if AddRoles(tmp) {
|
||||
affected = true
|
||||
}
|
||||
}
|
||||
|
||||
return affected
|
||||
}
|
||||
|
||||
func DeleteRole(role *Role) bool {
|
||||
roleId := role.GetId()
|
||||
permissions := GetPermissionsByRole(roleId)
|
||||
|
Reference in New Issue
Block a user