自定义Laravel护照检查 [英] Custom Laravel Passport Check

查看:127
本文介绍了自定义Laravel护照检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否仍然有要向findForPassport添加或传递另外1个变量?

Is There anyway to add or pass 1 more variable to findForPassport ?

在默认的laravel护照登录中,我只能传递2个变量(用户名,密码),但我想再传递1个变量,并在findForPassport中检查该用户是否属于其他表.

In default laravel passport login, i only can pass 2 variable (username, password),but I want to pass 1 more variable and check in findForPassport if that user is belong other table or not .

推荐答案

我希望这个答案可以对其他人有所帮助.
如果要添加变量并将此变量传递给User Authenticate模型中的findPassport函数,则需要更新护照中的3类:
  -供应商\联盟\ oauth2-server \ src \ Repositories \ UserRepositoryInterface
中的 UserRepositoryInterface   -供应商\联盟\ oauth2-server \ src \ Grant \ PasswordGrant中的 PasswordGrant
  -供应商\ laravel \ passport \ src \ Bridge \ UserRepository中的 UserRepository

在示例代码中,我将添加父变量,并且代码将如下所示

I hope this answer can help to other.
If you want to add variable and pass this variable to findPassport function in User Authenticate model , you need to update 3 class in passport :
  - UserRepositoryInterface in vendor\league\oauth2-server\src\Repositories\UserRepositoryInterface
  - PasswordGrant in vendor\league\oauth2-server\src\Grant\PasswordGrant
  - UserRepository in vendor\laravel\passport\src\Bridge\UserRepository

in the example code I will add parent variable and code will look like this

+在UserRepositoryInterface类中

+in UserRepositoryInterface class

interface UserRepositoryInterface extends RepositoryInterface
{

/**
 * Get a user entity.
 *
 * @param string                $username
 * @param string                $password
 * @param string                $grantType    The grant type used
 * @param ClientEntityInterface $clientEntity
 *
 * @return UserEntityInterface
 */
public function getUserEntityByUserCredentials(
    $username,
    $password,
    $parent,                           <------variable example 
    $grantType,
    ClientEntityInterface $clientEntity
);
}

+在PasswordGrant类中

+in PasswordGrant class

class PasswordGrant extends AbstractGrant{ 
protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client)
{
    $username = $this->getRequestParameter('username', $request);
    if (is_null($username)) {
        throw OAuthServerException::invalidRequest('username');
    }

    $password = $this->getRequestParameter('password', $request);
    if (is_null($password)) {
        throw OAuthServerException::invalidRequest('password');
    }

  /**
  * Get a user parent.
  * varaible example 
  */
    $parent = $this->getRequestParameter('parent', $request);  
    if (is_null($parent)) {
        throw OAuthServerException::invalidRequest('password');
    }

    $user = $this->userRepository->getUserEntityByUserCredentials(
        $username,
        $password,
        $parent,                          <--- variable example get from request
        $this->getIdentifier(),
        $client
    );
    if ($user instanceof UserEntityInterface === false) {
        $this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request));

        throw OAuthServerException::invalidCredentials();
    }

    return $user;
}  }

+在UserRepository类中

+in UserRepository class

class UserRepository implements UserRepositoryInterface
{

public function getUserEntityByUserCredentials($username, $password, $parent, $grantType, ClientEntityInterface $clientEntity) 
/*add 1more parameter that implement from UserRepositoryInterface*/
{
    $provider = config('auth.guards.api.provider');

    if (is_null($model = config('auth.providers.'.$provider.'.model'))) {
        throw new RuntimeException('Unable to determine authentication model from configuration.');
    }

    if (method_exists($model, 'findForPassport')) {
        $user = (new $model)->findForPassport($username,$parent);    <--- finally we pass parent variable to findForPassport here
    } else {
        $user = (new $model)->where('email', $username)->first();
    }

    if (! $user) {
        return;
    } elseif (method_exists($user, 'validateForPassportPasswordGrant')) {
        if (! $user->validateForPassportPasswordGrant($password)) {
            return;
        }
    } elseif (! $this->hasher->check($password, $user->getAuthPassword())) {
        return;
    }

    return new User($user->getAuthIdentifier());
}
}

然后您可以从findForPassport的参数中获取$ parent值,但请确保您以有说服力的User返回值.如果要加入表,可以在下面的示例代码中查看

then u can get $parent value from parameter in findForPassport .but make sure you return value as eloquent User .if you want to join table , you can look my example code below

class User extends Authenticatable{

..........
public function findForPassport($identifier,$parent) {
    $a = $this
        ->Join('role as r', 'r.user_id', '=', 'users.id')
        ->get();
    return $a->where('name', $identifier)->where('role_id',$parent)->first();

  }
}

这篇关于自定义Laravel护照检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆