Skip to content

Commit

Permalink
accounts alive
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed May 28, 2015
1 parent bf0bd61 commit fdf94e1
Show file tree
Hide file tree
Showing 11 changed files with 422 additions and 108 deletions.
161 changes: 98 additions & 63 deletions controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,107 @@

namespace hipanel\modules\hosting\controllers;

use hipanel\modules\hosting\models\Account;
use Yii;
use yii\base\Response;
use yii\web\NotFoundHttpException;

class AccountController extends \hipanel\base\CrudController
{
public function actionCreateFtp () {
return $this->actionCreate('ftponly');
}

public function actionCreate ($type = 'user') {
if (!in_array($type, ['user', 'ftponly'])) {
throw new NotFoundHttpException('Account type is unknown');
}
$model = new Account(['scenario' => 'insert-' . $type]);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}

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

public function actionSetPassword ($id) {
$model = $this->findModel($id);
$model->scenario = 'set-password';
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) {
Yii::$app->getSession()->addFlash('success', [
'title' => $model->login,
'text' => Yii::t('app', 'Password changing task has been successfully added to queue'),
]);

return $this->redirect(['view', 'id' => $model->id]);
} else {
/// TODO: do
return $this->render('view', ['model' => $model]);
}
}

/**
* @param $id
* @return string|Response
* @throws NotFoundHttpException
*/
public function actionSetAllowedIps ($id) {
$model = $this->findModel($id);
$model->scenario = 'set-allowed-ips';
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) {
$flash = [
'type' => 'success',
'text' => Yii::t('app', 'Allowed IPs changing task has been successfully added to queue')
];
} else {
$flash['type'] = 'error';
if ($model->hasErrors()) {
$flash['text'] = $model->getFirstError('sshftp_ips');
} else {
$flash['text'] = Yii::t('app', 'An error occurred when trying to change allowed IPs');
}
}

Yii::$app->getSession()->addFlash($flash['type'], $flash['text']);

return $this->redirect(['view', 'id' => $model->id]);
public function actions()
{
return [
'create' => [
'class' => 'hipanel\actions\SwitchAction',
'success' => Yii::t('app', 'Account create task has been created successfully'),
'error' => Yii::t('app', 'Error while creating account'),
'scenario' => 'create-user',
'GET html | GET pjax' => [
'class' => 'hipanel\actions\RenderAction',
'view' => 'create',
'params' => [
'model' => function ($action) {
return $action->controller->newModel(['scenario' => 'create-user']);
},
],
],
'POST html | POST pjax' => [
'save' => true,
'success' => [
'class' => 'hipanel\actions\RedirectAction',
'url' => function ($action, $model) {
return ['view', 'id' => $model->id];
}
],
'error' => [
'class' => 'hipanel\actions\RenderAction',
'view' => 'create',
'params' => [
'model' => function ($action, $model) {
return $model;
},
'type' => 'user'
],
],
],
],
'create-ftponly' => [
'class' => 'hipanel\actions\SwitchAction',
'success' => Yii::t('app', 'Account create task has been created successfully'),
'error' => Yii::t('app', 'Error while creating account'),
'GET html | GET pjax' => [
'class' => 'hipanel\actions\RenderAction',
'view' => 'create',
'params' => [
'model' => function ($action) {
return $action->controller->newModel(['scenario' => 'create-ftponly']);
}
],
],
'POST html | POST pjax' => [
'save' => true,
'success' => [
'class' => 'hipanel\actions\RedirectAction',
'url' => function ($action, $model) {
return ['view', 'id' => $model->id];
}
],
'error' => [
'class' => 'hipanel\actions\RenderAction',
'view' => 'create',
'params' => [
'model' => function ($action, $model) {
return $model;
}
],
],
],
],
'set-password' => [
'class' => 'hipanel\actions\SwitchAction',
'success' => Yii::t('app', 'Password changing task has been successfully added to queue'),
'error' => Yii::t('app', 'An error occurred when trying to change password'),
'POST html | POST pjax' => [
'save' => true,
'success' => [
'class' => 'hipanel\actions\RedirectAction',
'url' => function ($action, $model) {
return ['view', 'id' => $model->id];
}
]
],
],
'set-allowed-ips' => [
'class' => 'hipanel\actions\SwitchAction',
'success' => Yii::t('app', 'Allowed IPs changing task has been successfully added to queue'),
'error' => Yii::t('app', 'An error occurred when trying to change allowed IPs'),
'POST html | POST pjax' => [
'save' => true,
'success' => [
'class' => 'hipanel\actions\RedirectAction',
'url' => function ($action, $model) {
return ['view', 'id' => $model->id];
}
]
],
],
];
}
}
20 changes: 20 additions & 0 deletions grid/AccountGridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

namespace hipanel\modules\hosting\grid;

use hipanel\grid\ActionColumn;
use hipanel\grid\MainColumn;
use hipanel\grid\RefColumn;
use hipanel\modules\hosting\widgets\account\State;
use hipanel\modules\server\grid\ServerColumn;

class AccountGridView extends \hipanel\grid\BoxedGridView
{
Expand All @@ -19,6 +23,22 @@ static public function defaultColumns()
'attribute' => 'login',
'filterAttribute' => 'account_like',
],
'state' => [
'class' => RefColumn::className(),
'format' => 'raw',
'value' => function ($model) {
return State::widget(compact('model'));
},
'gtype' => 'state,db',
],
'server' => [
'class' => ServerColumn::className()
],
'actions' => [
'class' => ActionColumn::className(),
'template' => '{view} {update} {delete}'
],

];
}
}
33 changes: 24 additions & 9 deletions models/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class Account extends \hipanel\base\Model

use \hipanel\base\ModelTrait;

public function init()
{
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']);
}

public function rules()
{
return [
Expand All @@ -36,7 +41,7 @@ public function rules()
'type',
],
'safe',
'on' => ['insert-user', 'insert-ftponly']
'on' => ['create-user', 'create-ftponly']
],
[
[
Expand All @@ -47,7 +52,7 @@ public function rules()
'type',
],
'required',
'on' => ['insert-user', 'insert-ftponly']
'on' => ['create-user', 'create-ftponly']
],
[
['password'],
Expand All @@ -60,31 +65,31 @@ public function rules()
'compareAttribute' => 'login',
'message' => Yii::t('app', 'Password must not be equal to login'),
'operator' => '!=',
'on' => ['insert-user', 'insert-ftponly', 'update', 'set-password'],
'on' => ['create-user', 'create-ftponly', 'update', 'set-password'],
],
[
'login',
LoginValidator::className(),
'on' => ['insert-user', 'insert-ftponly', 'set-password']
'on' => ['create-user', 'create-ftponly', 'set-password']
],
[
'login',
'in',
'range' => ['root', 'toor'],
'not' => true,
'on' => ['insert-user', 'insert-ftponly'],
'on' => ['create-user', 'create-ftponly'],
'message' => Yii::t('app', 'You can not use this login')
],
[
'sshftp_ips',
'filter',
'filter' => function ($value) { return ArrayHelper::csplit($value); },
'on' => ['insert-user', 'insert-ftponly', 'update']
'on' => ['create-user', 'create-ftponly', 'update']
],
[
'sshftp_ips',
IpAddressValidator::className(),
'on' => ['insert-user', 'insert-ftponly', 'update', 'set-allowed-ips'],
'on' => ['create-user', 'create-ftponly', 'update', 'set-allowed-ips'],
'exclusion' => true
]
];
Expand Down Expand Up @@ -137,8 +142,18 @@ public function scenarioCommands()
{
return [
'set-allowed-ips' => [null, 'SetAllowedIPs'],
'insert-user' => 'create',
'insert-ftponly' => 'create',
'create-user' => 'create',
'create-ftponly' => 'create',
];
}

public function onBeforeInsert()
{
if ($this->scenario == 'create-user') {
$this->type = 'user';
} elseif ($this->scenario == 'create-ftponly') {
$this->type = 'ftponly';
}
return true;
}
}
73 changes: 73 additions & 0 deletions views/account/_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/* @var $this View */
/* @var $model hipanel\modules\hosting\models\Account */
/* @var $type string */

use hipanel\base\View;
use hipanel\modules\client\widgets\combo\ClientCombo;
use hipanel\modules\hosting\widgets\combo\AccountCombo;
use hipanel\modules\hosting\widgets\combo\DbServiceCombo;
use hipanel\modules\server\widgets\combo\ServerCombo;
use hipanel\widgets\PasswordInput;
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\helpers\Url;


$type2action = [
'user' => 'create',
'ftponly' => 'create-ftp'
];
?>
<div class="row">
<div class="col-md-4">
<div class="box box-danger">
<div class="box-body">
<div class="ticket-form" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<?php
if ($model->isNewRecord) {
$action = Url::to(in_array($type, $model->getKnownTypes()) ? $type2action[$type] : 'create');
} else {
$action = Url::toRoute(['update', 'id' => $model->id]);
}

$form = ActiveForm::begin(['action' => $action]);
?>
<!-- Properties -->

<?php
print $form->field($model, 'client')->widget(ClientCombo::className());
print $form->field($model, 'server')->widget(ServerCombo::className());

print $form->field($model, 'login');
print $form->field($model, 'password')->widget(PasswordInput::className());

print $form->field($model, 'sshftp_ips')
->hint(Yii::t('app', 'Access to the account is opened by default. Please input the IPs, for which the access to the server will be granted'))
->input('text', [
'data' => [
'title' => Yii::t('app', 'IP restrictions'),
'content' => Yii::t('app', 'Text about IP restrictions'),
]
]);
?>

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

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

<!-- ticket-_form -->
</div>
</div>

<?php

$this->registerJs("
$('#account-sshftp_ips').popover({placement: 'top', trigger: 'focus'});
");
13 changes: 13 additions & 0 deletions views/account/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/* @var $this yii\web\View */
/* @var $model hipanel\modules\ticket\models\Thread */
/* @var $type string */

$this->title = Yii::t('app', 'Create account');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Accounts'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="account-create">
<?= $this->render('_form', compact('model')) ?>
</div>
Loading

0 comments on commit fdf94e1

Please sign in to comment.