Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BeatSwitch lock doctrine example
<?php
//src/library/App/Entity/CallerPermission.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="lock_caller_permission")
*/
class CallerPermission
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="caller_type", type="string", length=100, nullable=false)
*/
protected $callerType;
/**
* @var string
*
* @ORM\Column(name="caller_id", type="integer", nullable=false)
*/
protected $callerId;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=50, nullable=false)
*/
protected $type;
/**
* @var string
*
* @ORM\Column(name="action", type="string", length=100, nullable=false)
*/
protected $action;
/**
* @var string
*
* @ORM\Column(name="resource_type", type="string", length=100, nullable=true)
*/
protected $resourceType;
/**
* @var string
*
* @ORM\Column(name="resource_id", type="integer", nullable=true)
*/
protected $resourceId;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set callerType
*
* @param string $callerType
* @return CallerPermission
*/
public function setCallerType($callerType)
{
$this->callerType = $callerType;
return $this;
}
/**
* Get callerType
*
* @return string
*/
public function getCallerType()
{
return $this->callerType;
}
/**
* Set callerId
*
* @param integer $callerId
* @return CallerPermission
*/
public function setCallerId($callerId)
{
$this->callerId = $callerId;
return $this;
}
/**
* Get callerId
*
* @return integer
*/
public function getCallerId()
{
return $this->callerId;
}
/**
* Set type
*
* @param string $type
* @return CallerPermission
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set action
*
* @param string $action
* @return CallerPermission
*/
public function setAction($action)
{
$this->action = $action;
return $this;
}
/**
* Get action
*
* @return string
*/
public function getAction()
{
return $this->action;
}
/**
* Set resourceType
*
* @param string $resourceType
* @return CallerPermission
*/
public function setResourceType($resourceType)
{
$this->resourceType = $resourceType;
return $this;
}
/**
* Get resourceType
*
* @return string
*/
public function getResourceType()
{
return $this->resourceType;
}
/**
* Set resourceId
*
* @param integer $resourceId
* @return CallerPermission
*/
public function setResourceId($resourceId)
{
$this->resourceId = $resourceId;
return $this;
}
/**
* Get resourceId
*
* @return integer
*/
public function getResourceId()
{
return $this->resourceId;
}
/**
* @return array
*/
public function toArray()
{
return [
'id' => $this->getId(),
'caller_type' => $this->getCallerType(),
'caller_id' => $this->getCallerId(),
'type' => $this->getType(),
'action' => $this->getAction(),
'resource' => $this->getResourceType(),
'resource_id' => $this->getResourceId()
];
}
}
<?php
//src/library/App/Lock/DoctrineDriver.php
namespace App\Lock;
use BeatSwitch\Lock\Callers\Caller;
use BeatSwitch\Lock\Drivers\Driver;
use BeatSwitch\Lock\Permissions\Permission;
use BeatSwitch\Lock\Permissions\PermissionFactory;
use BeatSwitch\Lock\Roles\Role;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
use App\Entity\CallerPermission;
use App\Entity\RolePermission;
/**
* Class DoctrineDriver
*/
class DoctrineDriver implements Driver
{
/**
* CallerPermission Entity
*/
const CALLER_ENTITY = '\App\Entity\CallerPermission';
/**
* RolePermission Entity
*/
const ROLE_ENTITY = '\App\Entity\RolePermission';
/**
* @var EntityManager
*/
protected $em;
/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* Returns all the permissions for a caller
*
* @param \BeatSwitch\Lock\Callers\Caller $caller
* @return \BeatSwitch\Lock\Permissions\Permission[]
*/
public function getCallerPermissions(Caller $caller)
{
$qb = $this->em->createQueryBuilder();
$qb->select('c')->from(self::CALLER_ENTITY, 'c');
$qb->where('c.callerType = :callerType')->setParameter('callerType', $caller->getCallerType());
$qb->andWhere('c.callerId = :callerId')->setParameter('callerId', $caller->getCallerId());
$permissions = $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
return PermissionFactory::createFromData($permissions);
}
/**
* Stores a new permission into the driver for a caller
*
* @param \BeatSwitch\Lock\Callers\Caller $caller
* @param \BeatSwitch\Lock\Permissions\Permission
* @return void
*/
public function storeCallerPermission(Caller $caller, Permission $permission)
{
$callerPermission = new CallerPermission();
$callerPermission->setCallerType($caller->getCallerType());
$callerPermission->setCallerId($caller->getCallerId());
$callerPermission->setType($permission->getType());
$callerPermission->setAction($permission->getAction());
$callerPermission->setResourceType($permission->getResourceType());
$callerPermission->setResourceId($permission->getResourceId());
$this->em->persist($callerPermission);
$this->em->flush();
}
/**
* Removes a permission from the driver for a caller
*
* @param \BeatSwitch\Lock\Callers\Caller $caller
* @param \BeatSwitch\Lock\Permissions\Permission
* @return void
*/
public function removeCallerPermission(Caller $caller, Permission $permission)
{
$permission = $this->em->getRepository(self::CALLER_ENTITY)->findOneBy([
'callerType' => $caller->getCallerType(),
'callerId' => $caller->getCallerId(),
'type' => $permission->getType(),
'action' => $permission->getAction(),
'resourceType' => $permission->getResourceType(),
'resourceId' => $permission->getResourceId()
]);
if ($permission) {
$this->em->remove($permission);
$this->em->flush();
}
}
/**
* Checks if a permission is stored for a user
*
* @param \BeatSwitch\Lock\Callers\Caller $caller
* @param \BeatSwitch\Lock\Permissions\Permission
* @return bool
*/
public function hasCallerPermission(Caller $caller, Permission $permission)
{
$permission = $this->em->getRepository(self::CALLER_ENTITY)->findOneBy([
'callerType' => $caller->getCallerType(),
'callerId' => $caller->getCallerId(),
'type' => $permission->getType(),
'action' => $permission->getAction(),
'resourceType' => $permission->getResourceType(),
'resourceId' => $permission->getResourceId()
]);
return ($permission) ? true : false;
}
/**
* Returns all the permissions for a role
*
* @param Role $role
* @return \BeatSwitch\Lock\Permissions\Permission[]
*/
public function getRolePermissions(Role $role)
{
$qb = $this->em->createQueryBuilder();
$qb->select('r')->from(self::ROLE_ENTITY, 'r');
$qb->where('r.role = :role')->setParameter('role', $role->getRoleName());
$permissions = $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
return PermissionFactory::createFromData($permissions);
}
/**
* Stores a new permission for a role
*
* @param Role $role
* @param \BeatSwitch\Lock\Permissions\Permission
* @return void
*/
public function storeRolePermission(Role $role, Permission $permission)
{
$rolePermission = new RolePermission();
$rolePermission->setRole($role->getRoleName());
$rolePermission->setType($permission->getType());
$rolePermission->setAction($permission->getAction());
$rolePermission->setResourceType($permission->getResourceType());
$rolePermission->setResourceId($permission->getResourceId());
$this->em->persist($rolePermission);
$this->em->flush();
}
/**
* Removes a permission for a role
*
* @param Role $role
* @param \BeatSwitch\Lock\Permissions\Permission
* @return void
*/
public function removeRolePermission(Role $role, Permission $permission)
{
$permission = $this->em->getRepository(self::ROLE_ENTITY)->findOneBy([
'role' => $role->getRoleName(),
'type' => $permission->getType(),
'action' => $permission->getAction(),
'resourceType' => $permission->getResourceType(),
'resourceId' => $permission->getResourceId()
]);
if ($permission) {
$this->em->remove($permission);
$this->em->flush();
}
}
/**
* Checks if a permission is stored for a role
*
* @param Role $role
* @param \BeatSwitch\Lock\Permissions\Permission
* @return bool
*/
public function hasRolePermission(Role $role, Permission $permission)
{
$permission = $this->em->getRepository(self::ROLE_ENTITY)->findOneBy([
'role' => $role->getRoleName(),
'type' => $permission->getType(),
'action' => $permission->getAction(),
'resourceType' => $permission->getResourceType(),
'resourceId' => $permission->getResourceId()
]);
return ($permission) ? true : false;
}
}
<?php
//tests/App/Lock/DoctrineDriverTest.php
namespace App\Lock;
use BeatSwitch\Lock\Tests\PersistentDriverTestCase;
use App\Lock\DoctrineDriver;
/**
* Class DoctrineDriverTest
*/
class DoctrineDriverTest extends PersistentDriverTestCase
{
public function setUp()
{
//Entity Manager
$em = \App\Resource::getEntityManager();
// Don't forget to reset your DB here.
// Bootstrap driver.
$this->driver = new DoctrineDriver($em);
parent::setUp();
}
}
CREATE TABLE lock_caller_permission (
id INT AUTO_INCREMENT NOT NULL,
caller_type VARCHAR(100) NOT NULL,
caller_id INT NOT NULL,
type VARCHAR(50) NOT NULL,
action VARCHAR(100) NOT NULL,
resource_type VARCHAR(100) DEFAULT NULL,
resource_id INT DEFAULT NULL,
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE lock_role_permission (
id INT AUTO_INCREMENT NOT NULL,
role VARCHAR(100) NOT NULL,
type VARCHAR(50) NOT NULL,
action VARCHAR(100) NOT NULL,
resource_type VARCHAR(100) DEFAULT NULL,
resource_id INT DEFAULT NULL,
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
<?php
$driver = new \App\Lock\DoctrineDriver($em);
// Init the lock manager.
$manager = new \BeatSwitch\Lock\Manager($driver);
// Init the caller.
$caller = new \BeatSwitch\Lock\Callers\SimpleCaller('users', 1, ['editor']);
/** @var \BeatSwitch\Lock\Callers\CallerLock $lock */
$lock = $manager->caller($caller);
$lock->allow('create');
$permissions = $driver->getCallerPermissions($caller);
<?php
//src/library/App/Entity/RolePermission.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="lock_role_permission")
*/
class RolePermission
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="role", type="string", length=100, nullable=false)
*/
protected $role;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=50, nullable=false)
*/
protected $type;
/**
* @var string
*
* @ORM\Column(name="action", type="string", length=100, nullable=false)
*/
protected $action;
/**
* @var string
*
* @ORM\Column(name="resource_type", type="string", length=100, nullable=true)
*/
protected $resourceType;
/**
* @var string
*
* @ORM\Column(name="resource_id", type="integer", nullable=true)
*/
protected $resourceId;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set role
*
* @param string $role
* @return RolePermission
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return string
*/
public function getRole()
{
return $this->role;
}
/**
* Set type
*
* @param string $type
* @return RolePermission
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set action
*
* @param string $action
* @return RolePermission
*/
public function setAction($action)
{
$this->action = $action;
return $this;
}
/**
* Get action
*
* @return string
*/
public function getAction()
{
return $this->action;
}
/**
* Set resourceType
*
* @param string $resourceType
* @return RolePermission
*/
public function setResourceType($resourceType)
{
$this->resourceType = $resourceType;
return $this;
}
/**
* Get resourceType
*
* @return string
*/
public function getResourceType()
{
return $this->resourceType;
}
/**
* Set resourceId
*
* @param integer $resourceId
* @return RolePermission
*/
public function setResourceId($resourceId)
{
$this->resourceId = $resourceId;
return $this;
}
/**
* Get resourceId
*
* @return integer
*/
public function getResourceId()
{
return $this->resourceId;
}
/**
* @return array
*/
public function toArray()
{
return [
'id' => $this->getId(),
'role' => $this->getRole(),
'type' => $this->getType(),
'action' => $this->getAction(),
'resource' => $this->getResourceType(),
'resource_id' => $this->getResourceId()
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment