Laravel: Enable Sentry user account be used in multiple computers

旧城冷巷雨未停 提交于 2019-12-02 23:40:18

Extension to Nico Kaag's answer and implementation of spamoom's comment:

/app/config/packages/cartalyst/sentry/config.php

...
    // Modify users array to point to custom model.    

'users' => array(
    'model' => 'User',
    'login_attribute' => 'email',
),    

...

/app/models/User.php

use Cartalyst\Sentry\Users\Eloquent\User as SentryUser;

class User extends SentryUser
{

    ...

    ...

    // Override the SentryUser getPersistCode method.

    public function getPersistCode()
    {
        if (!$this->persist_code)
        {
            $this->persist_code = $this->getRandomString();

            // Our code got hashed
            $persistCode = $this->persist_code;

            $this->save();

            return $persistCode;            
        }
        return $this->persist_code;
    }
}

It is possible, but not supported by Sentry itself. To do this, you have to change some core code in Sentry, or find a way to override the User class that's in the Sentry code.

The function you need to adjust is "GetPresistCode()" in the User model, which can be found in:

/vendor/cartalyst/sentry/src/Cartalyst/Sentry/Users/Eloquent/User.php

And this is how the function should look like (not tested):

/**
 * Gets a code for when the user is
 * persisted to a cookie or session which
 * identifies the user.
 *
 * @return string
 */
public function getPersistCode()
{
    if (!$this->persist_code) {
        $this->persist_code = $this->getRandomString();

        // Our code got hashed
        $persistCode = $this->persist_code;

        $this->save();

        return $persistCode;
    }
    return $this->persist_code;
}

I have to say that I highly recommend you don't change the code in Sentry, and that you find another way around, but that might be really hard.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!