Skip to content

Commit d94505c

Browse files
author
sergi
committed
Merge branch 'v1' of github.com:HiWay-Media/evo into v1
2 parents 6a4c211 + 8d3e37a commit d94505c

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

lib/gpath/functions.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package gpath
22

33
import (
4-
"github.com/getevo/evo/lib/text"
5-
copy2 "github.com/otiai10/copy"
64
"io"
75
"io/ioutil"
86
"os"
97
"path/filepath"
108
"strings"
9+
10+
"github.com/getevo/evo/lib/log"
11+
"github.com/getevo/evo/lib/text"
12+
copy2 "github.com/otiai10/copy"
1113
)
1214

1315
// MakePath create path recursive
@@ -36,7 +38,11 @@ func RSlash(path string) string {
3638
// IsDirExist checks if is directory exist
3739
func IsDirExist(path string) bool {
3840
info, err := os.Stat(path)
39-
if os.IsNotExist(err) || info == nil {
41+
if err != nil {
42+
if os.IsNotExist(err) || info == nil {
43+
return false
44+
}
45+
log.Error("Unable to access fs:", err.Error())
4046
return false
4147
}
4248
return info.IsDir()
@@ -45,7 +51,11 @@ func IsDirExist(path string) bool {
4551
// IsDir checks if path is a directory
4652
func IsDir(path string) bool {
4753
info, err := os.Stat(path)
48-
if os.IsNotExist(err) {
54+
if err != nil {
55+
if os.IsNotExist(err) {
56+
return false
57+
}
58+
log.Error("Unable to access fs:", err.Error())
4959
return false
5060
}
5161
return info.IsDir()
@@ -54,7 +64,11 @@ func IsDir(path string) bool {
5464
// IsFileExist checks if file exist
5565
func IsFileExist(path string) bool {
5666
info, err := os.Stat(path)
57-
if os.IsNotExist(err) {
67+
if err != nil {
68+
if os.IsNotExist(err) {
69+
return false
70+
}
71+
log.Error("Unable to access fs:", err.Error())
5872
return false
5973
}
6074
return !info.IsDir()

user.role.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package evo
33
import (
44
"errors"
55
"fmt"
6+
67
"github.com/getevo/evo/lib/log"
78
"github.com/getevo/evo/lib/validate"
89
"gorm.io/gorm"
@@ -12,22 +13,42 @@ var memoryRolePermissions = cMap{}
1213

1314
func updateRolePermissions() {
1415
memoryRolePermissions.Init()
16+
17+
// Load all roles at once
1518
var roles []Role
1619
db.Find(&roles)
20+
21+
// Load all role permissions at once
22+
var rolePerms []RolePermission
23+
db.Find(&rolePerms)
24+
25+
// Load all permissions at once
26+
var permissions []Permission
27+
db.Find(&permissions)
28+
29+
// Create maps for quick lookup
30+
rolePermMap := make(map[uint][]RolePermission)
31+
for _, rp := range rolePerms {
32+
rolePermMap[rp.RoleID] = append(rolePermMap[rp.RoleID], rp)
33+
}
34+
35+
permissionMap := make(map[uint]Permission)
36+
for _, perm := range permissions {
37+
permissionMap[perm.ID] = perm
38+
}
39+
40+
// Process roles and their permissions
1741
for _, role := range roles {
18-
var rolePerms []RolePermission
19-
db.Where("role_id = ?", role.ID).Find(&rolePerms)
20-
var permissions Permissions
21-
for _, perm := range rolePerms {
22-
var p Permission
23-
if errors.Is(db.Where("id = ?", perm.PermissionID).Take(&p).Error, gorm.ErrRecordNotFound) {
24-
log.Warning("Roles: found inconsistency, automatically remove permission id %d to fix.", perm.PermissionID)
25-
db.Delete(RolePermission{}, "id = ?", perm.PermissionID)
42+
var perms Permissions
43+
for _, rp := range rolePermMap[role.ID] {
44+
if perm, exists := permissionMap[rp.PermissionID]; exists {
45+
perms = append(perms, perm)
2646
} else {
27-
permissions = append(permissions, p)
47+
log.Warning("Roles: found inconsistency, automatically remove permission id %d to fix.", rp.PermissionID)
48+
db.Delete(&RolePermission{}, "id = ?", rp.ID)
2849
}
2950
}
30-
memoryRolePermissions.Set(role.ID, &permissions)
51+
memoryRolePermissions.Set(role.ID, &perms)
3152
}
3253
}
3354

0 commit comments

Comments
 (0)