Skip to content

Commit

Permalink
[Authentication] enhances api tokens management
Browse files Browse the repository at this point in the history
  • Loading branch information
Elorfin committed Mar 9, 2021
1 parent 1e28454 commit a18c94b
Show file tree
Hide file tree
Showing 27 changed files with 388 additions and 277 deletions.
25 changes: 25 additions & 0 deletions src/main/app/Entity/Restriction/Locked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Claroline\AppBundle\Entity\Restriction;

use Doctrine\ORM\Mapping as ORM;

trait Locked
{
/**
* @ORM\Column(name="is_locked", type="boolean", options={"default" = 0})
*
* @var bool
*/
protected $locked = false;

public function isLocked(): bool
{
return $this->locked;
}

public function setLocked(bool $locked)
{
$this->locked = $locked;
}
}
71 changes: 11 additions & 60 deletions src/main/authentication/Entity/ApiToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Claroline\AuthenticationBundle\Entity;

use Claroline\AppBundle\Entity\Identifier\Id;
use Claroline\AppBundle\Entity\Identifier\Uuid;
use Claroline\AppBundle\Entity\Meta\Description;
use Claroline\AppBundle\Entity\Restriction\Locked;
use Claroline\CoreBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;

Expand All @@ -21,15 +24,11 @@
*/
class ApiToken
{
use Id;
use Description;
use Locked;
use Uuid;

/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\ManyToOne(targetEntity="Claroline\CoreBundle\Entity\User")
*/
Expand All @@ -40,77 +39,29 @@ class ApiToken
*/
private $token;

/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;

public function __construct()
{
$this->refreshUuid();
$this->token = mb_substr(bin2hex(openssl_random_pseudo_bytes(36)), 0, 36);
}

/**
* Get the value of Id.
*
* @return mixed
*/
public function getId()
{
return $this->id;
}

/**
* Set the value of Id.
*
* @param mixed id
*
* @return self
*/
public function setId($id)
{
$this->id = $id;

return $this;
}

/**
* Get the value of User.
*
* @return User
*/
public function getUser()
public function getUser(): ?User
{
return $this->user;
}

/**
* Set the value of User.
*
* @param User user
*
* @return self
*/
public function setUser($user)
public function setUser(User $user)
{
$this->user = $user;

return $this;
}

public function getToken()
public function getToken(): string
{
return $this->token;
}

public function setDescription($description)
{
$this->description = $description;
}

public function getDescription()
public function setToken(string $token)
{
return $this->description;
$this->token = $token;
}
}
22 changes: 2 additions & 20 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\Restriction\Locked;
use Claroline\CoreBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;

Expand All @@ -16,6 +17,7 @@
class IpUser
{
use Id;
use Locked;

/**
* @ORM\Column(type="string", nullable=false, unique=true)
Expand All @@ -41,16 +43,6 @@ class IpUser
*/
private $user;

/**
* Disallow edition/deletion from application.
* Useful to declare a third party app without worrying about a user deleting it.
*
* @ORM\Column(name="is_locked", type="boolean")
*
* @var bool
*/
private $locked = false;

public function getIp(): ?string
{
return $this->ip;
Expand Down Expand Up @@ -81,16 +73,6 @@ public function setUser(User $user)
$this->user = $user;
}

public function isLocked(): bool
{
return $this->locked;
}

public function setLocked(bool $locked)
{
$this->locked = $locked;
}

public function inRange(string $ip)
{
if ($this->range) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Claroline\AuthenticationBundle\Installation\Migrations\pdo_mysql;

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

/**
* Auto-generated migration based on mapping information: modify it with caution.
*
* Generation date: 2021/03/09 08:07:05
*/
class Version20210309080703 extends AbstractMigration
{
public function up(Schema $schema)
{
$this->addSql('
ALTER TABLE claro_api_token
ADD is_locked TINYINT(1) DEFAULT "0" NOT NULL
');
$this->addSql('
ALTER TABLE claro_ip_user CHANGE is_locked is_locked TINYINT(1) DEFAULT "0" NOT NULL
');
}

public function down(Schema $schema)
{
$this->addSql('
ALTER TABLE claro_api_token
DROP is_locked
');
$this->addSql('
ALTER TABLE claro_ip_user CHANGE is_locked is_locked TINYINT(1) NOT NULL
');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
Claroline\AuthenticationBundle\Serializer\ApiTokenSerializer:
tags: [ claroline.serializer ]
arguments:
- '@security.authorization_checker'
- '@Claroline\CoreBundle\API\Serializer\User\UserSerializer'
- '@Claroline\AppBundle\Persistence\ObjectManager'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const IpsTool = props =>
type: MODAL_BUTTON,
icon: 'fa fa-plus',
label: trans('add_ip', {}, 'security'),
target: `${props.path}/tokens/form`,
primary: true,
modal: [MODAL_IP_PARAMETERS, {
onSave: () => props.invalidateList()
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit a18c94b

Please # to comment.