src/Biz/BaseService.php line 75

Open in your IDE?
  1. <?php
  2. namespace Biz;
  3. use AppBundle\Common\ArrayToolkit;
  4. use Biz\Common\HTMLHelper;
  5. use Biz\ResourceManage\Service\ResourceManageService;
  6. use Biz\ResourceManage\Type\BaseType;
  7. use Biz\User\CurrentUser;
  8. use Codeages\Biz\Framework\Event\Event;
  9. use Codeages\Biz\Framework\Service\Exception\AccessDeniedException;
  10. use Codeages\Biz\Framework\Service\Exception\InvalidArgumentException;
  11. use Codeages\Biz\Framework\Service\Exception\NotFoundException;
  12. use Codeages\Biz\Framework\Service\Exception\ServiceException;
  13. use Monolog\Logger;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Topxia\Service\Common\ServiceKernel;
  16. class BaseService extends \Codeages\Biz\Framework\Service\BaseService
  17. {
  18.     private $lock null;
  19.     protected function createDao($alias)
  20.     {
  21.         return $this->biz->dao($alias);
  22.     }
  23.     /**
  24.      * @return CurrentUser
  25.      */
  26.     public function getCurrentUser()
  27.     {
  28.         return $this->biz['user'];
  29.     }
  30.     /**
  31.      * 过滤OrgIds
  32.      *
  33.      * 返回管理权限范围的OrgIds
  34.      *
  35.      * @param $conditions
  36.      *
  37.      * @return array
  38.      */
  39.     public function prepareOrgIds($conditions)
  40.     {
  41.         $orgIds $this->getCurrentUser()->getManageOrgIdsRecursively();
  42.         if (!isset($conditions['orgIds'])) {
  43.             return empty($orgIds) ? [-1] : $orgIds;
  44.         }
  45.         if (!is_array($conditions['orgIds'])) {
  46.             $conditions['orgIds'] = explode(','$conditions['orgIds']);
  47.         }
  48.         $conditions['orgIds'] = array_intersect($orgIds$conditions['orgIds']);
  49.         return empty($conditions['orgIds']) ? [-1] : array_values($conditions['orgIds']);
  50.     }
  51.     protected function processResourceManagePermission($resourceType$resourceManageArr$resource)
  52.     {
  53.         if (empty($resourceManageArr)) {
  54.             return;
  55.         }
  56.         $this->getResourceManageService()->setResourceManageData([
  57.             'resourceType' => $resourceType,
  58.             'resourceId' => $resource['id'],
  59.             'manageType' => $resourceManageArr['manageType'],
  60.             'targetIds' => $resourceManageArr['manageTargetIds'],
  61.         ]);
  62.     }
  63.     protected function createService($alias)
  64.     {
  65.         return $this->biz->service($alias);
  66.     }
  67.     protected function getResourceManageService(): ResourceManageService
  68.     {
  69.         return $this->createService('ResourceManage:ResourceManageService');
  70.     }
  71.     /**
  72.      * @return EventDispatcherInterface
  73.      */
  74.     private function getDispatcher()
  75.     {
  76.         return $this->biz['dispatcher'];
  77.     }
  78.     /**
  79.      * @param string      $eventName
  80.      * @param Event|mixed $subject
  81.      *
  82.      * @return object
  83.      */
  84.     protected function dispatchEvent(string $eventName$subject$arguments = [])
  85.     {
  86.         if ($subject instanceof Event) {
  87.             $event $subject;
  88.         } else {
  89.             $event = new Event($subject$arguments);
  90.         }
  91.         return $this->getDispatcher()->dispatch($event$eventName);
  92.     }
  93.     protected function beginTransaction()
  94.     {
  95.         $this->biz['db']->beginTransaction();
  96.     }
  97.     protected function commit()
  98.     {
  99.         $this->biz['db']->commit();
  100.     }
  101.     protected function rollback()
  102.     {
  103.         $this->biz['db']->rollback();
  104.     }
  105.     /**
  106.      * @return Logger
  107.      */
  108.     protected function getLogger()
  109.     {
  110.         return $this->biz['logger'];
  111.     }
  112.     /**
  113.      * @param string $message
  114.      *
  115.      * @return AccessDeniedException
  116.      */
  117.     protected function createAccessDeniedException($message 'Access Denied')
  118.     {
  119.         return new AccessDeniedException($message);
  120.     }
  121.     /**
  122.      * @param string $message
  123.      *
  124.      * @return InvalidArgumentException
  125.      */
  126.     protected function createInvalidArgumentException($message '')
  127.     {
  128.         return new InvalidArgumentException($message);
  129.     }
  130.     /**
  131.      * @param string $message
  132.      *
  133.      * @return NotFoundException
  134.      */
  135.     protected function createNotFoundException($message '')
  136.     {
  137.         return new NotFoundException($message);
  138.     }
  139.     /**
  140.      * @param string $message
  141.      *
  142.      * @return ServiceException
  143.      */
  144.     protected function createServiceException($message ''$code 0)
  145.     {
  146.         return new ServiceException($message$code);
  147.     }
  148.     protected function fillOrgId($fields)
  149.     {
  150.         $magic $this->biz->service('System:SettingService')->get('magic');
  151.         if (isset($magic['enable_org']) && $magic['enable_org']) {
  152.             if (!empty($fields['orgCode'])) {
  153.                 $org ServiceKernel::instance()->createService('Org:OrgService')->getOrgByOrgCode($fields['orgCode']);
  154.                 if (empty($org)) {
  155.                     throw $this->createNotFoundException('部门不存在,更新失败');
  156.                 }
  157.                 $fields['orgId'] = $org['id'];
  158.                 $fields['orgCode'] = $org['orgCode'];
  159.             } else {
  160.                 unset($fields['orgCode']);
  161.             }
  162.         } else {
  163.             unset($fields['orgCode']);
  164.         }
  165.         return $fields;
  166.     }
  167.     protected function purifyHtml($html$trusted false$handleOuterLink true)
  168.     {
  169.         /**
  170.          * @var HTMLHelper
  171.          */
  172.         $htmlHelper $this->biz['html_helper'];
  173.         return $htmlHelper->purify($html$trusted$handleOuterLink);
  174.     }
  175.     protected function getLock()
  176.     {
  177.         if (!$this->lock) {
  178.             $this->lock = new Lock($this->biz);
  179.         }
  180.         return $this->lock;
  181.     }
  182.     protected function trans($message$arguments = [], $domain null$locale null)
  183.     {
  184.         return ServiceKernel::instance()->trans($message$arguments$domain$locale);
  185.     }
  186.     protected function isOrgIdAccessible($orgId)
  187.     {
  188.         $currentUser $this->getCurrentUser();
  189.         $userOrgCodes $currentUser['orgCodes'];
  190.         if (in_array('1.'$userOrgCodes)) {
  191.             return true;
  192.         }
  193.         $userAccessibleOrgs ServiceKernel::instance()->createService('Org:OrgService')->findOrgsByPrefixOrgCodes($userOrgCodes);
  194.         $userAccessibleOrgIds ArrayToolkit::column($userAccessibleOrgs'id');
  195.         return in_array($orgId$userAccessibleOrgIds);
  196.     }
  197.     protected function getResourceManageClass($type): BaseType
  198.     {
  199.         return $this->biz->offsetGet('resource_manage.'.$type);
  200.     }
  201. }