File: /home/aliazzsr/api.crm.vqode.com/controllers/AuthController.php
<?php
namespace app\controllers;
use app\models\LoginCredentials;
use app\models\messages\ErrorMessage;
use app\models\forms\Login;
use app\components\behaviors\CorsBehavior;
use yii\filters\auth\HttpBearerAuth;
use yii\web\Controller;
class AuthController extends Controller
{
public $enableCsrfValidation = false;
/**
* @inheritdoc
*/
public function behaviors()
{
$behaviors = parent::behaviors();
if (\Yii::$app->params['cors-headers']) {
$behaviors[] = [
'class' => CorsBehavior::className(),
'cors' => \Yii::$app->params['cors-headers'],
];
}
if (in_array($this->action->id, ['logout', 'verify'])) {
$behaviors[] = [
'class' => HttpBearerAuth::className(),
];
}
return $behaviors;
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'logout' => [
'class' => 'app\components\actions\BodyFormAction',
'formClass' => 'app\models\forms\Logout',
],
'reset-password' => [
'class' => 'app\components\actions\BodyFormAction',
'formClass' => 'app\models\forms\ForgotPassword',
],
'set-password' => [
'class' => 'app\components\actions\BodyFormAction',
'formClass' => 'app\models\forms\NewPassword',
],
];
}
/**
* @SWG\Post(
* path="/auth/login",
* tags={"Auth"},
* summary="Authenticates user by credentials provided.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(name="body", required=true, in="body", @SWG\Schema(ref="#/definitions/LoginCredentials")),
* @SWG\Response(response=200, description="The user authenticated successfully.",
* @SWG\Schema(ref="#/definitions/AuthToken")
* ),
* @SWG\Response(response=401, description="Invalid credentials.", @SWG\Schema(ref="#/definitions/MessageObject")),
* @SWG\Response(response=403, description="EULA not accepted.", @SWG\Schema(ref="#/definitions/MessageObject"))
* )
*/
public function actionLogin()
{
try {
$model = new Login();
if ($model->process()) {
$loginCreds = new LoginCredentials([
'identity' => \Yii::$app->user->identity,
]);
return $loginCreds->toArray();
}
} catch (\Throwable $e) {
return new ErrorMessage([
'code' => 500,
'message' => $e->getMessage(), //'Internal Server Error',
]);
}
$errors = $model->getFirstErrors();
return new ErrorMessage([
'code' => $model->httpCode,
'message' => reset($errors),
]);
}
/**
* @SWG\Get(
* path="/auth/verify",
* tags={"Auth"},
* security={{"bearer":{}}},
* summary="Validates access token.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Response(response=200, description="The user details.",
* @SWG\Schema(ref="#/definitions/AuthToken")
* ),
* @SWG\Response(response=401, description="Unauthorized.", @SWG\Schema(ref="#/definitions/MessageObject")),
* @SWG\Response(response=500, description="An error occurred.", @SWG\Schema(ref="#/definitions/MessageObject"))
* )
*/
public function actionVerify()
{
$loginCreds = new LoginCredentials([
'identity' => \Yii::$app->user->identity,
]);
return $loginCreds->toArray();
}
/**
* @SWG\Get(
* path="/auth/logout",
* tags={"Auth"},
* security={{"bearer":{}}},
* summary="Logging user out.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Response(response=200, description="User logged out successfully.", @SWG\Schema(ref="#/definitions/MessageObject")),
* @SWG\Response(response=401, description="Unauthorized.", @SWG\Schema(ref="#/definitions/MessageObject")),
* @SWG\Response(response=500, description="An error occurred.", @SWG\Schema(ref="#/definitions/MessageObject"))
* )
* @SWG\Post(
* path="/auth/reset-password",
* tags={"Auth"},
* summary="Sends an email to the user with the link to set a new password.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(name="body", required=true, in="body", @SWG\Schema(ref="#/definitions/ResetPasswordForm")),
* @SWG\Response(response=200, description="Email has been sent successfully."),
* @SWG\Response(response=422, description="Validation error."),
* @SWG\Response(response=500, description="Internal server error.")
* )
* @SWG\Post(
* path="/auth/set-password",
* tags={"Auth"},
* summary="Sets a new password for the user by token and email.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(name="body", required=true, in="body", @SWG\Schema(ref="#/definitions/SetPasswordForm")),
* @SWG\Response(response=200, description="A new password has been set successfully."),
* @SWG\Response(response=422, description="Validation error."),
* @SWG\Response(response=500, description="Internal server error.")
* )
*/
}
/**
* @SWG\Definition(
* definition="AuthToken",
* type="object",
* description="User authentication details",
* allOf={
* @SWG\Schema(ref="#/definitions/AuthToken"),
* @SWG\Schema(
* required={"user", "token", "permissions"},
* @SWG\Property(property="user", type="object", description="An error message", ref="#/definitions/User"),
* @SWG\Property(property="token", type="string", description="JWT Token", example="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...PaW1zFOqr4"),
* @SWG\Property(property="permissions", type="array", @SWG\Items(ref="#/definitions/CrudPermission")),
* )
* }
* )
* @SWG\Definition(
* definition="LoginCredentials",
* type="object",
* description="User login credentials",
* allOf={
* @SWG\Schema(ref="#/definitions/LoginCredentials"),
* @SWG\Schema(
* required={"username","password"},
* @SWG\Property(property="username", type="string", description="Email", example="[email protected]"),
* @SWG\Property(property="password", type="string", description="Password", example="aDm1nPassW00rD"),
* @SWG\Property(property="acceptEula", type="boolean", description="Does the user accept EULA", example=true)
* )
* }
* )
* @SWG\Definition(
* definition="ResetPasswordForm",
* type="object",
* description="Data to send a link to recovery a password to the user.",
* allOf={
* @SWG\Schema(ref="#/definitions/ResetPasswordForm"),
* @SWG\Schema(
* required={"email"},
* @SWG\Property(property="email", type="string", description="User email", example="[email protected]")
* )
* }
* )
* @SWG\Definition(
* definition="SetPasswordForm",
* type="object",
* description="Sets user password by token and email provided.",
* allOf={
* @SWG\Schema(ref="#/definitions/SetPasswordForm"),
* @SWG\Schema(
* required={"email","token","password"},
* @SWG\Property(property="email", type="string", description="User email", example="[email protected]"),
* @SWG\Property(property="token", type="string", description="Token", example="765222f-8069580-2acb0a"),
* @SWG\Property(property="password", type="string", description="A new password", example="QweAsd123")
* )
* }
* )
* @SWG\Definition(
* definition="MessageObject",
* type="object",
* description="Base messaging object",
* allOf={
* @SWG\Schema(ref="#/definitions/MessageObject"),
* @SWG\Schema(
* @SWG\Property(property="status", type="string", description="Operation result: error or success", example="error"),
* @SWG\Property(property="code", type="integer", description="HTTP Code", example="401"),
* @SWG\Property(property="message", type="string", description="An error message", example="Login failed: invalid credentials")
* )
* }
* )
*
*/