<?php
namespace Biz;
use AppBundle\Common\ArrayToolkit;
use Biz\Common\HTMLHelper;
use Biz\ResourceManage\Service\ResourceManageService;
use Biz\ResourceManage\Type\BaseType;
use Biz\User\CurrentUser;
use Codeages\Biz\Framework\Event\Event;
use Codeages\Biz\Framework\Service\Exception\AccessDeniedException;
use Codeages\Biz\Framework\Service\Exception\InvalidArgumentException;
use Codeages\Biz\Framework\Service\Exception\NotFoundException;
use Codeages\Biz\Framework\Service\Exception\ServiceException;
use Monolog\Logger;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Topxia\Service\Common\ServiceKernel;
class BaseService extends \Codeages\Biz\Framework\Service\BaseService
{
private $lock = null;
protected function createDao($alias)
{
return $this->biz->dao($alias);
}
/**
* @return CurrentUser
*/
public function getCurrentUser()
{
return $this->biz['user'];
}
/**
* 过滤OrgIds
*
* 返回管理权限范围的OrgIds
*
* @param $conditions
*
* @return array
*/
public function prepareOrgIds($conditions)
{
$orgIds = $this->getCurrentUser()->getManageOrgIdsRecursively();
if (!isset($conditions['orgIds'])) {
return empty($orgIds) ? [-1] : $orgIds;
}
if (!is_array($conditions['orgIds'])) {
$conditions['orgIds'] = explode(',', $conditions['orgIds']);
}
$conditions['orgIds'] = array_intersect($orgIds, $conditions['orgIds']);
return empty($conditions['orgIds']) ? [-1] : array_values($conditions['orgIds']);
}
protected function processResourceManagePermission($resourceType, $resourceManageArr, $resource)
{
if (empty($resourceManageArr)) {
return;
}
$this->getResourceManageService()->setResourceManageData([
'resourceType' => $resourceType,
'resourceId' => $resource['id'],
'manageType' => $resourceManageArr['manageType'],
'targetIds' => $resourceManageArr['manageTargetIds'],
]);
}
protected function createService($alias)
{
return $this->biz->service($alias);
}
protected function getResourceManageService(): ResourceManageService
{
return $this->createService('ResourceManage:ResourceManageService');
}
/**
* @return EventDispatcherInterface
*/
private function getDispatcher()
{
return $this->biz['dispatcher'];
}
/**
* @param string $eventName
* @param Event|mixed $subject
*
* @return object
*/
protected function dispatchEvent(string $eventName, $subject, $arguments = [])
{
if ($subject instanceof Event) {
$event = $subject;
} else {
$event = new Event($subject, $arguments);
}
return $this->getDispatcher()->dispatch($event, $eventName);
}
protected function beginTransaction()
{
$this->biz['db']->beginTransaction();
}
protected function commit()
{
$this->biz['db']->commit();
}
protected function rollback()
{
$this->biz['db']->rollback();
}
/**
* @return Logger
*/
protected function getLogger()
{
return $this->biz['logger'];
}
/**
* @param string $message
*
* @return AccessDeniedException
*/
protected function createAccessDeniedException($message = 'Access Denied')
{
return new AccessDeniedException($message);
}
/**
* @param string $message
*
* @return InvalidArgumentException
*/
protected function createInvalidArgumentException($message = '')
{
return new InvalidArgumentException($message);
}
/**
* @param string $message
*
* @return NotFoundException
*/
protected function createNotFoundException($message = '')
{
return new NotFoundException($message);
}
/**
* @param string $message
*
* @return ServiceException
*/
protected function createServiceException($message = '', $code = 0)
{
return new ServiceException($message, $code);
}
protected function fillOrgId($fields)
{
$magic = $this->biz->service('System:SettingService')->get('magic');
if (isset($magic['enable_org']) && $magic['enable_org']) {
if (!empty($fields['orgCode'])) {
$org = ServiceKernel::instance()->createService('Org:OrgService')->getOrgByOrgCode($fields['orgCode']);
if (empty($org)) {
throw $this->createNotFoundException('部门不存在,更新失败');
}
$fields['orgId'] = $org['id'];
$fields['orgCode'] = $org['orgCode'];
} else {
unset($fields['orgCode']);
}
} else {
unset($fields['orgCode']);
}
return $fields;
}
protected function purifyHtml($html, $trusted = false, $handleOuterLink = true)
{
/**
* @var HTMLHelper
*/
$htmlHelper = $this->biz['html_helper'];
return $htmlHelper->purify($html, $trusted, $handleOuterLink);
}
protected function getLock()
{
if (!$this->lock) {
$this->lock = new Lock($this->biz);
}
return $this->lock;
}
protected function trans($message, $arguments = [], $domain = null, $locale = null)
{
return ServiceKernel::instance()->trans($message, $arguments, $domain, $locale);
}
protected function isOrgIdAccessible($orgId)
{
$currentUser = $this->getCurrentUser();
$userOrgCodes = $currentUser['orgCodes'];
if (in_array('1.', $userOrgCodes)) {
return true;
}
$userAccessibleOrgs = ServiceKernel::instance()->createService('Org:OrgService')->findOrgsByPrefixOrgCodes($userOrgCodes);
$userAccessibleOrgIds = ArrayToolkit::column($userAccessibleOrgs, 'id');
return in_array($orgId, $userAccessibleOrgIds);
}
protected function getResourceManageClass($type): BaseType
{
return $this->biz->offsetGet('resource_manage.'.$type);
}
}