| Current Path : /var/www/html/protegev2/backend/modules/mimin/controllers/ |
| Current File : /var/www/html/protegev2/backend/modules/mimin/controllers/UserController.php |
<?php
namespace backend\modules\mimin\controllers;
use Yii;
use backend\modules\mimin\models\User;
use backend\modules\mimin\models\AuthAssignment;
use backend\modules\mimin\models\AuthItem;
use backend\modules\mimin\models\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\filters\AccessControl;
use yii\helpers\Url;
/**
* UserController implements the CRUD actions for User model.
*/
class UserController extends Controller {
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all User models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider,]);
}
/**
* Displays a single User model.
* @param integer $id
* @return mixed
*/
public function actionView($id) {
$model = $this->findModel($id);
$authAssignments = AuthAssignment::find()->where(['user_id' => $model->id])->column();
$authItems = ArrayHelper::map(AuthItem::find()->where(['type' => 1])->asArray()->all(), 'name', 'name');
$authAssignment = new AuthAssignment(['user_id' => $model->id,]);
if (Yii::$app->request->post()) {
$authAssignment->load(Yii::$app->request->post());
// delete all role
AuthAssignment::deleteAll(['user_id' => $model->id]);
if (is_array($authAssignment->item_name)) {
foreach ($authAssignment->item_name as $item) {
if (!in_array($item, $authAssignments)) {
$authAssignment2 = new AuthAssignment(['user_id' => $model->id,]);
$authAssignment2->item_name = $item;
$authAssignment2->created_at = time();
$authAssignment2->save();
$authAssignments = AuthAssignment::find()->where(['user_id' => $model->id])->column();
}
}
}
Yii::$app->session->setFlash('success', Yii::t('app', 'Data hase been save'));
return $this->redirect(['index']);
}
$authAssignment->item_name = $authAssignments;
return $this->render('view', ['model' => $model, 'authAssignment' => $authAssignment, 'authItems' => $authItems,]);
}
/**
* Creates a new User model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new User();
$model->scenario = 'create';
$model->status = 1;
$model->created_at = date("Y-m-d H:i:s");
$model->created_by = Yii::$app->user->identity->id;
$model->updated_at = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
if ($model->load(Yii::$app->request->post())) {
$pwd = $model->password_hash;
$model->setPassword($model->password_hash);
$model->status = $model->status == 1 ? 10 : 0;
if ($model->save()) {
if (!empty($model->userroles)) {
foreach ($model->userroles as $item) {
$authAssignment2 = new AuthAssignment();
$authAssignment2->user_id = $model->id;
$authAssignment2->item_name = $item;
$authAssignment2->created_at = date("Y-m-d H:i:s");
$authAssignment2->save(false);
}
}
$to_mail = $model->email;
$subject_mail = 'CMS Login Details';
$content_mail = '<p>Hi ' . $model->fullname . ',</p>
<p>Welcome to ' . Yii::$app->params['systemName'] . '!</p>
<p>You now can access the system as admin using below details:</p>
<p>URL: <a href="' . Url::base(true) . '" target="_blank">' . Url::base(true) . '</a></p>
<p>Username: ' . $model->username . '</p>
<p>Password: ' . $pwd . '</p>
<p>- Admin -</p>';
if (!empty($to_mail))
Yii::$app->emailModule->sendemail($to_mail, $subject_mail, $content_mail);
$this->getView()->registerJs("swal.fire({
title:'User has been added',
icon: 'success',
text: 'Login details have been emailed to the user',
}).then(function(result){
window.location = '" . Url::toRoute('/mimin/user/index') . "';
});");
}
}
return $this->render('create', ['model' => $model]);
}
public function actionResetpwd() {
$output = "failed";
if (($model = User::findOne($_POST['id'])) !== null) {
$pwd = Yii::$app->security->generateRandomString(10);
$model->password_hash = \Yii::$app->security->generatePasswordHash($pwd);
if ($model->save(false)) {
$to_mail = $model->email;
$subject_mail = 'Reset Password';
$content_mail = '<p>Hi ' . $model->fullname . ',</p>
<p>Welcome to ' . Yii::$app->params['systemName'] . '!</p>
<p>Your password has been reset. You can access the system now using below details :</p>
<p>URL: <a href="' . Url::base(true) . '" target="_blank">' . Url::base(true) . '</a></p>
<p>Username: ' . $model->username . '</p>
<p>Password: ' . $pwd . '</p>
<p>- Admin -</p>';
if (!empty($to_mail))
Yii::$app->emailModule->sendemail($to_mail, $subject_mail, $content_mail);
$output = 'success';
}
}
return $output;
}
/**
* Updates an existing User model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id) {
$model = $this->findModel($id);
$model->updated_at = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$model->status = 1;
if ($model->status == 10)
$model->status = 0;
$model->userroles = ArrayHelper::map((new \yii\db\Query())->from('auth_assignment')->where(['=', 'user_id', $id])->all(), 'item_name', 'item_name');
if ($model->load(Yii::$app->request->post())) {
$model->status = $model->status == 1 ? 10 : 0;
if ($model->save()) {
AuthAssignment::deleteAll(['user_id' => $model->id]);
if (!empty($model->userroles)) {
foreach ($model->userroles as $item) {
$authAssignment2 = new AuthAssignment();
$authAssignment2->user_id = $model->id;
$authAssignment2->item_name = $item;
$authAssignment2->created_at = date("Y-m-d H:i:s");
$authAssignment2->save(false);
}
}
$this->getView()->registerJs("swal.fire({
title:'Saved',
icon: 'success',
text: 'User has been updated',
}).then(function(result){
window.location = '" . Url::toRoute('/mimin/user/index') . "';
});");
}
}
return $this->render('update', ['model' => $model]);
}
/**
* Deletes an existing User model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
/* public function actionDelete($id) {
$model = $this->findModel($id);
$authAssignments = AuthAssignment::find()->where(['user_id' => $model->id,])->all();
foreach ($authAssignments as $authAssignment) {
$authAssignment->delete();
}
Yii::$app->session->setFlash('success', 'Delete success');
$model->delete();
return $this->redirect(['index']);
} */
public function actionDelete() {
$id = $_POST['id'];
$status = 'failed';
if (($model = User::findOne($id)) !== null) {
AuthAssignment::deleteAll(['user_id' => $model->id]);
$model->delete();
$status = 'success';
}
return $status;
}
/**
* Finds the User model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return User the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = User::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}