protected function validate(\ICanboogie\Errors $errors) { global $core; $request = $this->request; $token = $request['token']; if (!$token) { $errors['token'] = $this->t("The nonce login Token is required."); return false; } $this->ticket = $ticket = $core->models['users.noncelogin']->filter_by_token($token)->one; if (!$ticket) { $errors['token'] = $this->t("Unknown token."); return false; } if ($ticket->expire_at < DateTime::now()) { $errors['expire_at'] = $this->t("This nonce login ticket has expired at :date.", array(':date' => $ticket->expire_at->local->as_db)); return false; } if ($ticket->ip != $request->ip) { $errors['ip'] = $this->t("The IP address doesn't match the one of the initial request."); return false; } try { $ticket->user; } catch (RecordNotFound $e) { $errors['uid'] = $this->t("The user associated with this nonce login no longer exists."); return false; } return true; }
public function test_expire_at() { $t = new Ticket(); $d = $t->expire_at; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertTrue($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('0000-00-00 00:00:00', $d->as_db); $t->expire_at = '2013-03-07 18:30:45'; $d = $t->expire_at; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertFalse($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('2013-03-07 18:30:45', $d->as_db); $t->expire_at = new DateTime('2013-03-07 18:30:45', 'utc'); $d = $t->expire_at; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertFalse($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('2013-03-07 18:30:45', $d->as_db); $t->expire_at = null; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $t->expire_at = DateTime::now(); $properties = $t->__sleep(); $this->assertArrayHasKey('expire_at', $properties); $array = $t->to_array(); $this->assertArrayHasKey('expire_at', $array); }
protected function lazy_get_properties() { $properties = parent::lazy_get_properties(); if (!$this->key) { $properties['created'] = DateTime::now(); } return $properties; }
/** * Adds the `status` and `notify` properties if they are not defined, they default to * `pending` and `no`. * * @throws \InvalidArgumentException if the value of the `notify` property is not one of `no`, * `yes`, `author` or `done`. */ public function save(array $properties, $key = null, array $options = array()) { $properties += array(Comment::CREATED => DateTime::now(), Comment::STATUS => 'pending', Comment::NOTIFY => 'no'); if (!in_array($properties[Comment::NOTIFY], array('no', 'yes', 'author', 'done'))) { throw new \InvalidArgumentException(\ICanBoogie\format('Invalid value for property %property: %value', array('%property' => Comment::NOTIFY, '%value' => $properties[Comment::NOTIFY]))); } return parent::save($properties, $key, $options); }
protected function lazy_get_properties() { $properties = parent::lazy_get_properties(); unset($properties['user_id']); if (!$this->key) { $properties['user_id'] = $this->app->user->id; $properties['created'] = DateTime::now(); } return $properties; }
/** * Overrides the method to handle the following properties: * * `uid`: Only users with the PERMISSION_ADMINISTER permission can choose the user of records. * If the user saving a record has no such permission, the Node::UID property is removed from * the properties created by the parent method. * * `siteid`: If the user is creating a new record or the user has no permission to choose the * record's site, the property is set to the value of the working site's id. * * Also, the following default values are used: * * - `uid`: 0 * - `nativeid`: 0 * - `language`: an empty string */ protected function lazy_get_properties() { global $core; $properties = parent::lazy_get_properties() + [Node::UID => 0, Node::NATIVEID => 0, Node::LANGUAGE => '']; $user = $core->user; if (!$user->has_permission(Module::PERMISSION_ADMINISTER, $this->module)) { unset($properties[Node::UID]); } if (!$this->key || !$user->has_permission(Module::PERMISSION_MODIFY_BELONGING_SITE)) { $properties[Node::SITEID] = $core->site_id; } $properties[Node::UPDATED_AT] = DateTime::now(); return $properties; }
public function test_process() { $request = Request::from(['is_post' => true, 'request_params' => [Operation::DESTINATION => 'nodes', Operation::NAME => 'save', 'title' => "My Example"]]); $operation = new FakeSaveOperation(); $response = $operation($request); $record = $operation->record; $this->assertEmpty($record->uid); $this->assertEquals(1, $record->siteid); $this->assertNotEmpty($record->uuid); $this->assertEquals("My Example", $record->title); $this->assertEquals("my-example", $record->slug); $this->assertEquals("nodes", $record->constructor); $this->assertEmpty($record->is_online); $this->assertEmpty($record->nativeid); $this->assertEquals(DateTime::now()->utc, $record->created_at); $this->assertEquals(DateTime::now()->utc, $record->updated_at); }
public static function markup_time_ago(array $args, $engine, $template) { static $properties = ['y' => "year", 'm' => "month", 'd' => "day", 'h' => "hour", 'i' => "minute", 's' => "second"]; $datetime = $args['select']; if (!$datetime) { return null; } $diff = DateTime::now()->diff($datetime); foreach ($properties as $property => $name) { $value = $diff->{$property}; if (!$value) { continue; } if ($property == 's' && $value < 60) { $str = "Just now"; } else { $str = $value == 1 ? "one {$name} ago" : "{$value} " . \ICanBoogie\pluralize($name) . " ago"; } return $template ? $engine($template, $str) : $str; } }
/** * Sets the datetime in a property. * * @param mixed $property Reference to the property to set. * @param mixed $datetime Date and time. */ public static function set(&$property, $datetime) { $property = $datetime === 'now' ? DateTime::now() : $datetime; }
/** * The following headers are always modified: * * - `Cache-Control`: sets _cacheable_ to _public_. * - `Expires`: is set to "+1 month". * * If the status code is {@link Stauts::NOT_MODIFIED} the following headers are unset: * * - `Content-Type` * - `Content-Length` * * Otherwise, the following header is set: * * - `Content-Type`: * * @inheritdoc */ protected function finalize(Headers &$headers, &$body) { parent::finalize($headers, $body); $status = $this->status->code; $expires = $this->expires; $headers['Expires'] = $expires; $headers['Cache-Control']->cacheable = 'public'; $headers['Cache-Control']->max_age = $expires->timestamp - DateTime::now()->timestamp; $headers['Content-Type'] = $this->content_type; $headers['Etag'] = $this->etag; if ($status === Status::NOT_MODIFIED) { $this->finalize_for_not_modified($headers); return; } if ($status === Status::PARTIAL_CONTENT) { $this->finalize_for_partial_content($headers); return; } $this->finalize_for_other($headers); }
public function test_date() { $r = new Content(); $d = $r->date; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertTrue($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('0000-00-00 00:00:00', $d->as_db); $r->date = '2013-03-07 18:30:45'; $d = $r->date; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertFalse($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('2013-03-07 18:30:45', $d->as_db); $r->date = new DateTime('2013-03-07 18:30:45', 'utc'); $d = $r->date; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertFalse($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('2013-03-07 18:30:45', $d->as_db); $r->date = null; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $r->date = DateTime::now(); $properties = $r->__sleep(); $this->assertArrayHasKey('date', $properties); $array = $r->to_array(); $this->assertArrayHasKey('date', $array); }
public function test_create_at_MUST_be_modified_during_save() { $node = new Node(); $node->title = "Example"; $this->assertTrue($node->updated_at->is_empty); $node->save(); $now = DateTime::now()->utc; $this->assertFalse($node->created_at->is_empty); $this->assertEquals($now, $node->created_at); $node->updated_at = '-2 week'; $node->save(); $this->assertEquals($now, $node->created_at); $node->delete(); }
protected function alter_persistent_properties(array $properties, \ICanBoogie\ActiveRecord\Model $model) { $properties = parent::alter_persistent_properties($properties, $model); if (!$this->result_id && $properties['created_at']->is_empty) { $properties['created_at'] = DateTime::now(); } if ($properties['updated_at']->is_empty) { $properties['updated_at'] = DateTime::now(); } return $properties; }