PermissionController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace Dcat\Admin\Controllers;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Layout\Content;
  6. use Dcat\Admin\Layout\Row;
  7. use Dcat\Admin\Models\Repositories\Permission;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\SimpleGrid;
  10. use Dcat\Admin\Tree;
  11. use Illuminate\Routing\Controller;
  12. use Illuminate\Support\Str;
  13. class PermissionController extends Controller
  14. {
  15. use HasResourceActions;
  16. /**
  17. * Index interface.
  18. *
  19. * @param Content $content
  20. *
  21. * @return Content
  22. */
  23. public function index(Content $content)
  24. {
  25. if (request(SimpleGrid::QUERY_NAME)) {
  26. return $content->body($this->simpleGrid());
  27. }
  28. return $content
  29. ->title(trans('admin.permissions'))
  30. ->description(trans('admin.list'))
  31. ->body(function (Row $row) {
  32. if (request('_layout')) {
  33. $row->column(12, $this->grid());
  34. } else {
  35. $row->column(12, $this->treeView());
  36. }
  37. });
  38. }
  39. /**
  40. * Show interface.
  41. *
  42. * @param mixed $id
  43. * @param Content $content
  44. *
  45. * @return Content
  46. */
  47. public function show($id, Content $content)
  48. {
  49. return $content
  50. ->title(trans('admin.permissions'))
  51. ->description(trans('admin.detail'))
  52. ->body($this->detail($id));
  53. }
  54. /**
  55. * Edit interface.
  56. *
  57. * @param $id
  58. * @param Content $content
  59. *
  60. * @return Content
  61. */
  62. public function edit($id, Content $content)
  63. {
  64. return $content
  65. ->title(trans('admin.permissions'))
  66. ->description(trans('admin.edit'))
  67. ->body($this->form()->edit($id));
  68. }
  69. /**
  70. * Create interface.
  71. *
  72. * @param Content $content
  73. *
  74. * @return Content
  75. */
  76. public function create(Content $content)
  77. {
  78. return $content
  79. ->title(trans('admin.permissions'))
  80. ->description(trans('admin.create'))
  81. ->body($this->form());
  82. }
  83. protected function simpleGrid()
  84. {
  85. $grid = new SimpleGrid(new Permission());
  86. $grid->id->bold()->sortable();
  87. $grid->slug;
  88. $grid->name;
  89. $grid->filter(function (Grid\Filter $filter) {
  90. $filter->like('slug');
  91. $filter->like('name');
  92. });
  93. return $grid;
  94. }
  95. /**
  96. * @return \Dcat\Admin\Tree
  97. */
  98. protected function treeView()
  99. {
  100. $model = config('admin.database.permissions_model');
  101. $tree = new Tree(new $model());
  102. $tree->disableCreateButton();
  103. $tree->tools(function (Tree\Tools $tools) {
  104. $label = trans('admin.table');
  105. $url = url(request()->getPathInfo()).'?_layout=1';
  106. $tools->add("<a class='btn btn-sm btn-default' href='{$url}'>$label</a>");
  107. });
  108. $tree->branch(function ($branch) {
  109. $payload = "<div class='pull-left' style='min-width:310px'><b>{$branch['name']}</b>&nbsp;&nbsp;[<span class='text-blue'>{$branch['slug']}</span>]";
  110. $path = array_filter($branch['http_path']);
  111. if (! $path) {
  112. return $payload.'</div>&nbsp;';
  113. }
  114. $max = 3;
  115. if (count($path) > $max) {
  116. $path = array_slice($path, 0, $max);
  117. array_push($path, '...');
  118. }
  119. $method = $branch['http_method'] ?: [];
  120. $path = collect($path)->map(function ($path) use ($branch, &$method) {
  121. if (Str::contains($path, ':')) {
  122. [$me, $path] = explode(':', $path);
  123. $method = array_merge($method, explode(',', $me));
  124. }
  125. if ($path !== '...' && ! empty(config('admin.route.prefix'))) {
  126. $path = admin_base_path($path);
  127. }
  128. return "<code>$path</code>";
  129. })->implode('&nbsp;&nbsp;');
  130. $method = collect($method ?: ['ANY'])->unique()->map(function ($name) {
  131. return strtoupper($name);
  132. })->map(function ($name) {
  133. return "<span class='label label-primary'>{$name}</span>";
  134. })->implode('&nbsp;').'&nbsp;';
  135. $payload .= "</div>&nbsp; $method<a class=\"dd-nodrag\">$path</a>";
  136. return $payload;
  137. });
  138. return $tree;
  139. }
  140. /**
  141. * Make a grid builder.
  142. *
  143. * @return Grid
  144. */
  145. protected function grid()
  146. {
  147. $grid = new Grid(new Permission());
  148. $grid->id('ID')->bold()->sortable();
  149. $grid->name->tree();
  150. $grid->slug->label('primary');
  151. $grid->http_path->display(function ($path) {
  152. if (! $path) {
  153. return;
  154. }
  155. $method = $this->http_method ?: ['ANY'];
  156. $method = collect($method)->map(function ($name) {
  157. return strtoupper($name);
  158. })->map(function ($name) {
  159. return "<span class='label label-primary'>{$name}</span>";
  160. })->implode('&nbsp;').'&nbsp;';
  161. return collect($path)->filter()->map(function ($path) use ($method) {
  162. if (Str::contains($path, ':')) {
  163. [$method, $path] = explode(':', $path);
  164. $method = collect(explode(',', $method))->map(function ($name) {
  165. return strtoupper($name);
  166. })->map(function ($name) {
  167. return "<span class='label label-primary'>{$name}</span>";
  168. })->implode('&nbsp;').'&nbsp;';
  169. }
  170. if (! empty(config('admin.route.prefix'))) {
  171. $path = admin_base_path($path);
  172. }
  173. return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
  174. })->implode('');
  175. });
  176. $grid->created_at;
  177. $grid->updated_at->sortable();
  178. $grid->disableEditButton();
  179. $grid->showQuickEditButton();
  180. $grid->enableDialogCreate();
  181. $grid->tools(function (Grid\Tools $tools) {
  182. $tools->batch(function (Grid\Tools\BatchActions $actions) {
  183. $actions->disableDelete();
  184. });
  185. $label = trans('admin.default');
  186. $url = url(request()->getPathInfo());
  187. $tools->append("<a class='btn btn-sm btn-default' href='{$url}'>$label</a>");
  188. });
  189. $grid->filter(function (Grid\Filter $filter) {
  190. $filter->like('slug');
  191. $filter->like('name');
  192. $filter->like('http_path');
  193. });
  194. return $grid;
  195. }
  196. /**
  197. * Make a show builder.
  198. *
  199. * @param mixed $id
  200. *
  201. * @return Show
  202. */
  203. protected function detail($id)
  204. {
  205. $show = new Show($id, new Permission());
  206. $show->id;
  207. $show->slug;
  208. $show->name;
  209. $show->http_path->unescape()->as(function ($path) {
  210. return collect($path)->filter()->map(function ($path) {
  211. $method = $this->http_method ?: ['ANY'];
  212. if (Str::contains($path, ':')) {
  213. [$method, $path] = explode(':', $path);
  214. $method = explode(',', $method);
  215. }
  216. $method = collect($method)->map(function ($name) {
  217. return strtoupper($name);
  218. })->map(function ($name) {
  219. return "<span class='label label-primary'>{$name}</span>";
  220. })->implode('&nbsp;');
  221. if (! empty(config('admin.route.prefix'))) {
  222. $path = '/'.trim(config('admin.route.prefix'), '/').$path;
  223. }
  224. return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
  225. })->implode('');
  226. });
  227. $show->created_at;
  228. $show->updated_at;
  229. return $show;
  230. }
  231. /**
  232. * Make a form builder.
  233. *
  234. * @return Form
  235. */
  236. public function form()
  237. {
  238. return Form::make(new Permission(), function (Form $form) {
  239. $permissionTable = config('admin.database.permissions_table');
  240. $connection = config('admin.database.connection');
  241. $id = $form->key();
  242. $form->display('id', 'ID');
  243. $form->text('slug', trans('admin.slug'))
  244. ->required()
  245. ->creationRules(['required', "unique:{$connection}.{$permissionTable}"])
  246. ->updateRules(['required', "unique:{$connection}.{$permissionTable},slug,$id"]);
  247. $form->text('name', trans('admin.name'))->required();
  248. $form->multipleSelect('http_method', trans('admin.http.method'))
  249. ->options($this->getHttpMethodsOptions())
  250. ->help(trans('admin.all_methods_if_empty'));
  251. $form->tags('http_path', trans('admin.http.path'))
  252. ->options($this->getRoutes());
  253. $form->display('created_at', trans('admin.created_at'));
  254. $form->display('updated_at', trans('admin.updated_at'));
  255. $form->disableViewButton();
  256. $form->disableViewCheck();
  257. });
  258. }
  259. /**
  260. * @return array
  261. */
  262. public function getRoutes()
  263. {
  264. $prefix = config('admin.route.prefix');
  265. return collect(app('router')->getRoutes())->map(function ($route) use ($prefix) {
  266. if (! Str::startsWith($uri = $route->uri(), $prefix)) {
  267. return;
  268. }
  269. return Str::replaceFirst($prefix, '', preg_replace('/{.*}+/', '*', $uri));
  270. })->filter()->all();
  271. }
  272. /**
  273. * Get options of HTTP methods select field.
  274. *
  275. * @return array
  276. */
  277. protected function getHttpMethodsOptions()
  278. {
  279. $permissionModel = config('admin.database.permissions_model');
  280. return array_combine($permissionModel::$httpMethods, $permissionModel::$httpMethods);
  281. }
  282. }