Merge pull request #5180 from christianbeeznest/social-groups
Social: Improve group management and add image upload featurepull/5189/head
commit
9b0e6fc6df
@ -0,0 +1,73 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
namespace Chamilo\CoreBundle\Security\Authorization\Voter; |
||||||
|
|
||||||
|
use Chamilo\CoreBundle\Entity\Usergroup; |
||||||
|
use Chamilo\CoreBundle\Repository\Node\UsergroupRepository; |
||||||
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
||||||
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
||||||
|
use Symfony\Component\Security\Core\Security; |
||||||
|
use Symfony\Component\Security\Core\User\UserInterface; |
||||||
|
|
||||||
|
class UsergroupVoter extends Voter |
||||||
|
{ |
||||||
|
public const CREATE = 'CREATE'; |
||||||
|
public const VIEW = 'VIEW'; |
||||||
|
public const EDIT = 'EDIT'; |
||||||
|
public const DELETE = 'DELETE'; |
||||||
|
|
||||||
|
public function __construct( |
||||||
|
private Security $security, |
||||||
|
private UsergroupRepository $usergroupRepository |
||||||
|
) {} |
||||||
|
|
||||||
|
protected function supports(string $attribute, $subject): bool |
||||||
|
{ |
||||||
|
$options = [ |
||||||
|
self::CREATE, |
||||||
|
self::VIEW, |
||||||
|
self::EDIT, |
||||||
|
self::DELETE, |
||||||
|
]; |
||||||
|
|
||||||
|
// if the attribute isn't one we support, return false |
||||||
|
if (!\in_array($attribute, $options, true)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
// only vote on Post objects inside this voter |
||||||
|
return $subject instanceof Usergroup; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool |
||||||
|
{ |
||||||
|
$currentUser = $token->getUser(); |
||||||
|
if (!$currentUser instanceof UserInterface) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if ($this->security->isGranted('ROLE_ADMIN')) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** @var Usergroup $usergroup */ |
||||||
|
$usergroup = $subject; |
||||||
|
|
||||||
|
switch ($attribute) { |
||||||
|
case self::EDIT: |
||||||
|
return $this->canEdit($usergroup, $currentUser); |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
private function canEdit(Usergroup $usergroup, $currentUser): bool |
||||||
|
{ |
||||||
|
return $this->usergroupRepository->isGroupModerator($usergroup->getId(), $currentUser->getId()); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue