PermissionController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\Support\Str;
  13. class PermissionController extends AdminController
  14. {
  15. /**
  16. * Get content title.
  17. *
  18. * @return string
  19. */
  20. protected function title()
  21. {
  22. return trans('admin.permissions');
  23. }
  24. /**
  25. * Index interface.
  26. *
  27. * @param Content $content
  28. *
  29. * @return Content
  30. */
  31. public function index(Content $content)
  32. {
  33. if (request(SimpleGrid::QUERY_NAME)) {
  34. return $content->body($this->simpleGrid());
  35. }
  36. return $content
  37. ->title($this->title())
  38. ->description(trans('admin.list'))
  39. ->body($this->treeView());
  40. }
  41. protected function simpleGrid()
  42. {
  43. $grid = new SimpleGrid(new Permission());
  44. $grid->id->sortable();
  45. $grid->slug;
  46. $grid->name;
  47. $grid->filter(function (Grid\Filter $filter) {
  48. $filter->like('slug');
  49. $filter->like('name');
  50. });
  51. return $grid;
  52. }
  53. /**
  54. * @return \Dcat\Admin\Tree
  55. */
  56. protected function treeView()
  57. {
  58. $model = config('admin.database.permissions_model');
  59. $tree = new Tree(new $model());
  60. $tree->disableCreateButton();
  61. $tree->branch(function ($branch) {
  62. $payload = "<div class='pull-left' style='min-width:310px'><b>{$branch['name']}</b>&nbsp;&nbsp;[<span class='text-blue'>{$branch['slug']}</span>]";
  63. $path = array_filter($branch['http_path']);
  64. if (! $path) {
  65. return $payload.'</div>&nbsp;';
  66. }
  67. $max = 3;
  68. if (count($path) > $max) {
  69. $path = array_slice($path, 0, $max);
  70. array_push($path, '...');
  71. }
  72. $method = $branch['http_method'] ?: [];
  73. $path = collect($path)->map(function ($path) use ($branch, &$method) {
  74. if (Str::contains($path, ':')) {
  75. [$me, $path] = explode(':', $path);
  76. $method = array_merge($method, explode(',', $me));
  77. }
  78. if ($path !== '...' && ! empty(config('admin.route.prefix'))) {
  79. $path = trim(admin_base_path($path), '/');
  80. }
  81. $color = Admin::color()->primaryDarker();
  82. return "<code style='color:{$color}'>$path</code>";
  83. })->implode('&nbsp;&nbsp;');
  84. $method = collect($method ?: ['ANY'])->unique()->map(function ($name) {
  85. return strtoupper($name);
  86. })->map(function ($name) {
  87. return "<span class='label bg-primary'>{$name}</span>";
  88. })->implode('&nbsp;').'&nbsp;';
  89. $payload .= "</div>&nbsp; $method<a class=\"dd-nodrag\">$path</a>";
  90. return $payload;
  91. });
  92. return $tree;
  93. }
  94. /**
  95. * Make a show builder.
  96. *
  97. * @param mixed $id
  98. *
  99. * @return Show
  100. */
  101. protected function detail($id)
  102. {
  103. $show = new Show($id, new Permission());
  104. $show->id;
  105. $show->slug;
  106. $show->name;
  107. $show->http_path->unescape()->as(function ($path) {
  108. return collect($path)->filter()->map(function ($path) {
  109. $method = $this->http_method ?: ['ANY'];
  110. if (Str::contains($path, ':')) {
  111. [$method, $path] = explode(':', $path);
  112. $method = explode(',', $method);
  113. }
  114. $method = collect($method)->map(function ($name) {
  115. return strtoupper($name);
  116. })->map(function ($name) {
  117. return "<span class='label bg-primary'>{$name}</span>";
  118. })->implode('&nbsp;');
  119. if (! empty(config('admin.route.prefix'))) {
  120. $path = '/'.trim(config('admin.route.prefix'), '/').$path;
  121. }
  122. return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
  123. })->implode('');
  124. });
  125. $show->created_at;
  126. $show->updated_at;
  127. return $show;
  128. }
  129. /**
  130. * Make a form builder.
  131. *
  132. * @return Form
  133. */
  134. public function form()
  135. {
  136. return Form::make(new Permission(), function (Form $form) {
  137. $permissionTable = config('admin.database.permissions_table');
  138. $connection = config('admin.database.connection');
  139. $permissionModel = config('admin.database.permissions_model');
  140. $id = $form->getKey();
  141. $form->display('id', 'ID');
  142. $form->select('parent_id', trans('admin.parent_id'))
  143. ->options($permissionModel::selectOptions())
  144. ->saving(function ($v) {
  145. return (int) $v;
  146. });
  147. $form->text('slug', trans('admin.slug'))
  148. ->required()
  149. ->creationRules(['required', "unique:{$connection}.{$permissionTable}"])
  150. ->updateRules(['required', "unique:{$connection}.{$permissionTable},slug,$id"]);
  151. $form->text('name', trans('admin.name'))->required();
  152. $form->multipleSelect('http_method', trans('admin.http.method'))
  153. ->options($this->getHttpMethodsOptions())
  154. ->help(trans('admin.all_methods_if_empty'));
  155. $form->tags('http_path', trans('admin.http.path'))
  156. ->options($this->getRoutes());
  157. $form->display('created_at', trans('admin.created_at'));
  158. $form->display('updated_at', trans('admin.updated_at'));
  159. $form->disableViewButton();
  160. $form->disableViewCheck();
  161. });
  162. }
  163. /**
  164. * @return array
  165. */
  166. public function getRoutes()
  167. {
  168. $prefix = config('admin.route.prefix');
  169. return collect(app('router')->getRoutes())->map(function ($route) use ($prefix) {
  170. if (! Str::startsWith($uri = $route->uri(), $prefix)) {
  171. return;
  172. }
  173. return Str::replaceFirst($prefix, '', preg_replace('/{.*}+/', '*', $uri));
  174. })->filter()->all();
  175. }
  176. /**
  177. * Get options of HTTP methods select field.
  178. *
  179. * @return array
  180. */
  181. protected function getHttpMethodsOptions()
  182. {
  183. $permissionModel = config('admin.database.permissions_model');
  184. return array_combine($permissionModel::$httpMethods, $permissionModel::$httpMethods);
  185. }
  186. }