diff --git a/src/controllers/AccountController.php b/src/controllers/AccountController.php index c33b72f6..ee3b4571 100644 --- a/src/controllers/AccountController.php +++ b/src/controllers/AccountController.php @@ -17,8 +17,8 @@ public function actions() { return [ 'index' => [ - 'class' => 'hipanel\actions\IndexAction', - 'data' => function ($action) { + 'class' => 'hipanel\actions\IndexAction', + 'data' => function ($action) { return [ 'stateData' => $action->controller->getStateData(), 'typeData' => $action->controller->getTypeData(), @@ -26,39 +26,47 @@ public function actions() } ], 'view' => [ - 'class' => 'hipanel\actions\ViewAction' + 'class' => 'hipanel\actions\ViewAction', + 'findOptions' => [ + 'with_mail_settings' => true + ] ], - 'create' => [ - 'class' => 'hipanel\actions\SmartCreateAction', + 'create' => [ + 'class' => 'hipanel\actions\SmartCreateAction', 'success' => Yii::t('app', 'Account creating task has been added to queue'), - 'error' => Yii::t('app', 'An error occurred when trying to create account') + 'error' => Yii::t('app', 'An error occurred when trying to create account') ], - 'create-ftponly' => [ - 'class' => 'hipanel\actions\SmartCreateAction', + 'create-ftponly' => [ + 'class' => 'hipanel\actions\SmartCreateAction', 'success' => Yii::t('app', 'Account creating task has been added to queue'), - 'error' => Yii::t('app', 'An error occurred when trying to create account') + 'error' => Yii::t('app', 'An error occurred when trying to create account') ], - 'set-password' => [ - 'class' => 'hipanel\actions\SmartUpdateAction', + 'set-password' => [ + 'class' => 'hipanel\actions\SmartUpdateAction', 'success' => Yii::t('app', 'Password changed'), - 'error' => Yii::t('app', 'Failed to change password'), + 'error' => Yii::t('app', 'Failed to change password'), ], 'set-allowed-ips' => [ - 'class' => 'hipanel\actions\SmartUpdateAction', + 'class' => 'hipanel\actions\SmartUpdateAction', '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'), + 'error' => Yii::t('app', 'An error occurred when trying to change allowed IPs'), ], - 'validate-form' => [ + 'set-mail-settings' => [ + 'class' => 'hipanel\actions\SmartUpdateAction', + 'success' => Yii::t('app', 'Mail settings where changed'), + 'error' => Yii::t('app', 'An error occurred when trying to change mail settings'), + ], + 'validate-form' => [ 'class' => 'hipanel\actions\ValidateFormAction', ], - 'delete' => [ - 'class' => 'hipanel\actions\SmartDeleteAction', + 'delete' => [ + 'class' => 'hipanel\actions\SmartDeleteAction', 'success' => Yii::t('app', 'Account deleting task has been added to queue'), - 'error' => Yii::t('app', 'An error occurred when trying to delete account') + 'error' => Yii::t('app', 'An error occurred when trying to delete account') ], 'get-directories-list' => [ - 'class' => 'hipanel\actions\SearchAction', - 'findOptions' => ['with_directories' => true], + 'class' => 'hipanel\actions\SearchAction', + 'findOptions' => ['with_directories' => true], 'ajaxResponseFormatter' => function ($action) { $results = []; diff --git a/src/models/Account.php b/src/models/Account.php index 024667d2..5f98777d 100644 --- a/src/models/Account.php +++ b/src/models/Account.php @@ -14,59 +14,71 @@ class Account extends \hipanel\base\Model { - use \hipanel\base\ModelTrait; + const TYPE_SSH = 'user'; + const TYPE_FTP = 'ftponly'; + public function init() { $this->on(static::EVENT_BEFORE_VALIDATE, [$this, 'onBeforeValidate']); + $this->on(static::EVENT_AFTER_FIND, [$this, 'onAfterFind']); } public function rules() { return [ - [['id', 'client_id', 'device_id', 'server_id', 'seller_id', 'uid', 'gid'], 'integer'], + [['id', 'client_id', 'device_id', 'server_id', 'seller_id', 'uid', 'gid'], 'integer'], [ ['login', 'password', 'shell', 'client', 'path', 'home', 'device', 'server', 'seller'], 'safe' ], - [['type', 'type_label', 'state', 'state_label'], 'safe'], - [['ip', 'allowed_ips', 'objects_count', 'request_state', 'request_state_label', 'mail_settings'], 'safe'], - [['login', 'server', 'password', 'sshftp_ips', 'type'], 'safe', 'on' => ['create', 'create-ftponly']], - [['login', 'server', 'password', 'type'], 'required', 'on' => ['create', 'create-ftponly']], - [['account', 'path'], 'required', 'on' => ['create-ftponly']], - [['login'], 'required', 'on' => ['set-password']], - [['password'], 'required', 'on' => ['set-password']], - [['password'], + [['type', 'type_label', 'state', 'state_label'], 'safe'], + [['ip', 'allowed_ips', 'objects_count', 'request_state', 'request_state_label', 'mail_settings', 'per_hour_limit'], 'safe'], + [['login', 'server', 'password', 'sshftp_ips', 'type'], 'safe', 'on' => ['create', 'create-ftponly']], + [['login', 'server', 'password', 'type'], 'required', 'on' => ['create', 'create-ftponly']], + [['account', 'path'], 'required', 'on' => ['create-ftponly']], + [['login'], 'required', 'on' => ['set-password']], + [['password'], 'required', 'on' => ['set-password']], + [ + ['password'], 'compare', 'compareAttribute' => 'login', - 'message' => Yii::t('app', 'Password must not be equal to login'), - 'operator' => '!=', - 'on' => ['create', 'create-ftponly', 'update', 'set-password'], + 'message' => Yii::t('app', 'Password must not be equal to login'), + 'operator' => '!=', + 'on' => ['create', 'create-ftponly', 'update', 'set-password'], ], - [['login'], LoginValidator::className(), 'on' => ['create', 'create-ftponly', 'set-password']], + [['login'], LoginValidator::className(), 'on' => ['create', 'create-ftponly', 'set-password']], [ ['login'], 'in', - 'range' => ['root', 'toor'], - 'not' => true, - 'on' => ['create', 'create-ftponly'], + 'range' => ['root', 'toor'], + 'not' => true, + 'on' => ['create', 'create-ftponly'], 'message' => Yii::t('app', 'You can not use this login') ], [ ['sshftp_ips'], 'filter', - 'filter' => function ($value) { return StringHelper::explode($value); }, - 'on' => ['create', 'create-ftponly', 'update', 'set-allowed-ips'] + 'filter' => function ($value) { + return StringHelper::explode($value); + }, + 'on' => ['create', 'create-ftponly', 'update', 'set-allowed-ips'] ], [ ['sshftp_ips'], 'each', 'rule' => [IpValidator::className(), 'negationChar' => true, 'subnet' => null], - 'on' => ['create', 'create-ftponly', 'update', 'set-allowed-ips'] + 'on' => ['create', 'create-ftponly', 'update', 'set-allowed-ips'] + ], + [ + ['id'], + 'required', + 'on' => ['set-password', 'set-allowed-ips', 'set-mail-settings', 'delete'] ], - [['id'], 'required', 'on' => ['set-password', 'set-allowed-ips', 'delete']], - [['account', 'server'], 'required', 'on' => ['get-directories-list']], + [['id'], 'canSetMailSettings', 'on' => ['set-mail-settings']], + [['block_send'], 'boolean', 'on' => ['set-mail-settings']], + [['account', 'server'], 'required', 'on' => ['get-directories-list']], ]; } @@ -76,12 +88,14 @@ public function rules() public function attributeLabels() { return $this->mergeAttributeLabels([ - 'login_like' => Yii::t('app', 'Login'), - 'type_label' => Yii::t('app', 'state'), + 'login_like' => Yii::t('app', 'Login'), + 'type_label' => Yii::t('app', 'state'), 'state_label' => Yii::t('app', 'state'), 'allowed_ips' => Yii::t('app', 'Allowed IPs'), - 'sshftp_ips' => Yii::t('app', 'IP to access on the server via SSH or FTP'), - 'server_id' => Yii::t('app', 'Server'), + 'sshftp_ips' => Yii::t('app', 'IP to access on the server via SSH or FTP'), + 'server_id' => Yii::t('app', 'Server'), + 'block_send' => Yii::t('app', 'Block outgoing post'), + 'per_hour_limit' => Yii::t('app', 'Maximum letters per hour'), ]); } @@ -95,7 +109,6 @@ public function goodStates() */ public function isOperable() { - return false; /// TODO: all is operable for admin if (!in_array($this->state, $this->goodStates())) { return false; @@ -114,11 +127,15 @@ public function getKnownTypes() return ['user', 'ftponly']; } + public function canSetMailSettings() { + return $this->type === self::TYPE_SSH && Yii::$app->user->can('support'); + } + public function scenarioCommands() { return [ 'set-allowed-ips' => [null, 'SetAllowedIPs'], - 'create-ftponly' => 'create', + 'create-ftponly' => 'create', ]; } @@ -131,4 +148,12 @@ public function onBeforeValidate() } return true; } + + public function onAfterFind() { + if (!empty($this->mail_settings)) { + foreach ($this->mail_settings as $k => $v) { + $this->{$k} = $v; + } + } + } } diff --git a/src/views/account/set-mail-settings.php b/src/views/account/set-mail-settings.php new file mode 100644 index 00000000..eb0d12c0 --- /dev/null +++ b/src/views/account/set-mail-settings.php @@ -0,0 +1,69 @@ +title = $model->login; +$this->subtitle = Yii::t('app', 'account mail config') . ' #' . $model->id; +$this->breadcrumbs->setItems([ + ['label' => 'Accounts', 'url' => ['index']], + ['label' => $model->login, 'url' => ['view', 'id' => $model->id]], + Yii::t('app', 'Mail config') +]); + + +$form = ActiveForm::begin([ + 'id' => 'dynamic-form', + 'enableClientValidation' => true, + 'validateOnBlur' => true, + 'enableAjaxValidation' => true, + 'validationUrl' => Url::toRoute(['validate-form', 'scenario' => $model->scenario]), +]); ?> + +
+ $model) { ?> +
+
+
+
+
+ field($model, "[$i]id")->hiddenInput()->label(false) ?> + +
+ user->can('support')) { ?> +
+ field($model, "[$i]client")->widget(ClientCombo::className(), ['inputOptions' => ['readonly' => true]]) ?> +
+ +
+ field($model, "[$i]server")->widget(ServerCombo::className(), ['inputOptions' => ['readonly' => true]]) ?> +
+
+ field($model, "[$i]login")->widget(SshAccountCombo::className(), ['inputOptions' => ['readonly' => true]]) ?> +
+
+ field($model, 'per_hour_limit'); + echo $form->field($model, 'block_send')->checkbox(); + ?> +
+
+
+
+
+ +
+ 'btn btn-default']) ?> +   + 'btn btn-default', 'onclick' => 'history.go(-1)']) ?> + - form->field($model, 'password')->widget(PasswordInput::className())->label(false); + form->field($model, + 'password')->widget(PasswordInput::className())->label(false); echo Html::activeHiddenInput($model, 'login'); ModalButton::end(); @@ -105,19 +106,49 @@ ModalButton::end(); ?> + canSetMailSettings()) { ?> +
  • + $model, + 'scenario' => 'set-mail-settings', + 'button' => [ + 'label' => '' . Yii::t('app', 'Mail settings'), + 'disabled' => !$model->isOperable(), + ], + 'modal' => [ + 'header' => Html::tag('h4', Yii::t('app', 'Enter mail settings')), + 'headerOptions' => ['class' => 'label-info'], + 'footer' => [ + 'label' => Yii::t('app', 'Change'), + 'data-loading-text' => Yii::t('app', 'Changing...'), + 'class' => 'btn btn-info', + ] + ] + ]); + + echo $modalButton->form->field($model, 'per_hour_limit'); + echo $modalButton->form->field($model, 'block_send')->checkbox(); + + ModalButton::end(); + ?> +
  • +
  • $model, + 'model' => $model, 'scenario' => 'delete', - 'button' => ['label' => '' . Yii::t('app', 'Delete')], - 'body' => Yii::t('app', 'Are you sure you want to delete account {name}? You will loose all data, that relates this account!', ['name' => $model->login]), - 'modal' => [ - 'header' => Html::tag('h4', Yii::t('app', 'Confirm account deleting deleting')), + 'button' => ['label' => '' . Yii::t('app', 'Delete')], + 'body' => Yii::t('app', + 'Are you sure you want to delete account {name}? You will loose all data, that relates this account!', + ['name' => $model->login]), + 'modal' => [ + 'header' => Html::tag('h4', Yii::t('app', 'Confirm account deleting deleting')), 'headerOptions' => ['class' => 'label-danger'], - 'footer' => [ - 'label' => Yii::t('app', 'Delete'), + 'footer' => [ + 'label' => Yii::t('app', 'Delete'), 'data-loading-text' => Yii::t('app', 'Deleting...'), - 'class' => 'btn btn-danger', + 'class' => 'btn btn-danger', ] ] ]) ?> @@ -132,23 +163,24 @@
    false]); - $box->beginHeader(); - echo $box->renderTitle(Yii::t('app', 'Account information')); - $box->endHeader(); - $box->beginBody(); - echo AccountGridView::detailView([ - 'boxed' => false, - 'model' => $model, - 'columns' => [ - 'seller_id', - 'client_id', - ['attribute' => 'login'], - 'type', - 'state', - 'sshftp_ips' - ], - ]); - $box->endBody(); + $box->beginHeader(); + echo $box->renderTitle(Yii::t('app', 'Account information')); + $box->endHeader(); + $box->beginBody(); + echo AccountGridView::detailView([ + 'boxed' => false, + 'model' => $model, + 'columns' => [ + 'seller_id', + 'client_id', + ['attribute' => 'login'], + 'type', + 'state', + 'sshftp_ips', + 'per_hour_limit' + ], + ]); + $box->endBody(); $box->end(); ?>