From ff9ea490f2d72cc5d00395e303730b9055d0ed63 Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Tue, 26 Mar 2019 10:31:48 +0100 Subject: [PATCH 1/2] a bit of cleanup, a bit of improvement --- db/mysql/FileMigration.php | 21 ++++++------- db/traits/ActiveRecordAccessTrait.php | 43 +++++++++++++++++++-------- widgets/AccessInput.php | 29 +++++++++++++----- 3 files changed, 61 insertions(+), 32 deletions(-) diff --git a/db/mysql/FileMigration.php b/db/mysql/FileMigration.php index 40f2ad0..f6c140d 100755 --- a/db/mysql/FileMigration.php +++ b/db/mysql/FileMigration.php @@ -22,7 +22,7 @@ class FileMigration extends Migration { - public $file = null; + public $file; public $mysqlExecutable = 'mysql'; public function init() @@ -42,6 +42,9 @@ public function init() } } + /** + * @return bool + */ public function up() { preg_match('/host=([^;]*)/', $this->db->dsn, $hostMatches); @@ -49,11 +52,7 @@ public function up() preg_match('/dbname=([^;]*)/', $this->db->dsn, $databaseMatches); $databaseName = $databaseMatches[1]; preg_match('/port=([^;]*)/', $this->db->dsn, $portMatches); - if (isset($portMatches[1])) { - $port = $portMatches[1]; - } else { - $port = "3306"; - } + $port = $portMatches[1] ?? '3306'; $command = new Command($this->mysqlExecutable); $command->addArg('-h', $hostName); @@ -65,14 +64,12 @@ public function up() #echo " ".$cmd . "\n"; // TODO echo only with --verbose exec($cmd, $output, $return); - if ($return !== 0) { - //var_dump($output, $return); - return false; - } else { - return true; - } + return !$return === 0; } + /** + * @return bool + */ public function down() { echo $this::className() . " cannot be reverted.\n"; diff --git a/db/traits/ActiveRecordAccessTrait.php b/db/traits/ActiveRecordAccessTrait.php index 475e182..56e573e 100755 --- a/db/traits/ActiveRecordAccessTrait.php +++ b/db/traits/ActiveRecordAccessTrait.php @@ -43,6 +43,7 @@ trait ActiveRecordAccessTrait /** * @return array with access field names + * @throws \yii\base\InvalidConfigException */ public static function accessColumnAttributes() { @@ -85,7 +86,7 @@ public static function find() // access read check if ($accessRead) { - $queryType = ($accessOwnerCheck) ? 'orWhere' : 'where'; + $queryType = $accessOwnerCheck ? 'orWhere' : 'where'; $authItems = implode(',', array_keys(self::getUsersAuthItems())); $checkInSetQuery = self::getInSetQueryPart($accessRead, $authItems); $query->$queryType($checkInSetQuery); @@ -123,7 +124,7 @@ public function beforeSave($insert) // skip check if model has no changes if (empty($this->getDirtyAttributes())) { - Yii::trace('Model has no changes, skipping permission check', __METHOD__); + Yii::debug('Model has no changes, skipping permission check', __METHOD__); return true; } @@ -139,9 +140,9 @@ public function beforeSave($insert) } return true; - } else { - return false; } + + return false; } /** @@ -165,9 +166,9 @@ public function beforeDelete() } return true; - } else { - return false; } + + return false; } /** @@ -194,7 +195,7 @@ public static function getUsersAuthItems() if (!$items) { - \Yii::trace("Get and check UsersAuthItems", __METHOD__); + \Yii::debug('Get and check UsersAuthItems', __METHOD__); // auth manager $authManager = \Yii::$app->authManager; @@ -221,6 +222,9 @@ public static function getUsersAuthItems() } + /** + * @return mixed|string + */ public static function getDefaultAccessDomain() { // return first found permission $AuthManager = \Yii::$app->authManager; @@ -273,10 +277,11 @@ public static function getDefaultAccessUpdateDelete() { * @param array $authItems * * @return string|null + * @throws \yii\base\InvalidConfigException */ public function authItemArrayToString($action, array $authItems) { - if (!in_array($action, self::accessColumnAttributes())) { + if (!in_array($action, self::accessColumnAttributes(),true)) { return null; } @@ -285,13 +290,15 @@ public function authItemArrayToString($action, array $authItems) /** * Encode access column by action from csv to array + * * @param $action * * @return array|null + * @throws \yii\base\InvalidConfigException */ public function authItemStringToArray($action) { - if (!in_array($action, self::accessColumnAttributes())) { + if (!in_array($action, self::accessColumnAttributes(),true)) { return null; } $arr = explode(',', $this->$action); @@ -304,6 +311,7 @@ public function authItemStringToArray($action) * @param null $action * * @return bool + * @throws \yii\base\InvalidConfigException */ public function hasPermission($action) { @@ -357,14 +365,17 @@ private function addAccessError($action, $attribute) \Yii::$app->session->addFlash('error', $msg); } $this->addError($attribute, $msg); - \Yii::info('User ID: #' . \Yii::$app->user->id . ' | ' . $msg, get_called_class()); + \Yii::info('User ID: #' . \Yii::$app->user->id . ' | ' . $msg, static::class); } - + /** * Return correct part of check in set query for current DB + * * @param $accessRead * @param $authItems + * * @return string + * @throws UnsupportedDbException */ private static function getInSetQueryPart($accessRead, $authItems) { @@ -379,10 +390,16 @@ private static function getInSetQueryPart($accessRead, $authItems) } } - // extract property from table name with schema + + /** + * Extract property from table name with schema + * @param $schemaProperty + * + * @return bool|string + */ private function getSchemaProperty($schemaProperty){ // extract property from table name with schema - if (strstr($schemaProperty, '.')) { + if (false !== strpos($schemaProperty, '.')) { $prop = substr($schemaProperty, strrpos($schemaProperty, '.') + 1); } else { $prop = $schemaProperty; diff --git a/widgets/AccessInput.php b/widgets/AccessInput.php index 1ff8d2b..3444131 100644 --- a/widgets/AccessInput.php +++ b/widgets/AccessInput.php @@ -10,19 +10,30 @@ use dmstr\db\traits\ActiveRecordAccessTrait; -use dmstr\modules\redirect\models\ActiveRecord; use kartik\select2\Select2; +use yii\base\Model; use yii\base\Widget; -use yii\widgets\InputWidget; use Yii; +use yii\widgets\ActiveForm; +/** + * @package dmstr\widgets + * + * @property ActiveForm $form + * @property Model $model + * + * @property string $fieldOwner + * @property string $fieldDomain + * @property string $fieldRead + * @property string $fieldUpdate + * @property string $fieldDelete + * @property string $fieldAppend + */ class AccessInput extends Widget { public $form; public $model; - public $accessFields = ['owner', 'domain', 'read', 'update', 'delete']; - public $fieldOwner = 'access_owner'; public $fieldDomain = 'access_domain'; public $fieldRead = 'access_read'; @@ -30,12 +41,15 @@ class AccessInput extends Widget public $fieldDelete = 'access_delete'; public $fieldAppend = 'access_append'; + /** + * @return string + */ public function run() { $return = ''; $userAuthItems = $this->model::getUsersAuthItems(); $userDomains = $this->optsAccessDomain(); - $disabled = !$this->model->hasPermission('access_update'); + $disabled = !$this->model->hasPermission($this->fieldUpdate); $return .= $this->form ->field($this->model, $this->fieldOwner) @@ -44,7 +58,7 @@ public function run() foreach (['domain', 'read', 'update', 'delete'] as $access) { $fieldName = 'field' . ucfirst($access); $return .= $this->form->field($this->model, $this->{$fieldName})->widget( - Select2::classname(), + Select2::class, [ 'data' => $access === 'domain' ? $userDomains : $userAuthItems, 'options' => ['placeholder' => Yii::t('pages', 'Select ...')], @@ -68,7 +82,8 @@ public function optsAccessDomain() if (Yii::$app->user->can('access.availableDomains:any')) { $availableLanguages[ActiveRecordAccessTrait::$_all] = 'GLOBAL'; foreach (\Yii::$app->urlManager->languages as $availablelanguage) { - $availableLanguages[mb_strtolower($availablelanguage)] = mb_strtolower($availablelanguage); + $lc_language = mb_strtolower($availablelanguage); + $availableLanguages[$lc_language] = $lc_language; } } else { // allow current value From 440e2aecae6c9fa239b98199528daae86542cf9d Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Tue, 26 Mar 2019 10:43:08 +0100 Subject: [PATCH 2/2] correct return value. code > 0 not good --- db/mysql/FileMigration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/mysql/FileMigration.php b/db/mysql/FileMigration.php index f6c140d..7144b82 100755 --- a/db/mysql/FileMigration.php +++ b/db/mysql/FileMigration.php @@ -64,7 +64,7 @@ public function up() #echo " ".$cmd . "\n"; // TODO echo only with --verbose exec($cmd, $output, $return); - return !$return === 0; + return $return === 0; } /**