|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpCollective\MenuMaker\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Routing\Controller; |
|
6
|
|
|
use PhpCollective\MenuMaker\Storage\Menu; |
|
7
|
|
|
use PhpCollective\MenuMaker\Storage\Permission; |
|
8
|
|
|
use PhpCollective\MenuMaker\Http\Requests\PermissionRequest as Request; |
|
9
|
|
|
|
|
10
|
|
|
class PermissionController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Show the form for editing the specified resource. |
|
14
|
|
|
* |
|
15
|
|
|
* @param int $id |
|
16
|
|
|
* |
|
17
|
|
|
* @return \Illuminate\View\View |
|
18
|
|
|
*/ |
|
19
|
|
|
public function index() |
|
20
|
|
|
{ |
|
21
|
|
|
$sections = Menu::sections()->pluck('name', 'id'); |
|
22
|
|
|
$actions = Permission::actions(); |
|
23
|
|
|
return view('menu-maker::permissions.index', compact('sections', 'actions')); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function selected() { |
|
27
|
|
|
|
|
28
|
|
|
if(request()->ajax()) |
|
29
|
|
|
{ |
|
30
|
|
|
$menu_id = request('m_id'); |
|
31
|
|
|
$selected = Menu::findOrFail($menu_id)->permissions()->select('namespace', 'controller', 'method', 'action')->get()->toArray(); |
|
32
|
|
|
$selectedActions = []; |
|
33
|
|
|
foreach ($selected as $action) { |
|
34
|
|
|
$selectedActions[] = $action['namespace'] . '-' . $action['controller'] . '-' . $action['method'] . '-' . $action['action']; |
|
35
|
|
|
} |
|
36
|
|
|
return response()->json(compact('selectedActions'), 200); |
|
37
|
|
|
} |
|
38
|
|
|
return response()->json([ |
|
39
|
|
|
'responseText' => 'Not a ajax request' |
|
40
|
|
|
], 400); |
|
41
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Update the specified resource in storage. |
|
46
|
|
|
* |
|
47
|
|
|
* @param Request $request |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
50
|
|
|
*/ |
|
51
|
|
|
public function update(Request $request) |
|
52
|
|
|
{ |
|
53
|
|
|
$menu_id = Menu::findParent(); |
|
54
|
|
|
$actions = $request->input('actions'); |
|
55
|
|
|
$data = []; |
|
56
|
|
|
if ($actions && count($actions) > 0) { |
|
57
|
|
|
foreach ($actions as $action) { |
|
58
|
|
|
$parts = explode('-', $action); |
|
59
|
|
|
$data[] = new Permission([ |
|
60
|
|
|
'namespace' => $parts[0], |
|
61
|
|
|
'controller' => $parts[1], |
|
62
|
|
|
'method' => $parts[2], |
|
63
|
|
|
'action' => $parts[3] |
|
64
|
|
|
]); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
$menu = Menu::findOrFail($menu_id); |
|
68
|
|
|
$menu->permissions()->delete(); |
|
69
|
|
|
$menu->permissions()->saveMany($data); |
|
70
|
|
|
|
|
71
|
|
|
return redirect() |
|
72
|
|
|
->back() |
|
73
|
|
|
->withInput() |
|
74
|
|
|
->withMessage(__('menu-maker::alerts.permissions-updated', ['name' => $menu->name])); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|