N°2060 [WIP] Initialisation of the portal application: Refactor to make services 'combodo.current_user' and 'combodo.current_contact.photo_url' work

This commit is contained in:
Molkobain
2019-06-28 17:24:54 +02:00
parent 0d3e48475e
commit f71edbf45b
6 changed files with 106 additions and 170 deletions

View File

@@ -48,7 +48,7 @@ services:
bind:
$sPortalCachePath: !php/const PORTAL_CACHE_PATH
$sPortalId: !php/const PORTAL_ID
$sCombodoPortalBaseAbsolutePath: !php/const COMBODO_PORTAL_BASE_ABSOLUTE_PATH
$sCombodoPortalInstanceAbsoluteUrl: !php/const COMBODO_PORTAL_INSTANCE_ABSOLUTE_URL
# Makes classes in src/ available to be used as services
# This creates a service per class whose id is the fully-qualified class name
@@ -66,10 +66,11 @@ services:
_instanceof:
Combodo\iTop\Portal\EventListener\UserProvider:
tags: [{ name: 'kernel.event_listener', event: 'kernel.request' }]
calls:
- [setContainer, ['@service_container']]
Combodo\iTop\Portal\EventListener\ApplicationContextSetUrlMakerClass:
tags: [{ name: 'kernel.event_listener', event: 'kernel.request' }]
# Add more service definitions when explicit configuration is needed
# Please note that last definitions always *replace* previous ones
@@ -77,14 +78,6 @@ services:
Combodo\iTop\Portal\VariableAccessor\CombodoPortalInstanceConf:
arguments:
- '%combodo.portal.instance.conf%'
combodo.current_contact.photo_url:
class: Combodo\iTop\Portal\VariableAccessor\CombodoCurrentContactPhotoUrl
arguments:
- '@Combodo\iTop\Portal\Security\ItopUser'
combodo.current_user:
class: Combodo\iTop\Portal\VariableAccessor\CombodoCurrentUser
arguments:
- '@Combodo\iTop\Portal\Security\ItopUser'
# Legacy code as a service: since it is not in the auto-wiring path, it needs to be explicitly declared
ModuleDesign:
@@ -101,6 +94,18 @@ services:
- '@Combodo\iTop\Portal\Twig\AppVariable.inner'
- '@service_container'
# Standard services
combodo.current_contact.photo_url:
public: true
class: Combodo\iTop\Portal\VariableAccessor\CombodoCurrentContactPhotoUrl
arguments: ['@combodo.current_user']
# Note: This service is initialized with a UserLocal object as it needs a class that can be instantiated.
# Anyway, it will be replaced with the real class by UserProvider in onKernelRequestEvent.
# Note: Services relying on this one should use \User in their signature and not \UserLocal.
combodo.current_user:
public: true
class: UserLocal
# Aliases
brick_collection:
alias: Combodo\iTop\Portal\Brick\BrickCollection

View File

@@ -21,14 +21,11 @@
namespace Combodo\iTop\Portal\EventListener;
use Combodo\iTop\Portal\Security\ItopUser;
use Combodo\iTop\Portal\VariableAccessor\CombodoCurrentContactPhotoUrl;
use MetaModel;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Exception;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use utils;
use User;
use Dict;
use LoginWebPage;
use UserRights;
@@ -40,27 +37,24 @@ use ModuleDesign;
* @package Combodo\iTop\Portal\EventListener
* @since 2.7.0
*/
class UserProvider
class UserProvider implements ContainerAwareInterface
{
/** @var \ModuleDesign */
private $oModuleDesign;
/** @var ItopUser */
private $oItopUser;
/** @var string */
private $sCombodoPortalBaseAbsoluteUrl;
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
/**
* UserProvider constructor.
*
* @param \ModuleDesign $oModuleDesign
* @param \Combodo\iTop\Portal\Security\ItopUser $oItopUser
* @param string $sCombodoPortalBaseAbsolutePath Automatically bind to parameter of the name (see services.yml for other examples)
* @param \User $oUser
*/
public function __construct(ModuleDesign $oModuleDesign, ItopUser $oItopUser, $sCombodoPortalBaseAbsolutePath)
public function __construct(ModuleDesign $oModuleDesign)
{
$this->oModuleDesign = $oModuleDesign;
$this->oItopUser = $oItopUser;
$this->sCombodoPortalBaseAbsoluteUrl = $sCombodoPortalBaseAbsolutePath;
}
/**
@@ -89,47 +83,16 @@ class UserProvider
$oUser = UserRights::GetUserObject();
if ($oUser === null)
{
throw new \Exception('Could not load connected user.');
throw new Exception('Could not load connected user.');
}
$this->oItopUser->setUser($oUser);
// Contact
$sContactPhotoUrl = "{$this->sCombodoPortalBaseAbsoluteUrl}img/user-profile-default-256px.png";
// - Checking if we can load the contact
try
{
$oContact = UserRights::GetContactObject();
}
catch (Exception $e)
{
$oAllowedOrgSet = $oUser->Get('allowed_org_list');
if ($oAllowedOrgSet->Count() > 0)
{
throw new Exception('Could not load contact related to connected user. (Tip: Make sure the contact\'s organization is among the user\'s allowed organizations)');
}
else
{
throw new Exception('Could not load contact related to connected user.');
}
}
// - Retrieving picture
if ($oContact)
{
if (MetaModel::IsValidAttCode(get_class($oContact), 'picture'))
{
$oImage = $oContact->Get('picture');
if (is_object($oImage) && !$oImage->IsEmpty())
{
$sContactPhotoUrl = $oImage->GetDownloadURL(get_class($oContact), $oContact->GetKey(), 'picture');
}
else
{
$sContactPhotoUrl = MetaModel::GetAttributeDef(get_class($oContact), 'picture')->Get('default_image');
}
}
}
$this->oItopUser->setContactPhotoUrl($sContactPhotoUrl);
$this->container->set('combodo.current_user', $oUser);
}
/**
* Sets the container.
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}

View File

@@ -1,62 +0,0 @@
<?php
/**
* Copyright (C) 2013-2019 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*
*
*/
/**
* Created by Bruno DA SILVA, working for Combodo
* Date: 29/01/19
* Time: 15:04
*/
namespace Combodo\iTop\Portal\Security;
class ItopUser
{
private $oUser;
private $sContactPhotoUrl;
public function setUser($oUser)
{
$this->oUser = $oUser;
}
public function setContactPhotoUrl($sContactPhotoUrl)
{
$this->sContactPhotoUrl = $sContactPhotoUrl;
}
/**
* @return mixed
*/
public function getUser()
{
return $this->oUser;
}
/**
* @return mixed
*/
public function getContactPhotoUrl()
{
return $this->sContactPhotoUrl;
}
}

View File

@@ -20,7 +20,6 @@ abstract class AbstractVariableAccessor
*/
public function __construct($mVariableToStore)
{
$this->mStoredVariable = $mVariableToStore;
}

View File

@@ -28,13 +28,84 @@
namespace Combodo\iTop\Portal\VariableAccessor;
class CombodoCurrentContactPhotoUrl extends AbstractVariableAccessor
use Exception;
use MetaModel;
use User;
use UserRights;
class CombodoCurrentContactPhotoUrl
{
/**
* @var \User
*/
private $oUser;
private $sCombodoPortalInstanceAbsoluteUrl;
private $sContactPhotoUrl;
public function __construct(User $oUser, $sCombodoPortalInstanceAbsoluteUrl)
{
$this->oUser = $oUser;
$this->sCombodoPortalInstanceAbsoluteUrl = $sCombodoPortalInstanceAbsoluteUrl;
$this->sContactPhotoUrl = null;
}
/**
* @return string
*/
public function __toString()
{
return $this->getVariable()->getContactPhotoUrl();
if($this->sContactPhotoUrl === null)
{
$this->sContactPhotoUrl = $this->ComputeContactPhotoUrl();
}
return $this->sContactPhotoUrl;
}
/**
* @return string
*
* @throws \CoreException
* @throws \Exception
*/
private function ComputeContactPhotoUrl()
{
// Contact
$sContactPhotoUrl = "{$this->sCombodoPortalInstanceAbsoluteUrl}img/user-profile-default-256px.png";
// - Checking if we can load the contact
try
{
$oContact = UserRights::GetContactObject();
}
catch (Exception $e)
{
$oAllowedOrgSet = $this->oUser->Get('allowed_org_list');
if ($oAllowedOrgSet->Count() > 0)
{
throw new Exception('Could not load contact related to connected user. (Tip: Make sure the contact\'s organization is among the user\'s allowed organizations)');
}
else
{
throw new Exception('Could not load contact related to connected user.');
}
}
// - Retrieving picture
if ($oContact)
{
if (MetaModel::IsValidAttCode(get_class($oContact), 'picture'))
{
$oImage = $oContact->Get('picture');
if (is_object($oImage) && !$oImage->IsEmpty())
{
$sContactPhotoUrl = $oImage->GetDownloadURL(get_class($oContact), $oContact->GetKey(), 'picture');
}
else
{
$sContactPhotoUrl = MetaModel::GetAttributeDef(get_class($oContact), 'picture')->Get('default_image');
}
}
}
return $sContactPhotoUrl;
}
}

View File

@@ -1,40 +0,0 @@
<?php
/**
* Copyright (C) 2013-2019 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*
*
*/
/**
* Created by Bruno DA SILVA, working for Combodo
* Date: 28/02/19
* Time: 16:59
*/
namespace Combodo\iTop\Portal\VariableAccessor;
class CombodoCurrentUser extends AbstractVariableAccessor
{
/**
* @return string
*/
public function __toString()
{
return $this->getVariable()->getUser();
}
}