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:
leoil
2023-05-28 11:29:43 +08:00
committed by GitHub
parent c7cea331e2
commit 34151c0095
12 changed files with 440 additions and 4 deletions

View File

@ -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)