oroinc / platform

Main OroPlatform package with core functionality.
Other
627 stars 351 forks source link

TagRepository::deleteTaggingByParams() Dangerous behaviour ! #1047

Open sadortun opened 3 years ago

sadortun commented 3 years ago

Summary
Hi !

I was very lucky to detect this issue before some major side effects. And i am not sure if this is the intended behaviour.

Calling TagManager::deleteTagging([ 1 , 2 , 3] , 'My\Entity') is working perfectly fine and as intended, but if you specify an empty array [] as a list of tags to delete, the method delete everything, instead of not deleting any tags.

This behaviour would be similar to calling $myCollection->removeElement() and having the entire collection wiped out. instead of not deleting anything.

    public function deleteTaggingByParams(array $tags, $entityClassName, $entityId, User $owner = null)
    {
        $builder = $this
            ->getDeleteTaggingQueryBuilder($entityClassName)
            ->andWhere('t.recordId = :entityId')
            ->setParameter('entityId', $entityId);

        if (!empty($tags)) {
            $builder->andWhere($builder->expr()->in('t.tag', ':tags'))->setParameter('tags', $tags);
        }
// .....

This method seems to be only used once in the TagListener, and uses the [] to delete tagging before deleting a Taggable entity.

IMO, you really should make the deleteTagging() method safe, and add another method deleteAllTagging() that is used when you want to delete everything.

Steps to reproduce
TagRepository::deleteTaggingByParams([ ] , 'My\Entity')

Actual Result No changes in tags

Expected Result
All tags associated to the entity are deleted.

Details about your environment

anyt commented 3 years ago

Hi Samuel, It's a valid improvement. Feel free to create a PR with the fix.

sadortun commented 3 years ago

@anyt Sure, will do. Ill get back to you in about 2 weeks.