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

KJ: Issue 1496 役割グループ設定のインポート/エクスポート #1557

Merged
merged 10 commits into from
Feb 7, 2025
7 changes: 7 additions & 0 deletions config/exment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,13 @@
*/
'text_qr_button_ja' => env('EXMENT_TEXT_QR_BUTTON_JA', '二次元バーコード'),

/*
|--------------------------------------------------------------------------
| show import export button on role group view
|--------------------------------------------------------------------------
|
*/
'role_group_import_export' => env('ROLE_GROUP_IMPORT_EXPORT', false),
/*
|--------------------------------------------------------------------------
| Unavailable user view ( for tables )
Expand Down
11 changes: 11 additions & 0 deletions resources/lang/en/exment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,12 @@
'role_group_name' => 'Role Group Name',
'role_group_view_name' => 'Role Group View Name',
'description_system_admin' => '<span class="red">*To add system administrator privileges, go to "System Settings / System Administrator" from the menu.</span>',
'role_group_id' => 'Role group ID',
'role_group_permission_type' => 'Types of permission settings',
'role_group_target_plugin' => 'Target plugin ID',
'role_group_target_table' => 'Target table ID',
'role_group_user_org_type' => 'User/Organization type',
'role_group_user_org_target_id' => 'User/Organization ID',
'permissions' => 'Role Detail',
'permission_setting' => 'Permission Setting',
'user_organization_setting' => 'User/organization setting',
Expand Down Expand Up @@ -2113,6 +2119,7 @@

'error' => [
'cannot_accessable_and_value' => 'The permissions "Browse all data" and "%s" cannot be set at the same time.',
'cannot_plugin_access_permission' => 'This is a plugin for which usage and access cannot be configured.',
],
],

Expand Down Expand Up @@ -2280,6 +2287,7 @@
'error_flow' => 'Error Handling',
'import_error_message' => 'Error Message',
'import_error_format' => 'Line %d : %s',
'import_error_format_sheet' => '%s(Line %d) : %s',
'target_column_name' => 'Replacement target column name (alphanumeric characters)',

'help' => [
Expand All @@ -2304,6 +2312,9 @@
'file_column_not_match' => 'The column name :column_name does not exist in the file / image column of table :table_name.',
'file_not_found' => 'The file :file_name does not exist in the capture directory :dir_path.',
'file_column_extension_not_match' => 'Specify the same value for the extension of the column file_name and the column display_file_name.',
'target_table_not_found' => 'The table for the specified privilege does not exist.',
'permission_not_exists' => 'Permission "%s" does not exist.',
'user_org_not_exists' => 'The specified %s does not exist.',
],
],
'sendmail' => [
Expand Down
11 changes: 11 additions & 0 deletions resources/lang/ja/exment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,12 @@
'role_group_name' => '役割グループ名(英数字)',
'role_group_view_name' => '役割グループ表示名',
'description_system_admin' => '<span class="red">※システム管理者権限の追加は、メニューの「システム設定」→「システム管理者」より追加してください。</span>',
'role_group_id' => '役割グループID',
'role_group_permission_type' => '権限設定の種類',
'role_group_target_plugin' => '対象のプラグインID',
'role_group_target_table' => '対象のテーブルID',
'role_group_user_org_type' => 'ユーザー・組織区分',
'role_group_user_org_target_id' => 'ユーザー・組織ID',
'permissions' => '権限詳細',
'permission_setting' => '権限設定',
'user_organization_setting' => 'ユーザー・組織設定',
Expand Down Expand Up @@ -2114,6 +2120,7 @@

'error' => [
'cannot_accessable_and_value' => '権限「全データの参照」と「%s」を同時に設定することはできません。',
'cannot_plugin_access_permission' => '利用・アクセスを設定できないプラグインです',
],
],

Expand Down Expand Up @@ -2280,6 +2287,7 @@
'error_flow' => 'エラー時処理',
'import_error_message' => 'エラーメッセージ',
'import_error_format' => '行%d : %s',
'import_error_format_sheet' => '%s(行%d) : %s',
'target_column_name' => '置換対象列名(英数字)',

'help' => [
Expand All @@ -2304,6 +2312,9 @@
'file_column_not_match' => '列名 :column_name は、テーブル :table_name のファイル・画像列に存在しません。',
'file_not_found' => 'ファイル :file_name が、取込ディレクトリ :dir_path に存在しません。',
'file_column_extension_not_match' => '列file_nameと列display_file_nameの拡張子は、同じ値を指定してください。',
'target_table_not_found' => '指定された権限対象のテーブルが存在しません。',
'permission_not_exists' => '権限「%s」は存在しません。',
'user_org_not_exists' => '指定された%sは存在しません。',
],
],
'sendmail' => [
Expand Down
56 changes: 55 additions & 1 deletion src/Controllers/RoleGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Encore\Admin\Widgets\Box;
use Encore\Admin\Auth\Permission as Checker;
use Encore\Admin\Form as AdminForm;
use Exceedone\Exment\Services\DataImportExport;

class RoleGroupController extends AdminControllerBase
{
Expand Down Expand Up @@ -62,11 +63,20 @@ protected function grid()
$grid->disableCreateButton();
}

$grid->tools(function (Grid\Tools $tools) use ($hasCreatePermission) {
// create exporter
$service = $this->getImportExportService($grid);
$grid->exporter($service);

$grid->tools(function (Grid\Tools $tools) use ($grid, $hasCreatePermission) {
if (!$hasCreatePermission) {
$tools->disableBatchActions();
}
$tools->prepend(new Tools\SystemChangePageMenu());
if (boolval(config('exment.role_group_import_export', false)) && $hasCreatePermission) {
$button = new Tools\ExportImportButton(admin_url('role_group'), $grid, false, true);
$button->setBaseKey('common');
$tools->prepend($button);
}
});

$grid->disableExport();
Expand Down Expand Up @@ -100,6 +110,21 @@ protected function grid()
return $grid;
}

protected function getImportExportService($grid = null)
{
// create exporter
return (new DataImportExport\DataImportExportService())
->exportAction(new DataImportExport\Actions\Export\RoleGroupAction(
[
'grid' => $grid,
]
))->importAction(new DataImportExport\Actions\Import\RoleGroupAction(
[
'primary_key' => app('request')->input('select_primary_key') ?? null,
]
));
}

/**
* Create interface.
*
Expand Down Expand Up @@ -758,4 +783,33 @@ protected function hasPermission_UserOrganization()
{
return \Exment::user()->hasPermission([Permission::ROLE_GROUP_ALL, Permission::ROLE_GROUP_USER_ORGANIZATION]);
}

/**
* get import modal
*/
public function importModal(Request $request)
{
$service = $this->getImportExportService();
return $service->getImportModal();
}

/**
* @param Request $request
*/
public function import(Request $request)
{
// create exporter
$service = $this->getImportExportService()
->format($request->file('custom_table_file'));

if ($service->format() == 'csv') {
$file = $request->file('custom_table_file');
$file_name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$service->filebasename($file_name);
}

$result = $service->import($request);

return getAjaxResponse($result);
}
}
2 changes: 1 addition & 1 deletion src/Enums/RoleGroupType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getRoleGroupHelps()
});
}

protected function getRoleGroupPermissions()
public function getRoleGroupPermissions()
{
switch ($this->lowerKey()) {
case self::SYSTEM()->lowerKey():
Expand Down
5 changes: 4 additions & 1 deletion src/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ protected function mapExmentWebRotes()
$this->setResouce($router, 'login_setting', 'LoginSettingController');
$this->setResouce($router, 'api_setting', 'ApiSettingController');
$this->setResouce($router, 'plugin', 'PluginController');
$this->setResouce($router, 'role_group', 'RoleGroupController');
$this->setResouce($router, 'table', 'CustomTableController');
$this->setResouce($router, 'workflow', 'WorkflowController');

Expand All @@ -143,6 +142,10 @@ protected function mapExmentWebRotes()
$router->post("loginuser/import", 'LoginUserController@import');
$router->resource('loginuser', 'LoginUserController', ['except'=> ['create']]);

$router->get("role_group/importModal", 'RoleGroupController@importModal');
$router->post("role_group/import", 'RoleGroupController@import');
$this->setResouce($router, 'role_group', 'RoleGroupController');

$router->get('role', function () {
return redirect(admin_urls('role_group'));
});
Expand Down
57 changes: 57 additions & 0 deletions src/Services/DataImportExport/Actions/Export/RoleGroupAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Exceedone\Exment\Services\DataImportExport\Actions\Export;

use Exceedone\Exment\Services\DataImportExport\Providers\Export;

class RoleGroupAction extends ExportActionBase implements ActionInterface
{
/**
* laravel-admin grid
*/
protected $grid;

public function __construct($args = [])
{
$this->grid = array_get($args, 'grid');
}

public function datalist()
{
$providers = [];
$providers[] = new Export\RoleGroupProvider([
'grid' => $this->grid
]);
$providers[] = new Export\RoleGroupPermissionSystemProvider([
'grid' => $this->grid
]);
$providers[] = new Export\RoleGroupPermissionRoleProvider([
'grid' => $this->grid
]);
$providers[] = new Export\RoleGroupPermissionPluginProvider([
'grid' => $this->grid
]);
$providers[] = new Export\RoleGroupPermissionMasterProvider([
'grid' => $this->grid
]);
$providers[] = new Export\RoleGroupPermissionTableProvider([
'grid' => $this->grid
]);
$providers[] = new Export\RoleGroupUserOrganizationProvider([
'grid' => $this->grid
]);

$datalist = [];
foreach ($providers as $provider) {
$datalist[] = ['name' => $provider->name(), 'outputs' => $provider->data()];
$this->count .= $provider->getCount();
}

return $datalist;
}

public function filebasename()
{
return 'role_group';
}
}
123 changes: 123 additions & 0 deletions src/Services/DataImportExport/Actions/Import/RoleGroupAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Exceedone\Exment\Services\DataImportExport\Actions\Import;

use Exceedone\Exment\Services\DataImportExport\Providers\Import;
use Exceedone\Exment\Model\Define;

class RoleGroupAction implements ActionInterface
{
protected $primary_key;

public function __construct($args = [])
{
$this->primary_key = array_get($args, 'primary_key', 'id');
}

public function import($datalist, $options = [])
{
// get target data and model list
$data_imports = [];
foreach ($datalist as $table_name => &$data) {
$provider = $this->getProvider($table_name);
if (!isset($provider)) {
continue;
}

$dataObject = $provider->getDataObject($data, $options);

// validate data
list($data_import, $error_data) = $provider->validateImportData($dataObject);

// if has error data, return error data
if (is_array($error_data) && count($error_data) > 0) {
return response([
'result' => false,
'toastr' => exmtrans('common.message.import_error'),
'errors' => ['import_error_message' => ['type' => 'input', 'message' => implode("\r\n", $error_data)]],
], 400);
}
$data_imports[] = [
'provider' => $provider,
'data_import' => $data_import
];
}

foreach ($data_imports as $data_import) {
// execute imoport
$provider = $data_import['provider'];
foreach ($data_import['data_import'] as $index => &$row) {
$provider->importData($row);
}
}

return [
'result' => true,
'toastr' => exmtrans('common.message.import_success')
];
}

/**
* filter
*/
public function filterDatalist($datalist)
{
return $datalist;
}

// Import Modal --------------------------------------------------

/**
* get import modal endpoint. not contains "import" and "admin"
*/
public function getImportEndpoint()
{
return 'role_group';
}

public function getImportHeaderViewName()
{
return exmtrans('menu.system_definitions.role_group');
}

/**
* get primary key list.
*/
public function getPrimaryKeys()
{
// default list
$keys = getTransArray(['id'], "custom_value.import.key_options");
return $keys;
}

/**
* set_import_modal_items. it sets at form footer
*/
public function setImportModalItems(&$form)
{
return $this;
}

protected function getProvider(string $table_name)
{
switch ($table_name) {
case 'role_group':
return new Import\RoleGroupProvider([
'primary_key' => $this->primary_key,
]);
case 'role_group_permission_system':
return new Import\RoleGroupPermissionSystemProvider();
case 'role_group_permission_role':
return new Import\RoleGroupPermissionRoleProvider();
case 'role_group_permission_plugin':
return new Import\RoleGroupPermissionPluginProvider();
case 'role_group_permission_master':
return new Import\RoleGroupPermissionMasterProvider();
case 'role_group_permission_table':
return new Import\RoleGroupPermissionTableProvider();
case 'role_group_user_organization':
return new Import\RoleGroupUserOrganizationProvider();
}

}
}
Loading
Loading