diff --git a/src/CoreBundle/Controller/SecurityController.php b/src/CoreBundle/Controller/SecurityController.php index d00a744881..0ef4a08b50 100644 --- a/src/CoreBundle/Controller/SecurityController.php +++ b/src/CoreBundle/Controller/SecurityController.php @@ -6,7 +6,9 @@ declare(strict_types=1); namespace Chamilo\CoreBundle\Controller; +use Chamilo\CoreBundle\Entity\TrackELoginRecord; use Chamilo\CoreBundle\Entity\User; +use Chamilo\CoreBundle\Framework\Container; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; @@ -14,6 +16,7 @@ use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\SerializerInterface; +use DateTime; class SecurityController extends AbstractController { @@ -45,6 +48,17 @@ class SecurityController extends AbstractController $user = $this->getUser(); $data = null; if ($user) { + // Log of connection attempts + $trackELoginRecord = new TrackELoginRecord(); + $trackELoginRecord + ->setUsername($user->getUsername()) + ->setLoginDate(new DateTime()) + ->setUserIp(api_get_real_ip()) + ->setSuccess(true) + ; + $repo = Container::getTrackELoginRecordRepository(); + $repo->create($trackELoginRecord); + $userClone = clone $user; $userClone->setPassword(''); $data = $this->serializer->serialize($userClone, JsonEncoder::FORMAT); diff --git a/src/CoreBundle/Entity/Room.php b/src/CoreBundle/Entity/Room.php index 39863a59c9..59a7682e01 100644 --- a/src/CoreBundle/Entity/Room.php +++ b/src/CoreBundle/Entity/Room.php @@ -41,7 +41,7 @@ class Room protected ?string $geolocation = null; /** - * @ORM\Column(name="ip", type="string", length=39, nullable=true, unique=false) + * @ORM\Column(name="ip", type="string", length=45, nullable=true, unique=false) */ protected ?string $ip = null; diff --git a/src/CoreBundle/Entity/TrackEAccess.php b/src/CoreBundle/Entity/TrackEAccess.php index 77c305a911..404d78d48f 100644 --- a/src/CoreBundle/Entity/TrackEAccess.php +++ b/src/CoreBundle/Entity/TrackEAccess.php @@ -55,7 +55,7 @@ class TrackEAccess protected int $accessSessionId; /** - * @ORM\Column(name="user_ip", type="string", length=39, nullable=false) + * @ORM\Column(name="user_ip", type="string", length=45, nullable=false) */ protected string $userIp; diff --git a/src/CoreBundle/Entity/TrackECourseAccess.php b/src/CoreBundle/Entity/TrackECourseAccess.php index 15bf4090f7..e88b693e76 100644 --- a/src/CoreBundle/Entity/TrackECourseAccess.php +++ b/src/CoreBundle/Entity/TrackECourseAccess.php @@ -68,7 +68,7 @@ class TrackECourseAccess protected int $sessionId; /** - * @ORM\Column(name="user_ip", type="string", length=39, nullable=false) + * @ORM\Column(name="user_ip", type="string", length=45, nullable=false) */ protected string $userIp; diff --git a/src/CoreBundle/Entity/TrackEExercise.php b/src/CoreBundle/Entity/TrackEExercise.php index 26bbf7ffbf..99e02a1cec 100644 --- a/src/CoreBundle/Entity/TrackEExercise.php +++ b/src/CoreBundle/Entity/TrackEExercise.php @@ -137,7 +137,7 @@ class TrackEExercise protected float $maxScore; /** - * @ORM\Column(name="user_ip", type="string", length=39, nullable=false) + * @ORM\Column(name="user_ip", type="string", length=45, nullable=false) */ #[Assert\NotBlank] #[Groups(['track_e_exercise:read'])] diff --git a/src/CoreBundle/Entity/TrackELogin.php b/src/CoreBundle/Entity/TrackELogin.php index fa24b30190..a35fe0991e 100644 --- a/src/CoreBundle/Entity/TrackELogin.php +++ b/src/CoreBundle/Entity/TrackELogin.php @@ -39,7 +39,7 @@ class TrackELogin protected DateTime $loginDate; /** - * @ORM\Column(name="user_ip", type="string", length=39, nullable=false) + * @ORM\Column(name="user_ip", type="string", length=45, nullable=false) */ protected string $userIp; diff --git a/src/CoreBundle/Entity/TrackELoginRecord.php b/src/CoreBundle/Entity/TrackELoginRecord.php new file mode 100644 index 0000000000..31bf514250 --- /dev/null +++ b/src/CoreBundle/Entity/TrackELoginRecord.php @@ -0,0 +1,144 @@ +username; + } + + /** + * Set the username. + * + * @param string $username + * + * @return $this + */ + public function setUsername(string $username): self + { + $this->username = $username; + + return $this; + } + + /** + * Set Login date. + * + * @param DateTime $loginDate + * + * @return $this + */ + public function setLoginDate(DateTime $loginDate): self + { + $this->loginDate = $loginDate; + + return $this; + } + + /** + * Get login date. + * + * @return DateTime + */ + public function getLoginDate() + { + return $this->loginDate; + } + + /** + * Set user ip. + * + * @param string $userIp + * + * @return $this + */ + public function setUserIp(string $userIp): self + { + $this->userIp = $userIp; + + return $this; + } + + /** + * Get user Ip. + * + * @return string + */ + public function getUserIp() + { + return $this->userIp; + } + + /** + * Get the success value. + * + * @return bool + */ + public function getSuccess(): bool + { + return $this->success; + } + + /** + * Set the success value. + * + * @param bool $boolean + * + * @return $this + */ + public function setSuccess(bool $boolean): self + { + $this->success = $boolean; + + return $this; + } +} diff --git a/src/CoreBundle/Entity/TrackEOnline.php b/src/CoreBundle/Entity/TrackEOnline.php index 4f7b6761bc..1a65a00a24 100644 --- a/src/CoreBundle/Entity/TrackEOnline.php +++ b/src/CoreBundle/Entity/TrackEOnline.php @@ -42,7 +42,7 @@ class TrackEOnline protected DateTime $loginDate; /** - * @ORM\Column(name="user_ip", type="string", length=39, nullable=false) + * @ORM\Column(name="user_ip", type="string", length=45, nullable=false) */ protected string $userIp; diff --git a/src/CoreBundle/EventSubscriber/LoginFailureSubscriber.php b/src/CoreBundle/EventSubscriber/LoginFailureSubscriber.php index 75259daa5c..361a0d85a3 100644 --- a/src/CoreBundle/EventSubscriber/LoginFailureSubscriber.php +++ b/src/CoreBundle/EventSubscriber/LoginFailureSubscriber.php @@ -6,9 +6,12 @@ declare(strict_types=1); namespace Chamilo\CoreBundle\EventSubscriber; +use Chamilo\CoreBundle\Entity\TrackELoginRecord; +use Chamilo\CoreBundle\Framework\Container; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; use Symfony\Component\Security\Http\Event\LoginFailureEvent; +use DateTime; class LoginFailureSubscriber implements EventSubscriberInterface { @@ -26,6 +29,15 @@ class LoginFailureSubscriber implements EventSubscriberInterface $userBadge = $passport->getBadge(UserBadge::class); $username = $userBadge->getUserIdentifier(); - error_log($username); + // Log of connection attempts + $trackELoginRecord = new TrackELoginRecord(); + $trackELoginRecord + ->setUsername($username) + ->setLoginDate(new DateTime()) + ->setUserIp(api_get_real_ip()) + ->setSuccess(false) + ; + $repo = Container::getTrackELoginRecordRepository(); + $repo->create($trackELoginRecord); } } diff --git a/src/CoreBundle/Framework/Container.php b/src/CoreBundle/Framework/Container.php index bd90e15399..c358268d33 100644 --- a/src/CoreBundle/Framework/Container.php +++ b/src/CoreBundle/Framework/Container.php @@ -34,6 +34,7 @@ use Chamilo\CoreBundle\Repository\SocialPostRepository; use Chamilo\CoreBundle\Repository\SysAnnouncementRepository; use Chamilo\CoreBundle\Repository\TagRepository; use Chamilo\CoreBundle\Repository\TrackEExerciseRepository; +use Chamilo\CoreBundle\Repository\TrackELoginRecordRepository; use Chamilo\CoreBundle\Serializer\UserToJsonNormalizer; use Chamilo\CoreBundle\Settings\SettingsManager; use Chamilo\CoreBundle\Tool\ToolChain; @@ -639,4 +640,9 @@ class Container { return self::$container->get(SocialPostRepository::class); } + + public static function getTrackELoginRecordRepository(): TrackELoginRecordRepository + { + return self::$container->get(TrackELoginRecordRepository::class); + } } diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20220628180435.php b/src/CoreBundle/Migrations/Schema/V200/Version20220628180435.php new file mode 100644 index 0000000000..73fb17c5d0 --- /dev/null +++ b/src/CoreBundle/Migrations/Schema/V200/Version20220628180435.php @@ -0,0 +1,38 @@ +hasTable('track_e_login_record')) { + $this->addSql( + 'CREATE TABLE track_e_login_record (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(100) NOT NULL, login_date DATETIME NOT NULL COMMENT "(DC2Type:datetime)", user_ip VARCHAR(39) NOT NULL, success TINYINT(1) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;' + ); + } + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + } +} diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20220909165130.php b/src/CoreBundle/Migrations/Schema/V200/Version20220909165130.php new file mode 100644 index 0000000000..2ca38d795d --- /dev/null +++ b/src/CoreBundle/Migrations/Schema/V200/Version20220909165130.php @@ -0,0 +1,80 @@ +addSql( + 'ALTER TABLE track_e_exercises CHANGE user_ip user_ip VARCHAR(45) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_course_access CHANGE user_ip user_ip VARCHAR(45) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE room CHANGE ip ip VARCHAR(45) DEFAULT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_access CHANGE user_ip user_ip VARCHAR(45) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_online CHANGE user_ip user_ip VARCHAR(45) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_login CHANGE user_ip user_ip VARCHAR(45) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_login_record CHANGE user_ip user_ip VARCHAR(45) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE c_wiki CHANGE user_ip user_ip VARCHAR(45) NOT NULL;' + ); + } + + public function down(Schema $schema): void + { + $this->addSql( + 'ALTER TABLE c_wiki CHANGE user_ip user_ip VARCHAR(39) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_login_record CHANGE user_ip user_ip VARCHAR(39) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_login CHANGE user_ip user_ip VARCHAR(39) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_online CHANGE user_ip user_ip VARCHAR(39) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_access CHANGE user_ip user_ip VARCHAR(39) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE room CHANGE ip ip VARCHAR(39) DEFAULT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_course_access CHANGE user_ip user_ip VARCHAR(39) NOT NULL;' + ); + $this->addSql( + 'ALTER TABLE track_e_exercises CHANGE user_ip user_ip VARCHAR(39) NOT NULL;' + ); + } +} diff --git a/src/CoreBundle/Repository/TrackELoginRecordRepository.php b/src/CoreBundle/Repository/TrackELoginRecordRepository.php new file mode 100644 index 0000000000..a8f7c69688 --- /dev/null +++ b/src/CoreBundle/Repository/TrackELoginRecordRepository.php @@ -0,0 +1,25 @@ +getEntityManager()->persist($trackELoginRecord); + $this->getEntityManager()->flush(); + } +} diff --git a/src/CourseBundle/Entity/CWiki.php b/src/CourseBundle/Entity/CWiki.php index 222fc27ae1..fd1f00eb5f 100644 --- a/src/CourseBundle/Entity/CWiki.php +++ b/src/CourseBundle/Entity/CWiki.php @@ -160,7 +160,7 @@ class CWiki extends AbstractResource implements ResourceInterface protected string $tag; /** - * @ORM\Column(name="user_ip", type="string", length=39, nullable=false) + * @ORM\Column(name="user_ip", type="string", length=45, nullable=false) */ protected string $userIp;