amsgames / laravel-shop

Laravel shop package
MIT License
479 stars 167 forks source link

using with Sentry #2

Closed akyunus closed 9 years ago

akyunus commented 9 years ago

I know this package isn't suppor for guest users at the moment. i start a project with Sentry but it cant handle together. When i use Auth class i see there is no problem. Am i missing something ? Do you have any idea how can i adapt laravel-shop with Sentry

RichardLindhout commented 9 years ago

+1 for guest support

amostajo commented 9 years ago

Sentry uses its own User Model class within the package: https://github.com/cartalyst/sentry/blob/2.1/src/Cartalyst/Sentry/Users/Eloquent/User.php

You need to add ShopUserTrait to the model: https://github.com/amsgames/laravel-shop#user

What you can do is create your own user model that extends from the sentry one:

namespace YourProjectNamespace;

use Amsgames\LaravelShop\Traits\ShopUserTrait;
use Cartalyst\Sentry\Users\Eloquent\User as Model;

class User extends Model {

    use ShopUserTrait;

}

And then change whatever you need to change in the config files:

config\auth.php
// And maybe sentry if there is any
config\sentry.php

EDIT: Also, when LaravelShop's migrations are created review them just to verify that their are matching the data type and column name of sentry's users db table.

akyunus commented 9 years ago

as i see laravel-shop uses laravels Auth class. Where i use Sentry class for authentication. that must be the problem i think.

amostajo commented 9 years ago

It also appears that Sentry doesn't use Laravel's Auth facade.

It means that it doesn't use the Auth class at all:

// This doesn't work with sentry
Auth::check();
Auth::user();
// Instead sentry uses its own class
Sentry::check();
Sentry::getUser();

So you might need to do some extra work and override some functions in the cart model created: https://github.com/amsgames/laravel-shop#cart

These are the lines that will need a change: https://github.com/amsgames/laravel-shop/blob/v0.2/src/traits/ShopCartTrait.php#L199-L229

Like in example:

use Sentry;

class Cart extends ShopCartModel 
{
    /**
     * Scope to current user cart.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query  Query.
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeWhereCurrent($query)
    {
        if (!Sentry::check()) return $query;
        return $query->whereUser(Sentry::getUser()->shopId);
    }

    // MORE OVERRIDES HERE
}

The second observation will be when calling to Shop::checkout() and Shop::placeOrder().

If you don't pass the $cart parameter it will use the Auth facade to call for the current users cart, which will not work with Sentry. So you are forced to pass the cart as parameter. Like Shop::checkout($cart).

EDIT: I will make a future change so Shop calls to Cart:current() instead of Auth::user()->cart,

phillipmadsen commented 8 years ago

Would something like this work? Just wondering what kind of universal code I can put in its place.

        if (Auth::guest()) 
            return $query;
        elseif (Auth::check())
            return $query->whereUser(Auth::user()->shopId);
        elseif (!Sentry::check()) 
            return $query;
        elseif (Sentry::check())
            return $query->whereUser(Sentry::getUser()->shopId);
        endif
phillipmadsen commented 8 years ago

Or how about this?


   /**
     * Scope to current user cart.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query  Query.
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    if (Auth::check())
    public function scopeWhereCurrent($query)
    {
        if (Auth::guest()) return $query;
        return $query->whereUser(Auth::user()->shopId);
    }
    endif

    if (Sentry::check())
    public function scopeWhereCurrent($query)
    {
        if (!Sentry::check()) return $query;
        return $query->whereUser(Sentry::getUser()->shopId);
    }
    endif

    /**
     * Scope to current user cart and returns class model.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query  Query.
     *
     * @return this
     */
    if (Auth::check())
    public function scopeCurrent($query)
    {
        if (Auth::guest()) return;
        $cart = $query->whereCurrent()->first();
        if (empty($cart)) {
            $cart = call_user_func( Config::get('shop.cart') . '::create', [
                'user_id' =>  Auth::user()->shopId
            ]);
        }
        return $cart;
    }
    endif

    if (Sentry::check())

    public function scopeCurrent($query)
    {
        if (Sentry::check()) return;
        $cart = $query->whereCurrent()->first();
        if (empty($cart)) {
            $cart = call_user_func( Config::get('shop.cart') . '::create', [
                'user_id' =>  Sentry::getUser()->shopId
            ]);
        }
        return $cart;
    }

    endif