Skip to content

Commit

Permalink
[User] Implement groups to simplify roles and permissions.
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Oct 25, 2015
1 parent fc65bfa commit b15d646
Show file tree
Hide file tree
Showing 16 changed files with 385 additions and 3 deletions.
86 changes: 86 additions & 0 deletions src/Clastic/UserBundle/Controller/GroupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* This file is part of the Clastic package.
*
* (c) Dries De Peuter <dries@nousefreak.be>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Clastic\UserBundle\Controller;

use Clastic\BackofficeBundle\Controller\AbstractModuleController;
use Clastic\UserBundle\Entity\Group;
use Clastic\UserBundle\Form\Type\GroupFormType;
use Symfony\Component\Form\Form;

/**
* GroupController.
*
* @author Dries De Peuter <dries@nousefreak.be>
*/
class GroupController extends AbstractModuleController
{
/**
* @return string
*/
protected function getType()
{
return 'user_group';
}

/**
* @return string
*/
protected function getListTemplate()
{
return 'ClasticUserBundle:Backoffice:group_list.html.twig';
}

/**
* @param Group $data
*
* @return Form
*/
protected function buildForm($data)
{
return $this->createForm(new GroupFormType(), $data);
}

/**
* @return string
*/
protected function getEntityName()
{
return 'ClasticUserBundle:Group';
}

/**
* @param Group $data
*
* @return string
*/
protected function resolveDataTitle($data)
{
if (!$data->getId()) {
return 'New';
}

return $data->getName();
}

/**
* @param int $id
*
* @return Group
*/
protected function resolveData($id)
{
if (!is_null($id)) {
return $this->getDoctrine()->getRepository('ClasticUserBundle:Group')
->find($id);
}

return new Group('');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ protected function configureFOSUserBundle(ContainerBuilder $container)
case 'fos_user':
$container->prependExtensionConfig(
$name,
array(
[
'db_driver' => 'orm',
'firewall_name' => 'backoffice',
'user_class' => 'Clastic\UserBundle\Entity\User',
)
'group' => ['group_class' => 'Clastic\UserBundle\Entity\Group'],
]
);
break;
}
Expand Down
23 changes: 23 additions & 0 deletions src/Clastic/UserBundle/Entity/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* This file is part of the Clastic package.
*
* (c) Dries De Peuter <dries@nousefreak.be>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Clastic\UserBundle\Entity;

use FOS\UserBundle\Model\Group as BaseGroup;

/**
* @author Dries De Peuter <dries@nousefreak.be>
*/
class Group extends BaseGroup
{
function __toString()
{
return $this->getName();
}
}
21 changes: 21 additions & 0 deletions src/Clastic/UserBundle/Entity/GroupRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This file is part of the Clastic package.
*
* (c) Dries De Peuter <dries@nousefreak.be>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Clastic\UserBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
* GroupRepository.
*
* @author Dries De Peuter <dries@nousefreak.be>
*/
class GroupRepository extends EntityRepository
{
}
2 changes: 1 addition & 1 deletion src/Clastic/UserBundle/Entity/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Doctrine\ORM\EntityRepository;

/**
* TextRepository.
* UserRepository.
*
* @author Dries De Peuter <dries@nousefreak.be>
*/
Expand Down
102 changes: 102 additions & 0 deletions src/Clastic/UserBundle/Form/Type/GroupFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

/**
* This file is part of the Clastic package.
*
* (c) Dries De Peuter <dries@nousefreak.be>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Clastic\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
* UserType.
*
* @author Dries De Peuter <dries@nousefreak.be>
*/
class GroupFormType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
$builder->create('tabs', 'tabs', ['inherit_data' => true])
->add($this->createGeneralTab($builder))
->add($this->createActionTab($builder))
);
}

private function getAvailableRoles(FormBuilderInterface $builder)
{
$roles = [
'ROLE_ADMIN' => 'Admin',
'ROLE_USER' => 'User',
];

return $roles;
}

private function createGeneralTab(FormBuilderInterface $builder)
{
return $this->createTab(
$builder,
'general',
['label' => 'user.form.tab.general.label']
)
->add('name', 'text', ['label' => 'user_group.form.tab.general.field.name'])
->add('roles', 'multi_select', [
'choices' => $this->getAvailableRoles($builder),
'label' => 'user.form.tab.role.field.roles',
]);
}

private function createActionTab(FormBuilderInterface $builder)
{
return $builder->create('actions', 'tabs_tab_actions', [
'mapped' => false,
'inherit_data' => true,
])
->add('save', 'submit', [
'label' => 'node.form.tab.action.field.save',
'attr' => ['class' => 'btn btn-success'],
]);
}

private function createTab(FormBuilderInterface $builder, $name, $options = array())
{
$options = array_replace(
$options,
['inherit_data' => true]
);

return $builder->create($name, 'tabs_tab', $options);
}

/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'translation_domain' => 'clastic',
]);
}

/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'clastic_user_group';
}
}
10 changes: 10 additions & 0 deletions src/Clastic/UserBundle/Form/Type/UserFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add($this->createGeneralTab($builder))
->add($this->createPasswordTab($builder))
->add($this->createRoleTab($builder))
->add($this->createGroupTab($builder))
->add($this->createActionTab($builder))
);
}
Expand Down Expand Up @@ -102,6 +103,15 @@ private function createRoleTab(FormBuilderInterface $builder)
));
}

private function createGroupTab(FormBuilderInterface $builder)
{
return $this->createTab($builder, 'group', array('label' => 'user.form.tab.group.label'))
->add('groups', 'entity_multi_select', array(
'class' => 'ClasticUserBundle:Group',
'label' => 'user.form.tab.group.field.groups',
));
}

private function createActionTab(FormBuilderInterface $builder)
{
return $builder->create('actions', 'tabs_tab_actions', array(
Expand Down
39 changes: 39 additions & 0 deletions src/Clastic/UserBundle/Module/GroupModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* This file is part of the Clastic package.
*
* (c) Dries De Peuter <dries@nousefreak.be>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Clastic\UserBundle\Module;

use Clastic\CoreBundle\Module\AdministrationModuleInterface;
use Clastic\CoreBundle\Module\ModuleInterface;

/**
* @author Dries De Peuter <dries@nousefreak.be>
*/
class GroupModule implements ModuleInterface, AdministrationModuleInterface
{
/**
* The name of the module.
*
* @return string
*/
public function getName()
{
return 'Group';
}

/**
* The the unique identifier of the module.
*
* @return string
*/
public function getIdentifier()
{
return 'user_group';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity table="Groups" repository-class="Clastic\UserBundle\Entity\GroupRepository" name="Clastic\UserBundle\Entity\Group">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
</entity>
</doctrine-mapping>
10 changes: 10 additions & 0 deletions src/Clastic/UserBundle/Resources/config/doctrine/User.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<many-to-many field="groups" target-entity="Clastic\UserBundle\Entity\Group">
<join-table name="UserGroup">
<join-columns>
<join-column name="user_id" referenced-column-name="id"/>
</join-columns>
<inverse-join-columns>
<join-column name="group_id" referenced-column-name="id" />
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
</doctrine-mapping>
13 changes: 13 additions & 0 deletions src/Clastic/UserBundle/Resources/config/routing_backoffice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@
<route id="clastic_backoffice_user_delete" path="/user/delete/{id}">
<default key="_controller">ClasticUserBundle:User:delete</default>
</route>

<route id="clastic_backoffice_user_group_list" path="/user_group/list">
<default key="_controller">ClasticUserBundle:Group:list</default>
</route>

<route id="clastic_backoffice_user_group_form" path="/user_group/edit/{id}">
<default key="_controller">ClasticUserBundle:Group:form</default>
<default key="id" xsi:nil="true" />
</route>

<route id="clastic_backoffice_user_group_delete" path="/user_group/delete/{id}">
<default key="_controller">ClasticUserBundle:Group:delete</default>
</route>
</routes>
4 changes: 4 additions & 0 deletions src/Clastic/UserBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

<parameters>
<parameter key="clastic.user.module.class">Clastic\UserBundle\Module\UserModule</parameter>
<parameter key="clastic.user_group.module.class">Clastic\UserBundle\Module\GroupModule</parameter>
</parameters>

<services>
<service id="clastic.user.module" class="%clastic.user.module.class%">
<tag name="clastic.module" node_module="false" alias="user" />
</service>
<service id="clastic.user_group.module" class="%clastic.user_group.module.class%">
<tag name="clastic.module" node_module="false" alias="user_group" />
</service>
</services>
</container>
11 changes: 11 additions & 0 deletions src/Clastic/UserBundle/Resources/translations/clastic.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ user.form:
label: Roles
field:
roles: Roles
group:
label: Groups
field:
groups: Groups

user_group:
form:
tab:
general:
field:
name: Name

0 comments on commit b15d646

Please sign in to comment.