Completed
Pull Request — master (#34)
by
unknown
03:22
created

Account::getValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Hosting Plugin for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-hosting
6
 * @package   hipanel-module-hosting
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\models;
12
13
use hipanel\helpers\StringHelper;
14
use hipanel\modules\hosting\models\query\AccountQuery;
15
use hipanel\modules\hosting\validators\LoginValidator;
16
use Yii;
17
18
/**
19
 * Class Account
20
 * @package hipanel\modules\hosting\models
21
 *
22
 * @property $values
23
 */
24
class Account extends \hipanel\base\Model
25
{
26
    use \hipanel\base\ModelTrait;
27
28
    public static $i18nDictionary = 'hipanel:hosting';
29
30
    const TYPE_SSH = 'user';
31
    const TYPE_FTP = 'ftponly';
32
33
    const STATE_OK = 'ok';
34
    const STATE_BLOCKED = 'blocked';
35
    const STATE_DELETED = 'deleted';
36
37
    public function init()
38
    {
39
        $this->on(static::EVENT_BEFORE_VALIDATE, [$this, 'onBeforeValidate']);
40
        $this->on(static::EVENT_AFTER_FIND, [$this, 'onAfterFind']);
41
    }
42
43
    public function rules()
44
    {
45
        return [
46
            [['id', 'client_id', 'device_id', 'server_id', 'seller_id'], 'integer'],
47
            [
48
                ['login', 'password', 'shell', 'client', 'path', 'home', 'device', 'server', 'seller', 'uid', 'gid'],
49
                'safe',
50
            ],
51
            [['type', 'type_label', 'state', 'state_label'], 'safe'],
52
            [['ip', 'allowed_ips', 'objects_count', 'request_state', 'request_state_label', 'mail_settings', 'per_hour_limit'], 'safe'],
53
            [['login', 'server', 'password', 'sshftp_ips', 'type'], 'safe', 'on' => ['create', 'create-ftponly']],
54
            [['login', 'server', 'password', 'type'], 'required', 'on' => ['create', 'create-ftponly']],
55
            [['account', 'path'], 'required', 'on' => ['create-ftponly']],
56
            [['login'], 'required', 'on' => ['change-password']],
57
            [['password'], 'required', 'on' => ['change-password']],
58
            [
59
                ['password'],
60
                'compare',
61
                'compareAttribute' => 'login',
62
                'message' => Yii::t('hipanel', 'Password must not be equal to login'),
63
                'operator' => '!=',
64
                'on' => ['create', 'create-ftponly', 'update', 'change-password'],
65
            ],
66
            [['login'], LoginValidator::class, 'on' => ['create', 'create-ftponly', 'change-password']],
67
            [
68
                ['login'],
69
                'in',
70
                'range' => ['root', 'toor'],
71
                'not' => true,
72
                'on' => ['create', 'create-ftponly'],
73
                'message' => Yii::t('hipanel:hosting', 'You can not use this login'),
74
            ],
75
            [
76
                ['sshftp_ips'],
77
                'filter',
78
                'filter' => function ($value) {
79
                    return StringHelper::explode($value);
80
                },
81
                'on' => ['create', 'create-ftponly', 'update', 'set-allowed-ips'],
82
            ],
83
            [
84
                ['sshftp_ips'],
85
                'each',
86
                'rule' => ['ip', 'negation' => true, 'subnet' => null],
87
                'on' => ['create', 'create-ftponly', 'update', 'set-allowed-ips'],
88
            ],
89
            [
90
                ['id'],
91
                'required',
92
                'on' => ['change-password', 'set-allowed-ips', 'set-mail-settings', 'set-system-settings', 'set-ghost-options', 'delete'],
93
            ],
94
            [['id'], 'canSetMailSettings', 'on' => ['set-mail-settings']],
95
            [['block_send'], 'boolean', 'on' => ['set-mail-settings']],
96
            [['home', 'gid', 'uid'], 'safe', 'on' => ['set-system-settings']],
97
            [['account', 'server'], 'required', 'on' => ['get-directories-list']],
98
            [['type', 'comment'], 'required', 'on' => ['enable-block']],
99
            [['comment'], 'safe', 'on' => ['disable-block']],
100
        ];
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function attributeLabels()
107
    {
108
        return $this->mergeAttributeLabels([
109
            'allowed_ips'    => Yii::t('hipanel:hosting', 'Allowed IPs'),
110
            'sshftp_ips'     => Yii::t('hipanel:hosting', 'IP to access on the server via SSH or FTP'),
111
            'block_send'     => Yii::t('hipanel:hosting', 'Block outgoing post'),
112
            'per_hour_limit' => Yii::t('hipanel:hosting', 'Maximum letters per hour'),
113
            'path'           => Yii::t('hipanel:hosting:account', 'Home directory'),
114
        ]);
115
    }
116
117
    public function goodStates()
118
    {
119
        return [self::STATE_OK];
120
    }
121
122
    public function getValues()
123
    {
124
        return $this->hasOne(AccountValues::class, ['obj_id' => 'obj_id']);
125
    }
126
127
    /**
128
     * @return bool
129
     */
130
    public function isOperable()
131
    {
132
        /// TODO: all is operable for admin
133
        if (!in_array($this->state, $this->goodStates(), true)) {
0 ignored issues
show
Documentation introduced by
The property state does not exist on object<hipanel\modules\hosting\models\Account>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
134
            return false;
135
        }
136
137
        return true;
138
    }
139
140
    public function getSshFtpIpsList()
141
    {
142
        return implode(', ', empty($this->sshftp_ips) ? ['0.0.0.0/0'] : $this->sshftp_ips);
0 ignored issues
show
Documentation introduced by
The property sshftp_ips does not exist on object<hipanel\modules\hosting\models\Account>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
143
    }
144
145
    public function getKnownTypes()
146
    {
147
        return [static::TYPE_FTP, static::TYPE_SSH];
148
    }
149
150
    public function getIsBlocked()
151
    {
152
        return $this->state === self::STATE_BLOCKED;
0 ignored issues
show
Documentation introduced by
The property state does not exist on object<hipanel\modules\hosting\models\Account>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
    }
154
155
    public function canSetMailSettings()
156
    {
157
        return $this->type === self::TYPE_SSH && Yii::$app->user->can('support');
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\hosting\models\Account>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
158
    }
159
160
    public function scenarioActions()
161
    {
162
        return [
163
            'set-allowed-ips' => 'set-allowed-IPs',
164
            'create-ftponly' => 'create',
165
        ];
166
    }
167
168
    public function onBeforeValidate()
169
    {
170
        if ($this->scenario === 'create') {
171
            $this->type = static::TYPE_SSH;
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\hosting\models\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
172
        } elseif ($this->scenario === 'create-ftponly') {
173
            $this->type = static::TYPE_FTP;
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\hosting\models\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
174
        }
175
176
        return true;
177
    }
178
179
    public function onAfterFind()
180
    {
181
        if (!empty($this->mail_settings)) {
0 ignored issues
show
Documentation introduced by
The property mail_settings does not exist on object<hipanel\modules\hosting\models\Account>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
182
            foreach ($this->mail_settings as $k => $v) {
0 ignored issues
show
Documentation introduced by
The property mail_settings does not exist on object<hipanel\modules\hosting\models\Account>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
183
                $this->{$k} = $v;
184
            }
185
        }
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     * @return AccountQuery
191
     */
192
    public static function find(array $options = []): AccountQuery
193
    {
194
        return new AccountQuery(get_called_class(), [
195
            'options' => $options,
196
        ]);
197
    }
198
}
199