parent
faac2a098d
commit
876d1d2919
@ -0,0 +1,66 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Admin; |
||||
|
||||
use Sonata\AdminBundle\Admin\Admin; |
||||
use Sonata\AdminBundle\Form\FormMapper; |
||||
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
||||
use Sonata\AdminBundle\Datagrid\ListMapper; |
||||
|
||||
/** |
||||
* Class ToolAdmin |
||||
* @package Chamilo\CoreBundle\Admin |
||||
*/ |
||||
class ToolAdmin extends Admin |
||||
{ |
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
protected function configureFormFields(FormMapper $formMapper) |
||||
{ |
||||
$formMapper |
||||
->add('name') |
||||
->add('description', 'ckeditor') |
||||
->add('toolResourceRights', 'sonata_type_collection', array( |
||||
'cascade_validation' => true, |
||||
), array( |
||||
//'allow_delete' => true, |
||||
//'by_reference' => false, |
||||
'edit' => 'inline', |
||||
'inline' => 'table', |
||||
//'btn_add' => true, |
||||
//'multiple' => true |
||||
//'sortable' => 'position', |
||||
//'link_parameters' => array('content' => $users), |
||||
'admin_code' => 'sonata.admin.tool_resource_rights' |
||||
) |
||||
) |
||||
/*->add('image', 'sonata_media_type', array( |
||||
'provider' => 'sonata.media.provider.image', |
||||
'context' => 'default' |
||||
));*/ |
||||
; |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
protected function configureDatagridFilters(DatagridMapper $datagridMapper) |
||||
{ |
||||
$datagridMapper |
||||
->add('name') |
||||
; |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
protected function configureListFields(ListMapper $listMapper) |
||||
{ |
||||
$listMapper |
||||
->addIdentifier('id') |
||||
->addIdentifier('name') |
||||
; |
||||
} |
||||
} |
||||
@ -0,0 +1,60 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Admin; |
||||
|
||||
use Chamilo\CoreBundle\Entity\ToolResourceRights; |
||||
use Sonata\AdminBundle\Admin\Admin; |
||||
use Sonata\AdminBundle\Form\FormMapper; |
||||
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
||||
use Sonata\AdminBundle\Datagrid\ListMapper; |
||||
use Sonata\AdminBundle\Show\ShowMapper; |
||||
|
||||
/** |
||||
* Class ToolResourceRightsAdmin |
||||
* @package Chamilo\CoreBundle\Admin |
||||
*/ |
||||
class ToolResourceRightsAdmin extends Admin |
||||
{ |
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
protected function configureFormFields(FormMapper $formMapper) |
||||
{ |
||||
$formMapper |
||||
->add('tool') |
||||
->add( |
||||
'role', |
||||
'choice', |
||||
array('choices' => ToolResourceRights::getDefaultRoles()) |
||||
) |
||||
->add( |
||||
'mask', |
||||
'choice', |
||||
array('choices' => ToolResourceRights::getMaskList()) |
||||
) |
||||
; |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
protected function configureDatagridFilters(DatagridMapper $datagridMapper) |
||||
{ |
||||
$datagridMapper |
||||
->add('role') |
||||
; |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
protected function configureListFields(ListMapper $listMapper) |
||||
{ |
||||
$listMapper |
||||
->addIdentifier('id') |
||||
->addIdentifier('role') |
||||
->addIdentifier('mask') |
||||
; |
||||
} |
||||
} |
||||
@ -0,0 +1,109 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Entity\Resource; |
||||
|
||||
use Chamilo\CourseBundle\Entity\CGroupInfo; |
||||
use Doctrine\ORM\Mapping as ORM; |
||||
use Gedmo\Mapping\Annotation as Gedmo; |
||||
use Chamilo\UserBundle\Entity\User; |
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
|
||||
/** |
||||
* @ORM\Entity |
||||
* @ORM\Table(name="resource_rights") |
||||
*/ |
||||
class ResourceRights |
||||
{ |
||||
/** |
||||
* @ORM\Id |
||||
* @ORM\Column(type="integer") |
||||
* @ORM\GeneratedValue(strategy="AUTO") |
||||
*/ |
||||
protected $id; |
||||
|
||||
/** |
||||
* @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink") |
||||
* @ORM\JoinColumn(name="resource_link_id", referencedColumnName="id") |
||||
*/ |
||||
protected $resourceLink; |
||||
|
||||
/** |
||||
* @var string |
||||
* |
||||
* @ORM\Column(name="role", type="string", length=255, nullable=false) |
||||
*/ |
||||
protected $role; |
||||
|
||||
/** |
||||
* @var string |
||||
* |
||||
* @ORM\Column(name="mask", type="integer", nullable=false) |
||||
*/ |
||||
protected $mask; |
||||
|
||||
/** |
||||
* @return int |
||||
*/ |
||||
public function getId() |
||||
{ |
||||
return $this->id; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getMask() |
||||
{ |
||||
return $this->mask; |
||||
} |
||||
|
||||
/** |
||||
* @param string $mask |
||||
*/ |
||||
public function setMask($mask) |
||||
{ |
||||
$this->mask = $mask; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getResourceLink() |
||||
{ |
||||
return $this->resourceLink; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $resourceLink |
||||
*/ |
||||
public function setResourceLink($resourceLink) |
||||
{ |
||||
$this->resourceLink = $resourceLink; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getRole() |
||||
{ |
||||
return $this->role; |
||||
} |
||||
|
||||
/** |
||||
* @param string $role |
||||
*/ |
||||
public function setRole($role) |
||||
{ |
||||
$this->role = $role; |
||||
} |
||||
|
||||
/** |
||||
* @param int $id |
||||
*/ |
||||
public function setId($id) |
||||
{ |
||||
$this->id = $id; |
||||
} |
||||
} |
||||
@ -0,0 +1,171 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Entity; |
||||
|
||||
use Doctrine\ORM\Mapping as ORM; |
||||
use Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap; |
||||
use Symfony\Component\Security\Acl\Permission\BasicPermissionMap; |
||||
use Symfony\Component\Security\Acl\Permission\MaskBuilder; |
||||
|
||||
/** |
||||
* Tool |
||||
* |
||||
* @ORM\Table(name="tool_resource_rights") |
||||
* @ORM\Entity |
||||
*/ |
||||
class ToolResourceRights |
||||
{ |
||||
/** |
||||
* @var integer |
||||
* |
||||
* @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false) |
||||
* @ORM\Id |
||||
* @ORM\GeneratedValue(strategy="AUTO") |
||||
*/ |
||||
private $id; |
||||
|
||||
/** |
||||
* @var string |
||||
* |
||||
* @ORM\Column(name="role", type="string", length=255, nullable=false) |
||||
*/ |
||||
protected $role; |
||||
|
||||
/** |
||||
* @var string |
||||
* |
||||
* @ORM\Column(name="mask", type="integer", nullable=false) |
||||
*/ |
||||
protected $mask; |
||||
|
||||
/** |
||||
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Tool", inversedBy="toolResourceRights", cascade={"persist"}) |
||||
* @ORM\JoinColumn(name="tool_id", referencedColumnName="id") |
||||
**/ |
||||
protected $tool; |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function __toString() |
||||
{ |
||||
return (string)$this->getMask(); |
||||
} |
||||
|
||||
/** |
||||
* @return Tool |
||||
*/ |
||||
public function getTool() |
||||
{ |
||||
return $this->tool; |
||||
} |
||||
|
||||
/** |
||||
* @param Tool $tool |
||||
* |
||||
* @return $this |
||||
*/ |
||||
public function setTool($tool) |
||||
{ |
||||
$this->tool = $tool; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getRole() |
||||
{ |
||||
return $this->role; |
||||
} |
||||
|
||||
/** |
||||
* @param string $role |
||||
* |
||||
* @return $this |
||||
*/ |
||||
public function setRole($role) |
||||
{ |
||||
$this->role = $role; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @return int |
||||
*/ |
||||
public function getMask() |
||||
{ |
||||
return $this->mask; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $mask |
||||
* |
||||
* @return $this |
||||
*/ |
||||
public function setMask($mask) |
||||
{ |
||||
$this->mask = $mask; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* Get id |
||||
* |
||||
* @return integer |
||||
*/ |
||||
public function getId() |
||||
{ |
||||
return $this->id; |
||||
} |
||||
|
||||
/** |
||||
* @return array |
||||
*/ |
||||
public static function getDefaultRoles() |
||||
{ |
||||
return array( |
||||
'ROLE_STUDENT' => 'student', |
||||
'ROLE_TEACHER' => 'teacher' |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @return array |
||||
*/ |
||||
public static function getMaskList() |
||||
{ |
||||
$builder = new MaskBuilder(); |
||||
$builder |
||||
->add('view') |
||||
->add('edit') |
||||
; |
||||
|
||||
$readerMask = $builder->get(); |
||||
|
||||
$builder = new MaskBuilder(); |
||||
$builder |
||||
->add('view') |
||||
->add('edit') |
||||
; |
||||
$editorMask = $builder->get(); |
||||
|
||||
$builder = new MaskBuilder(); |
||||
$builder |
||||
->add('view') |
||||
->add('edit') |
||||
->add('delete') |
||||
; |
||||
$ownerMask = $builder->get(); |
||||
|
||||
return array( |
||||
$readerMask => 'reader', |
||||
$editorMask => 'editor', |
||||
$ownerMask => 'owner' |
||||
); |
||||
} |
||||
} |
||||
@ -0,0 +1,98 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Security\Authorization\Voter; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Resource\ResourceLink; |
||||
use Chamilo\CoreBundle\Entity\Resource\ResourceRights; |
||||
use Chamilo\CoreBundle\Entity\ToolResourceRights; |
||||
use Symfony\Component\Security\Acl\Permission\MaskBuilder; |
||||
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter; |
||||
use Symfony\Component\Security\Core\User\UserInterface; |
||||
|
||||
/** |
||||
* Class ResourceVoter |
||||
* @package Chamilo\CoreBundle\Security\Authorization\Voter |
||||
*/ |
||||
class ResourceLinkVoter extends AbstractVoter |
||||
{ |
||||
const VIEW = 'view'; |
||||
const EDIT = 'edit'; |
||||
const DELETE = 'delete'; |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function getSupportedAttributes() |
||||
{ |
||||
return array(self::VIEW, self::EDIT, self::DELETE); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function getSupportedClasses() |
||||
{ |
||||
return array('Chamilo\CoreBundle\Entity\Resource\ResourceLink'); |
||||
} |
||||
|
||||
/** |
||||
* @param string $attribute |
||||
* @param ResourceLink $resourceLink |
||||
* @param null $user |
||||
* . |
||||
* @return bool |
||||
*/ |
||||
protected function isGranted($attribute, $resourceLink, $user = null) |
||||
{ |
||||
// make sure there is a user object (i.e. that the user is logged in) |
||||
if (!$user instanceof UserInterface) { |
||||
return false; |
||||
} |
||||
|
||||
$userSent = $resourceLink->getUser(); |
||||
|
||||
// Owner. |
||||
if (isset($userSent) && |
||||
$user->getUsername() == $userSent->getUsername()) { |
||||
return true; |
||||
} |
||||
|
||||
$rightFromResourceLink = $resourceLink->getRights(); |
||||
|
||||
if ($rightFromResourceLink->count()) { |
||||
/** @var ResourceRights $right */ |
||||
$rights = $rightFromResourceLink; |
||||
} else { |
||||
$rights = $resourceLink->getResourceNode()->getTool()->getToolResourceRights(); |
||||
} |
||||
|
||||
$roles = array(); |
||||
foreach ($rights as $right) { |
||||
$roles[$right->getRole()] = $right->getMask() ; |
||||
} |
||||
|
||||
$mask = new MaskBuilder(); |
||||
$mask->add($attribute); |
||||
$code = $mask->get(); |
||||
|
||||
switch ($attribute) { |
||||
case self::VIEW: |
||||
|
||||
if ($user->getRoles()) |
||||
var_dump($code); |
||||
exit; |
||||
break; |
||||
case self::EDIT: |
||||
break; |
||||
} |
||||
|
||||
// Course is visible? |
||||
if ($attribute == self::VIEW) { |
||||
return true; |
||||
} |
||||
|
||||
|
||||
return false; |
||||
} |
||||
} |
||||
@ -0,0 +1,74 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Security\Authorization\Voter; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Resource\ResourceNode; |
||||
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter; |
||||
use Symfony\Component\Security\Core\User\UserInterface; |
||||
|
||||
/** |
||||
* Class ResourceVoter |
||||
* @package Chamilo\CoreBundle\Security\Authorization\Voter |
||||
*/ |
||||
class ResourceVoter extends AbstractVoter |
||||
{ |
||||
const VIEW = 'view'; |
||||
const EDIT = 'edit'; |
||||
const DELETE = 'delete'; |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function getSupportedAttributes() |
||||
{ |
||||
return array(self::VIEW, self::EDIT, self::DELETE); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function getSupportedClasses() |
||||
{ |
||||
return array('Chamilo\CoreBundle\Entity\Resource\ResourceNode'); |
||||
} |
||||
|
||||
/** |
||||
* @param string $attribute |
||||
* @param ResourceNode $resourceNode |
||||
* @param null $user |
||||
* @return bool |
||||
*/ |
||||
protected function isGranted($attribute, $resourceNode, $user = null) |
||||
{ |
||||
// make sure there is a user object (i.e. that the user is logged in) |
||||
if (!$user instanceof UserInterface) { |
||||
return false; |
||||
} |
||||
|
||||
// Owner. |
||||
if ($user->getUsername() == $resourceNode->getCreator()->getUsername()) { |
||||
return true; |
||||
} |
||||
|
||||
switch ($attribute) { |
||||
case self::VIEW: |
||||
|
||||
break; |
||||
case self::EDIT: |
||||
break; |
||||
} |
||||
|
||||
// Course is visible? |
||||
if ($attribute == self::VIEW && $resourceNode->isActive()) { |
||||
return true; |
||||
} |
||||
|
||||
// Teacher |
||||
if ($attribute == self::EDIT && $user->getId() === $course->getOwner()->getId()) { |
||||
return true; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue