From 445daecfc0a0a2d42bae89ea8ab250d51d03d0e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 24 Oct 2014 12:19:30 +0200 Subject: [PATCH 1/5] implements user-scope-role-scope first work --- .gitignore | 1 + Entity/Agent.php | 64 ----------- Entity/User.php | 108 ++++++++++++++++++ .../{Agent.orm.yml => Center.orm.yml} | 8 +- Resources/config/doctrine/RoleScope.orm.yml | 17 +++ Resources/config/doctrine/Scope.orm.yml | 16 +++ Resources/config/doctrine/User.orm.yml | 21 ++++ Tests/Fixtures/App/AppKernel.php | 1 + Tests/Fixtures/App/config/config.yml | 14 +++ Tests/Fixtures/App/config/parameters.yml.dist | 11 ++ composer.json | 6 +- 11 files changed, 197 insertions(+), 70 deletions(-) delete mode 100644 Entity/Agent.php create mode 100644 Entity/User.php rename Resources/config/doctrine/{Agent.orm.yml => Center.orm.yml} (58%) create mode 100644 Resources/config/doctrine/RoleScope.orm.yml create mode 100644 Resources/config/doctrine/Scope.orm.yml create mode 100644 Resources/config/doctrine/User.orm.yml create mode 100644 Tests/Fixtures/App/config/parameters.yml.dist diff --git a/.gitignore b/.gitignore index 68c0e6f92..8383a1071 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ web/bundles/* # Configuration files app/config/parameters.ini app/config/parameters.yml +Tests/Fixtures/App/config/parameters.yml #composer composer.lock diff --git a/Entity/Agent.php b/Entity/Agent.php deleted file mode 100644 index fe6d63c98..000000000 --- a/Entity/Agent.php +++ /dev/null @@ -1,64 +0,0 @@ -id; - } - - /** - * Set name - * - * @param string $name - * @return Agent - */ - public function setName($name) - { - $this->name = $name; - - return $this; - } - - /** - * Get name - * - * @return string - */ - public function getName() - { - return $this->name; - } - - public function __toString() { - return parent::__toString(); - } -} diff --git a/Entity/User.php b/Entity/User.php new file mode 100644 index 000000000..16c09063b --- /dev/null +++ b/Entity/User.php @@ -0,0 +1,108 @@ +id; + } + + /** + * Set username + * + * @param string $name + * @return Agent + */ + public function setUsername($name) + { + $this->username = $name; + + return $this; + } + + /** + * Get name + * + * @return string + */ + public function getName() + { + return $this->username; + } + + public function __toString() { + return $this->getUsername(); + } + + public function eraseCredentials() + { + + } + + public function getPassword() + { + return $this->password; + } + + public function getRoles() + { + return 'ROLE_USER'; + } + + public function getSalt() + { + return $this->salt; + } + + public function getUsername() + { + return $this->username; + } + + function setPassword($password) + { + $this->password = $password; + return $this; + } + + function setSalt($salt) + { + $this->salt = $salt; + return $this; + } + + + +} diff --git a/Resources/config/doctrine/Agent.orm.yml b/Resources/config/doctrine/Center.orm.yml similarity index 58% rename from Resources/config/doctrine/Agent.orm.yml rename to Resources/config/doctrine/Center.orm.yml index adec421b4..16856e519 100644 --- a/Resources/config/doctrine/Agent.orm.yml +++ b/Resources/config/doctrine/Center.orm.yml @@ -1,6 +1,6 @@ -Chill\MainBundle\Entity\Agent: +Chill\MainBundle\Entity\Center: type: entity - table: agents + table: centers id: id: type: integer @@ -9,6 +9,4 @@ Chill\MainBundle\Entity\Agent: strategy: AUTO fields: name: - type: string - length: 80 - + type: json_array \ No newline at end of file diff --git a/Resources/config/doctrine/RoleScope.orm.yml b/Resources/config/doctrine/RoleScope.orm.yml new file mode 100644 index 000000000..32ecd4f86 --- /dev/null +++ b/Resources/config/doctrine/RoleScope.orm.yml @@ -0,0 +1,17 @@ +Chill\MainBundle\Entity\RoleScope: + type: entity + table: role_scopes + id: + id: + type: integer + id: true + generator: + strategy: AUTO + fields: + role: + type: string + length: 255 + oneToMany: + scope: + targetEntity: Chill\MainBundle\Entity\Scope + mappedBy: roleScopes \ No newline at end of file diff --git a/Resources/config/doctrine/Scope.orm.yml b/Resources/config/doctrine/Scope.orm.yml new file mode 100644 index 000000000..a79ab3d29 --- /dev/null +++ b/Resources/config/doctrine/Scope.orm.yml @@ -0,0 +1,16 @@ +Chill\MainBundle\Entity\Scope: + type: entity + table: scopes + id: + id: + type: integer + id: true + generator: + strategy: AUTO + fields: + name: + type: json_array + manyToOne: + roleScopes: + targetEntity: Chill\MainBundle\Entity\RoleScope + inversedBy: scope \ No newline at end of file diff --git a/Resources/config/doctrine/User.orm.yml b/Resources/config/doctrine/User.orm.yml new file mode 100644 index 000000000..7edb8230a --- /dev/null +++ b/Resources/config/doctrine/User.orm.yml @@ -0,0 +1,21 @@ +Chill\MainBundle\Entity\User: + type: entity + table: users + id: + id: + type: integer + id: true + generator: + strategy: AUTO + fields: + username: + type: string + length: 80 + password: + type: string + length: 255 + salt: + type: string + length: 255 + + diff --git a/Tests/Fixtures/App/AppKernel.php b/Tests/Fixtures/App/AppKernel.php index de2e11b9c..001d9f4a1 100644 --- a/Tests/Fixtures/App/AppKernel.php +++ b/Tests/Fixtures/App/AppKernel.php @@ -13,6 +13,7 @@ class AppKernel extends Kernel new Symfony\Bundle\SecurityBundle\SecurityBundle(), new Symfony\Bundle\TwigBundle\TwigBundle(), new \Symfony\Bundle\AsseticBundle\AsseticBundle(), + new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), ); } diff --git a/Tests/Fixtures/App/config/config.yml b/Tests/Fixtures/App/config/config.yml index 0046eed72..65d55d368 100644 --- a/Tests/Fixtures/App/config/config.yml +++ b/Tests/Fixtures/App/config/config.yml @@ -1,3 +1,6 @@ +imports: + - { resource: parameters.yml } + framework: secret: Not very secret router: { resource: "%kernel.root_dir%/config/routing.yml" } @@ -9,3 +12,14 @@ framework: profiler: { only_exceptions: false } templating: engines: ['twig'] + +# Doctrine Configuration +doctrine: + dbal: + driver: pdo_pgsql + host: "%database_host%" + port: "%database_port%" + dbname: "%database_name%" + user: "%database_user%" + password: "%database_password%" + charset: UTF8 diff --git a/Tests/Fixtures/App/config/parameters.yml.dist b/Tests/Fixtures/App/config/parameters.yml.dist new file mode 100644 index 000000000..38fab7fd2 --- /dev/null +++ b/Tests/Fixtures/App/config/parameters.yml.dist @@ -0,0 +1,11 @@ +parameters: + database_host: 127.0.0.1 + database_port: 5435 + database_name: chill + database_user: chill + database_password: chill + locale: fr + secret: ThisTokenIsNotSoSecretChangeIt + debug_toolbar: true + debug_redirects: false + use_assetic_controller: true \ No newline at end of file diff --git a/composer.json b/composer.json index 05765c64f..c09d49c25 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,11 @@ "symfony/monolog-bundle": "~2.4", "symfony/framework-bundle": "2.5.*", "symfony/yaml": "2.5.*", - "symfony/symfony": "2.5.*" + "symfony/symfony": "2.5.*", + "doctrine/dbal": "2.5.*@dev", + "doctrine/orm": "2.5.*@dev", + "doctrine/common": "2.4.*@dev", + "doctrine/doctrine-bundle": "~1.2@dev" }, "require-dev": { "symfony/dom-crawler": "2.5" From ec3e08ff7914cc5422b523dbad9f26311a6a6c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 27 Oct 2014 09:08:23 +0100 Subject: [PATCH 2/5] continue work - create loadfixtures --- DataFixtures/ORM/LoadAgents.php | 63 ------------- DataFixtures/ORM/LoadCenters.php | 59 ++++++++++++ DataFixtures/ORM/LoadRoleScopes.php | 79 ++++++++++++++++ DataFixtures/ORM/LoadScopes.php | 82 ++++++++++++++++ DataFixtures/ORM/LoadUsers.php | 40 ++++++++ Entity/Center.php | 59 ++++++++++++ Entity/PermissionGroup.php | 84 +++++++++++++++++ Entity/RoleScope.php | 94 +++++++++++++++++++ Entity/Scope.php | 93 ++++++++++++++++++ Entity/User.php | 2 +- Resources/config/doctrine/Center.orm.yml | 3 +- .../config/doctrine/PermissionGroup.orm.yml | 16 ++++ Resources/config/doctrine/Scope.orm.yml | 4 +- composer.json | 3 +- 14 files changed, 613 insertions(+), 68 deletions(-) delete mode 100644 DataFixtures/ORM/LoadAgents.php create mode 100644 DataFixtures/ORM/LoadCenters.php create mode 100644 DataFixtures/ORM/LoadRoleScopes.php create mode 100644 DataFixtures/ORM/LoadScopes.php create mode 100644 DataFixtures/ORM/LoadUsers.php create mode 100644 Entity/Center.php create mode 100644 Entity/PermissionGroup.php create mode 100644 Entity/RoleScope.php create mode 100644 Entity/Scope.php create mode 100644 Resources/config/doctrine/PermissionGroup.orm.yml diff --git a/DataFixtures/ORM/LoadAgents.php b/DataFixtures/ORM/LoadAgents.php deleted file mode 100644 index 80c7a841d..000000000 --- a/DataFixtures/ORM/LoadAgents.php +++ /dev/null @@ -1,63 +0,0 @@ - - */ -class LoadAgents extends AbstractFixture implements ContainerAwareInterface { - - /** - * - * @var ContainerInterface - */ - private $container; - - const AGENT_STRING = 'agent'; - - public function getOrder() { - return 1000; - } - - public function setContainer(ContainerInterface $container = null) { - $this->container = $container; - } - - public function load(ObjectManager $manager) { - - echo "creating agents... \n"; - - $userManager = $this->container->get('fos_user.user_manager'); - - for ($i = 0; $i < 10; $i++) { - $username = 'agent'.$i; - echo "creating agent $username (password $username) \n"; - - $user = $userManager->createUser(); - - $user->setUsername($username) - ->setPassword($username) - ->setName($username) - ->setEmail($username.'@chill.be'); - - $this->container->get('fos_user.user_manager')->updateUser($user, false); - - $this->addReference($username, $user); - } - - $manager->flush(); - } - - - - -} diff --git a/DataFixtures/ORM/LoadCenters.php b/DataFixtures/ORM/LoadCenters.php new file mode 100644 index 000000000..e1a7f0873 --- /dev/null +++ b/DataFixtures/ORM/LoadCenters.php @@ -0,0 +1,59 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\DataFixtures\ORM; + +use Doctrine\Common\DataFixtures\AbstractFixture; +use Doctrine\Common\DataFixtures\OrderedFixtureInterface; +use Doctrine\Common\Persistence\ObjectManager; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Chill\MainBundle\Entity\Center; + +/** + * + * + * @author Julien Fastré + */ +class LoadCenters extends AbstractFixture implements OrderedFixtureInterface +{ + public function getOrder() + { + return 100; + } + + public function load(ObjectManager $manager) + { + $centerA = new Center(); + $centerA->setName('Center A'); + + $manager->persist($centerA); + $this->addReference('centerA', $centerA); + + $centerB = new Center(); + $centerB->setName('center B'); + + $manager->persist($centerB); + $this->addReference('centerB', $centerB); + + $manager->flush(); + + } +} diff --git a/DataFixtures/ORM/LoadRoleScopes.php b/DataFixtures/ORM/LoadRoleScopes.php new file mode 100644 index 000000000..a6330bcb9 --- /dev/null +++ b/DataFixtures/ORM/LoadRoleScopes.php @@ -0,0 +1,79 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\DataFixtures\ORM; + +use Doctrine\Common\DataFixtures\AbstractFixture; +use Doctrine\Common\DataFixtures\OrderedFixtureInterface; +use Doctrine\Common\Persistence\ObjectManager; +use Chill\MainBundle\Entity\RoleScope; +use Chill\MainBundle\DataFixtures\ORM\LoadScopes; + +/** + * + * + * @author Julien Fastré + */ +class LoadRoleScopes extends AbstractFixture implements OrderedFixtureInterface +{ + public function getOrder() + { + return 300; + } + + public static $permissions = array( + 'CHILL_FOO_SEE' => array( + 'names' => array( + 'fr' => 'voir foo', + 'en' => 'see foo', + 'nl' => 'zie foo' + ) + ), + 'CHILL_FOO_EDIT' => array( + 'names' => array( + 'fr' => 'modifier foo', + 'en' => 'edit foo', + 'nl' => 'editie foo' + ) + ) + ); + + public static $references = array(); + + public function load(ObjectManager $manager) + { + foreach (static::$permissions as $key => $permission) { + foreach(LoadScopes::$references as $scopeReference) { + $roleScope = new RoleScope(); + $roleScope->setRole($key) + ->setScope($this->getReference($scopeReference)) + ; + $reference = 'role_scope_'.$key.'_'.$this->getReference($scopeReference)->getName()['en']; + var_dump($reference); + $this->addReference($reference, $roleScope); + $manager->persist($roleScope); + static::$references[] = $reference; + } + } + + $manager->flush(); + } + +} diff --git a/DataFixtures/ORM/LoadScopes.php b/DataFixtures/ORM/LoadScopes.php new file mode 100644 index 000000000..d3a7f16fd --- /dev/null +++ b/DataFixtures/ORM/LoadScopes.php @@ -0,0 +1,82 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\DataFixtures\ORM; + +use Doctrine\Common\DataFixtures\AbstractFixture; +use Doctrine\Common\DataFixtures\OrderedFixtureInterface; +use Doctrine\Common\Persistence\ObjectManager; + +/** + * Create scopes + * + * @author Julien Fastré + */ +class LoadScopes extends AbstractFixture implements OrderedFixtureInterface +{ + public function getOrder() + { + return 200; + } + + public $scopes = array( + array( + 'names' => array( + 'fr' => 'tous', + 'en' => 'all', + 'nl' => 'algemeen' + ), + ), + array( + 'names' => array( + 'fr' => 'social', + 'en' => 'social', + 'nl' => 'sociaal' + ) + ), + array( + 'names' => array( + 'fr' => 'administratif', + 'en' => 'administrative', + 'nl' => 'administratief' + ) + ) + ); + + public static $references = array(); + + public function load(ObjectManager $manager) + { + + $scopesReferences = array(); + + foreach ($this->scopes as $new) { + $scope = new \Chill\MainBundle\Entity\Scope(); + $scope->setName($new['names']); + + $manager->persist($scope); + $reference = 'scope_'.$new['names']['en']; + $this->addReference($reference, $scope); + static::$references[] = $reference; + } + + $manager->flush(); + } +} diff --git a/DataFixtures/ORM/LoadUsers.php b/DataFixtures/ORM/LoadUsers.php new file mode 100644 index 000000000..222b8d03d --- /dev/null +++ b/DataFixtures/ORM/LoadUsers.php @@ -0,0 +1,40 @@ + + */ +class LoadUsers extends AbstractFixture implements ContainerAwareInterface +{ + + /** + * + * @var ContainerInterface + */ + private $container; + + public function getOrder() + { + return 1000; + } + + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + + public function load(ObjectManager $manager) + { + + } + +} diff --git a/Entity/Center.php b/Entity/Center.php new file mode 100644 index 000000000..75edd18c4 --- /dev/null +++ b/Entity/Center.php @@ -0,0 +1,59 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\Entity; + +/** + * + * + * @author Julien Fastré + */ +class Center +{ + /** + * + * @var string + */ + private $name; + + /** + * + * @var int + */ + private $id; + + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getId() + { + return $this->id; + } + + +} diff --git a/Entity/PermissionGroup.php b/Entity/PermissionGroup.php new file mode 100644 index 000000000..cf2b76b05 --- /dev/null +++ b/Entity/PermissionGroup.php @@ -0,0 +1,84 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\Entity; + +use Chill\MainBundle\Entity\RoleScope; + + + +/** + * + * + * @author Julien Fastré + */ +class PermissionGroup +{ + /** + * + * @var int + */ + private $id; + + /** + * + * @var array + */ + private $name; + + /** + * + * @var \Doctrine\Common\Collections\Collection + */ + private $roleScopes; + + public function __construct() + { + $this->roleScopes = new \Doctrine\Common\Collections\ArrayCollection(); + } + + public function getId() + { + return $this->id; + } + + public function getName() + { + return $this->name; + } + + public function getRoleScopes() + { + return $this->roleScopes; + } + + public function setName(array $name) + { + $this->name = $name; + return $this; + } + + public function addRoleScope(RoleScope $roleScope) + { + $this->roleScopes->add($roleScope); + } + + +} diff --git a/Entity/RoleScope.php b/Entity/RoleScope.php new file mode 100644 index 000000000..8d85ff4a9 --- /dev/null +++ b/Entity/RoleScope.php @@ -0,0 +1,94 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\Entity; + +/** + * + * + * @author Julien Fastré + */ +class RoleScope +{ + /** + * + * @var int + */ + private $id; + + /** + * + * @var string + */ + private $role; + + /** + * + * @var Scope + */ + private $scope; + + public function getId() + { + return $this->id; + } + + /** + * + * @return string + */ + public function getRole() + { + return $this->role; + } + + /** + * + * @return Scope + */ + public function getScope() + { + return $this->scope; + } + + /** + * + * @param type $role + * @return \Chill\MainBundle\Entity\RoleScope + */ + public function setRole($role) + { + $this->role = $role; + return $this; + } + + /** + * + * @param \Chill\MainBundle\Entity\Scope $scope + * @return \Chill\MainBundle\Entity\RoleScope + */ + public function setScope(Scope $scope) + { + $this->scope = $scope; + return $this; + } + + +} diff --git a/Entity/Scope.php b/Entity/Scope.php new file mode 100644 index 000000000..4a55d3c26 --- /dev/null +++ b/Entity/Scope.php @@ -0,0 +1,93 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\Entity; + +use Chill\MainBundle\Entity\RoleScope; + +/** + * + * + * @author Julien Fastré + */ +class Scope +{ + /** + * + * @var int + */ + private $id; + + /** + * translatable names + * + * @var array + */ + private $name = array(); + + /** + * + * @var \Doctrine\Common\Collections\Collection + */ + private $roleScopes; + + public function __construct() + { + $this->roleScopes = new \Doctrine\Common\Collections\ArrayCollection(); + } + + /** + * + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * + * @return array + */ + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * + * @return \Doctrine\Common\Collections\Collection + */ + public function getRoleScopes() + { + return $this->roleScopes; + } + + public function addRoleScope(RoleScope $roleScope) + { + $this->roleScopes->add($roleScope); + } +} diff --git a/Entity/User.php b/Entity/User.php index 16c09063b..1eeac1060 100644 --- a/Entity/User.php +++ b/Entity/User.php @@ -7,7 +7,7 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\UserInterface; /** - * Agent + * User */ class User implements UserInterface { diff --git a/Resources/config/doctrine/Center.orm.yml b/Resources/config/doctrine/Center.orm.yml index 16856e519..491495faf 100644 --- a/Resources/config/doctrine/Center.orm.yml +++ b/Resources/config/doctrine/Center.orm.yml @@ -9,4 +9,5 @@ Chill\MainBundle\Entity\Center: strategy: AUTO fields: name: - type: json_array \ No newline at end of file + type: string + length: 255 \ No newline at end of file diff --git a/Resources/config/doctrine/PermissionGroup.orm.yml b/Resources/config/doctrine/PermissionGroup.orm.yml new file mode 100644 index 000000000..f7c7de5ec --- /dev/null +++ b/Resources/config/doctrine/PermissionGroup.orm.yml @@ -0,0 +1,16 @@ +Chill\MainBundle\Entity\PermissionGroup: + type: entity + table: permission_groups + id: + id: + type: integer + id: true + generator: + strategy: AUTO + fields: + name: + type: json_array + manyToMany: + roleScopes: + targetEntity: Chill\MainBundle\Entity\RoleScope + \ No newline at end of file diff --git a/Resources/config/doctrine/Scope.orm.yml b/Resources/config/doctrine/Scope.orm.yml index a79ab3d29..188d8c307 100644 --- a/Resources/config/doctrine/Scope.orm.yml +++ b/Resources/config/doctrine/Scope.orm.yml @@ -10,7 +10,7 @@ Chill\MainBundle\Entity\Scope: fields: name: type: json_array - manyToOne: + oneToMany: roleScopes: targetEntity: Chill\MainBundle\Entity\RoleScope - inversedBy: scope \ No newline at end of file + mappedBy: scope \ No newline at end of file diff --git a/composer.json b/composer.json index c09d49c25..7aa6cfe73 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "doctrine/doctrine-bundle": "~1.2@dev" }, "require-dev": { - "symfony/dom-crawler": "2.5" + "symfony/dom-crawler": "2.5", + "doctrine/doctrine-fixtures-bundle": "~2.2" } } From 7fc8b1ca1ecc74620be30e7fc274ed115172db89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 28 Oct 2014 18:24:34 +0100 Subject: [PATCH 3/5] create Access Control Model --- DataFixtures/ORM/LoadCenters.php | 33 +++-- DataFixtures/ORM/LoadGroupCenters.php | 63 ++++++++++ DataFixtures/ORM/LoadPermissionsGroup.php | 87 +++++++++++++ DataFixtures/ORM/LoadRoleScopes.php | 9 +- DataFixtures/ORM/LoadUsers.php | 48 +++++-- Entity/Center.php | 22 ++++ Entity/GroupCenter.php | 118 ++++++++++++++++++ ...rmissionGroup.php => PermissionsGroup.php} | 10 +- Entity/User.php | 118 +++++++++++++++--- Resources/config/doctrine/Center.orm.yml | 6 +- Resources/config/doctrine/GroupCenter.orm.yml | 16 +++ ...Group.orm.yml => PermissionsGroup.orm.yml} | 5 +- Resources/config/doctrine/RoleScope.orm.yml | 4 +- Resources/config/doctrine/User.orm.yml | 10 ++ 14 files changed, 501 insertions(+), 48 deletions(-) create mode 100644 DataFixtures/ORM/LoadGroupCenters.php create mode 100644 DataFixtures/ORM/LoadPermissionsGroup.php create mode 100644 Entity/GroupCenter.php rename Entity/{PermissionGroup.php => PermissionsGroup.php} (93%) create mode 100644 Resources/config/doctrine/GroupCenter.orm.yml rename Resources/config/doctrine/{PermissionGroup.orm.yml => PermissionsGroup.orm.yml} (76%) diff --git a/DataFixtures/ORM/LoadCenters.php b/DataFixtures/ORM/LoadCenters.php index e1a7f0873..d0bb7178f 100644 --- a/DataFixtures/ORM/LoadCenters.php +++ b/DataFixtures/ORM/LoadCenters.php @@ -39,21 +39,30 @@ class LoadCenters extends AbstractFixture implements OrderedFixtureInterface return 100; } + public static $centers = array( + array( + 'name' => 'Center A', + 'ref' => 'centerA' + ), + array( + 'name' => 'Center B', + 'ref' => 'centerB' + ) + ); + + public static $refs = array(); + public function load(ObjectManager $manager) { - $centerA = new Center(); - $centerA->setName('Center A'); - - $manager->persist($centerA); - $this->addReference('centerA', $centerA); - - $centerB = new Center(); - $centerB->setName('center B'); - - $manager->persist($centerB); - $this->addReference('centerB', $centerB); + foreach (static::$centers as $new) { + $centerA = new Center(); + $centerA->setName($new['name']); + + $manager->persist($centerA); + $this->addReference($new['ref'], $centerA); + static::$refs[] = $new['ref']; + } $manager->flush(); - } } diff --git a/DataFixtures/ORM/LoadGroupCenters.php b/DataFixtures/ORM/LoadGroupCenters.php new file mode 100644 index 000000000..04a34d080 --- /dev/null +++ b/DataFixtures/ORM/LoadGroupCenters.php @@ -0,0 +1,63 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\DataFixtures\ORM; + +use Doctrine\Common\DataFixtures\AbstractFixture; +use Doctrine\Common\DataFixtures\OrderedFixtureInterface; +use Doctrine\Common\Persistence\ObjectManager; +use Chill\MainBundle\Entity\GroupCenter; +use Chill\MainBundle\DataFixtures\ORM\LoadCenters; +use Chill\MainBundle\DataFixtures\ORM\LoadPermissionsGroup; + +/** + * + * + * @author Julien Fastré + */ +class LoadGroupCenters extends AbstractFixture implements OrderedFixtureInterface +{ + public function getOrder() + { + return 500; + } + + public static $refs = array(); + + public function load(ObjectManager $manager) + { + foreach (LoadCenters::$refs as $centerRef) { + foreach (LoadPermissionsGroup::$refs as $permissionGroupRef) { + $GroupCenter = new GroupCenter(); + $GroupCenter->setCenter($this->getReference($centerRef)); + $GroupCenter->addPermissionGroup($this->getReference($permissionGroupRef)); + + $manager->persist($GroupCenter); + + $reference = $centerRef.'_'.$permissionGroupRef; + $this->addReference($reference, $GroupCenter); + static::$refs[] = $reference; + echo "Creating $reference... \n"; + } + } + + $manager->flush(); + } +} diff --git a/DataFixtures/ORM/LoadPermissionsGroup.php b/DataFixtures/ORM/LoadPermissionsGroup.php new file mode 100644 index 000000000..b2ed78934 --- /dev/null +++ b/DataFixtures/ORM/LoadPermissionsGroup.php @@ -0,0 +1,87 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\DataFixtures\ORM; + +use Doctrine\Common\DataFixtures\AbstractFixture; +use Doctrine\Common\DataFixtures\OrderedFixtureInterface; +use Doctrine\Common\Persistence\ObjectManager; +use Chill\MainBundle\Entity\PermissionsGroup; + +/** + * + * + * @author Julien Fastré + */ +class LoadPermissionsGroup extends AbstractFixture implements OrderedFixtureInterface +{ + public function getOrder() + { + return 400; + } + + public static $permissionGroup = array( + array( + 'name' => 'social', + 'role_scopes' => array( + 'role_scope_CHILL_FOO_EDIT_social', + 'role_scope_CHILL_FOO_SEE_administrative', + "role_scope_CHILL_FOO_EDIT_all" + ) + ), + array( + 'name' => 'administrative', + 'role_scopes' => array( + "role_scope_CHILL_FOO_SEE_social", + "role_scope_CHILL_FOO_EDIT_administrative", + "role_scope_CHILL_FOO_EDIT_all" + ) + ), + array( + 'name' => 'direction', + 'role_scopes' => array( + "role_scope_CHILL_FOO_EDIT_all", + "role_scope_CHILL_FOO_SEE_DETAILS_social", + "role_scope_CHILL_FOO_SEE_DETAILS_administrative" + ) + ) + ); + + public static $refs = array(); + + public function load(ObjectManager $manager) + { + foreach (static::$permissionGroup as $new) { + $permissionGroup = new PermissionsGroup(); + $permissionGroup->setName($new['name']); + foreach ($new['role_scopes'] as $roleScopeRef) { + $permissionGroup->addRoleScope($this->getReference($roleScopeRef)); + } + + $manager->persist($permissionGroup); + $reference = 'permission_group_'.$new['name']; + echo "Creating $reference \n"; + $this->setReference($reference, $permissionGroup); + static::$refs[] = $reference; + } + + $manager->flush(); + } +} diff --git a/DataFixtures/ORM/LoadRoleScopes.php b/DataFixtures/ORM/LoadRoleScopes.php index a6330bcb9..9693576d6 100644 --- a/DataFixtures/ORM/LoadRoleScopes.php +++ b/DataFixtures/ORM/LoadRoleScopes.php @@ -46,6 +46,13 @@ class LoadRoleScopes extends AbstractFixture implements OrderedFixtureInterface 'nl' => 'zie foo' ) ), + 'CHILL_FOO_SEE_DETAILS' => array( + 'names' => array( + 'fr' => 'voir foo avec détails', + 'en' => 'see foo with details', + 'nl' => 'zie foo in details' + ) + ), 'CHILL_FOO_EDIT' => array( 'names' => array( 'fr' => 'modifier foo', @@ -66,7 +73,7 @@ class LoadRoleScopes extends AbstractFixture implements OrderedFixtureInterface ->setScope($this->getReference($scopeReference)) ; $reference = 'role_scope_'.$key.'_'.$this->getReference($scopeReference)->getName()['en']; - var_dump($reference); + echo "Creating $reference \n"; $this->addReference($reference, $roleScope); $manager->persist($roleScope); static::$references[] = $reference; diff --git a/DataFixtures/ORM/LoadUsers.php b/DataFixtures/ORM/LoadUsers.php index 222b8d03d..df5ca2100 100644 --- a/DataFixtures/ORM/LoadUsers.php +++ b/DataFixtures/ORM/LoadUsers.php @@ -7,15 +7,20 @@ use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Chill\MainBundle\DataFixtures\ORM\LoadCenters; +use Chill\MainBundle\DataFixtures\ORM\LoadPermissionsGroup; +use Chill\MainBundle\Entity\User; /** - * Load agents into database + * Load fixtures users into database + * + * create a user for each permission_group and center. + * username and password are identicals. * * @author Julien Fastré */ -class LoadUsers extends AbstractFixture implements ContainerAwareInterface +class LoadUsers extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface { - /** * * @var ContainerInterface @@ -26,15 +31,42 @@ class LoadUsers extends AbstractFixture implements ContainerAwareInterface { return 1000; } - - public function setContainer(ContainerInterface $container = null) - { - $this->container = $container; - } + + public static $refs = array(); public function load(ObjectManager $manager) { + foreach(LoadCenters::$refs as $centerRef) { + foreach(LoadPermissionsGroup::$refs as $permissionGroupRef) { + $user = new User(); + + $permissionGroup = $this->getReference($permissionGroupRef); + $center = $this->getReference($centerRef); + $username = $center->getName().'_'.$permissionGroup->getName(); + + $user->setUsername($username) + ->setPassword($this->container->get('security.encoder_factory') + ->getEncoder($user) + ->encodePassword($username, $user->getSalt())); + $user->addGroupCenter($this->getReference($centerRef.'_'.$permissionGroupRef)); + + $manager->persist($user); + $this->addReference($username, $user); + static::$refs[] = $user->getUsername(); + echo "Creating user with username ".$user->getUsername()."... \n"; + } + } + $manager->flush(); + } + + public function setContainer(ContainerInterface $container = null) + { + if (NULL === $container) { + throw new \LogicException('$container should not be null'); + } + + $this->container = $container; } } diff --git a/Entity/Center.php b/Entity/Center.php index 75edd18c4..fddb54441 100644 --- a/Entity/Center.php +++ b/Entity/Center.php @@ -39,6 +39,17 @@ class Center */ private $id; + /** + * + * @var \Doctrine\Common\Collections\Collection + */ + private $groupCenters; + + public function __construct() + { + $this->groupCenters = new \Doctrine\Common\Collections\ArrayCollection(); + } + public function getName() { return $this->name; @@ -54,6 +65,17 @@ class Center { return $this->id; } + + public function getGroupCenters() + { + return $this->groupCenters; + } + + public function addGroupCenter(GroupCenter $groupCenter) + { + $this->groupCenters->add($groupCenter); + return $this; + } } diff --git a/Entity/GroupCenter.php b/Entity/GroupCenter.php new file mode 100644 index 000000000..d882ab526 --- /dev/null +++ b/Entity/GroupCenter.php @@ -0,0 +1,118 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\Entity; + +use Chill\MainBundle\Entity\Center; +use Chill\MainBundle\Entity\PermissionsGroup; +use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\ArrayCollection; + +/** + * + * + * @author Julien Fastré + */ +class GroupCenter +{ + /** + * + * @var int + */ + private $id; + + /** + * + * @var Center + */ + private $center; + + /** + * + * @var Collection + */ + private $users; + + /** + * + * @var Collection + */ + private $permissionGroups; + + public function __construct() + { + $this->permissionGroups = new ArrayCollection(); + $this->users = new ArrayCollection(); + } + + public function getId() + { + return $this->id; + } + + /** + * + * @return Center + */ + public function getCenter() + { + return $this->center; + } + + /** + * + * @return PermissionGroup[] + */ + public function getPermissionGroups() + { + return $this->permissionGroups; + } + + /** + * + * @param Center $center + * @return \Chill\MainBundle\Entity\GroupCenter + */ + public function setCenter(Center $center) + { + $this->center = $center; + return $this; + } + + /** + * + * @param PermissionGroup $permission + * @return \Chill\MainBundle\Entity\GroupCenter + */ + public function addPermissionGroup(PermissionsGroup $permission) + { + $this->permissionGroups->add($permission); + return $this; + } + + public function getUsers() + { + return $this->users; + } + + + + +} diff --git a/Entity/PermissionGroup.php b/Entity/PermissionsGroup.php similarity index 93% rename from Entity/PermissionGroup.php rename to Entity/PermissionsGroup.php index cf2b76b05..c3f78fcb0 100644 --- a/Entity/PermissionGroup.php +++ b/Entity/PermissionsGroup.php @@ -29,7 +29,7 @@ use Chill\MainBundle\Entity\RoleScope; * * @author Julien Fastré */ -class PermissionGroup +class PermissionsGroup { /** * @@ -39,7 +39,7 @@ class PermissionGroup /** * - * @var array + * @var string */ private $name; @@ -59,6 +59,10 @@ class PermissionGroup return $this->id; } + /** + * + * @return string + */ public function getName() { return $this->name; @@ -69,7 +73,7 @@ class PermissionGroup return $this->roleScopes; } - public function setName(array $name) + public function setName($name) { $this->name = $name; return $this; diff --git a/Entity/User.php b/Entity/User.php index 1eeac1060..76c3f4316 100644 --- a/Entity/User.php +++ b/Entity/User.php @@ -2,14 +2,14 @@ namespace Chill\MainBundle\Entity; - -use Doctrine\ORM\Mapping as ORM; -use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Component\Security\Core\User\AdvancedUserInterface; +use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\ArrayCollection; /** * User */ -class User implements UserInterface { +class User implements AdvancedUserInterface { /** * @var integer @@ -21,14 +21,43 @@ class User implements UserInterface { */ private $username; + /** + * + * @var string + */ private $password; - private $salt; + /** + * + * @var string + * @internal must be set to null if we use bcrypt + */ + private $salt = null; + + /** + * + * @var boolean + */ + private $locked = false; + + /** + * + * @var boolean + */ + private $enabled = true; + + /** + * + * @var Collection + */ + private $groupCenters; + + public function __construct() + { + $this->groupCenters = new ArrayCollection(); + } - - - /** * Get id * @@ -51,16 +80,6 @@ class User implements UserInterface { return $this; } - - /** - * Get name - * - * @return string - */ - public function getName() - { - return $this->username; - } public function __toString() { return $this->getUsername(); @@ -71,6 +90,10 @@ class User implements UserInterface { } + /** + * + * @return string + */ public function getPassword() { return $this->password; @@ -93,7 +116,7 @@ class User implements UserInterface { function setPassword($password) { - $this->password = $password; + $this->password = $password; return $this; } @@ -103,6 +126,63 @@ class User implements UserInterface { return $this; } + /** + * {@inheritdoc} + * + * @return boolean + */ + public function isAccountNonExpired() + { + return false; + } + /** + * {@inheritdoc} + * + */ + public function isAccountNonLocked() + { + return $this->locked; + } + + /** + * {@inheritdoc} + * + * @return boolean + */ + public function isCredentialsNonExpired() + { + return true; + } + + /** + * {@inheritdoc} + * + * @return boolean + */ + public function isEnabled() + { + return $this->enabled; + } + + /** + * + * @return GroupCenter[] + */ + public function getGroupCenters() + { + return $this->groupCenters; + } + + /** + * + * @param \Chill\MainBundle\Entity\GroupCenter $groupCenter + * @return \Chill\MainBundle\Entity\User + */ + public function addGroupCenter(GroupCenter $groupCenter) + { + $this->groupCenters->add($groupCenter); + return $this; + } } diff --git a/Resources/config/doctrine/Center.orm.yml b/Resources/config/doctrine/Center.orm.yml index 491495faf..8ac4355cc 100644 --- a/Resources/config/doctrine/Center.orm.yml +++ b/Resources/config/doctrine/Center.orm.yml @@ -10,4 +10,8 @@ Chill\MainBundle\Entity\Center: fields: name: type: string - length: 255 \ No newline at end of file + length: 255 + oneToMany: + groupCenters: + targetEntity: Chill\MainBundle\Entity\GroupCenter + mappedBy: groupCenters \ No newline at end of file diff --git a/Resources/config/doctrine/GroupCenter.orm.yml b/Resources/config/doctrine/GroupCenter.orm.yml new file mode 100644 index 000000000..37a7a81f5 --- /dev/null +++ b/Resources/config/doctrine/GroupCenter.orm.yml @@ -0,0 +1,16 @@ +Chill\MainBundle\Entity\GroupCenter: + type: entity + table: group_centers + id: + id: + type: integer + id: true + generator: + strategy: AUTO + manyToOne: + center: + targetEntity: Chill\MainBundle\Entity\Center + inversedBy: groupCenters + manyToMany: + permissionGroups: + targetEntity: Chill\MainBundle\Entity\PermissionsGroup \ No newline at end of file diff --git a/Resources/config/doctrine/PermissionGroup.orm.yml b/Resources/config/doctrine/PermissionsGroup.orm.yml similarity index 76% rename from Resources/config/doctrine/PermissionGroup.orm.yml rename to Resources/config/doctrine/PermissionsGroup.orm.yml index f7c7de5ec..c0ebe69fc 100644 --- a/Resources/config/doctrine/PermissionGroup.orm.yml +++ b/Resources/config/doctrine/PermissionsGroup.orm.yml @@ -1,4 +1,4 @@ -Chill\MainBundle\Entity\PermissionGroup: +Chill\MainBundle\Entity\PermissionsGroup: type: entity table: permission_groups id: @@ -9,7 +9,8 @@ Chill\MainBundle\Entity\PermissionGroup: strategy: AUTO fields: name: - type: json_array + type: string + length: 255 manyToMany: roleScopes: targetEntity: Chill\MainBundle\Entity\RoleScope diff --git a/Resources/config/doctrine/RoleScope.orm.yml b/Resources/config/doctrine/RoleScope.orm.yml index 32ecd4f86..4ab688b7b 100644 --- a/Resources/config/doctrine/RoleScope.orm.yml +++ b/Resources/config/doctrine/RoleScope.orm.yml @@ -11,7 +11,7 @@ Chill\MainBundle\Entity\RoleScope: role: type: string length: 255 - oneToMany: + manyToOne: scope: targetEntity: Chill\MainBundle\Entity\Scope - mappedBy: roleScopes \ No newline at end of file + inversedBy: roleScopes \ No newline at end of file diff --git a/Resources/config/doctrine/User.orm.yml b/Resources/config/doctrine/User.orm.yml index 7edb8230a..924b85ac1 100644 --- a/Resources/config/doctrine/User.orm.yml +++ b/Resources/config/doctrine/User.orm.yml @@ -17,5 +17,15 @@ Chill\MainBundle\Entity\User: salt: type: string length: 255 + nullable: true + enabled: + type: boolean + default: true + locked: + type: boolean + default: false + manyToMany: + groupCenters: + targetEntity: Chill\MainBundle\Entity\GroupCenter From 96b98c9e9d65d93b17b5a050cc0fbc11bb3421c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 4 Nov 2014 21:44:13 +0100 Subject: [PATCH 4/5] correct travis path --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 573e9287a..faf08fc08 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,4 @@ ChillMain An app for social-profit organisations -[![Build Status](https://travis-ci.org/Chill-project/Main.png)](http://travis-ci.org/#!/Champs-Libres/ChillMain) +[![Build Status](https://travis-ci.org/Chill-project/Main.png)](http://travis-ci.org/#!/Chill-project/Main.png) From e41ba817810764d6fd1042f8c5e26ecd0fcff4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 4 Nov 2014 21:44:55 +0100 Subject: [PATCH 5/5] fix typo in license --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7aa6cfe73..5fab1a8b9 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "chill-project/main", - "license": "GPL-3.0", + "license": "AGPL-3.0", "type": "symfony-bundle", "description": "The main bundle for the Chill App", "keywords" : ["chill", "social work"],