| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- /**
- * 管理权限分配.
- *
- * @author jim <jim458734573@gmail.com>
- * @date 2020-01-23 23:55:52
- */
- declare (strict_types=1);
- namespace Modules\Manage\Controllers;
- use App\Constants\CommonConstant;
- use App\Model\ManagerModel;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use App\Repositories\PermissionResourceRepository;
- use App\Repositories\PermissionRoleRepository;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Modules\Manage\Responses\CommonPagesResponse;
- use Modules\Manage\Responses\CommonSuccessPkResponse;
- use Modules\Manage\Responses\PermissionRoleDetailResponse;
- use Modules\Manage\Responses\PermissionRolePagesResponse;
- use Hyperf\DbConnection\Db;
- class PermissionController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 分页列表.
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed|CommonPagesResponse
- */
- public function resourceLists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'nullable|integer', 'page_size' => 'nullable|integer'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page']);
- $orders = [['id', 'ASC']];
- $lists = make(PermissionResourceRepository::class)->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
- return new CommonPagesResponse($lists);
- }
- /**
- * 删除.
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function resourceDelete(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $item = make(PermissionResourceRepository::class)->find($inputData['id']);
- if (empty($item)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldItem = clone $item;
- $result = $item->delete();
- if (!$result) {
- return json_fail('删除失败');
- } else {
- $logSubject = '删除一个权限资源,id:' . $oldItem->id;
- $logSvalue = $oldItem->id;
- $logDbData = ['after_items' => [], 'before_items' => [$oldItem->toArray()]];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessPkResponse($inputData['id']);
- }
- }
- /**
- * 详情角色.
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function roleDetail(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $item = make(PermissionRoleRepository::class)->find($inputData['id']);
- return new PermissionRoleDetailResponse($item);
- }
- /**
- * 添加角色.
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function roleAdd(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['name' => 'required|max:60', 'permissions' => 'required|array', 'squad' => 'nullable|integer'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- if (isset($inputData['permissions'])) {
- $permissions = !empty($inputData['permissions']) ? implode(',', $inputData['permissions']) : '';
- } else {
- $permissions = '';
- }
- if (!isset($inputData['squad'])) {
- $squad = 1000;
- } else {
- $squad = intval($inputData['squad']);
- }
- $setarr = ['name' => trim($inputData['name']), 'permissions' => $permissions, 'squad' => $squad];
- $result = make(PermissionRoleRepository::class)->create($setarr);
- if (!$result) {
- return json_fail('创建失败');
- } else {
- $logSubject = '添加一个角色, 新名称:' . $result->name;
- $logSvalue = $result->id;
- $logDbData = ['after_items' => [$result->toArray()], 'before_items' => []];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessPkResponse($result->id);
- }
- }
- /**
- * 修改角色.
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed|CommonSuccessPkResponse
- */
- public function roleModify(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required', 'name' => 'required|max:60', 'permissions' => 'required|array', 'squad' => 'nullable|integer'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $item = make(PermissionRoleRepository::class)->find($inputData['id']);
- if (empty($item)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldItem = clone $item;
- if (isset($inputData['permissions'])) {
- $permissions = !empty($inputData['permissions']) ? implode(',', $inputData['permissions']) : '';
- } else {
- $permissions = '';
- }
- if (!isset($inputData['squad'])) {
- $squad = 1000;
- } else {
- $squad = intval($inputData['squad']);
- }
- $oldName = $item->name;
- $name = trim($inputData['name']);
- $item->name = $name;
- $item->permissions = $permissions;
- $item->squad = $squad;
- $result = false;
- Db::beginTransaction();
- try {
- $result = $item->save();
- if ($result) {
- if ($name != $oldName) {
- // 批量替换冗余的管理角色名称.
- $rows = ManagerModel::where('role_id', $inputData['id'])->update(['role_name' => $name]);
- }
- }
- Db::commit();
- } catch (\Throwable $e) {
- Db::rollBack();
- }
- if (!$result) {
- return json_fail('修改失败');
- } else {
- $logSubject = '修改一个角色, 新名称:' . $item->name;
- $logSvalue = $item->id;
- $logDbData = ['after_items' => [$item->toArray()], 'before_items' => [$oldItem->toArray()]];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessPkResponse($item->id);
- }
- }
- /**
- * 删除角色.
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed|CommonSuccessPkResponse
- */
- public function roleDelete(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $item = make(PermissionRoleRepository::class)->find($inputData['id']);
- if (empty($item)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldItem = clone $item;
- $result = $item->delete();
- if (!$result) {
- return json_fail('删除失败');
- } else {
- $logSubject = '删除一个角色, 名称:' . $oldItem->name;
- $logSvalue = $oldItem->id;
- $logDbData = ['after_items' => [], 'before_items' => [$oldItem->toArray()]];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessPkResponse($inputData['id']);
- }
- }
- /**
- * 角色页列表.
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed|CommonPagesResponse
- */
- public function roleLists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'nullable|integer', 'page_size' => 'nullable|integer'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page']);
- $orders = [['squad', 'DESC'], ['id', 'DESC']];
- // 注意,单例分页一定要克隆一个(保证前面没用使用到paginate);
- $lists = make(PermissionRoleRepository::class)->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
- return new PermissionRolePagesResponse($lists);
- }
- /**
- * 获取有效树形权限.
- * @return mixed
- */
- public function allTree()
- {
- // $rTree = make(PermissionResourceRepository::class)->getAllEnableTree();
- $rTree = make(PermissionResourceRepository::class)->getAllEnableTreeName();
- return json_success($rTree);
- }
- }
|