Add permission checks for collection removal

- Implemented authorization checks in the RemoveCollection function to ensure users have the necessary permissions before removing a collection.
- Introduced error handling for unauthorized access attempts, returning appropriate responses for permission denials.
- Enhanced security by validating user access based on their authorization information.
This commit is contained in:
Max 2025-11-05 10:01:07 +08:00
parent d6713ecddb
commit 5c2f323e64

View file

@ -148,6 +148,9 @@ func CreateCollection(c *gin.Context) {
// RemoveCollection removes an existing collection // RemoveCollection removes an existing collection
func RemoveCollection(c *gin.Context) { func RemoveCollection(c *gin.Context) {
authInfo := authorized.GetInfo(c)
// Get collection ID from URL parameter // Get collection ID from URL parameter
collectionID := c.Param("collectionID") collectionID := c.Param("collectionID")
if collectionID == "" { if collectionID == "" {
@ -169,6 +172,27 @@ func RemoveCollection(c *gin.Context) {
return return
} }
// Check remove permission
hasPermission, err := checkCollectionPermission(authInfo, collectionID)
if err != nil {
errorResp := &response.ErrorResponse{
Code: response.ErrServerError.Code,
ErrorDescription: err.Error(),
}
response.RespondWithError(c, response.StatusForbidden, errorResp)
return
}
// 403 Forbidden
if !hasPermission {
errorResp := &response.ErrorResponse{
Code: response.ErrAccessDenied.Code,
ErrorDescription: "Forbidden: No permission to remove collection",
}
response.RespondWithError(c, response.StatusForbidden, errorResp)
return
}
// Call the actual RemoveCollection method // Call the actual RemoveCollection method
removed, err := kb.Instance.RemoveCollection(c.Request.Context(), collectionID) removed, err := kb.Instance.RemoveCollection(c.Request.Context(), collectionID)
if err != nil { if err != nil {