Tracking: Login: Add tracking of connection attempts - refs CT#3810

Authored-by: Christian <christian1827@gmail.com>
pull/4393/head
christianbeeznest 3 years ago committed by GitHub
parent a371e9450f
commit 518310dd5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      src/CoreBundle/Controller/SecurityController.php
  2. 2
      src/CoreBundle/Entity/Room.php
  3. 2
      src/CoreBundle/Entity/TrackEAccess.php
  4. 2
      src/CoreBundle/Entity/TrackECourseAccess.php
  5. 2
      src/CoreBundle/Entity/TrackEExercise.php
  6. 2
      src/CoreBundle/Entity/TrackELogin.php
  7. 144
      src/CoreBundle/Entity/TrackELoginRecord.php
  8. 2
      src/CoreBundle/Entity/TrackEOnline.php
  9. 14
      src/CoreBundle/EventSubscriber/LoginFailureSubscriber.php
  10. 6
      src/CoreBundle/Framework/Container.php
  11. 38
      src/CoreBundle/Migrations/Schema/V200/Version20220628180435.php
  12. 80
      src/CoreBundle/Migrations/Schema/V200/Version20220909165130.php
  13. 25
      src/CoreBundle/Repository/TrackELoginRecordRepository.php
  14. 2
      src/CourseBundle/Entity/CWiki.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);

@ -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;

@ -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;

@ -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;

@ -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'])]

@ -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;

@ -0,0 +1,144 @@
<?php
declare(strict_types = 1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Entity;
use Chamilo\CoreBundle\Traits\UserTrait;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
/**
* Track Login Record.
*
* @ORM\Table(name="track_e_login_record")
* @ORM\Entity
*/
class TrackELoginRecord
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
protected int $id;
/**
* @ORM\Column(name="username", type="string", length=100, nullable=false)
*/
protected string $username;
/**
* @ORM\Column(name="login_date", type="datetime", nullable=false)
*/
protected DateTime $loginDate;
/**
* @ORM\Column(name="user_ip", type="string", length=45, nullable=false)
*/
protected string $userIp;
/**
* @ORM\Column(name="success", type="boolean")
*/
protected bool $success;
/**
* Get the username.
*
* @return string
*/
public function getUsername(): string
{
return $this->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;
}
}

@ -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;

@ -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);
}
}

@ -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);
}
}

@ -0,0 +1,38 @@
<?php
declare(strict_types = 1);
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220628180435 extends AbstractMigrationChamilo
{
/**
* Return desription of the migration step.
*
* @return string
*/
public function getDescription(): string
{
return 'track login record';
}
public function up(Schema $schema): void
{
if (false === $schema->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
}
}

@ -0,0 +1,80 @@
<?php
declare(strict_types = 1);
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220909165130 extends AbstractMigrationChamilo
{
/**
* Return desription of the migration step.
*
* @return string
*/
public function getDescription(): string
{
return 'change field user_ip length 45 characters';
}
public function up(Schema $schema): void
{
$this->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;'
);
}
}

@ -0,0 +1,25 @@
<?php
declare(strict_types = 1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Repository;
use Chamilo\CoreBundle\Entity\TrackELoginRecord;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
final class TrackELoginRecordRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, TrackELoginRecord::class);
}
public function create(TrackELoginRecord $trackELoginRecord): void
{
$this->getEntityManager()->persist($trackELoginRecord);
$this->getEntityManager()->flush();
}
}

@ -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;

Loading…
Cancel
Save