Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add request time
  • Loading branch information
nickvergessen committed Aug 1, 2016
commit e0b5949a9f6ba661c2927d559231289517c0fe34
54 changes: 54 additions & 0 deletions apps/workflowengine/js/requesttimeplugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

(function() {

OCA.WorkflowEngine = OCA.WorkflowEngine || {};
OCA.WorkflowEngine.Plugins = OCA.WorkflowEngine.Plugins || {};

OCA.WorkflowEngine.Plugins.RequestTimePlugin = {
getCheck: function() {
return {
'class': 'OCA\\WorkflowEngine\\Check\\RequestTime',
'name': t('workflowengine', 'Request time'),
'operators': [
{'operator': 'in', 'name': t('workflowengine', 'between')},
{'operator': '!in', 'name': t('workflowengine', 'not between')}
]
};
},
render: function(element, check) {
if (check['class'] !== 'OCA\\WorkflowEngine\\Check\\RequestTime') {
return;
}

var placeholder = '["10:00 Europe\\/Berlin","16:00 Europe\\/Berlin"]'; // FIXME need a time picker JS plugin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MorrisJobke can you help with this maybe? I really suck at such stuff.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

$(element).css('width', '300px')
.attr('placeholder', placeholder)
.attr('title', t('workflowengine', 'Example: {placeholder}', {placeholder: placeholder}, undefined, {escape: false}))
.addClass('has-tooltip')
.tooltip({
placement: 'bottom'
});
}
};
})();

OC.Plugins.register('OCA.WorkflowEngine.CheckPlugins', OCA.WorkflowEngine.Plugins.RequestTimePlugin);
1 change: 1 addition & 0 deletions apps/workflowengine/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function() {
'filesizeplugin',
'filesystemtagsplugin',
'requestremoteaddressplugin',
'requesttimeplugin',
'requesturlplugin',
'requestuseragentplugin',
'usergroupmembershipplugin',
Expand Down
125 changes: 125 additions & 0 deletions apps/workflowengine/lib/Check/RequestTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\WorkflowEngine\Check;


use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\Storage\IStorage;
use OCP\WorkflowEngine\ICheck;

class RequestTime implements ICheck {

const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])';
const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\\\\\/[a-zA-Z\-\_]+)+)';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈 Just let DateTime parse the time stamp and then handle the exceptions that are thrown. This one here is so error prone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normal slashes aren't escaped in JSON BTW ;)


/** @var bool[] */
protected $cachedResults;

/** @var ITimeFactory */
protected $timeFactory;

/**
* @param ITimeFactory $timeFactory
*/
public function __construct(ITimeFactory $timeFactory) {
$this->timeFactory = $timeFactory;
}

/**
* @param IStorage $storage
* @param string $path
*/
public function setFileInfo(IStorage $storage, $path) {
// A different path doesn't change time, so nothing to do here.
}

/**
* @param string $operator
* @param string $value
* @return bool
*/
public function executeCheck($operator, $value) {
$valueHash = md5($value);

if (isset($this->cachedResults[$valueHash])) {
return $this->cachedResults[$valueHash];
}

$timestamp = $this->timeFactory->getTime();

$values = json_decode($value, true);
$timestamp1 = $this->getTimestamp($timestamp, $values[0]);
$timestamp2 = $this->getTimestamp($timestamp, $values[1]);

if ($timestamp1 < $timestamp2) {
$in = $timestamp1 <= $timestamp && $timestamp <= $timestamp2;
} else {
$in = $timestamp1 <= $timestamp || $timestamp <= $timestamp2;
}

return ($operator === 'in') ? $in : !$in;
}

/**
* @param int $currentTimestamp
* @param string $value Format: "H:i e"
* @return int
*/
protected function getTimestamp($currentTimestamp, $value) {
list($time1, $timezone1) = explode(' ', $value);
list($hour1, $minute1) = explode(':', $time1);
$date1 = new \DateTime('now', new \DateTimeZone($timezone1));
$date1->setTimestamp($currentTimestamp);
$date1->setTime($hour1, $minute1);

return $date1->getTimestamp();
}

/**
* @param string $operator
* @param string $value
* @throws \UnexpectedValueException
*/
public function validateCheck($operator, $value) {
if (!in_array($operator, ['in', '!in'])) {
throw new \UnexpectedValueException('Invalid operator', 1);
}

$regexValue = '\"' . self::REGEX_TIME . ' ' . self::REGEX_TIMEZONE . '\"';
$result = preg_match('/^\[' . $regexValue . ',' . $regexValue . '\]$/', $value, $matches);
if (!$result) {
throw new \UnexpectedValueException('Invalid time limits', 2);
}

try {
new \DateTimeZone(stripslashes($matches[3]));
} catch(\Exception $e) {
throw new \UnexpectedValueException('Invalid timezone1', 3);
}

try {
new \DateTimeZone(stripslashes($matches[6]));
} catch(\Exception $e) {
throw new \UnexpectedValueException('Invalid timezone2', 3);
}
}
}
146 changes: 146 additions & 0 deletions apps/workflowengine/tests/Check/RequestTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\WorkflowEngine\Tests\Check;


class RequestTimeTest extends \Test\TestCase {

/** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $timeFactory;

protected function setUp() {
parent::setUp();

$this->timeFactory = $this->getMockBuilder('OCP\AppFramework\Utility\ITimeFactory')
->getMock();
}

public function dataExecuteCheck() {
return [
[json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467870105, false], // 2016-07-07T07:41:45+02:00
[json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467873705, true], // 2016-07-07T08:41:45+02:00
[json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467902505, true], // 2016-07-07T16:41:45+02:00
[json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467906105, false], // 2016-07-07T17:41:45+02:00
[json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467870105, true], // 2016-07-07T07:41:45+02:00
[json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467873705, false], // 2016-07-07T08:41:45+02:00
[json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467902505, false], // 2016-07-07T16:41:45+02:00
[json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467906105, true], // 2016-07-07T17:41:45+02:00

[json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467843105, false], // 2016-07-07T07:41:45+09:30
[json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467846705, true], // 2016-07-07T08:41:45+09:30
[json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467875505, true], // 2016-07-07T16:41:45+09:30
[json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467879105, false], // 2016-07-07T17:41:45+09:30
[json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467843105, true], // 2016-07-07T07:41:45+09:30
[json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467846705, false], // 2016-07-07T08:41:45+09:30
[json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467875505, false], // 2016-07-07T16:41:45+09:30
[json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467879105, true], // 2016-07-07T17:41:45+09:30

[json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467916905, false], // 2016-07-07T07:41:45-11:00
[json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467920505, true], // 2016-07-07T08:41:45-11:00
[json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467949305, true], // 2016-07-07T16:41:45-11:00
[json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467952905, false], // 2016-07-07T17:41:45-11:00
[json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467916905, true], // 2016-07-07T07:41:45-11:00
[json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467920505, false], // 2016-07-07T08:41:45-11:00
[json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467949305, false], // 2016-07-07T16:41:45-11:00
[json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467952905, true], // 2016-07-07T17:41:45-11:00
];
}

/**
* @dataProvider dataExecuteCheck
* @param string $value
* @param int $timestamp
* @param bool $expected
*/
public function testExecuteCheckIn($value, $timestamp, $expected) {
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory);

$this->timeFactory->expects($this->once())
->method('getTime')
->willReturn($timestamp);

$this->assertEquals($expected, $check->executeCheck('in', $value));
}

/**
* @dataProvider dataExecuteCheck
* @param string $value
* @param int $timestamp
* @param bool $expected
*/
public function testExecuteCheckNotIn($value, $timestamp, $expected) {
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory);

$this->timeFactory->expects($this->once())
->method('getTime')
->willReturn($timestamp);

$this->assertEquals(!$expected, $check->executeCheck('!in', $value));
}

public function dataValidateCheck() {
return [
['in', json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin'])],
['!in', json_encode(['08:00 Europe/Berlin', '17:00 America/North_Dakota/Beulah'])],
['in', json_encode(['08:00 America/Port-au-Prince', '17:00 America/Argentina/San_Luis'])],
];
}

/**
* @dataProvider dataValidateCheck
* @param string $operator
* @param string $value
*/
public function testValidateCheck($operator, $value) {
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory);
$check->validateCheck($operator, $value);
}

public function dataValidateCheckInvalid() {
return [
['!!in', json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1, 'Invalid operator'],
['in', json_encode(['28:00 Europe/Berlin', '17:00 Europe/Berlin']), 2, 'Invalid time limits'],
['in', json_encode(['08:00 Europa/Berlin', '17:00 Europe/Berlin']), 3, 'Invalid timezone1'],
['in', json_encode(['08:00 Europe/Berlin', '17:00 Europa/Berlin']), 3, 'Invalid timezone2'],
['in', json_encode(['08:00 Europe/Bearlin', '17:00 Europe/Berlin']), 3, 'Invalid timezone1'],
['in', json_encode(['08:00 Europe/Berlin', '17:00 Europe/Bearlin']), 3, 'Invalid timezone2'],
];
}

/**
* @dataProvider dataValidateCheckInvalid
* @param string $operator
* @param string $value
* @param int $exceptionCode
* @param string $exceptionMessage
*/
public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage) {
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory);

try {
$check->validateCheck($operator, $value);
} catch (\UnexpectedValueException $e) {
$this->assertEquals($exceptionCode, $e->getCode());
$this->assertEquals($exceptionMessage, $e->getMessage());
}
}
}