Skip to content

Commit

Permalink
Updated user group system and added correct acl perm selection when a…
Browse files Browse the repository at this point in the history
…dding categories
  • Loading branch information
epixa committed Sep 18, 2011
1 parent fc1dbd5 commit cc72cdc
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 72 deletions.
6 changes: 0 additions & 6 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ jms_security_extra:
secure_controllers: true
secure_all_services: false



# FriendsOfSymfony UserBundle Configuration
fos_user:
db_driver: orm
Expand All @@ -72,7 +70,3 @@ fos_user:
algorithm: sha512
encode_as_base64: false
iterations: 10
registration:
form:
handler: talkfest.form.handler.registration

8 changes: 6 additions & 2 deletions src/Epixa/TalkfestBundle/Controller/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function viewAction($id, $page = 1)
{
$category = $this->getCategoryService()->get($id);

if (!$this->get('security.context')->isGranted('VIEW', $category)) {
throw new \Symfony\Component\Security\Core\Exception\AccessDeniedException();
}

return array(
'category' => $category,
'posts' => $this->getPostService()->getByCategory($category, $page),
Expand Down Expand Up @@ -69,7 +73,7 @@ public function addAction(Request $request)
$this->getCategoryService()->add($category);

$this->get('session')->setFlash('notice', 'Category created');
return $this->redirect($this->generateUrl('post_index_page'));
return $this->redirect($this->generateUrl('post_index'));
}
}

Expand Down Expand Up @@ -136,7 +140,7 @@ public function deleteAction($id, Request $request)
$service->delete($deletionOptions);

$this->get('session')->setFlash('notice', 'Category deleted');
return $this->redirect($this->generateUrl('post_index_page'));
return $this->redirect($this->generateUrl('post_index'));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Epixa/TalkfestBundle/Controller/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Sensio\Bundle\FrameworkExtraBundle\Configuration\Route,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Template,
Symfony\Component\HttpFoundation\Request,
JMS\SecurityExtraBundle\Annotation\Secure,
Epixa\TalkfestBundle\Entity\Post,
Epixa\TalkfestBundle\Entity\Comment,
Epixa\TalkfestBundle\Form\Type\CommentType,
Expand Down
4 changes: 3 additions & 1 deletion src/Epixa/TalkfestBundle/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ public function viewAction($id)
/* @var \Symfony\Component\HttpFoundation\Response $addCommentResponse */
$addCommentResponse = $this->forward('EpixaTalkfestBundle:Comment:add', array('post' => $post));
if ($addCommentResponse->isRedirection()) {
var_dump($addCommentResponse);
die('here');
return $addCommentResponse;
}

return array(
'form' => $addCommentResponse->getContent(),
'post' => $post,
Expand Down
23 changes: 22 additions & 1 deletion src/Epixa/TalkfestBundle/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Epixa\TalkfestBundle\Entity;

use Doctrine\ORM\Mapping as ORM,
Doctrine\Common\Collections\ArrayCollection,
Symfony\Component\Validator\Constraints as Assert;

/**
Expand Down Expand Up @@ -46,15 +47,25 @@ class Category
*/
protected $dateCreated;

/**
* @ORM\ManyToMany(targetEntity="Epixa\TalkfestBundle\Entity\Group")
* @ORM\JoinTable(name="talkfest_category_user_group",
* joinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;


/**
* Initializes a new Category
*
* The creation date is set to now and the topics collection is initialized.
* The creation date is set to now and the groups collection is initialized.
*/
public function __construct()
{
$this->setDateCreated('now');
$this->groups = new ArrayCollection();
}

/**
Expand Down Expand Up @@ -123,6 +134,16 @@ public function setDateCreated($date)
return $this;
}

/**
* Gets all the groups that can access this category
*
* @return \Epixa\TalkfestBundle\Entity\Group[]
*/
public function getGroups()
{
return $this->groups;
}

/**
* Converts the category to a string
*
Expand Down
31 changes: 15 additions & 16 deletions src/Epixa/TalkfestBundle/Entity/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace Epixa\TalkfestBundle\Entity;

use FOS\UserBundle\Entity\Group as BaseGroup,
Doctrine\ORM\Mapping as ORM;
Doctrine\ORM\Mapping as ORM,
Symfony\Component\Security\Core\Role\RoleInterface;

/**
* A representation of a user group
Expand All @@ -21,7 +22,7 @@
* @ORM\Table(name="talkfest_user_group")
* @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
*/
class Group extends BaseGroup
class Group extends BaseGroup implements RoleInterface
{
/**
* @ORM\Id
Expand All @@ -31,29 +32,27 @@ class Group extends BaseGroup
protected $id;

/**
* @ORM\Column(type="boolean", name="is_default")
* @var bool
* @ORM\Column(type="string", name="default_role")
*/
protected $isDefault = false;
protected $role;

/**
* Sets whether this is the default group or not
*
* @param bool $flag
* @return Group
* Converts the group to a string
*
* @return string
*/
public function setIsDefault($flag)
public function __toString()
{
$this->isDefault = (bool)$flag;
return $this;
return $this->getName();
}

/**
* Is this group the default group?
* @return bool
* Gets the default role for this group
*
* @return string
*/
public function isDefault()
public function getRole()
{
return $this->isDefault;
return $this->role;
}
}
2 changes: 1 addition & 1 deletion src/Epixa/TalkfestBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @ORM\Table(name="talkfest_user")
* @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
*/
class User extends BaseUser implements \FOS\UserBundle\Model\UserInterface
class User extends BaseUser
{
/**
* @ORM\Id
Expand Down
3 changes: 3 additions & 0 deletions src/Epixa/TalkfestBundle/Form/Type/CategoryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class CategoryType extends AbstractType
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name');
$builder->add('groups', null, array(
'label' => 'Groups that can access this category'
));
}

/**
Expand Down
32 changes: 15 additions & 17 deletions src/Epixa/TalkfestBundle/Service/CategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
namespace Epixa\TalkfestBundle\Service;

use Doctrine\ORM\NoResultException,
Symfony\Component\Security\Acl\Domain\ObjectIdentity,
Symfony\Component\Security\Acl\Permission\MaskBuilder,
Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity,
Epixa\TalkfestBundle\Entity\Category,
Epixa\TalkfestBundle\Model\CategoryDeletionOptions,
Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
Epixa\TalkfestBundle\Model\CategoryDeletionOptions;

/**
* Service for managing categories
Expand Down Expand Up @@ -62,6 +64,8 @@ public function add(Category $category)

$em->persist($category);
$em->flush();

$this->initCategoryAccess($category);
}

/**
Expand Down Expand Up @@ -122,23 +126,17 @@ public function delete(CategoryDeletionOptions $options)
}
}

/**
* Gets a choice list of categories
*
* If a category is provided, it is not included in the returned by the choice list.
*
* @param \Epixa\TalkfestBundle\Entity\Category|null $excludedCategory
* @return \Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList
*/
public function getCategoryChoiceList(Category $excludedCategory = null)
public function initCategoryAccess(Category $category)
{
/* @var \Epixa\TalkfestBundle\Repository\CategoryRepository $repo */
$em = $this->getEntityManager();
$repo = $em->getRepository('Epixa\TalkfestBundle\Entity\Category');
$qb = $repo->getSelectQueryBuilder();
$repo->excludeCategory($qb, $excludedCategory);
$aclProvider = $this->container->get('security.acl.provider');

foreach ($category->getGroups() as $group) {
$securityIdentity = new RoleSecurityIdentity($group->getRole());
$objectIdentity = ObjectIdentity::fromDomainObject($category);
$acl = $aclProvider->createAcl($objectIdentity);

return new EntityChoiceList($em, 'Epixa\TalkfestBundle\Entity\Category', null, $qb);
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_VIEW);
$aclProvider->updateAcl($acl);
}
}
}
15 changes: 0 additions & 15 deletions src/Epixa/TalkfestBundle/Service/GroupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,4 @@ public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* Gets the default user group
*
* @throws \RuntimeException If no default group is configured
* @return \Epixa\TalkfestBundle\Entity\Group
*/
public function getDefaultGroup()
{
$group = $this->repository->findOneBy(array('isDefault' => true));
if ($group === null) {
throw new \RuntimeException('No default group configured');
}
return $group;
}
}
31 changes: 18 additions & 13 deletions src/Epixa/TalkfestBundle/Service/PostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,7 @@ public function add(Post $post)
$em->persist($post);
$em->flush();

// creating the ACL
$aclProvider = $this->container->get('security.acl.provider');
$objectIdentity = ObjectIdentity::fromDomainObject($post);
$acl = $aclProvider->createAcl($objectIdentity);

// retrieving the security identity of the currently logged-in user
$securityContext = $this->container->get('security.context');
$user = $securityContext->getToken()->getUser();
$securityIdentity = UserSecurityIdentity::fromAccount($user);

// grant owner access
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_EDIT);
$aclProvider->updateAcl($acl);
$this->initPostAccess($post);

return $post;
}
Expand Down Expand Up @@ -144,4 +132,21 @@ public function delete(Post $post)
$em->remove($post);
$em->flush();
}

public function initPostAccess(Post $post)
{
// creating the ACL
$aclProvider = $this->container->get('security.acl.provider');
$objectIdentity = ObjectIdentity::fromDomainObject($post);
$acl = $aclProvider->createAcl($objectIdentity);

// retrieving the security identity of the currently logged-in user
$securityContext = $this->container->get('security.context');
$user = $securityContext->getToken()->getUser();
$securityIdentity = UserSecurityIdentity::fromAccount($user);

// grant owner access
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_EDIT);
$aclProvider->updateAcl($acl);
}
}

0 comments on commit cc72cdc

Please sign in to comment.