Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Authentication] fixes known ip deletion #2835

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/main/app/Controller/RequestDecoderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

trait RequestDecoderTrait
{
/**
* @return mixed|null
*/
protected function decodeRequest(Request $request)
protected function decodeRequest(Request $request): mixed
{
$decodedRequest = null;
if (!empty($request->getContent())) {
Expand All @@ -24,7 +21,7 @@ protected function decodeRequest(Request $request)
return $decodedRequest;
}

protected function decodeIdsString(Request $request, string $class, string $property = 'ids')
protected function decodeIdsString(Request $request, string $class, string $property = 'ids'): array
{
$ids = $request->query->all($property) ?? [];
if (empty($ids)) {
Expand Down
29 changes: 16 additions & 13 deletions src/main/authentication/Entity/IpUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Claroline\AuthenticationBundle\Entity;

use Claroline\AppBundle\Entity\Identifier\Id;
use Claroline\AppBundle\Entity\Identifier\Uuid;
use Claroline\AppBundle\Entity\Restriction\Locked;
use Claroline\CoreBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
Expand All @@ -12,43 +13,45 @@
* Used with the IpAuthenticator.
*
* @ORM\Table(name="claro_ip_user")
*
* @ORM\Entity
*/
class IpUser
{
use Id;
use Uuid;
use Locked;

/**
* @ORM\Column(type="string", nullable=false, unique=true)
*
* @var string
*/
private $ip;
private ?string $ip = null;

/**
* If true, the $ip field contains to ips separated by a , to define the range.
*
* @ORM\Column(name="is_range", type="boolean")
*
* @var bool
*/
private $range = false;
private bool $range = false;

/**
* @ORM\ManyToOne(targetEntity="Claroline\CoreBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*
* @var User
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $user;
private ?User $user = null;

public function __construct()
{
$this->refreshUuid();
}

public function getIp(): ?string
{
return $this->ip;
}

public function setIp(string $ip)
public function setIp(string $ip): void
{
$this->ip = $ip;
}
Expand All @@ -58,7 +61,7 @@ public function isRange(): bool
return $this->range;
}

public function setRange(bool $range)
public function setRange(bool $range): void
{
$this->range = $range;
}
Expand All @@ -68,12 +71,12 @@ public function getUser(): ?User
return $this->user;
}

public function setUser(User $user)
public function setUser(User $user): void
{
$this->user = $user;
}

public function inRange(string $ip)
public function inRange(string $ip): bool
{
if ($this->range) {
$range = explode(',', $this->ip);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Claroline\AuthenticationBundle\Installation\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated migration based on mapping information: modify it with caution.
*
* Generation date: 2024/08/26 11:03:14
*/
final class Version20240826110203 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('
ALTER TABLE claro_ip_user
ADD uuid VARCHAR(36) NOT NULL
');

$this->addSql('
UPDATE claro_ip_user SET uuid = (SELECT UUID())
');

$this->addSql('
CREATE UNIQUE INDEX UNIQ_FEB73761D17F50A6 ON claro_ip_user (uuid)
');
}

public function down(Schema $schema): void
{
$this->addSql('
DROP INDEX UNIQ_FEB73761D17F50A6 ON claro_ip_user
');
$this->addSql('
ALTER TABLE claro_ip_user
DROP uuid
');
}
}
27 changes: 13 additions & 14 deletions src/main/authentication/Serializer/IpUserSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Claroline\AuthenticationBundle\Serializer;

use Claroline\AppBundle\API\Options;
use Claroline\AppBundle\API\Serializer\SerializerInterface;
use Claroline\AppBundle\API\Serializer\SerializerTrait;
use Claroline\AppBundle\Persistence\ObjectManager;
use Claroline\AuthenticationBundle\Entity\IpUser;
Expand All @@ -13,39 +13,38 @@ class IpUserSerializer
{
use SerializerTrait;

/** @var ObjectManager */
private $om;
/** @var UserSerializer */
private $userSerializer;

public function __construct(
ObjectManager $om,
UserSerializer $userSerializer
private readonly ObjectManager $om,
private readonly UserSerializer $userSerializer
) {
$this->om = $om;
$this->userSerializer = $userSerializer;
}

public function getClass()
public function getClass(): string
{
return IpUser::class;
}

public function serialize(IpUser $object): array
{
return [
'id' => $object->getId(),
'id' => $object->getUuid(),
'ip' => $object->isRange() ? explode(',', $object->getIp()) : $object->getIp(),
'range' => $object->isRange(),
'user' => $this->userSerializer->serialize($object->getUser(), [Options::SERIALIZE_MINIMAL]),
'user' => $this->userSerializer->serialize($object->getUser(), [SerializerInterface::SERIALIZE_MINIMAL]),
'restrictions' => [
'locked' => $object->isLocked(),
],
];
}

public function deserialize(array $data, IpUser $object): IpUser
public function deserialize(array $data, IpUser $object, ?array $options = []): IpUser
{
if (!in_array(SerializerInterface::REFRESH_UUID, $options)) {
$this->sipe('id', 'setUuid', $data, $object);
} else {
$object->refreshUuid();
}

if (isset($data['ip'])) {
$object->setIp(is_array($data['ip']) ? implode(',', $data['ip']) : $data['ip']);
}
Expand Down
Loading