Skip to content

Commit

Permalink
[AdminBundle] Symfony 5 role incompatibility fix
Browse files Browse the repository at this point in the history
  • Loading branch information
acrobat committed Oct 6, 2021
1 parent f23fde0 commit 61c6f9a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 66 deletions.
7 changes: 7 additions & 0 deletions UPGRADE-6.1.md
@@ -0,0 +1,7 @@
AdminBundle
-----------

* The `Kunstmaan\AdminBundle\Entity\Role` class doesn't exents from the deprecated `Symfony\Component\Security\Core\Role\Role`
class if you run your code on symfony 5. The Role class was deprecated and removed in symfony 5. If you used this class
to check the `Role` entity change it to the `Kunstmaan\AdminBundle\Entity\Role` class.
The `Role` entity won't change if you run on symfony 3.4 but it's adviced to make this change already.
77 changes: 13 additions & 64 deletions src/Kunstmaan/AdminBundle/Entity/Role.php
Expand Up @@ -5,77 +5,26 @@
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\Role\Role as BaseRole;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
* @ORM\Table( name="kuma_roles" )
* @UniqueEntity("role")
*/
class Role extends BaseRole
{
// NEXT_MAJOR Remove the RolePropertiesTrait when symfony 4 support is removed
if (class_exists(BaseRole::class)) {
/**
* @ORM\Id
* @ORM\Column(type="integer", name="id")
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Entity
* @ORM\Table( name="kuma_roles" )
* @UniqueEntity("role")
*/
protected $id;

/**
* @Assert\NotBlank()
* @ORM\Column(type="string", name="role", unique=true, length=70)
*/
protected $role;

/**
* Populate the role field
*
* @param string $role
*/
public function __construct($role)
class Role extends BaseRole
{
$this->role = $role;
use RolePropertiesTrait;
}

/**
* Return the role field.
*
* @return string
*/
public function getRole()
{
return $this->role;
}

} else {
/**
* Return the string representation of the role entity.
* @ORM\Entity
* @ORM\Table( name="kuma_roles" )
* @UniqueEntity("role")
*/
public function __toString(): string
class Role
{
return (string) $this->role;
}

/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* Modify the role field.
*
* @param string $role ROLE_FOO etc
*
* @return RoleInterface
*/
public function setRole($role)
{
$this->role = $role;

return $this;
use RolePropertiesTrait;
}
}
79 changes: 79 additions & 0 deletions src/Kunstmaan/AdminBundle/Entity/RolePropertiesTrait.php
@@ -0,0 +1,79 @@
<?php

namespace Kunstmaan\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @final
*
* @internal
*/
trait RolePropertiesTrait
{
/**
* @ORM\Id
* @ORM\Column(type="integer", name="id")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @Assert\NotBlank()
* @ORM\Column(type="string", name="role", unique=true, length=70)
*/
protected $role;

/**
* Populate the role field
*
* @param string $role
*/
public function __construct($role)
{
$this->role = $role;
}

/**
* Return the role field.
*
* @return string
*/
public function getRole()
{
return $this->role;
}

/**
* Return the string representation of the role entity.
*/
public function __toString(): string
{
return (string) $this->role;
}

/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* Modify the role field.
*
* @param string $role ROLE_FOO etc
*
* @return Role
*/
public function setRole($role)
{
$this->role = $role;

return $this;
}
}
Expand Up @@ -155,9 +155,11 @@ public function getPermissions()
*/
public function getPermission($role)
{
if ($role instanceof \Symfony\Component\Security\Core\Role\Role) {
// NEXT_MAJOR remove undefined classes from this check
if ($role instanceof \Symfony\Component\Security\Core\Role\Role || $role instanceof Role) {
$role = $role->getRole();
}

if (isset($this->permissions[$role])) {
return $this->permissions[$role];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kunstmaan/AdminBundle/composer.json
Expand Up @@ -27,7 +27,7 @@
"symfony/cache": "^4.4|^5.3",
"symfony/config": "^4.4|^5.3",
"symfony/console": "^4.4|^5.3",
"symfony/dependency-injection": "^4.4v",
"symfony/dependency-injection": "^4.4|^5.3",
"symfony/dom-crawler": "^4.4|^5.3",
"symfony/event-dispatcher": "^4.4|^5.3",
"symfony/filesystem": "^4.4|^5.3",
Expand Down

0 comments on commit 61c6f9a

Please sign in to comment.