|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\components\repositories; |
|
4
|
|
|
|
|
5
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
|
6
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\base\Oauth2BaseRepository; |
|
7
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2UserRepositoryInterface; |
|
8
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2PasswordGrantUserInterface; |
|
9
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2UserInterface; |
|
10
|
|
|
use yii\base\InvalidConfigException; |
|
11
|
|
|
|
|
12
|
|
|
class Oauth2UserRepository extends Oauth2BaseRepository implements Oauth2UserRepositoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @inheritDoc |
|
16
|
|
|
* @return Oauth2UserInterface|string |
|
17
|
|
|
*/ |
|
18
|
10 |
|
public function getModelClass() |
|
19
|
|
|
{ |
|
20
|
10 |
|
return $this->_module->identityClass; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @inheritDoc |
|
25
|
|
|
*/ |
|
26
|
4 |
|
public function getUserEntityByIdentifier($identifier) |
|
27
|
|
|
{ |
|
28
|
4 |
|
$userClass = $this->getModelClass(); |
|
29
|
4 |
|
$user = $userClass::findIdentity($identifier); |
|
30
|
|
|
|
|
31
|
4 |
|
if (empty($user)) { |
|
32
|
1 |
|
return null; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
if (!($user instanceof Oauth2UserInterface)) { |
|
36
|
1 |
|
throw new \TypeError( |
|
37
|
1 |
|
$userClass . '::findIdentity() must return an instance of ' . Oauth2UserInterface::class |
|
|
|
|
|
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
return $user; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritDoc |
|
46
|
|
|
* @throws InvalidConfigException |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public function getUserEntityByUserCredentials( |
|
49
|
|
|
$username, |
|
50
|
|
|
$password, |
|
51
|
|
|
$grantType, |
|
52
|
|
|
ClientEntityInterface $clientEntity |
|
53
|
|
|
) { |
|
54
|
6 |
|
$userClass = $this->getModelClass(); |
|
55
|
6 |
|
if (!is_a($userClass, Oauth2PasswordGrantUserInterface::class, true)) { |
|
56
|
1 |
|
throw new \TypeError( |
|
57
|
1 |
|
'In order to support the `password` grant type, ' . $userClass |
|
|
|
|
|
|
58
|
1 |
|
. ' must implement ' . Oauth2PasswordGrantUserInterface::class |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** @var Oauth2UserInterface|null $user */ |
|
63
|
5 |
|
$user = $userClass::findByUsername($username); |
|
|
|
|
|
|
64
|
5 |
|
if (empty($user)) { |
|
65
|
1 |
|
return null; |
|
66
|
|
|
} |
|
67
|
4 |
|
if (!($user instanceof Oauth2UserInterface)) { |
|
|
|
|
|
|
68
|
1 |
|
throw new \TypeError( |
|
69
|
1 |
|
$userClass . '::findByUsername() must return an instance of ' . Oauth2UserInterface::class |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
3 |
|
if (!($user instanceof Oauth2PasswordGrantUserInterface)) { |
|
73
|
1 |
|
throw new \TypeError( |
|
74
|
1 |
|
'In order to support the `password` grant type, ' . $userClass |
|
75
|
1 |
|
. '::findByUsername() must return an instance of ' . Oauth2PasswordGrantUserInterface::class |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
if ($user->validatePassword($password)) { |
|
80
|
1 |
|
return $user; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|