public function preActivation(ConnectionInterface $con = null) { try { // Try find Acl DB Model AclQuery::create()->findOne(); CustomerGroupAclQuery::create()->findOne(); } catch (\Exception $e) { $database = new Database($con); $database->insertSql(null, [__DIR__ . DS . 'Config' . DS . 'thelia.sql']); } return true; }
/** * @covers AclXmlFileloader::load() */ public function testLoadModuleWithValidConfigFile() { // get the acl and group acl state $initialAcls = AclQuery::create()->find(); $initialCustomerGroupAcls = CustomerGroupAclQuery::create()->find(); // load a test module with a valid ACL configuration $this->aclXmlFileloader->load($this->getStubModule("ModuleValidConfigFile")); // assert that the initial acl and group acl are still here $finalAcls = AclQuery::create()->find(); foreach ($initialAcls as $acl) { $this->assertTrue($finalAcls->contains($acl)); } $finalCustomerGroupAcls = CustomerGroupAclQuery::create()->find(); foreach ($initialCustomerGroupAcls as $customerGroupAcl) { $this->assertTrue($finalCustomerGroupAcls->contains($customerGroupAcl)); } // ensure that the new ACL were created $this->assertEquals($initialAcls->count() + 2, $finalAcls->count()); $aclA = AclQuery::create()->findOneByCode("-customer-group-acl-unit-test-acl-a-"); $this->assertNotNull($aclA); $aclA->setLocale("en_US"); $this->assertEquals("Test ACL A", $aclA->getTitle()); $this->assertEquals("Test ACL A description.", $aclA->getDescription()); $aclA->setLocale("fr_FR"); $this->assertEquals("ACL de test A", $aclA->getTitle()); $this->assertEquals("Description de l'ACL de test A.", $aclA->getDescription()); $aclB = AclQuery::create()->findOneByCode("-customer-group-acl-unit-test-acl-b-"); $this->assertNotNull($aclB); $aclB->setLocale("en_US"); $this->assertEquals("Test ACL B", $aclB->getTitle()); $this->assertEquals("Test ACL B description.", $aclB->getDescription()); $aclB->setLocale("fr_FR"); $this->assertEquals("ACL de test B", $aclB->getTitle()); $this->assertEquals("Description de l'ACL de test B.", $aclB->getDescription()); // ensure that the group ACL were created $this->assertEquals($initialCustomerGroupAcls->count() + 11, $finalCustomerGroupAcls->count()); // group a $this->assertCustomerGroupAclExistsAndUnique($aclA, self::$testCustomerGroups[0], CustomerGroupAclAccessManager::getAccessPowsValue("VIEW")); $this->assertCustomerGroupAclExistsAndUnique($aclA, self::$testCustomerGroups[0], CustomerGroupAclAccessManager::getAccessPowsValue("CREATE")); $this->assertCustomerGroupAclExistsAndUnique($aclB, self::$testCustomerGroups[0], CustomerGroupAclAccessManager::getAccessPowsValue("VIEW")); // group b $this->assertCustomerGroupAclExistsAndUnique($aclB, self::$testCustomerGroups[1], CustomerGroupAclAccessManager::getAccessPowsValue("VIEW")); $this->assertCustomerGroupAclExistsAndUnique($aclB, self::$testCustomerGroups[1], CustomerGroupAclAccessManager::getAccessPowsValue("CREATE")); $this->assertCustomerGroupAclExistsAndUnique($aclB, self::$testCustomerGroups[1], CustomerGroupAclAccessManager::getAccessPowsValue("UPDATE")); $this->assertCustomerGroupAclExistsAndUnique($aclB, self::$testCustomerGroups[1], CustomerGroupAclAccessManager::getAccessPowsValue("DELETE")); // group c, uses group a accesses $this->assertCustomerGroupAclExistsAndUnique($aclA, self::$testCustomerGroups[2], CustomerGroupAclAccessManager::getAccessPowsValue("VIEW")); $this->assertCustomerGroupAclExistsAndUnique($aclA, self::$testCustomerGroups[2], CustomerGroupAclAccessManager::getAccessPowsValue("CREATE")); $this->assertCustomerGroupAclExistsAndUnique($aclB, self::$testCustomerGroups[2], CustomerGroupAclAccessManager::getAccessPowsValue("VIEW")); // group d, uses group a accesses for acl b $this->assertCustomerGroupAclExistsAndUnique($aclB, self::$testCustomerGroups[3], CustomerGroupAclAccessManager::getAccessPowsValue("VIEW")); }
public function testCreateCustomerGroupAcl() { /** @var Acl $testAcl */ $testAcl = $this->testAcls[0]; /** @var CustomerGroup $testGroup */ $testGroup = self::$testCustomerGroups[2]; $testAccessType = array_rand(CustomerGroupAclAccessManager::getAccessPows(), 1); $createEvent = new CustomerGroupAclEvent($testAcl->getId(), $testGroup->getId(), $testAccessType); $this->dispatcher->dispatch(CustomerGroupAclEvents::CUSTOMER_GROUP_ACL_UPDATE, $createEvent); $groupAcl = CustomerGroupAclQuery::create()->filterByAcl($testAcl)->filterByCustomerGroup($testGroup)->filterByType($testAccessType)->findOne(); $this->assertNotNull($groupAcl); $this->assertEquals(1, $groupAcl->getActivate()); }
/** * Create or toggle a customer group ACL. * * @param CustomerGroupAclEvent $event Customer group ACL event. * * @throws PropelException * * @todo Clarify what this should be doing. */ public function customerGroupAclUpdate(CustomerGroupAclEvent $event) { $customerGroupAcl = CustomerGroupAclQuery::create()->filterByAclId($event->getAclId())->filterByCustomerGroupId($event->getCustomerGroupId())->filterByType($event->getType())->findOne(); if (null === $customerGroupAcl) { $customerGroupAcl = new CustomerGroupAcl(); $customerGroupAcl->setAclId($event->getAclId())->setCustomerGroupId($event->getCustomerGroupId())->setType($event->getType())->setActivate(1)->save(); } else { if ($customerGroupAcl->getActivate() == 1) { $customerGroupAcl->setActivate(0); } else { $customerGroupAcl->setActivate(1); } $customerGroupAcl->save(); } }
/** * Load acl and customer group acl fixtures, in a new transaction level. * They will be rollback on tear-down. */ protected function loadAclFixtures() { Propel::getConnection()->beginTransaction(); $aclXmlFileLoader = new AclXmlFileloader(new Translator($this->container)); $aclXmlFileLoader->load($this->getStubModule("ModuleValidConfigFile")); foreach (static::$TEST_ACL_CODES as $aclCode) { $this->testAcls[] = AclQuery::create()->findOneByCode($aclCode); } foreach (static::$expectedAclFixturesAccesses as $customerGroupCode => $acls) { foreach ($acls as $aclCode => $accesses) { foreach ($accesses as $access) { /** @var CustomerGroupAclQuery $query */ $query = CustomerGroupAclQuery::create(); $query->useCustomerGroupQuery()->filterByCode($customerGroupCode)->endUse(); $query->useAclQuery()->filterByCode($aclCode)->endUse(); $query->filterByType(CustomerGroupAclAccessManager::getAccessPowsValue($access)); $this->testCustomerGroupAcls[] = $query->findOne(); } } } $this->aclFixturesLoaded = true; }
/** * Performs an INSERT on the database, given a CustomerGroupAcl or Criteria object. * * @param mixed $criteria Criteria or CustomerGroupAcl object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(CustomerGroupAclTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from CustomerGroupAcl object } // Set the correct dbName $query = CustomerGroupAclQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this Acl is new, it will return * an empty collection; or if this Acl has previously * been saved, it will retrieve related CustomerGroupAcls from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in Acl. * * @param Criteria $criteria optional Criteria object to narrow the query * @param ConnectionInterface $con optional connection object * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) * @return Collection|ChildCustomerGroupAcl[] List of ChildCustomerGroupAcl objects */ public function getCustomerGroupAclsJoinCustomerGroup($criteria = null, $con = null, $joinBehavior = Criteria::LEFT_JOIN) { $query = ChildCustomerGroupAclQuery::create(null, $criteria); $query->joinWith('CustomerGroup', $joinBehavior); return $this->getCustomerGroupAcls($query, $con); }
/** * Parses accesses of one customer group * Browse access and add them if not already existing * * @param $accesses * An array of all the access in the customergroupacl who is actually parsed * @param Acl $acl Acl propel object for what the access have to be created * @param CustomerGroup $customerGroup CustomerGroup propel object for who the access have to be created * */ protected function parseAccesses($accesses, Acl $acl, CustomerGroup $customerGroup) { /** @var SimpleXMLElement $access */ foreach ($accesses as $access) { if ("access" !== $access->getName()) { return; } if ("ALL" === $access->getAttributeAsPhp('right')) { //Add all access if not already exists foreach ($this->accessPows as $right) { $customerGroupAcl = CustomerGroupAclQuery::create()->filterByAcl($acl)->filterByCustomerGroup($customerGroup)->filterByType($right)->findOneOrCreate(); if (0 !== $customerGroupAcl->getActivate()) { $customerGroupAcl->setActivate(1)->save(); } } return; } //Add specific access if not already exists $customerGroupAcl = CustomerGroupAclQuery::create()->filterByAcl($acl)->filterByCustomerGroup($customerGroup)->filterByType($this->accessPows[$access->getAttributeAsPhp('right')])->findOneOrCreate(); if (0 !== $customerGroupAcl->getActivate()) { $customerGroupAcl->setActivate(1)->save(); } } }
/** * Check if the current user is granted access to a ressource. * * @param string|array $resources Resource name or resources list. * @param string|array $accesses Access name or accesses list. * @param boolean $accessOr Whether to return true if at least one resource/access couple is granted. * * @return boolean Whether access is granted. */ protected function performCheck($resources, $accesses, $accessOr = false) { /** @var Session $session */ $session = $this->request->getSession(); if ($session->getCustomerUser() === null || $session->has(CustomerGroup::getModuleCode()) === false) { return false; } $accessIdsList = []; foreach ($accesses as $access) { $accessIdsList[] = CustomerGroupAclAccessManager::getAccessPowsValue(strtoupper(trim($access))); } $accessIdsList = array_unique($accessIdsList); $groupId = $this->request->getSession()->get(CustomerGroup::getModuleCode())['id']; // For each acl be sure that the current customer has the right access $query = CustomerGroupAclQuery::create()->filterByActivate(1)->filterByCustomerGroupId($groupId)->filterByType($accessIdsList, Criteria::IN)->useAclQuery()->filterByCode($resources, Criteria::IN)->endUse(); $rights = $query->count(); $askedRights = count($resources) * count($accessIdsList); return $accessOr === true && $rights > 0 || $rights === $askedRights; }
/** * Get the associated ChildCustomerGroupAcl object * * @param ConnectionInterface $con Optional Connection object. * @return ChildCustomerGroupAcl The associated ChildCustomerGroupAcl object. * @throws PropelException */ public function getCustomerGroupAcl(ConnectionInterface $con = null) { if ($this->aCustomerGroupAcl === null && $this->id !== null) { $this->aCustomerGroupAcl = ChildCustomerGroupAclQuery::create()->findPk($this->id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aCustomerGroupAcl->addCustomerGroupAclI18ns($this); */ } return $this->aCustomerGroupAcl; }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see CustomerGroupAcl::setDeleted() * @see CustomerGroupAcl::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(CustomerGroupAclTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildCustomerGroupAclQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }