Skip to content

Commit f9e57b7

Browse files
committed
feat: allow admins to delete files and folders of other users
1 parent 1d9912d commit f9e57b7

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

controllers/DeleteFileController.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ func DeleteFileController(c echo.Context) error {
1515
return c.String(status, err.Error())
1616
}
1717

18+
// Determine admin status
19+
isAdmin, _ := c.Get("Admin").(bool)
20+
1821
// Business logic
1922
status, err := logic.DeleteFiles(&models.LinksDeleteValidation{
2023
LinkIDs: []models.LinkDeleteValidation{
2124
fileValidation,
2225
},
23-
}, c.Get("UserID").(uint))
26+
}, c.Get("UserID").(uint), isAdmin)
2427

2528
if err != nil {
2629
return c.String(status, err.Error())

controllers/DeleteFilesController.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ func DeleteFilesController(c echo.Context) error {
1515
return c.String(status, err.Error())
1616
}
1717

18+
// Determine admin status
19+
isAdmin, _ := c.Get("Admin").(bool)
20+
1821
// Business logic
19-
status, err := logic.DeleteFiles(&fileValidation, c.Get("UserID").(uint))
22+
status, err := logic.DeleteFiles(&fileValidation, c.Get("UserID").(uint), isAdmin)
2023

2124
if err != nil {
2225
return c.String(status, err.Error())

controllers/DeleteFolderController.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ func DeleteFolder(c echo.Context) error {
1515
return c.String(status, err.Error())
1616
}
1717

18+
// Determine admin status
19+
isAdmin, _ := c.Get("Admin").(bool)
20+
1821
// Business logic
1922
status, err := logic.DeleteFolders(&models.FoldersDeleteValidation{
2023
FolderIDs: []models.FolderDeleteValidation{
2124
folderValidation,
2225
},
23-
}, c.Get("UserID").(uint))
26+
}, c.Get("UserID").(uint), isAdmin)
2427
if err != nil {
2528
return c.String(status, err.Error())
2629
}

controllers/DeleteFoldersController.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ func DeleteFolders(c echo.Context) error {
1515
return c.String(status, err.Error())
1616
}
1717

18+
// Determine admin status
19+
isAdmin, _ := c.Get("Admin").(bool)
20+
1821
// Business logic
19-
status, err := logic.DeleteFolders(&folderValidation, c.Get("UserID").(uint))
22+
status, err := logic.DeleteFolders(&folderValidation, c.Get("UserID").(uint), isAdmin)
2023
if err != nil {
2124
return c.String(status, err.Error())
2225
}

logic/DeleteFiles.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/labstack/echo/v4"
1313
)
1414

15-
func DeleteFiles(fileValidation *models.LinksDeleteValidation, userID uint) (status int, err error) {
15+
func DeleteFiles(fileValidation *models.LinksDeleteValidation, userID uint, isAdmin bool) (status int, err error) {
1616
if len(fileValidation.LinkIDs) == 0 {
1717
return http.StatusBadRequest, errors.New("array LinkIDs is empty")
1818
}
@@ -24,9 +24,12 @@ func DeleteFiles(fileValidation *models.LinksDeleteValidation, userID uint) (sta
2424
linkIdDeleteMap := make(map[uint]bool, len(fileValidation.LinkIDs))
2525
linkIdDeleteList := []uint{}
2626
for _, LinkValidation := range fileValidation.LinkIDs {
27-
if res := inits.DB.First(&models.Link{
28-
UserID: userID,
29-
}, LinkValidation.LinkID); res.Error != nil {
27+
query := inits.DB.Model(&models.Link{})
28+
if !isAdmin {
29+
query = query.Where("user_id = ?", userID)
30+
}
31+
32+
if res := query.First(&models.Link{}, LinkValidation.LinkID); res.Error != nil {
3033
return http.StatusBadRequest, fmt.Errorf("linkID (%d) doesn't exist", LinkValidation.LinkID)
3134
}
3235
if linkIdDeleteMap[LinkValidation.LinkID] {

logic/DeleteFolders.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ and shortly after calls the delete method for the root folder too, it would try
1717
whole folder tree and delete all folders & files again. To prevent that there is an map variable
1818
that should prevent the user from calling this method multiple times concurrently.
1919
*/
20-
func DeleteFolders(folderValidation *models.FoldersDeleteValidation, userID uint) (status int, err error) {
20+
func DeleteFolders(folderValidation *models.FoldersDeleteValidation, userID uint, isAdmin bool) (status int, err error) {
2121

2222
if helpers.UserRequestAsyncObj.Blocked(userID) {
2323
return http.StatusTooManyRequests, errors.New("wait until the previous delete request finished")
@@ -38,10 +38,13 @@ func DeleteFolders(folderValidation *models.FoldersDeleteValidation, userID uint
3838
reqFolderIdDeleteList := []uint{}
3939
var parentFolderID uint = 0
4040
for i, FolderValidation := range folderValidation.FolderIDs {
41-
var dbFolder = models.Folder{
42-
UserID: userID,
41+
query := inits.DB.Model(&models.Folder{})
42+
if !isAdmin {
43+
query = query.Where("user_id = ?", userID)
4344
}
44-
if res := inits.DB.First(&dbFolder, FolderValidation.FolderID); res.Error != nil {
45+
46+
var dbFolder models.Folder
47+
if res := query.First(&dbFolder, FolderValidation.FolderID); res.Error != nil {
4548
return http.StatusBadRequest, fmt.Errorf("FolderID (%d) doesn't exist", FolderValidation.FolderID)
4649
}
4750
// check if has same parent folder
@@ -83,7 +86,7 @@ func DeleteFolders(folderValidation *models.FoldersDeleteValidation, userID uint
8386
if len(files) > 0 {
8487
if status, err := DeleteFiles(&models.LinksDeleteValidation{
8588
LinkIDs: files,
86-
}, userID); err != nil {
89+
}, userID, isAdmin); err != nil {
8790
return status, fmt.Errorf("failed to delete all files from folders: %v", err)
8891
}
8992
}

0 commit comments

Comments
 (0)