ValidationRule for File when deleting a folder

Hi,

I’m using validation rule to block deleting a folder and its working fine.
But the files inside the folder are getting deleted.
Is there an option to block files getting deleted (only when deleting a folder), when folder validation fails?

Litium version: 7.4.2

What is the rule’s condition? Can you create the validation rule for File?

Its a user group permission.
A file should be allowed to delete individually. But when deleting a folder without permission, we need to block files getting deleted.
Validation on folder just prevents folder from getting deleted

You can create a similar validation rule that you have for folder, but doing that for ValidationRuleBase<File> instead and inside the validation rule you can fetch the folder and do the needed checks there.

Hmmm. But issue is, that will block deleting an individual file.
I’m looking for a solution that will block deleting files, only when we try to delete its folder.

Delete file should work as usual, But delete folder should block deleting folder and all its files

I have not been able to find a perfect solution but you could try this.

The call stack is different if the call is done by a folder or file delete:

File:


Folder:

With this info we can add this validation:

using System;
using Litium.Media;
using Litium.Validations;

namespace Litium.Accelerator.ValidationRules
{
    public class ValidateFileNotDeletedWithFolder : ValidationRuleBase<File>
    {
        public override ValidationResult Validate(File entity, ValidationMode validationMode)
        {
            var result = new ValidationResult();

            if (validationMode != ValidationMode.Remove)
                return result;

            var isSingleFileDelete = Environment.StackTrace.Contains("WebApi.Media.Controllers.FileController.Delete");

            if (!isSingleFileDelete)
                result.AddError("*", "Files cannot be deleted together with folder.");

            return result;
        }
    }
}

It verifies that files can only be deleted if the call comes through FileController.Delete() - depending on where you want to allow delete from you will need to add other exceptions here also.

When testing I also noted another issue that validation errors for files does not show when they are triggered from a folder-delete, instead a general permission-message is shown:


Bug report is created for this: https://docs.litium.com/support/bugs/bug_details?id=50807

2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.