设置Laravel的Auth用户

Ben*_*Ben 2 laravel-4

我正在使用Laravel的Auth类来验证我网站上的用户(基本Auth::attempt(...)内容)。

最近有一个新的要求(是利益相关者!),现在需要用户创建新用户(辅助用户)。由于主要用户的登录是通过第三方系统进行的,因此我无法将辅助用户与主要用户存储在一起(并重用当前的身份验证系统)。

我想到的是以某种方式告诉Auth类登录并在Auth::user()方法上强制设置用户。

有没有办法做到这一点?

pun*_*eel 5

编辑

为此,您必须在二级用户模型中使用UserInterface该类

use Illuminate\Auth\UserInterface;
Run Code Online (Sandbox Code Playgroud)

然后,你需要实现5种所需的方法:getAuthIdentifiergetAuthPasswordgetRememberTokensetRememberTokengetRememberTokenName

由于显然config > auth不能在运行时更改,因此您必须手动检查用户的凭据,获取实例并执行操作Auth::login($secondaryUser)

<?php

use Illuminate\Auth\UserInterface;

class SecondaryUser extends Eloquent implements UserInterface {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'secondary_users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password');

    /**
     * Get the unique identifier for the secondary user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the secondary user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->remember_token;
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_token';
    }

    public function mainUser()
    {
        return $this->belongsTo('User');
    }

}
Run Code Online (Sandbox Code Playgroud)

原始答案

我不确定是否了解您想要的内容,但这可能会有所帮助:http : //laravel.com/docs/security#manually

$user = User::find(1);
Auth::login($user);
Run Code Online (Sandbox Code Playgroud)

如果您有2个user模型,我认为只要扩展主User类,它就应该起作用

  • 或者更简单:``Auth :: loginUsingId(1);``:) (2认同)