add customerapi + customer auth
This commit is contained in:
46
customerapi/controllers/RestController.php
Normal file
46
customerapi/controllers/RestController.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace customerapi\controllers;
|
||||
|
||||
|
||||
use common\components\Helper;
|
||||
use common\models\Card;
|
||||
use common\models\Customer;
|
||||
use yii\filters\auth\HttpBasicAuth;
|
||||
|
||||
class RestController extends \yii\web\Controller
|
||||
{
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
$behaviors = parent::behaviors();
|
||||
$behaviors['authenticator'] = [
|
||||
'class' => HttpBasicAuth::className(),
|
||||
'auth' => [$this, 'auth']
|
||||
];
|
||||
return $behaviors;
|
||||
}
|
||||
|
||||
public function auth($username, $password)
|
||||
{
|
||||
try {
|
||||
// $query = Card::find();
|
||||
// Card::addCardNumberCondition($query, Helper::fixAsciiChars($username));
|
||||
// $card = $query->one();
|
||||
|
||||
$customer = Customer::findOne(['email' => $username]);
|
||||
|
||||
|
||||
if (isset($customer)) {
|
||||
if ($customer->validatePassword($password)) {
|
||||
return $customer;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Yii::error("Failed to load user: " . $e->getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user