diff --git a/lib/classes/ACL.php b/lib/classes/ACL.php index 6ea8c0f3da..e49fcabf74 100644 --- a/lib/classes/ACL.php +++ b/lib/classes/ACL.php @@ -1153,38 +1153,27 @@ class ACL implements cache_cacheableInterface { $this->load_rights_bas(); - $sql_i = "INSERT INTO basusr (base_id, usr_id, actif) VALUES (:base_id, :usr_id, '1')"; - $sql_u = "UPDATE basusr SET UPDATE actif='1' WHERE base_id = :base_id AND usr_id = :usr_id"; - $stmt_i = $this->app->getApplicationBox()->get_connection()->prepare($sql_i); - $stmt_u = $this->app->getApplicationBox()->get_connection()->prepare($sql_u); - $usr_id = $this->user->getId(); foreach ($base_ids as $base_id) { - if (!isset($this->_rights_bas[$base_id]) || $this->_rights_bas[$base_id][self::ACTIF] === false) { - try { - $stmt_i->execute([':base_id' => $base_id, ':usr_id' => $usr_id]); - if($stmt_i->rowCount() > 0) { - $this->app['dispatcher']->dispatch( - AclEvents::ACCESS_TO_BASE_GRANTED, - new AccessToBaseGrantedEvent( - $this, - array( - 'base_id'=>$base_id - ) - ) - ); - } - else { - $stmt_u->execute([':base_id' => $base_id, ':usr_id' => $usr_id]); - } - } - catch(\Exception $e) { - // no-opp - } + if (isset($this->_rights_bas[$base_id]) && $this->_rights_bas[$base_id][self::ACTIF] == true) { + continue; + } + + if($this->try_give_access_to_base_insert($base_id, $usr_id) == true) { + $this->app['dispatcher']->dispatch( + AclEvents::ACCESS_TO_BASE_GRANTED, + new AccessToBaseGrantedEvent( + $this, + array( + 'base_id'=>$base_id + ) + ) + ); + } + else { + $this->try_give_access_to_base_update($base_id, $usr_id); } } - $stmt_u->closeCursor(); - $stmt_i->closeCursor(); $this->delete_data_from_cache(self::CACHE_RIGHTS_BAS); $this->inject_rights(); @@ -1192,6 +1181,45 @@ class ACL implements cache_cacheableInterface return $this; } + private function try_give_access_to_base_insert($base_id, $usr_id) + { + static $stmt = null; + if(!$stmt) { + $sql = "INSERT INTO basusr (base_id, usr_id, actif) VALUES (:base_id, :usr_id, '1')"; + $stmt = $this->app->getApplicationBox()->get_connection()->prepare($sql); + } + $inserted = false; + try { + $stmt->execute([':base_id' => $base_id, ':usr_id' => $usr_id]); + if ($stmt->rowCount() > 0) { + $inserted = true; + } + $stmt->closeCursor(); + } + catch(DBALException $e) { + // no-op, mostly the row did exist + } + + return $inserted; + } + + private function try_give_access_to_base_update($base_id, $usr_id) + { + static $stmt = null; + if(!$stmt) { + $sql = "UPDATE basusr SET UPDATE actif='1' WHERE base_id = :base_id AND usr_id = :usr_id"; + $stmt = $this->app->getApplicationBox()->get_connection()->prepare($sql); + } + + try { + $stmt->execute([':base_id' => $base_id, ':usr_id' => $usr_id]); + $stmt->closeCursor(); + } + catch(DBALException $e) { + // no-op, mostly the row was deleted + } + } + /** * * @param array $sbas_ids diff --git a/templates/web/prod/WorkZone/Story.html.twig b/templates/web/prod/WorkZone/Story.html.twig index 803a8af778..56ab777d0d 100644 --- a/templates/web/prod/WorkZone/Story.html.twig +++ b/templates/web/prod/WorkZone/Story.html.twig @@ -40,7 +40,7 @@ {% endif %} - {% if app.getAclForUser(app.getAuthenticatedUser()).has_right(cnstant('\\ACL::BAS_CHUPUB')) %} + {% if app.getAclForUser(app.getAuthenticatedUser()).has_right(constant('\\ACL::BAS_CHUPUB')) %} diff --git a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/RegistrationManipulatorTest.php b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/RegistrationManipulatorTest.php index 7517ebf6c2..0ee1a18699 100644 --- a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/RegistrationManipulatorTest.php +++ b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/RegistrationManipulatorTest.php @@ -40,11 +40,11 @@ class RegistrationManipulatorTest extends \PhraseanetTestCase $aclMock->expects($this->once())->method('give_access_to_sbas')->with($this->equalTo([self::$DI['collection']->get_sbas_id()])); $aclMock->expects($this->once())->method('give_access_to_base')->with($this->equalTo([self::$DI['collection']->get_base_id()])); $aclMock->expects($this->once())->method('update_rights_to_base')->with($this->equalTo(self::$DI['collection']->get_base_id()), $this->equalTo([ - \ACL::CANPUTINALBUM => '1', - \ACL::CANDWNLDHD => '1', - \ACL::NOWATERMARK => '0', - \ACL::CANDWNLDPREVIEW => '1', - \ACL::ACTIF => '1', + \ACL::CANPUTINALBUM => true, + \ACL::CANDWNLDHD => true, + \ACL::NOWATERMARK => false, + \ACL::CANDWNLDPREVIEW => true, + \ACL::ACTIF => true, ])); $aclProviderMock = $this->getMockBuilder('Alchemy\Phrasea\Authentication\ACLProvider')->disableOriginalConstructor()->getMock(); diff --git a/tests/classes/ACLTest.php b/tests/classes/ACLTest.php index 89f40fe935..bcfae0d306 100644 --- a/tests/classes/ACLTest.php +++ b/tests/classes/ACLTest.php @@ -452,21 +452,21 @@ class ACLTest extends \PhraseanetTestCase 'mask_and' => 42 ] ); - $this->assertEquals('42', $this->object->get_mask_and($base_id)); + $this->assertEquals(42, $this->object->get_mask_and($base_id)); $this->object->update_rights_to_base( $base_id, [ 'mask_and' => 1 ] ); - $this->assertEquals('1', $this->object->get_mask_and($base_id)); + $this->assertEquals(1, $this->object->get_mask_and($base_id)); $this->object->update_rights_to_base( $base_id, [ 'mask_and' => 0 ] ); - $this->assertEquals('0', $this->object->get_mask_and($base_id)); + $this->assertEquals(0, $this->object->get_mask_and($base_id)); } } } @@ -531,8 +531,9 @@ class ACLTest extends \PhraseanetTestCase $this->object->give_access_to_sbas([$databox->get_sbas_id()]); } - if ($n === 0) + if ($n === 0) { $this->fail('Not enough collection to test'); + } $this->object->give_access_to_base($base_ids); $bases = array_keys($this->object->get_granted_base());