Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a bit of cleanup, a bit of improvement #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions db/mysql/FileMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class FileMigration extends Migration
{

public $file = null;
public $file;
public $mysqlExecutable = 'mysql';

public function init()
Expand All @@ -42,18 +42,17 @@ public function init()
}
}

/**
* @return bool
*/
public function up()
{
preg_match('/host=([^;]*)/', $this->db->dsn, $hostMatches);
$hostName = $hostMatches[1];
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);
Expand All @@ -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";
Expand Down
43 changes: 30 additions & 13 deletions db/traits/ActiveRecordAccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ trait ActiveRecordAccessTrait

/**
* @return array with access field names
* @throws \yii\base\InvalidConfigException
*/
public static function accessColumnAttributes()
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -139,9 +140,9 @@ public function beforeSave($insert)
}

return true;
} else {
return false;
}

return false;
}

/**
Expand All @@ -165,9 +166,9 @@ public function beforeDelete()
}

return true;
} else {
return false;
}

return false;
}

/**
Expand All @@ -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;
Expand All @@ -221,6 +222,9 @@ public static function getUsersAuthItems()
}


/**
* @return mixed|string
*/
public static function getDefaultAccessDomain() {
// return first found permission
$AuthManager = \Yii::$app->authManager;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -304,6 +311,7 @@ public function authItemStringToArray($action)
* @param null $action
*
* @return bool
* @throws \yii\base\InvalidConfigException
*/
public function hasPermission($action)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
29 changes: 22 additions & 7 deletions widgets/AccessInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,46 @@


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'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not in use currently, but actually the idea was that the widget is configurable regarding which fields are shown.


public $fieldOwner = 'access_owner';
public $fieldDomain = 'access_domain';
public $fieldRead = 'access_read';
public $fieldUpdate = 'access_update';
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)
Expand All @@ -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 ...')],
Expand All @@ -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
Expand Down