Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forceDelete on User prevented by Foreign Key Constraints #963

Closed
claudious96 opened this issue Apr 9, 2019 · 1 comment
Closed

forceDelete on User prevented by Foreign Key Constraints #963

claudious96 opened this issue Apr 9, 2019 · 1 comment
Assignees
Labels
confirmed bug Something isn't working
Milestone

Comments

@claudious96
Copy link

When deleting a user from the admin panel, using the User model forceDelete option ($user->delete(true)), in the confirmation modal shows up an alert about a generic php error.
Seems like the foreign key constraints prevent the deletion of any activity related to the user.

For more insight see comments on #951.

My setups in which I reproduced the error:

  • UF 4.2 (vanilla)
  • OS Windows/Ubuntu 18.04
  • Apache 2.4.37
  • MariaDB 10.1.37 (tested also with MySql 5.7.25)
  • PHP 7.3.0 (tested also with 7.2.15)

This fix worked in my case (all setups above successfully tested) and won't break any custom Sprinkle
First I defined the classMapping of the Persistence model inside ServicesProvider.php

$container->extend('classMapper', function ($classMapper, $c) {
           ...
            $classMapper->setClassMapping('persistence', 'UserFrosting\Sprinkle\Account\Database\Models\Persistence');

            return $classMapper;
        });

And then here is the edited delete method of the User model

public function delete($hardDelete = false)
    {
        /** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
        $classMapper = static::$ci->classMapper;

        if ($hardDelete) {
            // Remove all role associations
            $this->roles()->detach();

            static::$ci->db->schema()->disableForeignKeyConstraints();
            // Remove all user activities
            $classMapper->staticMethod('activity', 'where', 'user_id', $this->id)->delete();

            // Remove all user tokens
            $classMapper->staticMethod('password_reset', 'where', 'user_id', $this->id)->delete();
            $classMapper->staticMethod('verification', 'where', 'user_id', $this->id)->delete();
            $classMapper->staticMethod('persistence', 'where', 'user_id', $this->id)->delete();

            // Delete the user
            $result = $this->forceDelete();
            static::$ci->db->schema()->enableForeignKeyConstraints();
        } else {
            // Soft delete the user, leaving all associated records alone
            $result = parent::delete();
        }

        return $result;
    }
@lcharette lcharette added this to the 4.2.x milestone Apr 10, 2019
@lcharette lcharette added the confirmed bug Something isn't working label Apr 10, 2019
@lcharette lcharette self-assigned this Apr 10, 2019
@lcharette lcharette reopened this Apr 15, 2019
@lcharette lcharette modified the milestones: 4.2.x, 4.2.1 Apr 15, 2019
@lcharette
Copy link
Member

Will be fixed in 4.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants