Add declare(strict_types=1), fix type hints

pull/3844/head
Julio Montoya 4 years ago
parent 51819d6c84
commit d6a368c532
  1. 65
      src/CoreBundle/Framework/Container.php
  2. 9
      src/CoreBundle/Framework/Exporter.php
  3. 2
      src/CoreBundle/Security/AccessDeniedHandler.php
  4. 8
      src/CoreBundle/Security/AuthenticationEntryPoint.php
  5. 14
      src/CoreBundle/Security/Authorization/Voter/CourseVoter.php
  6. 9
      src/CoreBundle/Security/Authorization/Voter/GroupVoter.php
  7. 68
      src/CoreBundle/Security/Authorization/Voter/ResourceNodeVoter.php
  8. 13
      src/CoreBundle/Security/Authorization/Voter/ResourceVoter.php
  9. 45
      src/CoreBundle/Security/Authorization/Voter/SessionVoter.php
  10. 4
      src/CoreBundle/Security/Encoder.php
  11. 30
      src/CoreBundle/Security/LoginFormAuthenticator.php
  12. 4
      src/CoreBundle/Security/TokenAuthenticator.php

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Framework;
@ -54,10 +56,14 @@ use Chamilo\CourseBundle\Repository\CThematicAdvanceRepository;
use Chamilo\CourseBundle\Repository\CThematicPlanRepository;
use Chamilo\CourseBundle\Repository\CThematicRepository;
use Chamilo\CourseBundle\Repository\CWikiRepository;
use CourseManager;
use Database;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
@ -73,36 +79,17 @@ use Twig\Environment;
*/
class Container
{
/** @var ContainerInterface|null */
public static $container;
public static $session;
public static $request;
public static $configuration;
public static $environment;
public static $urlGenerator;
public static $checker;
/** @var TranslatorInterface|null */
public static $translator;
public static $mailer;
public static $template;
public static $rootDir;
public static $logDir;
public static $tempDir;
public static $dataDir;
public static $courseDir;
public static $assets;
public static $htmlEditor;
public static $twig;
public static $roles;
/** @var string */
public static $legacyTemplate = '@ChamiloCore/Layout/layout_one_col.html.twig';
public static ?\Symfony\Component\DependencyInjection\ContainerInterface $container = null;
public static ?SessionInterface $session = null;
public static ?\Symfony\Component\HttpFoundation\Request $request = null;
public static ?\Symfony\Contracts\Translation\TranslatorInterface $translator = null;
public static Environment $twig;
public static string $legacyTemplate = '@ChamiloCore/Layout/layout_one_col.html.twig';
/**
* @param ContainerInterface $container
*/
public static function setContainer($container)
public static function setContainer($container): void
{
self::$container = $container;
}
@ -150,7 +137,7 @@ class Container
*/
public static function getProjectDir()
{
if (self::$container) {
if (null !== self::$container) {
return self::$container->get('kernel')->getProjectDir().'/';
}
@ -200,7 +187,7 @@ class Container
/**
* @param Request $request
*/
public static function setRequest($request)
public static function setRequest($request): void
{
self::$request = $request;
}
@ -210,7 +197,7 @@ class Container
*/
public static function getSession()
{
if (self::$container) {
if (null !== self::$container) {
return self::$container->get('session');
}
@ -238,7 +225,7 @@ class Container
*/
public static function getTranslator()
{
if (self::$translator) {
if (null !== self::$translator) {
return self::$translator;
}
@ -263,11 +250,11 @@ class Container
}
/**
* @return \Doctrine\ORM\EntityManager
* @return EntityManager
*/
public static function getEntityManager()
{
return \Database::getManager();
return Database::getManager();
}
public static function getUserManager(): UserRepository
@ -514,7 +501,7 @@ class Container
* @param string $message
* @param string $type error|success|warning|danger
*/
public static function addFlash($message, $type = 'success')
public static function addFlash($message, $type = 'success'): void
{
$session = self::getSession();
$session->getFlashBag()->add($type, $message);
@ -535,15 +522,15 @@ class Container
return self::$container->get(AssetRepository::class);
}
public static function setLegacyServices(ContainerInterface $container, bool $setSession = true)
public static function setLegacyServices(ContainerInterface $container, bool $setSession = true): void
{
\Database::setConnection($container->get('doctrine.dbal.default_connection'));
Database::setConnection($container->get('doctrine.dbal.default_connection'));
$em = $container->get('doctrine.orm.entity_manager');
\Database::setManager($em);
\CourseManager::setEntityManager($em);
\CourseManager::setCourseSettingsManager($container->get('Chamilo\CourseBundle\Manager\SettingsManager'));
Database::setManager($em);
CourseManager::setEntityManager($em);
CourseManager::setCourseSettingsManager($container->get('Chamilo\CourseBundle\Manager\SettingsManager'));
// Setting course tool chain (in order to create tools to a course)
\CourseManager::setToolList($container->get(ToolChain::class));
CourseManager::setToolList($container->get(ToolChain::class));
if ($setSession) {
self::$session = $container->get('session');
}

@ -1,9 +1,12 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Framework;
use RuntimeException;
use Sonata\Exporter\Handler;
use Sonata\Exporter\Source\SourceIteratorInterface;
use Sonata\Exporter\Writer\CsvWriter;
@ -21,7 +24,7 @@ class Exporter
* @param string $format
* @param string $filename
*
* @throws \RuntimeException
* @throws RuntimeException
*
* @return StreamedResponse
*/
@ -49,10 +52,10 @@ class Exporter
break;
default:
throw new \RuntimeException('Invalid format');
throw new RuntimeException('Invalid format');
}
$callback = function () use ($source, $writer) {
$callback = function () use ($source, $writer): void {
$handler = Handler::create($source, $writer);
$handler->export();
};

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Security;

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Security;
@ -13,8 +15,8 @@ use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface
class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
private $urlGenerator;
private $session;
private UrlGeneratorInterface $urlGenerator;
private SessionInterface $session;
public function __construct(UrlGeneratorInterface $urlGenerator, SessionInterface $session)
{
@ -25,7 +27,7 @@ class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
public function start(Request $request, AuthenticationException $authException = null): RedirectResponse
{
$message = $authException->getMessage();
if ($authException->getPrevious()) {
if (null !== $authException->getPrevious()) {
$message = $authException->getPrevious()->getMessage();
}

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Security\Authorization\Voter;
@ -22,9 +24,8 @@ class CourseVoter extends Voter
public const EDIT = 'EDIT';
public const DELETE = 'DELETE';
private $entityManager;
//private $courseManager;
private $security;
private EntityManagerInterface $entityManager;
private Security $security;
public function __construct(
EntityManagerInterface $entityManager,
@ -48,13 +49,8 @@ class CourseVoter extends Voter
if (!in_array($attribute, $options)) {
return false;
}
// only vote on Post objects inside this voter
if (!$subject instanceof Course) {
return false;
}
return true;
return $subject instanceof Course;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Security\Authorization\Voter;
@ -23,10 +25,7 @@ class GroupVoter extends Voter
public const EDIT = 'EDIT';
public const DELETE = 'DELETE';
//private $entityManager;
//private $courseManager;
//private $groupManager;
private $security;
private Security $security;
public function __construct(
//EntityManager $entityManager,
@ -85,7 +84,7 @@ class GroupVoter extends Voter
return true;
}
if (false === $group->getStatus()) {
if (!$group->getStatus()) {
return false;
}

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Security\Authorization\Voter;
@ -36,8 +38,8 @@ class ResourceNodeVoter extends Voter
public const ROLE_CURRENT_COURSE_SESSION_TEACHER = 'ROLE_CURRENT_COURSE_SESSION_TEACHER';
public const ROLE_CURRENT_COURSE_SESSION_STUDENT = 'ROLE_CURRENT_COURSE_SESSION_STUDENT';
private $requestStack;
private $security;
private RequestStack $requestStack;
private Security $security;
public function __construct(Security $security, RequestStack $requestStack)
{
@ -81,13 +83,8 @@ class ResourceNodeVoter extends Voter
if (!in_array($attribute, $options)) {
return false;
}
// only vote on ResourceNode objects inside this voter
if (!$subject instanceof ResourceNode) {
return false;
}
return true;
return $subject instanceof ResourceNode;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
@ -184,55 +181,42 @@ class ResourceNodeVoter extends Voter
if (null === $linkUser &&
$linkGroup instanceof CGroup && !empty($groupId) &&
$linkSession instanceof Session && !empty($sessionId) &&
$linkCourse instanceof Course && !empty($courseId)
$linkCourse instanceof Course && !empty($courseId) &&
($linkCourse->getId() === $courseId &&
$linkSession->getId() === $sessionId &&
$linkGroup->getIid() === $groupId)
) {
if ($linkCourse->getId() === $courseId &&
$linkSession->getId() === $sessionId &&
$linkGroup->getIid() === $groupId
) {
$linkFound = 3;
break;
}
$linkFound = 3;
break;
}
// Check if resource was sent inside a group in a base course.
if (null === $linkUser &&
empty($sessionId) &&
$linkGroup instanceof CGroup && !empty($groupId) &&
$linkCourse instanceof Course && !empty($courseId)
$linkCourse instanceof Course && !empty($courseId) && ($linkCourse->getId() === $courseId &&
$linkGroup->getIid() === $groupId)
) {
if ($linkCourse->getId() === $courseId &&
$linkGroup->getIid() === $groupId
) {
$linkFound = 4;
break;
}
$linkFound = 4;
break;
}
// Check if resource was sent to a course inside a session.
if (null === $linkUser &&
$linkSession instanceof Session && !empty($sessionId) &&
$linkCourse instanceof Course && !empty($courseId)
$linkCourse instanceof Course && !empty($courseId) && ($linkCourse->getId() === $courseId &&
$linkSession->getId() === $sessionId)
) {
if ($linkCourse->getId() === $courseId &&
$linkSession->getId() === $sessionId
) {
$linkFound = 5;
break;
}
$linkFound = 5;
break;
}
// Check if resource was sent to a course.
if (null === $linkUser &&
$linkCourse instanceof Course && !empty($courseId)) {
if ($linkCourse->getId() === $courseId) {
$linkFound = 6;
break;
}
$linkCourse instanceof Course && !empty($courseId) && $linkCourse->getId() === $courseId
) {
$linkFound = 6;
break;
}
/*if (ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility()) {
@ -438,11 +422,7 @@ class ResourceNodeVoter extends Voter
$acl->allow($superAdmin);
if ($token instanceof AnonymousToken) {
if ($acl->isAllowed('IS_AUTHENTICATED_ANONYMOUSLY', $linkId, $askedMask)) {
return true;
}
return false;
return $acl->isAllowed('IS_AUTHENTICATED_ANONYMOUSLY', $linkId, $askedMask);
}
foreach ($user->getRoles() as $role) {

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Security\Authorization\Voter;
@ -20,8 +22,8 @@ class ResourceVoter extends Voter
public const DELETE = 'DELETE';
public const EXPORT = 'EXPORT';
private $requestStack;
private $security;
private RequestStack $requestStack;
private Security $security;
public function __construct(Security $security, RequestStack $requestStack)
{
@ -64,13 +66,8 @@ class ResourceVoter extends Voter
if (!in_array($attribute, $options)) {
return false;
}
// only vote on ResourceNode objects inside this voter
if (!$subject instanceof AbstractResource) {
return false;
}
return true;
return $subject instanceof AbstractResource;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Security\Authorization\Voter;
@ -8,7 +10,9 @@ use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CoreBundle\Settings\SettingsManager;
use CourseManager;
use Doctrine\ORM\EntityManagerInterface;
use SessionManager;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
@ -25,10 +29,9 @@ class SessionVoter extends Voter
public const EDIT = 'EDIT';
public const DELETE = 'DELETE';
private $entityManager;
//private $courseManager;
private $security;
private $settingsManager;
private EntityManagerInterface $entityManager;
private Security $security;
private SettingsManager $settingsManager;
public function __construct(
EntityManagerInterface $entityManager,
@ -155,7 +158,7 @@ class SessionVoter extends Voter
private function sessionIsAvailableByDuration(Session $session, User $user)
{
$duration = $session->getDuration() * 24 * 60 * 60;
$courseAccess = \CourseManager::getFirstCourseAccessPerSessionAndUser(
$courseAccess = CourseManager::getFirstCourseAccessPerSessionAndUser(
$session->getId(),
$user->getId()
);
@ -176,7 +179,7 @@ class SessionVoter extends Voter
);
}
$userDurationData = \SessionManager::getUserSession(
$userDurationData = SessionManager::getUserSession(
$user->getId(),
$session->getId()
);
@ -205,11 +208,7 @@ class SessionVoter extends Voter
}
if ($checkSession) {
if ($this->allowed($user, $session)) {
return true;
}
return false;
return $this->allowed($user, $session);
}
return true;
@ -223,20 +222,12 @@ class SessionVoter extends Voter
$setting = $this->settingsManager->getSetting('session.allow_teachers_to_create_sessions');
if ('true' === $setting && $this->security->isGranted('ROLE_TEACHER')) {
return true;
}
return false;
return 'true' === $setting && $this->security->isGranted('ROLE_TEACHER');
}
private function allowManageAllSessions(): bool
{
if ($this->security->isGranted('ROLE_ADMIN') || $this->security->isGranted('ROLE_SESSION_MANAGER')) {
return true;
}
return false;
return $this->security->isGranted('ROLE_ADMIN') || $this->security->isGranted('ROLE_SESSION_MANAGER');
}
/**
@ -249,19 +240,15 @@ class SessionVoter extends Voter
}
if ($this->security->isGranted('ROLE_SESSION_MANAGER') &&
'true' !== $this->settingsManager->getSetting('session.allow_session_admins_to_manage_all_sessions')
'true' !== $this->settingsManager->getSetting('session.allow_session_admins_to_manage_all_sessions') && $session->getSessionAdmin()->getId() !== $user->getId()
) {
if ($session->getSessionAdmin()->getId() !== $user->getId()) {
return false;
}
return false;
}
if ($this->security->isGranted('ROLE_ADMIN') &&
'true' === $this->settingsManager->getSetting('session.allow_teachers_to_create_sessions')
'true' === $this->settingsManager->getSetting('session.allow_teachers_to_create_sessions') && $session->getGeneralCoach()->getId() !== $user->getId()
) {
if ($session->getGeneralCoach()->getId() !== $user->getId()) {
return false;
}
return false;
}
return true;

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Security;
@ -53,7 +55,7 @@ class Encoder extends BasePasswordEncoder
return $encoder->isPasswordValid($encoded, $raw, $salt);
}
private function getEncoder()
private function getEncoder(): void
{
/*switch ($this->passwordEncrypt) {
case 'none':

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Security;
@ -7,6 +9,7 @@ namespace Chamilo\CoreBundle\Security;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Hook\HookFactory;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
@ -34,17 +37,13 @@ class LoginFormAuthenticator extends AbstractGuardAuthenticator implements Passw
use TargetPathTrait;
private const LOGIN_ROUTE = 'login_json';
//private $entityManager;
public $serializer;
public $router;
//private $router;
private $passwordEncoder;
//private $formFactory;
// private $hookFactory;
private $userRepository;
private $csrfTokenManager;
private $urlGenerator;
public SerializerInterface $serializer;
public RouterInterface $router;
private UserPasswordEncoderInterface $passwordEncoder;
private UserRepository $userRepository;
private CsrfTokenManagerInterface $csrfTokenManager;
private UrlGeneratorInterface $urlGenerator;
public function __construct(
//EntityManagerInterface $entityManager,
@ -124,18 +123,13 @@ class LoginFormAuthenticator extends AbstractGuardAuthenticator implements Passw
}
/**
* @throws \Exception
* @throws Exception
*
* @return bool
*/
public function checkCredentials($credentials, UserInterface $user)
{
if ($this->passwordEncoder->isPasswordValid($user, $credentials['password'])) {
return true;
}
return false;
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
/*$hook = $this->hookFactory->build(CheckLoginCredentialsHook::class);
if (empty($hook)) {

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Chamilo\CoreBundle\Security;
use Chamilo\CoreBundle\Entity\User;
@ -15,7 +17,7 @@ use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
class TokenAuthenticator extends AbstractGuardAuthenticator
{
private $em;
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{

Loading…
Cancel
Save