Skip to content

Commit

Permalink
增加批量为用户组赋予目录下所有项目权限
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Apr 15, 2018
1 parent 9e7d5b8 commit 09a0437
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
25 changes: 23 additions & 2 deletions app/Http/Controllers/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace App\Http\Controllers;


use App\Repositories\Catalog;
use App\Repositories\Group;
use App\Repositories\Project;
use App\Repositories\User;
Expand Down Expand Up @@ -54,6 +55,9 @@ public function info(Request $request, $id)
$projects = $group->projects()->with('catalog')->get();
$projectsForSelect = Project::whereDoesntHave('groups', $subQuery)->with('catalog')->get();

// 目录列表,可以按照目录的维度批量选择目录下所有的项目批量为用户组授权
$catalogs = Catalog::all();

return view('group.info', [
'op' => 'groups',
'group' => $group,
Expand All @@ -62,6 +66,7 @@ public function info(Request $request, $id)
'projects' => $projects,
'projects_for_select' => $projectsForSelect,
'tab' => $request->input('tab', 'member'),
'catalogs' => $catalogs,
]);
}

Expand Down Expand Up @@ -226,10 +231,26 @@ public function grantProjects(Request $request, $id)
]
);

$projectIds = $request->input('projects');
$privilege = $request->input('privilege', 'r');
$projects = $request->input('projects');
$privilege = $request->input('privilege', 'r');

$projectIds = [];
foreach ($projects as $pro) {
if (starts_with($pro, '#')) {
/** @var Catalog $catalog */
$catalog = Catalog::where('id', trim($pro, '#'))->firstOrFail();
foreach ($catalog->projects()->pluck('id')->toArray() as $projectId) {
$projectIds[] = (int)$projectId;
}
} else {
$projectIds[] = (int)$pro;
}
}

$projectIds = array_unique($projectIds, SORT_NUMERIC);

$group = Group::where('id', $id)->firstOrFail();
$group->projects()->detach($projectIds);
$group->projects()->attach($projectIds, ['privilege' => $privilege == 'r' ? 2 : 1]);

$this->alertSuccess(__('common.operation_success'));
Expand Down
7 changes: 7 additions & 0 deletions resources/views/group/info.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,15 @@
<form method="post"
action="{!! wzRoute('admin:groups:projects:add', ['id' => $group->id]) !!}">
{{ csrf_field() }}

<div class="alert alert-info" role="alert">
以“<b>#</b>”开头的选项为项目目录,选择该项会将该目录下所有项目权限批量赋予用户组。
</div>
<div class="form-group">
<select name="projects[]" style="width: 440px;" class="form-control select2-multiple" id="wz-project-select" multiple>
@foreach($catalogs as $cat)
<option value="#{{ $cat->id }}" data-name="#{{ $cat->name }}">#{{ $cat->name }}</option>
@endforeach
@foreach($projects_for_select as $proj)
@php
$projectName = $proj->name . (empty($proj->catalog_id) ? '' : "(#{$proj->catalog->name})");
Expand Down

0 comments on commit 09a0437

Please sign in to comment.