findForPassportにさらに1つの変数を追加または渡す方法はありますか?
デフォルトのlaravelパスポートログインでは、2つの変数(ユーザー名、パスワード)しか渡すことができませんが、さらに1つの変数を渡し、そのユーザーが他のテーブルに属しているかどうかをfindForPassportでチェックインしたいと思います。
この答えが他の人に役立つことを願っています。
あなたは変数を追加し、ユーザー認証モデルでfindPassport関数に、この変数を渡したい場合は、パスポートの3クラスに更新する必要があります:
- UserRepositoryInterfaceベンダーで\リーグ\のOAuth2-サーバー\ SRC \リポジトリ\ UserRepositoryInterface
- PasswordGrantベンダー\リーグでの\ OAuth2-サーバー\ SRC \グラント\ PasswordGrant
- UserRepositoryベンダーで\ laravel \パスポート\ SRC \橋\ UserRepository
私は親の変数やコードを追加しますサンプルコードでは、次のようになります。
+ UserRepositoryInterfaceクラス内
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クラスで
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クラス内
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値を取得できますが、値を雄弁なユーザーとして返すようにしてください。テーブルに参加する場合は、以下のサンプルコードを参照してください。
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();
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加