File: //proc/1126/cwd/backend/controllers/SignInController.php
<?php
/**
* Created by PhpStorm.
* User: zein
* Date: 8/2/14
* Time: 11:20 AM
*/
namespace backend\controllers;
use backend\models\AccountForm;
use backend\models\LoginForm;
use Intervention\Image\ImageManagerStatic;
use League\Flysystem\FilesystemException;
use Psr\Http\Message\StreamInterface;
use trntv\filekit\actions\DeleteAction;
use trntv\filekit\actions\UploadAction;
use Yii;
use yii\filters\VerbFilter;
use yii\imagine\Image;
use yii\web\Controller;
class SignInController extends Controller
{
public $defaultAction = 'login';
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'logout' => ['post', 'get']
]
]
];
}
public function actions()
{
return [
'avatar-upload' => [
'class' => UploadAction::class,
'deleteRoute' => 'avatar-delete',
'on afterSave' => function ($event) {
$fs = $event->filesystem;
$path = $event->path;
try {
$binary = $fs->read($path);
$img = ImageManagerStatic::make($binary)->fit(215, 215);
$out = $img->stream('jpg', 90);
$payload = $out instanceof StreamInterface ? $out->getContents() : (string) $out;
$fs->write($path, $payload);
} catch (FilesystemException $e) {
Yii::error($e->getMessage(), __METHOD__);
}
}
],
'avatar-delete' => [
'class' => DeleteAction::class
]
];
}
public function actionLogin()
{
$this->layout = 'base';
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
//return $this->goBack();
return $this->redirect('/cms/user/index');
} else {
return $this->render('login', [
'model' => $model
]);
}
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
public function actionProfile()
{
$model = Yii::$app->user->identity->userProfile;
if ($model->load($_POST) && $model->save()) {
Yii::$app->session->setFlash('alert', [
'options' => ['class' => 'alert-success'],
'body' => Yii::t('backend', 'Your profile has been successfully saved', [], $model->locale)
]);
return $this->refresh();
}
return $this->render('profile', ['model' => $model]);
}
public function actionAccount()
{
$user = Yii::$app->user->identity;
$model = new AccountForm();
$model->username = $user->username;
$model->email = $user->email;
if ($model->load($_POST) && $model->validate()) {
$user->username = $model->username;
$user->email = $model->email;
if ($model->password) {
$user->setPassword($model->password);
}
$user->save();
Yii::$app->session->setFlash('alert', [
'options' => ['class' => 'alert-success'],
'body' => Yii::t('backend', 'Your account has been successfully saved')
]);
return $this->refresh();
}
return $this->render('account', ['model' => $model]);
}
}