| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Server module for HiPanel |
||||
| 4 | * |
||||
| 5 | * @link https://github.com/hiqdev/hipanel-module-server |
||||
| 6 | * @package hipanel-module-server |
||||
| 7 | * @license BSD-3-Clause |
||||
| 8 | * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
||||
| 9 | */ |
||||
| 10 | |||||
| 11 | namespace hipanel\modules\server\controllers; |
||||
| 12 | |||||
| 13 | use hipanel\actions\Action; |
||||
| 14 | use hipanel\actions\IndexAction; |
||||
| 15 | use hipanel\actions\SmartCreateAction; |
||||
| 16 | use hipanel\actions\SmartDeleteAction; |
||||
| 17 | use hipanel\actions\SmartUpdateAction; |
||||
| 18 | use hipanel\actions\RedirectAction; |
||||
| 19 | use hipanel\actions\ValidateFormAction; |
||||
| 20 | use hipanel\actions\ViewAction; |
||||
| 21 | use hipanel\base\CrudController; |
||||
| 22 | use hipanel\filters\EasyAccessControl; |
||||
| 23 | use hipanel\helpers\ArrayHelper; |
||||
| 24 | use hipanel\models\Ref; |
||||
| 25 | use hipanel\modules\server\models\MonitoringSettings; |
||||
| 26 | use hipanel\modules\server\forms\AssignSwitchesForm; |
||||
| 27 | use hipanel\modules\server\forms\HubSellForm; |
||||
| 28 | use hiqdev\hiart\Collection; |
||||
| 29 | use Yii; |
||||
| 30 | use yii\base\Event; |
||||
| 31 | use yii\web\NotFoundHttpException; |
||||
| 32 | |||||
| 33 | class HubController extends CrudController |
||||
| 34 | { |
||||
| 35 | public function behaviors() |
||||
| 36 | { |
||||
| 37 | return array_merge(parent::behaviors(), [ |
||||
| 38 | [ |
||||
| 39 | 'class' => EasyAccessControl::class, |
||||
| 40 | 'actions' => [ |
||||
| 41 | 'create' => 'hub.create', |
||||
| 42 | 'update,options' => 'hub.update', |
||||
| 43 | 'monitoring-settings' => 'server.manage-settings', |
||||
| 44 | '*' => 'hub.read', |
||||
| 45 | ], |
||||
| 46 | ], |
||||
| 47 | ]); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | public function actions() |
||||
| 51 | { |
||||
| 52 | return array_merge(parent::actions(), [ |
||||
| 53 | 'index' => [ |
||||
| 54 | 'class' => IndexAction::class, |
||||
| 55 | 'data' => function () { |
||||
| 56 | return [ |
||||
| 57 | 'types' => $this->getTypes(), |
||||
| 58 | ]; |
||||
| 59 | }, |
||||
| 60 | ], |
||||
| 61 | 'view' => [ |
||||
| 62 | 'on beforePerform' => function (Event $event) { |
||||
| 63 | /** @var \hipanel\actions\SearchAction $action */ |
||||
| 64 | $action = $event->sender; |
||||
| 65 | $dataProvider = $action->getDataProvider(); |
||||
| 66 | $dataProvider->query->joinWith([ |
||||
| 67 | 'bindings', |
||||
| 68 | 'hardwareSettings', |
||||
| 69 | ]); |
||||
| 70 | $dataProvider->query |
||||
| 71 | ->andWhere([ |
||||
| 72 | 'with_bindings' => 1, |
||||
| 73 | 'with_servers' => 1, |
||||
| 74 | 'with_hardwareSettings' => 1, |
||||
| 75 | ]); |
||||
| 76 | }, |
||||
| 77 | 'class' => ViewAction::class, |
||||
| 78 | 'data' => function () { |
||||
| 79 | return [ |
||||
| 80 | 'snmpOptions' => $this->getSnmpOptions(), |
||||
| 81 | 'digitalCapacityOptions' => $this->getDigitalCapacityOptions(), |
||||
| 82 | 'nicMediaOptions' => $this->getNicMediaOptions(), |
||||
| 83 | ]; |
||||
| 84 | }, |
||||
| 85 | ], |
||||
| 86 | 'validate-form' => [ |
||||
| 87 | 'class' => ValidateFormAction::class, |
||||
| 88 | ], |
||||
| 89 | 'create' => [ |
||||
| 90 | 'class' => SmartCreateAction::class, |
||||
| 91 | 'success' => Yii::t('hipanel:server:hub', 'Switch was created'), |
||||
| 92 | 'data' => function () { |
||||
| 93 | return [ |
||||
| 94 | 'types' => $this->getTypes(), |
||||
| 95 | ]; |
||||
| 96 | }, |
||||
| 97 | ], |
||||
| 98 | 'update' => [ |
||||
| 99 | 'class' => SmartUpdateAction::class, |
||||
| 100 | 'success' => Yii::t('hipanel:server:hub', 'Switch was updated'), |
||||
| 101 | 'data' => function () { |
||||
| 102 | return [ |
||||
| 103 | 'types' => $this->getTypes(), |
||||
| 104 | ]; |
||||
| 105 | }, |
||||
| 106 | ], |
||||
| 107 | 'options' => [ |
||||
| 108 | 'class' => SmartUpdateAction::class, |
||||
| 109 | 'success' => Yii::t('hipanel:server:hub', 'Options was updated'), |
||||
| 110 | 'data' => function () { |
||||
| 111 | return [ |
||||
| 112 | 'snmpOptions' => $this->getSnmpOptions(), |
||||
| 113 | 'digitalCapacityOptions' => $this->getDigitalCapacityOptions(), |
||||
| 114 | 'nicMediaOptions' => $this->getNicMediaOptions(), |
||||
| 115 | ]; |
||||
| 116 | }, |
||||
| 117 | ], |
||||
| 118 | 'sell' => [ |
||||
| 119 | 'class' => SmartUpdateAction::class, |
||||
| 120 | 'success' => Yii::t('hipanel:server:hub', 'Switches were sold'), |
||||
| 121 | 'view' => 'modal/_bulkSale', |
||||
| 122 | 'collection' => [ |
||||
| 123 | 'class' => Collection::class, |
||||
| 124 | 'model' => new HubSellForm(), |
||||
| 125 | 'scenario' => 'sell', |
||||
| 126 | ], |
||||
| 127 | 'data' => function (Action $action, array $data) { |
||||
| 128 | $result = []; |
||||
| 129 | foreach ($data['models'] as $model) { |
||||
| 130 | $result['models'][] = HubSellForm::fromHub($model); |
||||
| 131 | } |
||||
| 132 | $result['model'] = reset($result['models']); |
||||
| 133 | |||||
| 134 | return $result; |
||||
| 135 | }, |
||||
| 136 | 'on beforeSave' => function (Event $event) { |
||||
| 137 | /** @var \hipanel\actions\Action $action */ |
||||
| 138 | $action = $event->sender; |
||||
| 139 | $request = Yii::$app->request; |
||||
|
0 ignored issues
–
show
|
|||||
| 140 | |||||
| 141 | if ($request->isPost) { |
||||
| 142 | $values = []; |
||||
| 143 | foreach (['client_id', 'tariff_id', 'sale_time'] as $attribute) { |
||||
| 144 | $value = $request->post($attribute); |
||||
| 145 | if (!empty($value)) { |
||||
| 146 | $values[$attribute] = $value; |
||||
| 147 | } |
||||
| 148 | } |
||||
| 149 | foreach ($action->collection->models as $model) { |
||||
| 150 | foreach ($values as $attr => $value) { |
||||
| 151 | $model->setAttribute($attr, $value); |
||||
| 152 | } |
||||
| 153 | } |
||||
| 154 | } |
||||
| 155 | }, |
||||
| 156 | ], |
||||
| 157 | 'assign-switches' => [ |
||||
| 158 | 'class' => SmartUpdateAction::class, |
||||
| 159 | 'success' => Yii::t('hipanel:server:hub', 'Switches have been edited'), |
||||
| 160 | 'view' => 'assign-switches', |
||||
| 161 | 'on beforeFetch' => function (Event $event) { |
||||
| 162 | /** @var \hipanel\actions\SearchAction $action */ |
||||
| 163 | $action = $event->sender; |
||||
| 164 | $dataProvider = $action->getDataProvider(); |
||||
| 165 | $dataProvider->query->withBindings()->select(['*']); |
||||
| 166 | }, |
||||
| 167 | 'collection' => [ |
||||
| 168 | 'class' => Collection::class, |
||||
| 169 | 'model' => new AssignSwitchesForm(), |
||||
| 170 | 'scenario' => 'default', |
||||
| 171 | ], |
||||
| 172 | 'data' => function (Action $action, array $data) { |
||||
| 173 | $result = []; |
||||
| 174 | foreach ($data['models'] as $model) { |
||||
| 175 | $result['models'][] = AssignSwitchesForm::fromOriginalModel($model); |
||||
| 176 | } |
||||
| 177 | if (!$result['models']) { |
||||
| 178 | throw new NotFoundHttpException('There are no entries available for the selected operation.'); |
||||
| 179 | } |
||||
| 180 | $result['model'] = reset($result['models']); |
||||
| 181 | |||||
| 182 | return $result; |
||||
| 183 | }, |
||||
| 184 | ], |
||||
| 185 | 'delete' => [ |
||||
| 186 | 'class' => SmartDeleteAction::class, |
||||
| 187 | 'success' => Yii::t('hipanel:server:hub', 'Switches have been deleted'), |
||||
| 188 | ], |
||||
| 189 | 'validate-switches-form' => [ |
||||
| 190 | 'class' => ValidateFormAction::class, |
||||
| 191 | 'collection' => [ |
||||
| 192 | 'class' => Collection::class, |
||||
| 193 | 'model' => new AssignSwitchesForm(), |
||||
| 194 | ], |
||||
| 195 | ], |
||||
| 196 | 'validate-sell-form' => [ |
||||
| 197 | 'class' => ValidateFormAction::class, |
||||
| 198 | 'collection' => [ |
||||
| 199 | 'class' => Collection::class, |
||||
| 200 | 'model' => new HubSellForm(), |
||||
| 201 | ], |
||||
| 202 | ], |
||||
| 203 | 'monitoring-settings' => [ |
||||
| 204 | 'class' => SmartUpdateAction::class, |
||||
| 205 | 'success' => Yii::t('hipanel:server', 'Monitoring properties was changed'), |
||||
| 206 | 'view' => 'modal/monitoringSettings', |
||||
| 207 | 'scenario' => 'default', |
||||
| 208 | 'on beforeFetch' => function (Event $event) { |
||||
| 209 | /** @var \hipanel\actions\SearchAction $action */ |
||||
| 210 | $action = $event->sender; |
||||
| 211 | $query = $action->getDataProvider()->query; |
||||
| 212 | $query->withMonitoringSettings()->select(['*']); |
||||
| 213 | }, |
||||
| 214 | 'on beforeLoad' => function (Event $event) { |
||||
| 215 | /** @var Action $action */ |
||||
| 216 | $action = $event->sender; |
||||
| 217 | |||||
| 218 | $action->collection->setModel(MonitoringSettings::class); |
||||
|
0 ignored issues
–
show
hipanel\modules\server\m...nitoringSettings::class of type string is incompatible with the type array|hiqdev\hiart\ActiveRecord expected by parameter $model of hiqdev\hiart\Collection::setModel().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 219 | }, |
||||
| 220 | 'data' => function ($action) { |
||||
| 221 | return [ |
||||
| 222 | 'nicMediaOptions' => $action->controller->getFullFromRef('type,nic_media'), |
||||
| 223 | ]; |
||||
| 224 | }, |
||||
| 225 | 'POST html' => [ |
||||
| 226 | 'save' => true, |
||||
| 227 | 'success' => [ |
||||
| 228 | 'class' => RedirectAction::class, |
||||
| 229 | 'url' => function () { |
||||
| 230 | $hub = Yii::$app->request->post('MonitoringSettings'); |
||||
| 231 | |||||
| 232 | return ['@hub/view', 'id' => $hub['id']]; |
||||
| 233 | }, |
||||
| 234 | ], |
||||
| 235 | ], |
||||
| 236 | ], |
||||
| 237 | ]); |
||||
| 238 | } |
||||
| 239 | |||||
| 240 | protected function getTypes() |
||||
| 241 | { |
||||
| 242 | return $this->getFullFromRef('type,device,switch'); |
||||
| 243 | } |
||||
| 244 | |||||
| 245 | protected function getSnmpOptions() |
||||
| 246 | { |
||||
| 247 | return $this->getFullFromRef('type,snmp_version'); |
||||
| 248 | } |
||||
| 249 | |||||
| 250 | protected function getDigitalCapacityOptions() |
||||
| 251 | { |
||||
| 252 | return $this->getFullFromRef('type,digit_capacity'); |
||||
| 253 | } |
||||
| 254 | |||||
| 255 | protected function getNicMediaOptions() |
||||
| 256 | { |
||||
| 257 | return $this->getFullFromRef('type,nic_media'); |
||||
| 258 | } |
||||
| 259 | |||||
| 260 | protected function getFullFromRef($gtype) |
||||
| 261 | { |
||||
| 262 | $callingMethod = debug_backtrace()[1]['function']; |
||||
| 263 | $result = Yii::$app->get('cache')->getOrSet([$callingMethod], function () use ($gtype) { |
||||
| 264 | $result = ArrayHelper::map(Ref::find()->where([ |
||||
| 265 | 'gtype' => $gtype, |
||||
| 266 | 'select' => 'full', |
||||
| 267 | ])->all(), 'id', function ($model) { |
||||
| 268 | return Yii::t('hipanel:server:hub', $model->label); |
||||
| 269 | }); |
||||
| 270 | |||||
| 271 | return $result; |
||||
| 272 | }, 86400 * 24); // 24 days |
||||
| 273 | |||||
| 274 | return $result; |
||||
| 275 | } |
||||
| 276 | } |
||||
| 277 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.