Merge pull request #4825 from owncloud/fix-sharing-expiration-oracle

Fix Sharing "Expiration Date" on Oracle
This commit is contained in:
Bart Visscher 2013-09-16 12:16:44 -07:00
commit 9d18e16c77
5 changed files with 218 additions and 11 deletions

View File

@ -75,6 +75,7 @@ class OC_DB {
// do nothing if the connection already has been established // do nothing if the connection already has been established
if (!self::$connection) { if (!self::$connection) {
$config = new \Doctrine\DBAL\Configuration(); $config = new \Doctrine\DBAL\Configuration();
$eventManager = new \Doctrine\Common\EventManager();
switch($type) { switch($type) {
case 'sqlite': case 'sqlite':
case 'sqlite3': case 'sqlite3':
@ -123,6 +124,7 @@ class OC_DB {
$connectionParams['port'] = $port; $connectionParams['port'] = $port;
} }
$connectionParams['adapter'] = '\OC\DB\AdapterOCI8'; $connectionParams['adapter'] = '\OC\DB\AdapterOCI8';
$eventManager->addEventSubscriber(new \Doctrine\DBAL\Event\Listeners\OracleSessionInit);
break; break;
case 'mssql': case 'mssql':
$connectionParams = array( $connectionParams = array(
@ -142,7 +144,7 @@ class OC_DB {
$connectionParams['wrapperClass'] = 'OC\DB\Connection'; $connectionParams['wrapperClass'] = 'OC\DB\Connection';
$connectionParams['tablePrefix'] = OC_Config::getValue('dbtableprefix', 'oc_' ); $connectionParams['tablePrefix'] = OC_Config::getValue('dbtableprefix', 'oc_' );
try { try {
self::$connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); self::$connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config, $eventManager);
if ($type === 'sqlite' || $type === 'sqlite3') { if ($type === 'sqlite' || $type === 'sqlite3') {
// Sqlite doesn't handle query caching and schema changes // Sqlite doesn't handle query caching and schema changes
// TODO: find a better way to handle this // TODO: find a better way to handle this

View File

@ -178,4 +178,25 @@
</declaration> </declaration>
</table> </table>
<table>
<name>*dbprefix*timestamp</name>
<declaration>
<field>
<name>id</name>
<autoincrement>1</autoincrement>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<length>4</length>
</field>
<field>
<name>timestamptest</name>
<type>timestamp</type>
<default></default>
<notnull>false</notnull>
</field>
</declaration>
</table>
</database> </database>

View File

@ -75,4 +75,25 @@
</table> </table>
<table>
<name>*dbprefix*timestamp</name>
<declaration>
<field>
<name>id</name>
<autoincrement>1</autoincrement>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<length>4</length>
</field>
<field>
<name>timestamptest</name>
<type>timestamp</type>
<default></default>
<notnull>false</notnull>
</field>
</declaration>
</table>
</database> </database>

View File

@ -145,4 +145,42 @@ class Test_DB extends PHPUnit_Framework_TestCase {
$this->assertEquals(1, $result->numRows()); $this->assertEquals(1, $result->numRows());
} }
/**
* Tests whether the database is configured so it accepts and returns dates
* in the expected format.
*/
public function testTimestampDateFormat() {
$table = '*PREFIX*'.$this->test_prefix.'timestamp';
$column = 'timestamptest';
$expectedFormat = 'Y-m-d H:i:s';
$expected = new \DateTime;
$query = OC_DB::prepare("INSERT INTO `$table` (`$column`) VALUES (?)");
$result = $query->execute(array($expected->format($expectedFormat)));
$this->assertEquals(
1,
$result,
"Database failed to accept dates in the format '$expectedFormat'."
);
$id = OC_DB::insertid($table);
$query = OC_DB::prepare("SELECT * FROM `$table` WHERE `id` = ?");
$result = $query->execute(array($id));
$row = $result->fetchRow();
$actual = \DateTime::createFromFormat($expectedFormat, $row[$column]);
$this->assertInstanceOf(
'\DateTime',
$actual,
"Database failed to return dates in the format '$expectedFormat'."
);
$this->assertEquals(
$expected,
$actual,
'Failed asserting that the returned date is the same as the inserted.'
);
}
} }

View File

@ -29,6 +29,8 @@ class Test_Share extends PHPUnit_Framework_TestCase {
protected $group1; protected $group1;
protected $group2; protected $group2;
protected $resharing; protected $resharing;
protected $dateInFuture;
protected $dateInPast;
public function setUp() { public function setUp() {
OC_User::clearBackends(); OC_User::clearBackends();
@ -58,6 +60,12 @@ class Test_Share extends PHPUnit_Framework_TestCase {
OC::registerShareHooks(); OC::registerShareHooks();
$this->resharing = OC_Appconfig::getValue('core', 'shareapi_allow_resharing', 'yes'); $this->resharing = OC_Appconfig::getValue('core', 'shareapi_allow_resharing', 'yes');
OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes'); OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes');
// 20 Minutes in the past, 20 minutes in the future.
$now = time();
$dateFormat = 'Y-m-d H:i:s';
$this->dateInPast = date($dateFormat, $now - 20 * 60);
$this->dateInFuture = date($dateFormat, $now + 20 * 60);
} }
public function tearDown() { public function tearDown() {
@ -121,6 +129,26 @@ class Test_Share extends PHPUnit_Framework_TestCase {
} }
} }
protected function shareUserOneTestFileWithUserTwo() {
OC_User::setUserId($this->user1);
$this->assertTrue(
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ),
'Failed asserting that user 1 successfully shared text.txt with user 2.'
);
$this->assertEquals(
array('test.txt'),
OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that test.txt is a shared file of user 1.'
);
OC_User::setUserId($this->user2);
$this->assertEquals(
array('test.txt'),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 has access to test.txt after initial sharing.'
);
}
public function testShareWithUser() { public function testShareWithUser() {
// Invalid shares // Invalid shares
$message = 'Sharing test.txt failed, because the user '.$this->user1.' is the item owner'; $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the item owner';
@ -146,10 +174,7 @@ class Test_Share extends PHPUnit_Framework_TestCase {
} }
// Valid share // Valid share
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ)); $this->shareUserOneTestFileWithUserTwo();
$this->assertEquals(array('test.txt'), OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
OC_User::setUserId($this->user2);
$this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
// Attempt to share again // Attempt to share again
OC_User::setUserId($this->user1); OC_User::setUserId($this->user1);
@ -264,6 +289,66 @@ class Test_Share extends PHPUnit_Framework_TestCase {
$this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET)); $this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
} }
public function testShareWithUserExpirationExpired() {
$this->shareUserOneTestFileWithUserTwo();
OC_User::setUserId($this->user1);
$this->assertTrue(
OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast),
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
);
OC_User::setUserId($this->user2);
$this->assertFalse(
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 no longer has access to test.txt after expiration.'
);
}
public function testShareWithUserExpirationValid() {
$this->shareUserOneTestFileWithUserTwo();
OC_User::setUserId($this->user1);
$this->assertTrue(
OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture),
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
);
OC_User::setUserId($this->user2);
$this->assertEquals(
array('test.txt'),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 still has access to test.txt after expiration date has been set.'
);
}
protected function shareUserOneTestFileWithGroupOne() {
OC_User::setUserId($this->user1);
$this->assertTrue(
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ),
'Failed asserting that user 1 successfully shared text.txt with group 1.'
);
$this->assertEquals(
array('test.txt'),
OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that test.txt is a shared file of user 1.'
);
OC_User::setUserId($this->user2);
$this->assertEquals(
array('test.txt'),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 has access to test.txt after initial sharing.'
);
OC_User::setUserId($this->user3);
$this->assertEquals(
array('test.txt'),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 3 has access to test.txt after initial sharing.'
);
}
public function testShareWithGroup() { public function testShareWithGroup() {
// Invalid shares // Invalid shares
$message = 'Sharing test.txt failed, because the group foobar does not exist'; $message = 'Sharing test.txt failed, because the group foobar does not exist';
@ -285,12 +370,7 @@ class Test_Share extends PHPUnit_Framework_TestCase {
OC_Appconfig::setValue('core', 'shareapi_share_policy', $policy); OC_Appconfig::setValue('core', 'shareapi_share_policy', $policy);
// Valid share // Valid share
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ)); $this->shareUserOneTestFileWithGroupOne();
$this->assertEquals(array('test.txt'), OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
OC_User::setUserId($this->user2);
$this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
OC_User::setUserId($this->user3);
$this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
// Attempt to share again // Attempt to share again
OC_User::setUserId($this->user1); OC_User::setUserId($this->user1);
@ -410,4 +490,49 @@ class Test_Share extends PHPUnit_Framework_TestCase {
$this->assertEquals(array(), OCP\Share::getItemsShared('test')); $this->assertEquals(array(), OCP\Share::getItemsShared('test'));
} }
public function testShareWithGroupExpirationExpired() {
$this->shareUserOneTestFileWithGroupOne();
OC_User::setUserId($this->user1);
$this->assertTrue(
OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast),
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
);
OC_User::setUserId($this->user2);
$this->assertFalse(
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 no longer has access to test.txt after expiration.'
);
OC_User::setUserId($this->user3);
$this->assertFalse(
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 3 no longer has access to test.txt after expiration.'
);
}
public function testShareWithGroupExpirationValid() {
$this->shareUserOneTestFileWithGroupOne();
OC_User::setUserId($this->user1);
$this->assertTrue(
OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture),
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
);
OC_User::setUserId($this->user2);
$this->assertEquals(
array('test.txt'),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 still has access to test.txt after expiration date has been set.'
);
OC_User::setUserId($this->user3);
$this->assertEquals(
array('test.txt'),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 3 still has access to test.txt after expiration date has been set.'
);
}
} }