PHRAS-508_acl-cache

- fix unit tests
This commit is contained in:
Jean-Yves Gaulier
2016-10-26 19:14:11 +02:00
parent 0c75189a64
commit 45eea29710
4 changed files with 67 additions and 38 deletions

View File

@@ -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

View File

@@ -40,7 +40,7 @@
<img src="/assets/common/images/icons/feedback16.png"/>
</button>
{% endif %}
{% if app.getAclForUser(app.getAuthenticatedUser()).has_right(cnstant('\\ACL::BAS_CHUPUB')) %}
{% if app.getAclForUser(app.getAuthenticatedUser()).has_right(constant('\\ACL::BAS_CHUPUB')) %}
<button class="ui-corner-all TOOL_bridge_btn story_window" title="{{ 'action : bridge' | trans }}" data-href="{{ path("prod_bridge_manager") }}">
<img src="/assets/common/images/icons/door.png"/>
</button>

View File

@@ -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();

View File

@@ -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());