Skip to content

Commit e1800f1

Browse files
authored
feat: Check usage before deleting storage (#9322)
* feat(storage): Added role and user path checking functionality - Added `GetAllRoles` function to retrieve all roles - Added `GetAllUsers` function to retrieve all users - Added `firstPathSegment` function to extract the first segment of a path - Checks whether a storage object is used by a role or user, and returns relevant information for unusing it * fix(storage): Fixed a potential null value issue with not checking firstMount. - Added a check to see if `firstMount` is null to prevent logic errors. - Adjusted the loading logic of `GetAllRoles` and `GetAllUsers` to only execute when `firstMount` is non-null. - Fixed the `usedBy` check logic to ensure that an error message is returned under the correct conditions. - Optimized code structure to reduce unnecessary execution paths.
1 parent 16cce37 commit e1800f1

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

internal/db/role.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ func GetRoles(pageIndex, pageSize int) (roles []model.Role, count int64, err err
3434
return roles, count, nil
3535
}
3636

37+
func GetAllRoles() ([]model.Role, error) {
38+
var roles []model.Role
39+
if err := db.Find(&roles).Error; err != nil {
40+
return nil, errors.WithStack(err)
41+
}
42+
return roles, nil
43+
}
44+
3745
func CreateRole(r *model.Role) error {
3846
if err := db.Create(r).Error; err != nil {
3947
return errors.WithStack(err)

internal/db/user.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ func GetUsers(pageIndex, pageSize int) (users []model.User, count int64, err err
8383
return users, count, nil
8484
}
8585

86+
func GetAllUsers() ([]model.User, error) {
87+
var users []model.User
88+
if err := db.Find(&users).Error; err != nil {
89+
return nil, errors.WithStack(err)
90+
}
91+
return users, nil
92+
}
93+
8694
func DeleteUserById(id uint) error {
8795
return errors.WithStack(db.Delete(&model.User{}, id).Error)
8896
}

internal/op/storage.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ func GetStorageByMountPath(mountPath string) (driver.Driver, error) {
4141
return storageDriver, nil
4242
}
4343

44+
func firstPathSegment(p string) string {
45+
p = utils.FixAndCleanPath(p)
46+
p = strings.TrimPrefix(p, "/")
47+
if p == "" {
48+
return ""
49+
}
50+
if i := strings.Index(p, "/"); i >= 0 {
51+
return p[:i]
52+
}
53+
return p
54+
}
55+
4456
// CreateStorage Save the storage to database so storage can get an id
4557
// then instantiate corresponding driver and save it in memory
4658
func CreateStorage(ctx context.Context, storage model.Storage) (uint, error) {
@@ -267,6 +279,34 @@ func DeleteStorageById(ctx context.Context, id uint) error {
267279
if err != nil {
268280
return errors.WithMessage(err, "failed get storage")
269281
}
282+
firstMount := firstPathSegment(storage.MountPath)
283+
if firstMount != "" {
284+
roles, err := db.GetAllRoles()
285+
if err != nil {
286+
return errors.WithMessage(err, "failed to load roles")
287+
}
288+
users, err := db.GetAllUsers()
289+
if err != nil {
290+
return errors.WithMessage(err, "failed to load users")
291+
}
292+
var usedBy []string
293+
for _, r := range roles {
294+
for _, entry := range r.PermissionScopes {
295+
if firstPathSegment(entry.Path) == firstMount {
296+
usedBy = append(usedBy, "role:"+r.Name)
297+
break
298+
}
299+
}
300+
}
301+
for _, u := range users {
302+
if firstPathSegment(u.BasePath) == firstMount {
303+
usedBy = append(usedBy, "user:"+u.Username)
304+
}
305+
}
306+
if len(usedBy) > 0 {
307+
return errors.Errorf("storage is used by %s, please cancel usage first", strings.Join(usedBy, ", "))
308+
}
309+
}
270310
if !storage.Disabled {
271311
storageDriver, err := GetStorageByMountPath(storage.MountPath)
272312
if err != nil {

0 commit comments

Comments
 (0)