1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Controllers; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Auth\Database\Permission; |
6
|
|
|
use Encore\Admin\Form; |
7
|
|
|
use Encore\Admin\Grid; |
8
|
|
|
use Encore\Admin\Layout\Content; |
9
|
|
|
use Encore\Admin\Show; |
10
|
|
|
use Illuminate\Routing\Controller; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
|
13
|
|
|
class PermissionController extends Controller |
14
|
|
|
{ |
15
|
|
|
use HasResourceActions; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Index interface. |
19
|
|
|
* |
20
|
|
|
* @param Content $content |
21
|
|
|
* @return Content |
22
|
|
|
*/ |
23
|
|
|
public function index(Content $content) |
24
|
|
|
{ |
25
|
|
|
return $content |
26
|
|
|
->header(trans('admin.permissions')) |
27
|
|
|
->description(trans('admin.list')) |
28
|
|
|
->body($this->grid()->render()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Show interface. |
33
|
|
|
* |
34
|
|
|
* @param mixed $id |
35
|
|
|
* @param Content $content |
36
|
|
|
* @return Content |
37
|
|
|
*/ |
38
|
|
|
public function show($id, Content $content) |
39
|
|
|
{ |
40
|
|
|
return $content |
41
|
|
|
->header(trans('admin.permissions')) |
42
|
|
|
->description(trans('admin.detail')) |
43
|
|
|
->body($this->detail($id)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Edit interface. |
48
|
|
|
* |
49
|
|
|
* @param $id |
50
|
|
|
* |
51
|
|
|
* @param Content $content |
52
|
|
|
* @return Content |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function edit($id, Content $content) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return $content |
57
|
|
|
->header(trans('admin.permissions')) |
58
|
|
|
->description(trans('admin.edit')) |
59
|
|
|
->body($this->form()->edit($id)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create interface. |
64
|
|
|
* |
65
|
|
|
* @param Content $content |
66
|
|
|
* @return Content |
67
|
|
|
*/ |
68
|
|
|
public function create(Content $content) |
69
|
|
|
{ |
70
|
|
|
return $content |
71
|
|
|
->header(trans('admin.permissions')) |
72
|
|
|
->description(trans('admin.create')) |
73
|
|
|
->body($this->form()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Make a grid builder. |
78
|
|
|
* |
79
|
|
|
* @return Grid |
80
|
|
|
*/ |
81
|
|
|
protected function grid() |
82
|
|
|
{ |
83
|
|
|
$grid = new Grid(new Permission()); |
84
|
|
|
|
85
|
|
|
$grid->id('ID')->sortable(); |
|
|
|
|
86
|
|
|
$grid->slug(trans('admin.slug')); |
|
|
|
|
87
|
|
|
$grid->name(trans('admin.name')); |
|
|
|
|
88
|
|
|
|
89
|
|
View Code Duplication |
$grid->http_path(trans('admin.route'))->display(function ($path) { |
|
|
|
|
90
|
|
|
return collect(explode("\r\n", $path))->map(function ($path) { |
91
|
|
|
$method = $this->http_method ?: ['ANY']; |
|
|
|
|
92
|
|
|
|
93
|
|
|
if (Str::contains($path, ':')) { |
94
|
|
|
list($method, $path) = explode(':', $path); |
95
|
|
|
$method = explode(',', $method); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$method = collect($method)->map(function ($name) { |
99
|
|
|
return strtoupper($name); |
100
|
|
|
})->map(function ($name) { |
101
|
|
|
return "<span class='label label-primary'>{$name}</span>"; |
102
|
|
|
})->implode(' '); |
103
|
|
|
|
104
|
|
|
$path = '/'.trim(config('admin.route.prefix'), '/').$path; |
105
|
|
|
|
106
|
|
|
return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>"; |
107
|
|
|
})->implode(''); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
$grid->created_at(trans('admin.created_at')); |
|
|
|
|
111
|
|
|
$grid->updated_at(trans('admin.updated_at')); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$grid->tools(function (Grid\Tools $tools) { |
114
|
|
|
$tools->batch(function (Grid\Tools\BatchActions $actions) { |
115
|
|
|
$actions->disableDelete(); |
116
|
|
|
}); |
117
|
|
|
}); |
118
|
|
|
|
119
|
|
|
return $grid; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Make a show builder. |
124
|
|
|
* |
125
|
|
|
* @param mixed $id |
126
|
|
|
* @return Show |
127
|
|
|
*/ |
128
|
|
|
protected function detail($id) |
129
|
|
|
{ |
130
|
|
|
$show = new Show(Permission::findOrFail($id)); |
131
|
|
|
|
132
|
|
|
$show->id('ID'); |
|
|
|
|
133
|
|
|
$show->slug(trans('admin.slug')); |
|
|
|
|
134
|
|
|
$show->name(trans('admin.name')); |
|
|
|
|
135
|
|
|
|
136
|
|
View Code Duplication |
$show->http_path(trans('admin.route'))->as(function ($path) { |
|
|
|
|
137
|
|
|
return collect(explode("\r\n", $path))->map(function ($path) { |
138
|
|
|
$method = $this->http_method ?: ['ANY']; |
139
|
|
|
|
140
|
|
|
if (Str::contains($path, ':')) { |
141
|
|
|
list($method, $path) = explode(':', $path); |
142
|
|
|
$method = explode(',', $method); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$method = collect($method)->map(function ($name) { |
146
|
|
|
return strtoupper($name); |
147
|
|
|
})->map(function ($name) { |
148
|
|
|
return "<span class='label label-primary'>{$name}</span>"; |
149
|
|
|
})->implode(' '); |
150
|
|
|
|
151
|
|
|
$path = '/'.trim(config('admin.route.prefix'), '/').$path; |
152
|
|
|
|
153
|
|
|
return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>"; |
154
|
|
|
})->implode(''); |
155
|
|
|
}); |
156
|
|
|
|
157
|
|
|
$show->created_at(trans('admin.created_at')); |
|
|
|
|
158
|
|
|
$show->updated_at(trans('admin.updated_at')); |
|
|
|
|
159
|
|
|
|
160
|
|
|
return $show; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Make a form builder. |
165
|
|
|
* |
166
|
|
|
* @return Form |
167
|
|
|
*/ |
168
|
|
|
public function form() |
169
|
|
|
{ |
170
|
|
|
$form = new Form(new Permission()); |
171
|
|
|
|
172
|
|
|
$form->display('id', 'ID'); |
173
|
|
|
|
174
|
|
|
$form->text('slug', trans('admin.slug'))->rules('required'); |
175
|
|
|
$form->text('name', trans('admin.name'))->rules('required'); |
176
|
|
|
|
177
|
|
|
$form->multipleSelect('http_method', trans('admin.http.method')) |
178
|
|
|
->options($this->getHttpMethodsOptions()) |
179
|
|
|
->help(trans('admin.all_methods_if_empty')); |
180
|
|
|
$form->textarea('http_path', trans('admin.http.path')); |
181
|
|
|
|
182
|
|
|
$form->display('created_at', trans('admin.created_at')); |
183
|
|
|
$form->display('updated_at', trans('admin.updated_at')); |
184
|
|
|
|
185
|
|
|
return $form; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get options of HTTP methods select field. |
190
|
|
|
* |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
protected function getHttpMethodsOptions() |
194
|
|
|
{ |
195
|
|
|
return array_combine(Permission::$httpMethods, Permission::$httpMethods); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.