container get rid of setUser

This commit is contained in:
Yuri Kuznetsov
2020-06-22 12:38:40 +03:00
parent 6d696d94d8
commit 2340a58e8f
9 changed files with 45 additions and 28 deletions

View File

@@ -110,7 +110,7 @@ class Application
protected function getConfig()
{
return $this->getContainer()->get('config');
return $this->container->get('config');
}
public function run(string $name = 'default')
@@ -122,7 +122,7 @@ class Application
public function runClient()
{
$this->getContainer()->get('clientManager')->display();
$this->container->get('clientManager')->display();
exit;
}
@@ -133,7 +133,7 @@ class Application
}
$slim = $this->getSlim();
$container = $this->getContainer();
$container = $this->container;
$slim->any('.*', function() {});
@@ -235,13 +235,13 @@ class Application
public function runRebuild()
{
$dataManager = $this->getContainer()->get('dataManager');
$dataManager = $this->container->get('dataManager');
$dataManager->rebuild();
}
public function runClearCache()
{
$dataManager = $this->getContainer()->get('dataManager');
$dataManager = $this->container->get('dataManager');
$dataManager->clearCache();
}
@@ -250,7 +250,7 @@ class Application
$auth = $this->createAuth();
$auth->useNoAuth();
$consoleCommandManager = $this->getContainer()->get('consoleCommandManager');
$consoleCommandManager = $this->container->get('consoleCommandManager');
return $consoleCommandManager->run($command);
}
@@ -272,7 +272,7 @@ class Application
protected function routeHooks()
{
$container = $this->getContainer();
$container = $this->container;
$slim = $this->getSlim();
try {
@@ -330,7 +330,7 @@ class Application
}
try {
$controllerManager = $this->getContainer()->get('controllerManager');
$controllerManager = $this->container->get('controllerManager');
$result = $controllerManager->process(
$controllerName, $actionName, $params, $data, $slim->request(), $slim->response()
);
@@ -356,7 +356,7 @@ class Application
protected function getRouteList()
{
$routes = new Route($this->getConfig(), $this->getMetadata(), $this->getContainer()->get('fileManager'));
$routes = new Route($this->getConfig(), $this->getMetadata(), $this->container->get('fileManager'));
return $routes->getAll();
}
@@ -385,7 +385,7 @@ class Application
protected function initAutoloads()
{
$autoload = new Autoload($this->getConfig(), $this->getMetadata(), $this->getContainer()->get('fileManager'));
$autoload = new Autoload($this->getConfig(), $this->getMetadata(), $this->container->get('fileManager'));
$autoload->register();
}
@@ -393,19 +393,19 @@ class Application
{
foreach ($this->getMetadata()->get(['app', 'containerServices']) ?? [] as $name => $defs) {
if ($defs['preload'] ?? false) {
$this->getContainer()->get($name);
$this->container->get($name);
}
}
}
public function setBasePath(string $basePath)
{
$this->getContainer()->get('clientManager')->setBasePath($basePath);
$this->container->get('clientManager')->setBasePath($basePath);
}
public function getBasePath() : string
{
return $this->getContainer()->get('clientManager')->getBasePath();
return $this->container->get('clientManager')->getBasePath();
}
public function detectPortalId() : ?string
@@ -415,7 +415,7 @@ class Application
}
if (!empty($_COOKIE['auth-token'])) {
$token =
$this->getContainer()->get('entityManager')
$this->container->get('entityManager')
->getRepository('AuthToken')->where(['token' => $_COOKIE['auth-token']])->findOne();
if ($token && $token->get('portalId')) {
@@ -427,10 +427,10 @@ class Application
public function setupSystemUser()
{
$user = $this->getContainer()->get('entityManager')->getEntity('User', 'system');
$user = $this->container->get('entityManager')->getEntity('User', 'system');
$user->set('isAdmin', true); // TODO remove in 5.7
$user->set('type', 'system');
$this->getContainer()->setUser($user);
$this->getContainer()->get('entityManager')->setUser($user);
$this->container->set('user', $user);
$this->container->get('entityManager')->setUser($user);
}
}

View File

@@ -32,6 +32,8 @@ namespace Espo\Core;
use Espo\Core\InjectableFactory;
use Espo\Entities\User;
use Espo\Core\Exceptions\Error;
/**
* DI container for services. Lazy initialization is used. Services are instantiated only once.
* See https://docs.espocrm.com/development/di/.
@@ -81,15 +83,15 @@ class Container
return false;
}
/**
* Inject a user after authentication.
*/
public function setUser(User $user)
public function set(string $name, object $obj)
{
$this->set('user', $user);
if (!$this->configuration->isSettable($name)) {
throw new Error("Service '{$name}' is not settable.");
}
$this->setForced($name, $obj);
}
protected function set(string $name, object $obj)
protected function setForced(string $name, object $obj)
{
$this->data[$name] = $obj;
}

View File

@@ -86,4 +86,9 @@ class ContainerConfiguration
{
return $this->metadata->get(['app', 'containerServices', $name, 'dependencyList']) ?? null;
}
public function isSettable(string $name) : bool
{
return $this->metadata->get(['app', 'containerServices', $name, 'settable']) ?? false;
}
}

View File

@@ -49,7 +49,7 @@ class Container extends BaseContainer
public function setPortal(PortalEntity $portal)
{
$this->set('portal', $portal);
$this->setForced('portal', $portal);
$data = [];
foreach ($this->get('portal')->getSettingsAttributeList() as $attribute) {

View File

@@ -87,4 +87,11 @@ class ContainerConfiguration extends BaseContainerConfiguration
$this->metadata->get(['app', 'portalContainerServices', $name, 'dependencyList']) ??
parent::getServiceDependencyList($name);
}
public function isSettable(string $name) : bool
{
return
$this->metadata->get(['app', 'portalContainerServices', $name, 'settable']) ??
parent::isSettable($name);
}
}

View File

@@ -137,7 +137,7 @@ class Auth
$user->set('ipAddress', $_SERVER['REMOTE_ADDR']);
$entityManager->setUser($user);
$this->getContainer()->setUser($user);
$this->getContainer()->set('user', $user);
}
public function login($username, $password = null, $authenticationMethod = null)
@@ -272,7 +272,7 @@ class Auth
$user->set('ipAddress', $_SERVER['REMOTE_ADDR']);
$this->getEntityManager()->setUser($user);
$this->getContainer()->setUser($user);
$this->getContainer()->set('user', $user);
$secondStepRequired = false;

View File

@@ -108,7 +108,7 @@ class LDAP extends Espo
throw new Error("System user is not found.");
}
$this->getContainer()->setUser($systemUser);
$this->getContainer()->set('user', $systemUser);
$this->getEntityManager()->setUser($systemUser);
}

View File

@@ -10,5 +10,8 @@
},
"themeManager": {
"className": "Espo\\Core\\Utils\\ThemeManager"
},
"user": {
"settable": true
}
}

View File

@@ -78,7 +78,7 @@ class Installer
$this->app = new \Espo\Core\Application();
$user = $this->getEntityManager()->getEntity('User');
$this->app->getContainer()->setUser($user);
$this->app->getContainer()->set('user', $user);
require_once('install/core/InstallerConfig.php');
$this->installerConfig = new InstallerConfig();