summaryrefslogtreecommitdiff
path: root/autoload/Todo/SessionHelper.php
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2014-03-19 22:45:31 +0100
committerKevin Chabowski <kevin@kch42.de>2014-03-19 22:45:31 +0100
commitefbdc3bbd6cba8691391ec2192ea96adbfb8c029 (patch)
treebe6a69fab84d15d9fc1f8a50a02e8d57e9388c3b /autoload/Todo/SessionHelper.php
downloadtodo-efbdc3bbd6cba8691391ec2192ea96adbfb8c029.tar.gz
todo-efbdc3bbd6cba8691391ec2192ea96adbfb8c029.tar.bz2
todo-efbdc3bbd6cba8691391ec2192ea96adbfb8c029.zip
Initial commit
Diffstat (limited to 'autoload/Todo/SessionHelper.php')
-rw-r--r--autoload/Todo/SessionHelper.php49
1 files changed, 49 insertions, 0 deletions
diff --git a/autoload/Todo/SessionHelper.php b/autoload/Todo/SessionHelper.php
new file mode 100644
index 0000000..b4811ab
--- /dev/null
+++ b/autoload/Todo/SessionHelper.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Todo;
+
+abstract class SessionHelper {
+ protected $user = NULL;
+
+ private function populateLists($f3) {
+ $lists = array();
+ for($l = $this->user->lists(); !$l->dry(); $l->next()) {
+ $lists[] = array(
+ 'id' => $l->id,
+ 'name' => $l->name,
+ 'active' => ($l->id === $f3->get('listid')),
+ );
+ }
+ $f3->set('lists', $lists);
+ }
+
+ public function beforeRoute($f3) {
+ $id = $f3->get('SESSION.userID');
+ if(is_numeric($id) && ($id > 0)) {
+ $this->user = new \Todo\Model\User($f3->get('DB'));
+ if((!$this->user->byID($id)) || (!$this->user->active)) {
+ $this->user = NULL;
+ }
+ }
+ $f3->set('listid', -1);
+ }
+
+ public function afterRoute($f3) {
+ $f3->set('SESSION.userID', ($this->user === NULL) ? -1 : $this->user->id);
+ if($this->user !== NULL) {
+ $f3->set('user', $this->user->name);
+ $this->populateLists($f3);
+ } else {
+ $f3->set('user', '');
+ }
+ }
+
+ protected function needLogin($f3) {
+ if($this->user === NULL) {
+ $f3->set('error', 'You must be logged in to do that');
+ $f3->set('content', 'blank.html');
+ return true;
+ }
+ return false;
+ }
+} \ No newline at end of file