You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					71 lines
				
				1.8 KiB
			
		
		
			
		
	
	
					71 lines
				
				1.8 KiB
			| 
											8 years ago
										 | <?php
 | ||
|  | 
 | ||
|  | namespace common\auth;
 | ||
|  | 
 | ||
|  | use filsh\yii2\oauth2server\Module;
 | ||
|  | use OAuth2\Storage\UserCredentialsInterface;
 | ||
|  | use core\entities\user\User;
 | ||
|  | use core\repositories\user\read\UserReadRepository;
 | ||
|  | use Yii;
 | ||
|  | use yii\web\IdentityInterface;
 | ||
|  | 
 | ||
|  | class Identity implements IdentityInterface, UserCredentialsInterface
 | ||
|  | {
 | ||
|  |     public $user;
 | ||
|  | 
 | ||
|  |     public function __construct(User $user)
 | ||
|  |     {
 | ||
|  |         $this->user = $user;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public static function findIdentity($id)
 | ||
|  |     {
 | ||
|  |         $user = self::getRepository()->findActiveById($id);
 | ||
|  |         return $user ? new self($user): null;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public static function findIdentityByAccessToken($token, $type = null)
 | ||
|  |     {
 | ||
|  |         $data = self::getOauth()->getServer()->getResourceController()->getToken();
 | ||
|  |         return !empty($data['user_id']) ? static::findIdentity($data['user_id']) : null;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public function getId(): int
 | ||
|  |     {
 | ||
|  |         return $this->user->id;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public function getAuthKey(): string
 | ||
|  |     {
 | ||
|  |         return $this->user->auth_key;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public function validateAuthKey($authKey): bool
 | ||
|  |     {
 | ||
|  |         return $this->getAuthKey() === $authKey;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public function checkUserCredentials($username, $password): bool
 | ||
|  |     {
 | ||
|  |         if (!$user = self::getRepository()->findActiveByUsername($username)) {
 | ||
|  |             return false;
 | ||
|  |         }
 | ||
|  |         return $user->validatePassword($password);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public function getUserDetails($username): array
 | ||
|  |     {
 | ||
|  |         $user = self::getRepository()->findActiveByUsername($username);
 | ||
|  |         return ['user_id' => $user->id];
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     private static function getRepository(): UserReadRepository
 | ||
|  |     {
 | ||
|  |         return \Yii::$container->get(UserReadRepository::class);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     private static function getOauth(): Module
 | ||
|  |     {
 | ||
|  |         return Yii::$app->getModule('oauth2');
 | ||
|  |     }
 | ||
|  | }
 |