Skip to content

Commit

Permalink
added data pools #236
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Mar 27, 2019
1 parent 92b5fb1 commit 9f0652c
Show file tree
Hide file tree
Showing 20 changed files with 229 additions and 100 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This release contains new migrations and requires to run the `migrate` command a

### Added

+ [#236](https://github.com/luyadev/luya-module-admin/issues/236) Added multiple menu entries and CRUD view for same models (data pools).
+ [#228](https://github.com/luyadev/luya-module-admin/issues/228) New `sortField` attribute option for plugins.
+ [#94](https://github.com/luyadev/luya-module-admin/issues/94) Required CRUD fields are now highlight as bold text.
+ [#277](https://github.com/luyadev/luya-module-admin/issues/277) Using [unglue.io](https://unglue.io) to compile admin resources.
Expand Down
20 changes: 13 additions & 7 deletions src/components/AdminMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public function getModules()
'alias' => $alias,
'icon' => $item['icon'],
'searchModelClass' => $item['searchModelClass'],

];
}

Expand Down Expand Up @@ -280,7 +281,14 @@ public function getModuleItems($nodeId)
$alias = $data['groups'][$groupName]['items'][$groupItemKey]['alias'];
}

// if a pool is available, the route will be modified by appending the pool param
$pool = AdminMenuBuilder::getOptionValue($groupItemEntry, 'pool', null);
if ($pool) {
$data['groups'][$groupName]['items'][$groupItemKey]['route'] = $data['groups'][$groupName]['items'][$groupItemKey]['route'] . '?pool='.$pool;
}

$data['groups'][$groupName]['items'][$groupItemKey]['hiddenInMenu'] = AdminMenuBuilder::getOptionValue($groupItemEntry, 'hiddenInMenu', false);
$data['groups'][$groupName]['items'][$groupItemKey]['pool'] = AdminMenuBuilder::getOptionValue($groupItemEntry, 'pool', null);
$data['groups'][$groupName]['items'][$groupItemKey]['alias'] = $alias;
}

Expand Down Expand Up @@ -338,16 +346,14 @@ public function getItems()
* @param string $api The Api Endpoint
* @return array|boolean
*/
public function getApiDetail($api)
public function getApiDetail($api, $pool = null)
{
$items = $this->getItems();

$key = array_search($api, array_column($items, 'permissionApiEndpoint'));

if ($key !== false) {
return $items[$key];
if ($pool) {
$items = ArrayHelper::searchColumns($items, 'pool', $pool);
}

return false;
$items = array_values($items); // reset keys to fix isset error
return ArrayHelper::searchColumn($items, 'permissionApiEndpoint', $api);
}
}
30 changes: 25 additions & 5 deletions src/components/AdminMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class AdminMenuBuilder extends BaseObject implements AdminMenuBuilderInterface
*/
protected static $options = [
'hiddenInMenu', // whether the current menu should be hidden in the menu or not
'contextField', // a context field name which is used to generate the pool
'contextValue', // a context value for the given context field to generate the pool
'pool', // a context field name which is used to generate the pool
];

/**
Expand Down Expand Up @@ -170,7 +169,7 @@ public function group($name)
*/
public function itemApi($name, $route, $icon, $apiEndpoint, array $options = [])
{
$this->_menu[$this->_pointers['node']]['groups'][$this->_pointers['group']]['items'][] = [
$item = [
'alias' => $name,
'route' => $route,
'icon' => $icon,
Expand All @@ -180,11 +179,32 @@ public function itemApi($name, $route, $icon, $apiEndpoint, array $options = [])
'searchModelClass' => false,
'options' => $this->verifyOptions($options),
];

$this->_menu[$this->_pointers['node']]['groups'][$this->_pointers['group']]['items'][] = $item;

$this->_permissionApis[] = ['api' => $apiEndpoint, 'alias' => $name];
$this->_permissionApis[] = ['api' => $apiEndpoint, 'alias' => $name, 'pool' => $this->getOptionValue($item, 'pool', null)];

return $this;
}

/**
* Generate a permission for an API with a Pool
*
* @param string $name
* @param string $route
* @param string $icon
* @param string $apiEndpoint
* @param string $pool
* @param array $options
* @return AdminMenuBuilder
* @since 2.0.0
*/
public function itemPoolApi($name, $route, $icon, $apiEndpoint, $pool, array $options = [])
{
return $this->itemApi($name, $route, $icon, $apiEndpoint, array_merge($options, [
'pool' => $pool,
]));
}

/**
* Add an item to a group. Route items opens a angular view.
Expand Down Expand Up @@ -257,6 +277,6 @@ public static function getOptionValue(array $item, $optionName, $defaultValue =
return $defaultValue;
}

return (isset($item['options'][$optionName])) ? $item['options'][$optionName] : $defaultValue;
return isset($item['options'][$optionName]) ? $item['options'][$optionName] : $defaultValue;
}
}
88 changes: 36 additions & 52 deletions src/components/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Yii;
use luya\Exception;
use luya\admin\models\Auth as AuthModel;
use yii\db\Query;
use yii\helpers\ArrayHelper;

Expand Down Expand Up @@ -205,27 +206,22 @@ public function matchRoute($userId, $route)
* @param string $route The route which is an identifier.
* @param string $name A readable name for the route to display in the permissions system.
* @throws \luya\Exception
* @return number
* @return integer
*/
public function addRoute($moduleName, $route, $name)
{
$handler = (new Query())->select('COUNT(*) AS count')->from('{{%admin_auth}}')->where(['route' => $route])->one();
if ($handler['count'] == 1) {
return Yii::$app->db->createCommand()->update('{{%admin_auth}}', [
'alias_name' => $name,
'module_name' => $moduleName,
], ['route' => $route])->execute();
} elseif ($handler['count'] == 0) {
return Yii::$app->db->createCommand()->insert('{{%admin_auth}}', [
'alias_name' => $name,
'module_name' => $moduleName,
'is_crud' => false,
'route' => $route,
'api' => 0,
])->execute();
$model = AuthModel::find()->where(['route' => $route])->one();

if (!$model) {
$model = new AuthModel();
}

throw new Exception("Error while inserting/updating auth ROUTE '$route' with name '$name' in module '$moduleName'.");

$model->alias_name = $name;
$model->module_name = $moduleName;
$model->route = $route;
$model->save();

return $model->id;
}

/**
Expand All @@ -234,28 +230,30 @@ public function addRoute($moduleName, $route, $name)
* @param string $moduleName The name of the module where the route is located.
* @param string $apiEndpoint An API endpoint name like `admin-user-group` which is an identifier.
* @param string $name A readable name for the api to display in the permission system.
* @throws \luya\Exception
* @return number
* @param string $pool
* @return integer
*/
public function addApi($moduleName, $apiEndpoint, $name)
public function addApi($moduleName, $apiEndpoint, $name, $pool = null)
{
$handler = (new Query())->select('COUNT(*) AS count')->from('{{%admin_auth}}')->where(['api' => $apiEndpoint])->one();
if ($handler['count'] == 1) {
return Yii::$app->db->createCommand()->update('{{%admin_auth}}', [
'alias_name' => $name,
'module_name' => $moduleName,
], ['api' => $apiEndpoint])->execute();
} elseif ($handler['count'] == 0) {
return Yii::$app->db->createCommand()->insert('{{%admin_auth}}', [
'alias_name' => $name,
'module_name' => $moduleName,
'is_crud' => true,
'route' => null,
'api' => $apiEndpoint,
])->execute();
$where = ['api' => $apiEndpoint];
if (!empty($pool)) {
$where['pool'] = $pool;
}

throw new Exception("Error while inserting/updating auth API '$apiEndpoint' with name '$name' in module '$moduleName'.");

$model = AuthModel::find()->where($where)->one();

if (!$model) {
$model = new AuthModel();
}

$model->alias_name = $name;
$model->module_name = $moduleName;
$model->pool = $pool;
$model->api = $apiEndpoint;
$model->is_crud = 1;
$model->save();

return $model->id;
}

/**
Expand Down Expand Up @@ -299,23 +297,9 @@ public function getDatabaseAuths()
* @param array $data array with key apis and routes
* @return array
*/
public function prepareCleanup(array $data)
public function prepareCleanup(array $ids)
{
$toCleanup = [];
foreach ($data as $type => $items) {
switch ($type) {
case 'apis':
$q = (new Query())->select('*')->from('{{%admin_auth}}')->where(['not in', 'api', $items])->andWhere(['is_crud' => true])->all();
$toCleanup = ArrayHelper::merge($q, $toCleanup);
break;
case 'routes':
$q = (new Query())->select('*')->from('{{%admin_auth}}')->where(['not in', 'route', $items])->andWhere(['is_crud' => false])->all();
$toCleanup = ArrayHelper::merge($q, $toCleanup);
break;
}
}

return $toCleanup;
return AuthModel::find()->where(['not in', 'id', $ids])->asArray()->all();
}

/**
Expand Down
13 changes: 4 additions & 9 deletions src/importers/AuthImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,23 @@ class AuthImporter extends Importer
public function run()
{
$modules = Yii::$app->getModules();
$data = [
'apis' => [],
'routes' => [],
];
$ids = [];
foreach ($modules as $id => $moduleObject) {
$object = Yii::$app->getModule($id);
if (method_exists($object, 'getAuthApis')) {
foreach ($object->getAuthApis() as $item) {
$data['apis'][] = $item['api'];
Yii::$app->auth->addApi($object->id, $item['api'], $item['alias']);
$ids[] = Yii::$app->auth->addApi($object->id, $item['api'], $item['alias'], isset($item['pool']) ? $item['pool'] : null);
}
}

if (method_exists($object, 'getAuthRoutes')) {
foreach ($object->getAuthRoutes() as $item) {
$data['routes'][] = $item['route'];
Yii::$app->auth->addRoute($object->id, $item['route'], $item['alias']);
$ids[] = Yii::$app->auth->addRoute($object->id, $item['route'], $item['alias']);
}
}
}

$toClean = Yii::$app->auth->prepareCleanup($data);
$toClean = Yii::$app->auth->prepareCleanup($ids);
if (count($toClean) > 0) {
foreach ($toClean as $rule) {
$this->addLog('Deleted old menu auth rule "'.$rule['alias_name'].'" in module '.$rule['module_name'].'.');
Expand Down
25 changes: 25 additions & 0 deletions src/migrations/m190327_140741_auth_pool_field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use yii\db\Migration;

/**
* Class m190327_140741_auth_pool_field
*/
class m190327_140741_auth_pool_field extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('{{%admin_auth}}', 'pool', $this->string());
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('{{%admin_auth}}', 'pool', $this->string());
}
}
60 changes: 60 additions & 0 deletions src/models/Auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace luya\admin \models;

use Yii;

/**
* This is the model class for table "admin_auth".
*
* @property int $id
* @property string $alias_name
* @property string $module_name
* @property int $is_crud
* @property string $route
* @property string $api
* @property string $pool
*
* @author Basil Suter <[email protected]>
* @since 2.0.0
*/
class Auth extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%admin_auth}}';
}

/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['alias_name', 'module_name'], 'required'],
[['is_crud'], 'integer'],
[['alias_name', 'module_name'], 'string', 'max' => 60],
[['route', 'api'], 'string', 'max' => 200],
[['pool'], 'string', 'max' => 255, 'skipOnEmpty' => true],
];
}

/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'alias_name' => 'Alias Name',
'module_name' => 'Module Name',
'is_crud' => 'Is Crud',
'route' => 'Route',
'api' => 'Api',
'pool' => 'Pool',
];
}
}
Loading

0 comments on commit 9f0652c

Please sign in to comment.