PermissionController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace Dcat\Admin\Controllers;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Layout\Content;
  7. use Dcat\Admin\Layout\Row;
  8. use Dcat\Admin\Models\Repositories\Permission;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\SimpleGrid;
  11. use Dcat\Admin\Tree;
  12. use Illuminate\Routing\Controller;
  13. use Illuminate\Support\Str;
  14. class PermissionController extends Controller
  15. {
  16. use HasResourceActions;
  17. /**
  18. * Index interface.
  19. *
  20. * @param Content $content
  21. *
  22. * @return Content
  23. */
  24. public function index(Content $content)
  25. {
  26. if (request(SimpleGrid::QUERY_NAME)) {
  27. return $content->body($this->simpleGrid());
  28. }
  29. return $content
  30. ->header(trans('admin.permissions'))
  31. ->description(trans('admin.list'))
  32. ->body(function (Row $row) {
  33. if (request('_layout')) {
  34. $row->column(12, $this->grid());
  35. } else {
  36. $row->column(12, $this->treeView());
  37. }
  38. });
  39. }
  40. /**
  41. * Show interface.
  42. *
  43. * @param mixed $id
  44. * @param Content $content
  45. *
  46. * @return Content
  47. */
  48. public function show($id, Content $content)
  49. {
  50. return $content
  51. ->header(trans('admin.permissions'))
  52. ->description(trans('admin.detail'))
  53. ->body($this->detail($id));
  54. }
  55. /**
  56. * Edit interface.
  57. *
  58. * @param $id
  59. * @param Content $content
  60. *
  61. * @return Content
  62. */
  63. public function edit($id, Content $content)
  64. {
  65. return $content
  66. ->header(trans('admin.permissions'))
  67. ->description(trans('admin.edit'))
  68. ->body($this->form()->edit($id));
  69. }
  70. /**
  71. * Create interface.
  72. *
  73. * @param Content $content
  74. *
  75. * @return Content
  76. */
  77. public function create(Content $content)
  78. {
  79. return $content
  80. ->header(trans('admin.permissions'))
  81. ->description(trans('admin.create'))
  82. ->body($this->form());
  83. }
  84. protected function simpleGrid()
  85. {
  86. $grid = new SimpleGrid(new Permission());
  87. $grid->id->bold()->sortable();
  88. $grid->slug;
  89. $grid->name;
  90. $grid->filter(function (Grid\Filter $filter) {
  91. $filter->like('slug');
  92. $filter->like('name');
  93. });
  94. return $grid;
  95. }
  96. /**
  97. * @return \Dcat\Admin\Tree
  98. */
  99. protected function treeView()
  100. {
  101. $model = config('admin.database.permissions_model');
  102. $tree = new Tree(new $model());
  103. $tree->disableCreateButton();
  104. $tree->tools(function (Tree\Tools $tools) {
  105. $label = trans('admin.table');
  106. $url = url(request()->getPathInfo()).'?_layout=1';
  107. $tools->add("<a class='btn btn-sm btn-default' href='{$url}'>$label</a>");
  108. });
  109. $tree->branch(function ($branch) {
  110. $payload = "<div class='pull-left' style='min-width:310px'><b>{$branch['name']}</b>&nbsp;&nbsp;[<span class='text-blue'>{$branch['slug']}</span>]";
  111. $path = array_filter($branch['http_path']);
  112. if (! $path) {
  113. return $payload.'</div>&nbsp;';
  114. }
  115. $max = 3;
  116. if (count($path) > $max) {
  117. $path = array_slice($path, 0, $max);
  118. array_push($path, '...');
  119. }
  120. $method = $branch['http_method'] ?: [];
  121. $path = collect($path)->map(function ($path) use ($branch, &$method) {
  122. if (Str::contains($path, ':')) {
  123. [$me, $path] = explode(':', $path);
  124. $method = array_merge($method, explode(',', $me));
  125. }
  126. if ($path !== '...' && ! empty(config('admin.route.prefix'))) {
  127. $path = admin_base_path($path);
  128. }
  129. return "<code>$path</code>";
  130. })->implode('&nbsp;&nbsp;');
  131. $method = collect($method ?: ['ANY'])->unique()->map(function ($name) {
  132. return strtoupper($name);
  133. })->map(function ($name) {
  134. return "<span class='label label-primary'>{$name}</span>";
  135. })->implode('&nbsp;').'&nbsp;';
  136. $payload .= "</div>&nbsp; $method<a class=\"dd-nodrag\">$path</a>";
  137. return $payload;
  138. });
  139. return $tree;
  140. }
  141. /**
  142. * Make a grid builder.
  143. *
  144. * @return Grid
  145. */
  146. protected function grid()
  147. {
  148. $grid = new Grid(new Permission());
  149. $grid->disableCreateButton();
  150. $grid->id('ID')->bold()->sortable();
  151. $grid->slug->label('primary');
  152. $grid->name;
  153. $grid->http_path->display(function ($path) {
  154. if (! $path) {
  155. return;
  156. }
  157. $method = $this->http_method ?: ['ANY'];
  158. $method = collect($method)->map(function ($name) {
  159. return strtoupper($name);
  160. })->map(function ($name) {
  161. return "<span class='label label-primary'>{$name}</span>";
  162. })->implode('&nbsp;').'&nbsp;';
  163. return collect($path)->filter()->map(function ($path) use ($method) {
  164. if (Str::contains($path, ':')) {
  165. [$method, $path] = explode(':', $path);
  166. $method = collect(explode(',', $method))->map(function ($name) {
  167. return strtoupper($name);
  168. })->map(function ($name) {
  169. return "<span class='label label-primary'>{$name}</span>";
  170. })->implode('&nbsp;').'&nbsp;';
  171. }
  172. if (! empty(config('admin.route.prefix'))) {
  173. $path = admin_base_path($path);
  174. }
  175. return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
  176. })->implode('');
  177. });
  178. $grid->created_at;
  179. $grid->updated_at->sortable();
  180. $grid->tools(function (Grid\Tools $tools) {
  181. $tools->batch(function (Grid\Tools\BatchActions $actions) {
  182. $actions->disableDelete();
  183. });
  184. $label = trans('admin.default');
  185. $url = url(request()->getPathInfo());
  186. $tools->append("<a class='btn btn-sm btn-default' href='{$url}'>$label</a>");
  187. });
  188. $grid->filter(function (Grid\Filter $filter) {
  189. $filter->like('slug');
  190. $filter->like('name');
  191. $filter->like('http_path');
  192. });
  193. return $grid;
  194. }
  195. /**
  196. * Make a show builder.
  197. *
  198. * @param mixed $id
  199. *
  200. * @return Show
  201. */
  202. protected function detail($id)
  203. {
  204. $show = new Show(new Permission());
  205. $show->setKey($id);
  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 Admin::form(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. }