add mobile api

This commit is contained in:
2022-02-03 23:38:39 +01:00
parent 858f7b03fc
commit 9856411844
73 changed files with 43307 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: rocho
* Date: 2018.08.29.
* Time: 21:58
*/
namespace mobileapi\controllers;
use common\components\Helper;
use common\models\Card;
use common\models\Ticket;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
class CustomerApiController extends RestController
{
}

View File

@@ -0,0 +1,65 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: rocho
* Date: 2018.08.29.
* Time: 21:58
*/
namespace mobileapi\controllers;
use mobileapi\models\LoginForm;
use sizeg\jwt\Jwt;
use Yii;
use yii\web\UnauthorizedHttpException;
/** @noinspection PhpUnused */
class LoginController extends RestController
{
/**
* hash for password test is:
* $2y$13$D2BauYE2nhCdVDNatT9BMeWGxOvi5t5V6W2OUjr6sj2FRpb317Cpq
*
*/
/** @noinspection PhpUnused */
public function actionLogin()
{
$form = new LoginForm();
$form->load(\Yii::$app->request->post(), '');
if ($form->validate()) {
/** @var Jwt $jwt */
$jwt = Yii::$app->jwt;
$signer = $jwt->getSigner('HS256');
$key = $jwt->getKey();
$time = time();
// Adoption for lcobucci/jwt ^4.0 version
$token = $jwt->getBuilder()
->issuedBy('mobileapi')// Configures the issuer (iss claim)
->permittedFor('customer')// Configures the audience (aud claim)
->identifiedBy('A989C57D19E2AF756BA9585AC4CFAF7974AE3D2BCA7CCA7307B39AB28CC7C2C8', true)// Configures the id (jti claim), replicating as a header item
->issuedAt($time)// Configures the time that the token was issue (iat claim)
->expiresAt($time + 3600)// Configures the expiration time of the token (exp claim)
->withClaim('uid', $form->getCustomer()->getId())// Configures a new claim, called "uid"
->getToken($signer, $key); // Retrieves the generated token
return $this->asJson([
'token' => (string)$token,
]);
} else {
throw new UnauthorizedHttpException("Hibás e-mail cím vagy jelszó!");
}
}
protected function getOptionalActions()
{
return ['login'];
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: rocho
* Date: 2018.08.29.
* Time: 21:58
*/
namespace mobileapi\controllers;
use common\components\HttpStatus;
use Yii;
/** @noinspection PhpUnused */
class PingController extends RestController
{
/** @noinspection PhpUnused */
public function actionPing( )
{
Yii::$app->response->setStatusCode( HttpStatus::NO_CONTENT );
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace mobileapi\controllers;
use common\models\Customer;
use Exception;
use Lcobucci\JWT\Token;
use sizeg\jwt\JwtHttpBearerAuth;
use Yii;
use yii\filters\auth\AuthMethod;
use yii\rest\Controller;
class RestController extends Controller
{
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => JwtHttpBearerAuth::class,
'auth' => [$this, 'auth'],
'optional' => $this->getOptionalActions(),
];
return $behaviors;
}
/**
* This method will check the token
* @param Token $token
* @return Customer|null
*/
public function auth($token)
{
if ( !isset($token ) ) {
return null;
}
try {
$uid = (string) $token->getClaim('uid');
$customer = Customer::findOne(['id_customer' => $uid]);
if (isset($customer)) {
\Yii::$app->user->setIdentity($customer);
return $customer;
}
} catch (Exception $e) {
Yii::error('Failed to load customer: ' . $e->getMessage());
}
return null;
}
/**
* @see AuthMethod::$optional
* @return array
*/
protected function getOptionalActions(){
return [];
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace mobileapi\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
/**
* Site controller
*/
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout' ],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Displays homepage.
*
* @return mixed
*/
public function actionIndex()
{
return $this->render('index');
}
/**
* Logs out the current user.
*
* @return mixed
*/
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: rocho
* Date: 2018.08.29.
* Time: 21:58
*/
namespace mobileapi\controllers;
use common\models\Customer;
use mobileapi\models\PasswordChangeForm;
use yii\web\BadRequestHttpException;
/** @noinspection PhpUnused */
class UserController extends RestController
{
/**
* @throws \yii\base\InvalidConfigException
* @throws \yii\base\Exception
* @throws BadRequestHttpException
*/
public function actionPasswordChange()
{
$form = new PasswordChangeForm();
$form->scenario = "default";
$form->load(\Yii::$app->request->post(), '');
if (!$form->validate()) {
throw new BadRequestHttpException( $form->getErrorSummary(false)[0]);
}
$customer = Customer::findOne(\Yii::$app->user->id);
if (!$customer->validatePassword($form->passwordOld)) {
throw new BadRequestHttpException("Jelenlegi jelszó nem egyezik", "2");
}
$customer->setPassword($form->password);
$customer->save();
}
}