add customer api

This commit is contained in:
2019-10-08 22:33:25 +02:00
committed by Roland Schneider
parent 9aee187d11
commit 1300bfc752
33 changed files with 1164 additions and 246 deletions

View File

@@ -3,44 +3,58 @@
namespace customerapi\controllers;
use common\components\Helper;
use common\models\Card;
use common\models\Customer;
use yii\filters\auth\HttpBasicAuth;
use Exception;
use Lcobucci\JWT\Token;
use sizeg\jwt\JwtHttpBearerAuth;
use Yii;
use yii\filters\auth\AuthMethod;
use yii\rest\Controller;
class RestController extends \yii\web\Controller
class RestController extends Controller
{
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBasicAuth::className(),
'auth' => [$this, 'auth']
'class' => JwtHttpBearerAuth::class,
'auth' => [$this, 'auth'],
'optional' => $this->getOptionalActions(),
];
return $behaviors;
}
public function auth($username, $password)
/**
* This method will check the token
* @param Token $token
* @return Customer|null
*/
public function auth($token)
{
if ( !isset($token ) ) {
return null;
}
try {
// $query = Card::find();
// Card::addCardNumberCondition($query, Helper::fixAsciiChars($username));
// $card = $query->one();
$customer = Customer::findOne(['email' => $username]);
$uid = (string) $token->getClaim('uid');
$customer = Customer::findOne(['id_customer' => $uid]);
if (isset($customer)) {
if ($customer->validatePassword($password)) {
\Yii::$app->user->setIdentity($customer);
return $customer;
}
}
} catch (\Exception $e) {
\Yii::error("Failed to load user: " . $e->getMessage());
} catch (Exception $e) {
Yii::error('Failed to load customer: ' . $e->getMessage());
}
return null;
}
/**
* @see AuthMethod::$optional
* @return array
*/
protected function getOptionalActions(){
return [];
}
}