Skip to content

Commit

Permalink
Release early version containing basic features
Browse files Browse the repository at this point in the history
  • Loading branch information
prabowomurti committed Nov 26, 2016
2 parents bd20e68 + 9ef0e9d commit ba0f8e2
Show file tree
Hide file tree
Showing 28 changed files with 1,399 additions and 24 deletions.
3 changes: 3 additions & 0 deletions backend/assets/AppAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class AppAsset extends AssetBundle
];
public $js = [
];
public $jsOptions = [
'position' => \yii\web\View::POS_HEAD
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
Expand Down
7 changes: 6 additions & 1 deletion backend/controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use common\models\User;

/**
* Site controller
Expand All @@ -29,6 +30,10 @@ public function behaviors()
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action)
{
return Yii::$app->user->identity['role'] == User::ROLE_ADMIN;
}
],
],
],
Expand Down Expand Up @@ -65,7 +70,7 @@ public function actionLogin()
}

$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
if ($model->load(Yii::$app->request->post()) && $model->backendLogin()) {
return $this->goBack();
} else {
return $this->render('login', [
Expand Down
236 changes: 236 additions & 0 deletions backend/controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
<?php

namespace backend\controllers;

use Yii;
use common\models\User;
use common\models\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

/**
* UserController implements the CRUD actions for User model.
*/
class UserController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['index', 'view', 'edit', 'create', 'update', 'delete', 'multipledelete', 'profile'],
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action)
{
return
Yii::$app->user->identity['role'] == User::ROLE_SUPERADMIN ||
Yii::$app->user->identity['role'] == User::ROLE_ADMIN;
}
],
],
],
'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)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}

/**
* 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();

if ($model->load(Yii::$app->request->post()))
{
if (strlen($model->password) < 6)
{
$model->addError('password', 'Password should contain at least 6 characters.');
return $this->render('create', [
'model' => $model,
]);
}

$model->setPassword($model->password);
$model->generateAuthKey();

if ($model->save())
return $this->redirect(['view', 'id' => $model->id]);
else
{
return $this->render('create', [
'model' => $model,
]);
}

}
else
return $this->render('create', [
'model' => $model,
]);
}

/**
* 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);

if (Yii::$app->request->post())
{
$post_user = Yii::$app->request->post('User');

$model->username = $post_user['username'];
$model->email = $post_user['email'];
$model->role = $post_user['role'];
$model->status = $post_user['status'];

// update password
if ( ! empty($post_user['password']))
{
if (strlen($model->password) < 6)
{
$model->addError('password', 'Password should contain at least 6 characters.');
return $this->render('update', [
'model' => $model,
]);
}

$model->setPassword($post_user['password']);
}

if ($model->save())
return $this->redirect(['view', 'id' => $model->id]);
else
return $this->render('update', ['model' => $model,]);
}
else
return $this->render('update', ['model' => $model]);
}

/**
* Updates the logged in user's profile
* @param boolean $success
* @return mixed
*/
public function actionProfile($success = false)
{
$model = $this->findModel(Yii::$app->user->id);

if (Yii::$app->request->post())
{
$post_user = Yii::$app->request->post('User');

$model->username = $post_user['username'];

// update password
if ( ! empty($post_user['password']))
{
if (strlen($model->password) < 6)
{
$model->addError('password', 'Password should contain at least 6 characters.');
return $this->render('update', [
'model' => $model,
]);
}

$model->setPassword($post_user['password']);
}

if ($model->save())
return $this->redirect(['profile', 'success' => true]);
else
return $this->render('profile', ['model' => $model]);
}
else
return $this->render('profile', ['model' => $model, 'success' => $success]);
}

/**
* 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)
{
$this->findModel($id)->delete();

return $this->redirect(['index']);
}

/**
* Delete multiplede IDs
* @return mixed
*/
public function actionMultipledelete()
{
if (Yii::$app->request->isAjax)
{
$selected_ids = Yii::$app->request->post('selectedItems');
foreach ($selected_ids as $id)
$this->findModel($id)->delete();
}
}

/**
* 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.');
}
}
}
32 changes: 32 additions & 0 deletions backend/views/layouts/js/_multipledelete_script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script type="text/javascript">
$(document).ready(function (){
var selectedItems;

$('#delete_selected_items_btn').click(function (){
selectedItems = $('.grid-view').yiiGridView('getSelectedRows');

if ( ! selectedItems.length)
{
alert('Please select at least one item to be deleted');
return false;
}

if ( ! confirm('Are you sure to delete ' + selectedItems.length + ' items?')) return false;

var multipledeleteUrl = "<?=Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/multipledelete');?>";
$.ajax({
type: "POST",
url: multipledeleteUrl,
data: {selectedItems : selectedItems},
success: (function (e){
$.pjax.reload({container : '#w0'});
selectedItems = [];

}),
error: (function (e) {
alert("Can not delete selected items");
})
});
})
})
</script>
18 changes: 16 additions & 2 deletions backend/views/layouts/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* @var $this \yii\web\View */
/* @var $content string */

use common\models\User;
use backend\assets\AppAsset;
use yii\helpers\Html;
use yii\bootstrap\Nav;
Expand Down Expand Up @@ -37,9 +38,22 @@
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
];
if (Yii::$app->user->isGuest) {
if (Yii::$app->user->isGuest)
{
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
}
elseif (Yii::$app->user->identity['role'] == User::ROLE_SUPERADMIN)
{
$menuItems[] = [
'label' => 'Profile',
'url' => ['/user/profile'],
];

$menuItems[] = [
'label' => 'User',
'url' => ['/user/index'],
];

$menuItems[] = '<li>'
. Html::beginForm(['/site/logout'], 'post')
. Html::submitButton(
Expand Down
32 changes: 32 additions & 0 deletions backend/views/user/_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use common\models\User;
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\User */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="user-form">

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'email')->input('email', ['maxlength' => true]) ?>

<?= $form->field($model, 'password')->passwordInput(['placeholder' => 'Leave this field blank if you do not want to change the password', 'minlength' => 6]) ?>

<?= $form->field($model, 'role')->dropDownList(User::getRoleAsArray()); ?>

<?= $form->field($model, 'status')->dropDownList(User::getStatusAsArray()); ?>

<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>
Loading

0 comments on commit ba0f8e2

Please sign in to comment.