PermissionController.php 6.5 KB

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