classes/XLite/Model/Role.php line 11

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XLite\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * Role
  10.  *
  11.  * @ORM\Entity
  12.  * @ORM\Table  (name="roles")
  13.  */
  14. class Role extends \XLite\Model\Base\I18n
  15. {
  16.     /**
  17.      * ID
  18.      *
  19.      * @var integer
  20.      *
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue (strategy="AUTO")
  23.      * @ORM\Column         (type="integer", options={ "unsigned": true })
  24.      */
  25.     protected $id;
  26.     /**
  27.      * Permissions
  28.      *
  29.      * @var \Doctrine\Common\Collections\Collection
  30.      *
  31.      * @ORM\ManyToMany (targetEntity="XLite\Model\Role\Permission", mappedBy="roles", cascade={"merge","detach","persist"})
  32.      */
  33.     protected $permissions;
  34.     /**
  35.      * Profiles
  36.      *
  37.      * @var \Doctrine\Common\Collections\Collection
  38.      *
  39.      * @ORM\ManyToMany (targetEntity="XLite\Model\Profile", inversedBy="roles")
  40.      * @ORM\JoinTable (
  41.      *      name="profile_roles",
  42.      *      joinColumns={@ORM\JoinColumn (name="role_id", referencedColumnName="id", onDelete="CASCADE")},
  43.      *      inverseJoinColumns={@ORM\JoinColumn (name="profile_id", referencedColumnName="profile_id", onDelete="CASCADE")}
  44.      * )
  45.      */
  46.     protected $profiles;
  47.     /**
  48.      * @var \Doctrine\Common\Collections\Collection
  49.      *
  50.      * @ORM\OneToMany (targetEntity="XLite\Model\RoleTranslation", mappedBy="owner", cascade={"all"})
  51.      */
  52.     protected $translations;
  53.     /**
  54.      * Constructor
  55.      *
  56.      * @param array $data Entity properties OPTIONAL
  57.      */
  58.     public function __construct(array $data = [])
  59.     {
  60.         $this->permissions = new \Doctrine\Common\Collections\ArrayCollection();
  61.         $this->profiles    = new \Doctrine\Common\Collections\ArrayCollection();
  62.         parent::__construct($data);
  63.     }
  64.     /**
  65.      * Get public name
  66.      *
  67.      * @return string
  68.      */
  69.     public function getPublicName()
  70.     {
  71.         return $this->getName();
  72.     }
  73.     /**
  74.      * Check - specified permission is allowed or not
  75.      *
  76.      * @param string $code Permission code
  77.      *
  78.      * @return boolean
  79.      */
  80.     public function isPermissionAllowed($code)
  81.     {
  82.         $result false;
  83.         foreach ($this->getPermissions() as $permission) {
  84.             if ($permission->isAllowed($code)) {
  85.                 $result true;
  86.                 break;
  87.             }
  88.         }
  89.         return $result;
  90.     }
  91.     /**
  92.      * Check - specified permission (only one from list) is allowed
  93.      *
  94.      * @param string|array $code Permission code(s)
  95.      *
  96.      * @return boolean
  97.      */
  98.     public function isPermissionAllowedOr($code)
  99.     {
  100.         $result false;
  101.         $list = [];
  102.         foreach (func_get_args() as $code) {
  103.             if (is_array($code)) {
  104.                 foreach ($code as $permissionCode) {
  105.                     $list[] = $permissionCode;
  106.                 }
  107.             } else {
  108.                 $list[] = $code;
  109.             }
  110.         }
  111.         foreach ($list as $code) {
  112.             if ($this->isPermissionAllowed($code)) {
  113.                 $result true;
  114.                 break;
  115.             }
  116.         }
  117.         return $result;
  118.     }
  119.     /**
  120.      * Get id
  121.      *
  122.      * @return integer
  123.      */
  124.     public function getId()
  125.     {
  126.         return $this->id;
  127.     }
  128.     /**
  129.      * Add permissions
  130.      *
  131.      * @param \XLite\Model\Role\Permission $permissions
  132.      * @return Role
  133.      */
  134.     public function addPermissions(\XLite\Model\Role\Permission $permissions)
  135.     {
  136.         $this->permissions[] = $permissions;
  137.         return $this;
  138.     }
  139.     /**
  140.      * Get permissions
  141.      *
  142.      * @return \Doctrine\Common\Collections\Collection
  143.      */
  144.     public function getPermissions()
  145.     {
  146.         return $this->permissions;
  147.     }
  148.     /**
  149.      * Add profiles
  150.      *
  151.      * @param \XLite\Model\Profile $profiles
  152.      * @return Role
  153.      */
  154.     public function addProfiles(\XLite\Model\Profile $profiles)
  155.     {
  156.         $this->profiles[] = $profiles;
  157.         return $this;
  158.     }
  159.     /**
  160.      * Get profiles
  161.      *
  162.      * @return \Doctrine\Common\Collections\Collection
  163.      */
  164.     public function getProfiles()
  165.     {
  166.         return $this->profiles;
  167.     }
  168. }