public function actionRefresh()
{
$refresh_token = Yii::$app->request->headers->get('Authorization');
$refresh_token = substr($refresh_token, 7);
$decoded = User::getUserDataFromJWT($refresh_token);
$user = User::find()
->where(['id' => $decoded->data->user_id])
->one();
if ($user) {
if (Yii::$app->security->validatePassword($refresh_token, $user->refresh_token_hash)) {
$refresh_token = $user->generateRefreshToken(
[
'user_id' => $user->id,
]
);
$user->refresh_token = $refresh_token;
$userSaved = $user->save();
$jwt = $user->generateJWTtoken(
[
'user_id' => $user->id,
'username' => $user->username,
'email' => $user->email,
]
);
if (!$userSaved) {
$out['err']['user not saved'] = [
$user->getErrors(),
$user->errors,
$user->getAttributes(),
];
} else {
$out['access_token'] = $jwt;
$out['refresh_token'] = $refresh_token;
$out['user'] = $user->getAttributes();
}
} else {
$user->updateFailedLoginAttempts(false);
}
} else {
throw new NotFoundHttpException('User not found');
}
return $out;
}
public function actionLogin()
{
$in = \Yii::$app->request->post();
$email = $in['email'];
$password = $in['password'];
$username = $in['username'];
$user = User::find()
->where(['email' => $email])
->orWhere(['username' => $username])
->one();
if ($user) {
// Блокировка учетной записи B2
if ($user->isLoginBlocked()) {
throw new ForbiddenHttpException('Your account is blocked until ' . date("d.m.Y H:i:s", $user->login_locked_until));
}
if (Yii::$app->security->validatePassword($password, $user->password_hash)) {
$user->updateFailedLoginAttempts(true);
$jwt = $user->generateJWTtoken(
[
'user_id' => $user->id,
'username' => $user->username,
'email' => $user->email,
]
);
$refresh_token = $user->generateRefreshToken(
[
'user_id' => $user->id,
]
);
$user->refresh_token = $refresh_token;
$userSaved = $user->save();
if (!$userSaved) {
$out['err']['user not saved'] = [
$user->getErrors(),
$user->errors,
$user->getAttributes(),
];
} else {
$out['user'] = $user->getAttributes();
$out['access_token'] = $jwt;
$out['refresh_token'] = $refresh_token;
}
} else {
$user->updateFailedLoginAttempts(false);
}
} else {
throw new NotFoundHttpException('User not found');
}
return $out;
}
\Yii::info([
'$_FILES' => $_FILES,
'avatar' => UploadBind::get($this, 'avatar', ['active' => 1]),
'UploadedFile::getInstaceByName(avatar)' => UploadedFile::getInstanceByName('avatar'),
'UploadedFile::getInstacesByName(avatar)' => UploadedFile::getInstancesByName('avatar'),
'UploadedFile::getInstance($this, avatar)' => UploadedFile::getInstance($this, 'avatar'),
'UploadedFile::getInstances($this, avatar)' => UploadedFile::getInstances($this, 'avatar'),
], 'bulat / ' . (__METHOD__ ?: __FILE__));
public function getAnswerDestination($material_id)
{
$theme_id = Material::findOne($material_id)->theme_id;
$theme = Theme::findOne($theme_id);
$lesson_id = $theme->lesson_id;
$lesson = Lesson::findOne($lesson_id);
$course_id = $lesson->course_id;
$course = Course::findOne($course_id);
return (object) [
'course' => (object) [
'id' => $course_id,
'name' => $course->name
],
'lesson' => (object) [
'id' => $lesson_id,
'name' => $lesson->name
],
'theme' => (object) [
'id' => $theme_id,
'name' => $theme->name
],
];
}
public function fields()
{
$fields = parent::fields();
...
$fields['images'] = function (Material $model) {
return UploadBind::get($model, 'images', ['active' => 1]);
};
...
return $fields;
}
public static function get($model, $attrs = null, $conditions = [], $asQuery = 0, $addUploads = 1)
{
$defaultConditions = [
'model_name' => $model::className(),
'model_id' => $model->getAttribute('id'),
];
if ($attrs)
$defaultConditions['model_attr'] = $attrs;
$conditions = array_merge($defaultConditions, $conditions);
$q = UploadBind::find()->where($conditions)->orderBy('sort');
if ($asQuery) {
return $q;
}
/** @var UploadBind[] $binds */
$binds = $q->all();
if ($addUploads) {
$uploadsIds = ArrayHelper::getColumn($binds, 'upload_id');
//$uploads = Upload::findAll(['id' => $uploadsIds]);
$uploads = Upload::find()->where(['id' => $uploadsIds])->all();
$uploadsMap = ArrayHelper::map($uploads, 'id', function ($x) {
return $x;
});
foreach ($binds as $bind) {
$bind->upload = $uploadsMap[$bind->upload_id];
}
}
return $binds;
}
С логами:
То есть я проверяю хэш от одного токена с новым токеном и он выводит TRUE