2016-01-03 05:48:37 +05:30
|
|
|
<?php
|
2016-01-15 14:51:27 +05:30
|
|
|
namespace api\controllers;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
use api\models\LoginForm;
|
2016-01-03 05:48:37 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\filters\AccessControl;
|
|
|
|
|
|
|
|
class AuthenticationController extends Controller {
|
|
|
|
|
|
|
|
public function behaviors() {
|
|
|
|
return array_merge(parent::behaviors(), [
|
|
|
|
'access' => [
|
|
|
|
'class' => AccessControl::className(),
|
2016-01-15 14:51:27 +05:30
|
|
|
'only' => ['login'],
|
2016-01-03 05:48:37 +05:30
|
|
|
'rules' => [
|
|
|
|
[
|
2016-01-15 14:51:27 +05:30
|
|
|
'actions' => ['login', 'register'],
|
2016-01-03 05:48:37 +05:30
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['?'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verbs() {
|
|
|
|
return [
|
2016-01-15 14:51:27 +05:30
|
|
|
'login' => ['post'],
|
|
|
|
'register' => ['post'],
|
2016-01-03 05:48:37 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
public function actionLogin() {
|
|
|
|
$model = new LoginForm();
|
2016-01-03 05:48:37 +05:30
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (!$model->login()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
2016-01-15 14:51:27 +05:30
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
2016-01-03 05:48:37 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|