src/CorporateTrainingBundle/Biz/ManagePermission/Service/Impl/ManagePermissionOrgServiceImpl.php line 114

Open in your IDE?
  1. <?php
  2. namespace CorporateTrainingBundle\Biz\ManagePermission\Service\Impl;
  3. use AppBundle\Common\ArrayToolkit;
  4. use Biz\BaseService;
  5. use Biz\ResourceManage\Type\BaseType;
  6. use CorporateTrainingBundle\Biz\ManagePermission\Dao\ManagePermissionOrgDao;
  7. use CorporateTrainingBundle\Biz\ManagePermission\Service\ManagePermissionOrgService;
  8. use CorporateTrainingBundle\Biz\Org\Service\Impl\OrgServiceImpl;
  9. use CorporateTrainingBundle\Biz\ResourceUsePermissionShared\Service\ResourceUsePermissionSharedService;
  10. use CorporateTrainingBundle\Biz\User\Service\UserService;
  11. class ManagePermissionOrgServiceImpl extends BaseService implements ManagePermissionOrgService
  12. {
  13.     protected $orgManageArr = [
  14.         'pluginTestPaper' => 'testpaper',
  15.         ];
  16.     public function createOrgRecord($fields)
  17.     {
  18.         if (!ArrayToolkit::requireds($fields, ['userId''orgId'])) {
  19.             throw $this->createServiceException('parameter is invalid!');
  20.         }
  21.         $fields['createdUserId'] = $this->getCurrentUser()->getId();
  22.         $fields $this->filterFields($fields);
  23.         return $this->getManagePermissionOrgDao()->create($fields);
  24.     }
  25.     public function updateOrgRecord($id$fields)
  26.     {
  27.         return $this->getManagePermissionOrgDao()->update($id$fields);
  28.     }
  29.     /**
  30.      * 判断设置部门是否超出管理员的管理范围
  31.      *
  32.      * @param $newSettingOrgIds '新设置的部门Ids'
  33.      * @param $oldSettingOrgIds '原设置的部门Ids'
  34.      *
  35.      * @return bool
  36.      */
  37.     public function checkOrgManagePermission($newSettingOrgIds$oldSettingOrgIds)
  38.     {
  39.         $newSettingOrgIds $this->getOrgService()->wipeOffChildrenOrgIds($newSettingOrgIds);
  40.         $oldSettingOrgIds $this->getOrgService()->wipeOffChildrenOrgIds($oldSettingOrgIds);
  41.         $deleteOrgIds array_diff($oldSettingOrgIds$newSettingOrgIds);
  42.         $addOrgIds array_diff($newSettingOrgIds$oldSettingOrgIds);
  43.         $diffOrgIds array_merge($deleteOrgIds$addOrgIds);
  44.         $currentUserOrgIds $this->getCurrentUser()->getManageOrgIdsRecursively();
  45.         $diffOrgIds array_diff($diffOrgIds$currentUserOrgIds);
  46.         return empty($diffOrgIds);
  47.     }
  48.     public function setUserManagePermissionOrgsByCodes($userId$codes)
  49.     {
  50.         $orgs $this->getOrgService()->findOrgsByCodes($codes);
  51.         if (empty($orgs)) {
  52.             return;
  53.         }
  54.         return $this->setUserManagePermissionOrgs($userIdarray_column($orgs'id'));
  55.     }
  56.     public function setUserManagePermissionOrgs($userId$orgIds)
  57.     {
  58.         $this->beginTransaction();
  59.         try {
  60.             if (!empty($orgIds)) {
  61.                 $orgIds $this->getOrgService()->wipeOffChildrenOrgIds($orgIds);
  62.             }
  63.             $this->deleteOrgRecordsByUserId($userId);
  64.             $fields = ['userId' => $userId];
  65.             foreach ($orgIds as $orgId) {
  66.                 $fields['orgId'] = $orgId;
  67.                 $this->createOrgRecord($fields);
  68.             }
  69.             $this->commit();
  70.         } catch (\Exception $e) {
  71.             $this->rollback();
  72.             throw $e;
  73.         }
  74.     }
  75.     /**
  76.      * 获取用户能管理的orgIds
  77.      *
  78.      * @param $userId
  79.      *
  80.      * @return array
  81.      */
  82.     public function getUserPermissionSelectOrgIds($userId)
  83.     {
  84.         $userOrgCodes $this->findUserManageOrgCodesByUserId($userId);
  85.         if (empty($userOrgCodes)) {
  86.             return [];
  87.         }
  88.         $orgIds $this->getOrgService()->findOrgsByPrefixOrgCodes($userOrgCodes, ['id']);
  89.         return ArrayToolkit::column($orgIds'id');
  90.     }
  91.     /**
  92.      * @param $userId
  93.      *
  94.      * @return array 获取用户有权限管理的OrgIds
  95.      */
  96.     public function findUserManageOrgIdsByUserId($userId)
  97.     {
  98.         $records $this->getManagePermissionOrgDao()->findByUserId($userId);
  99.         if (empty($records)) {
  100.             return [];
  101.         }
  102.         $orgIds ArrayToolkit::column($records'orgId');
  103.         return $this->checkPermissionOrgHasChange($userId$orgIds);
  104.     }
  105.     /**
  106.      * @param $userId
  107.      *
  108.      * @return array 获取用户设置的管理范围OrgCodes
  109.      */
  110.     public function findUserManageOrgCodesByUserId($userId)
  111.     {
  112.         $records $this->getManagePermissionOrgDao()->findByUserId($userId);
  113.         if (empty($records)) {
  114.             return [];
  115.         }
  116.         $orgIds ArrayToolkit::column($records'orgId');
  117.         $permissionOrgIds $this->checkPermissionOrgHasChange($userId$orgIds);
  118.         $orgs $this->getOrgService()->findOrgsByIds($permissionOrgIds);
  119.         return ArrayToolkit::column($orgs'orgCode');
  120.     }
  121.     public function deleteOrgRecord($id)
  122.     {
  123.         return $this->getManagePermissionOrgDao()->delete($id);
  124.     }
  125.     public function deleteOrgRecordsByUserId($userId)
  126.     {
  127.         return $this->getManagePermissionOrgDao()->deleteByUserId($userId);
  128.     }
  129.     public function searchOrgRecords($conditions$orderBys$start$limit$columns = [])
  130.     {
  131.         return $this->getManagePermissionOrgDao()->search($conditions$orderBys$start$limit$columns);
  132.     }
  133.     public function findOrgRecordsByUserId($userId)
  134.     {
  135.         return $this->getManagePermissionOrgDao()->findByUserId($userId);
  136.     }
  137.     /**
  138.      * @param $menu
  139.      * @param $orgCode
  140.      * 反查有某个列表管理权限且能管某个组织机构的管理员
  141.      */
  142.     public function findMangeUsersByMenuCodeAndOrgCode($menu$orgCode): array
  143.     {
  144.         $roles $this->getRoleService()->searchRoles([], [], 0PHP_INT_MAX);
  145.         $users = [];
  146.         foreach ($roles as $role) {
  147.             if (in_array($menu$role['data'])) {
  148.                 $roleUsers $this->getUserService()->searchUsers(['roles' => "{$role['code']}"], [], 0PHP_INT_MAX, ['id']);
  149.                 $users array_merge($usersArrayToolkit::column($roleUsers'id'));
  150.             }
  151.         }
  152.         $users array_values(array_unique($users));
  153.         if (empty($users)) {
  154.             return [];
  155.         }
  156.         $orgIds explode('.'$orgCode);
  157.         $userIds $this->searchOrgRecords(['orgIds' => empty($orgIds) ? [-1] : $orgIds'userIds' => $users], [], 0PHP_INT_MAX, ['userId']);
  158.         if (empty($userIds)) {
  159.             return [];
  160.         }
  161.         $userIds array_diff(ArrayToolkit::column($userIds'userId'), [1]);
  162.         return array_values(array_unique($userIds));
  163.     }
  164.     /**
  165.      * @param string $type          使用授权类型
  166.      * @param int    $resourceId    资源Id
  167.      * @param int    $resourceOrgId 资源orgId
  168.      *
  169.      * @return bool
  170.      *              // 判断资源是否能够被当前用户使用
  171.      */
  172.     public function checkResourceUsePermission($type$resourceId$resourceOrgId 0): bool
  173.     {
  174.         if (empty($resourceId)) {
  175.             return false;
  176.         }
  177.         if ($this->getResourceUsePermissionSharedService()->checkResourceUseShared($type$resourceId$this->getCurrentUser()->getId())) {
  178.             return true;
  179.         }
  180.         if (!empty($this->orgManageArr[$type])) {
  181.             return $this->getResourceManageClass($this->orgManageArr[$type])->tryManageResourceByResourceId($resourceId);
  182.         }
  183.         if ($resourceOrgId) {
  184.             $org $this->getOrgService()->getOrg($resourceOrgId);
  185.             return !empty($org) && $this->getCurrentUser()->hasManagePermissionWithOrgCode($org['orgCode']);
  186.         }
  187.         return false;
  188.     }
  189.     public function diffCanbeAddOrgIds($newSettingOrgIds$oldSettingOrgIds): array
  190.     {
  191.         $newSettingOrgIds $this->getOrgService()->wipeOffChildrenOrgIds($newSettingOrgIds);
  192.         $oldSettingOrgIds $this->getOrgService()->wipeOffChildrenOrgIds($oldSettingOrgIds);
  193.         $currentUserOrgIds $this->getCurrentUser()->getManageOrgIdsRecursively();
  194.         $notManageableOrgIds array_diff($oldSettingOrgIds$currentUserOrgIds);
  195.         $diffOrgIds array_intersect($currentUserOrgIds$newSettingOrgIds);
  196.         return array_merge($notManageableOrgIds$diffOrgIds);
  197.     }
  198.     /**
  199.      * @param $userId
  200.      *
  201.      * @return mixed
  202.      *
  203.      * 判断管理范围是否有过跨级的更改
  204.      */
  205.     protected function checkPermissionOrgHasChange($userId, array $orgIds)
  206.     {
  207.         $permissionOrgIds $this->getOrgService()->wipeOffChildrenOrgIds($orgIds);
  208.         $diff array_diff($orgIds$permissionOrgIds);
  209.         if (!empty($diff)) {
  210.             $this->setUserManagePermissionOrgs($userId$permissionOrgIds);
  211.         }
  212.         return $permissionOrgIds;
  213.     }
  214.     protected function filterFields($fields)
  215.     {
  216.         return ArrayToolkit::parts(
  217.             $fields,
  218.             [
  219.                 'userId',
  220.                 'orgId',
  221.                 'createdUserId',
  222.             ]
  223.         );
  224.     }
  225.     /**
  226.      * @return ResourceUsePermissionSharedService
  227.      */
  228.     protected function getResourceUsePermissionSharedService()
  229.     {
  230.         return $this->createService('ResourceUsePermissionShared:ResourceUsePermissionSharedService');
  231.     }
  232.     /**
  233.      * @return OrgServiceImpl
  234.      */
  235.     protected function getOrgService()
  236.     {
  237.         return $this->createService('Org:OrgService');
  238.     }
  239.     /**
  240.      * @return ManagePermissionOrgDao
  241.      */
  242.     protected function getManagePermissionOrgDao()
  243.     {
  244.         return $this->createDao('CorporateTrainingBundle:ManagePermission:ManagePermissionOrgDao');
  245.     }
  246.     /**
  247.      * @return \CorporateTrainingBundle\Biz\Role\Service\Impl\RoleServiceImpl
  248.      */
  249.     protected function getRoleService()
  250.     {
  251.         return $this->createService('CorporateTrainingBundle:Role:RoleService');
  252.     }
  253.     /**
  254.      * @return UserService
  255.      */
  256.     protected function getUserService()
  257.     {
  258.         return $this->createService('User:UserService');
  259.     }
  260.     protected function getResourceManageClass($type): BaseType
  261.     {
  262.         return $this->biz->offsetGet('resource_manage.'.$type);
  263.     }
  264. }