rydurham / Sentinel

A Sentry bridge package for Laravel
http://www.ryandurham.com/projects/sentinel/
317 stars 68 forks source link

Soft Deleting #70

Closed novakben closed 10 years ago

novakben commented 10 years ago

Hi!

I'm trying to achieve a soft delete with this package.

I added a 'deleted_at' column to my users table and all the necessary code to my User model. When deleting it still removes the row entirely. Any suggestions?

rydurham commented 10 years ago

That is a good question. I will have to look into this. Are you using the soft delete trait in Laravel 4.2?

novakben commented 10 years ago

Yep.

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Database\Eloquent\SoftDeletingTrait; /* Maybe delete */

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait, SoftDeletingTrait;

    protected $table = 'users';
    protected $softDelete = true;

    protected $hidden = array('password', 'remember_token');
    public $timestamps = true;
    protected $dates = ['deleted_at'];
rydurham commented 10 years ago

Make sure you are extending the Sentry User Model:

use Cartalyst\Sentry\Sentry;

class User extends Cartalyst\Sentry\Users\Eloquent\User {
//....
}
novakben commented 10 years ago

Still not working...

rydurham commented 10 years ago

Ok - I will have to look into it. I should have some time in the next day or so.

novakben commented 10 years ago

Cheers

rydurham commented 10 years ago

I have not been able to recreate your issue. Here are the steps I took to set up soft-deleting using Sentinel:

Publish the Sentry Config file.

php artisan config:publish --path="vendor/rydurham/sentinel/vendor/cartalyst/sentry/src/config" cartalyst/sentry

Specify the User Model name in the Sentry config file (line 123):

'model' => 'User',

Create Migration:

public function up()
{
    Schema::table('users', function(Blueprint $table)
    {
        $table->timestamp('deleted_at')->nullable();
    });
}

Update the User Model:

use Cartalyst\Sentry\Sentry;

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class User extends Cartalyst\Sentry\Users\Eloquent\User {

    use SoftDeletingTrait;
    protected $dates = ['deleted_at'];

    //....
}

Have you had any luck on your end?

novakben commented 10 years ago

Oh wait! I havnt published the sentry config file, only sentinel's!

when I try to publish with your code, it says: Configuration published for package: cartalyst/sentry

But the folder is empty, there's no config file there. Also when I browse to vendor/rydurham/sentinel/ I don't see any vendor dir there... Any suggestions?

Thanks!

rydurham commented 10 years ago

You could try to manually publish the config file by creating an app/config/packages/cartalyst/sentry folder and copying the sentry config file to that location.

novakben commented 10 years ago

It worked!!! Haleluya ;) !

Another question, after my session expires, and my cookie ("remember me") should kick in, I get this error: Cannot use object of type Illuminate\Http\RedirectResponse as array

On this process:

function getUserData() {
    /* Validation and recognition */
    if ( ! Sentry::check()) { return Redirect::route('Sentinel\login'); }
    try { $user = Sentry::getUser(); }
    catch (Cartalyst\Sentry\Users\UserNotFoundException $e) { return Redirect::route('Sentinel\login'); }
    return $user;
}
function getWeddingData($user) {
    $wedding = Wedding::where('user_id', '=', $user['id'])->first();
    return $wedding;
}

execution in controller:

        $user = getUserData();
        $wedding = getWeddingData($user);

This error breaks on the $wedding = Wedding::... line. Do you think it's related?

rydurham commented 10 years ago

I would move the Sentry::check() logic into a filter (or make use of one of Sentinel's filters) rather than doing it directly in the controller. Sentry is trying to redirect to a login page rather than passing along the user data.

novakben commented 10 years ago

Got it thanks!!