From b2bfd4e4111bd7af983681abd4fdd7ef05fe5731 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Fri, 4 Jan 2013 10:57:25 +0100 Subject: [PATCH] Initial session support. --- composer.json | 2 +- composer.lock | 15 +-- src/config/development.php | 4 + src/lib/Application.php | 55 +++++++++-- src/lib/DI.php | 16 +++- src/templates/_base.html | 148 +++++++++++++++++++----------- src/templates/_generic/error.html | 44 +++++++++ src/templates/server.html | 4 +- src/www/js/xo.js | 34 ++++--- src/www/session.php | 49 ++++++++++ 10 files changed, 282 insertions(+), 89 deletions(-) create mode 100644 src/templates/_generic/error.html create mode 100644 src/www/session.php diff --git a/composer.json b/composer.json index 67dd326339..99745ac6c0 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "require": { "php": ">=5.3.0", "monolog/monolog": "~1.2", - "vates/gallic": "~0.2.4", + "vates/gallic": "dev-master", "vates/xo-lib": "dev-master" }, "autoload": { diff --git a/composer.lock b/composer.lock index 4a2970f675..4ac0342e52 100644 --- a/composer.lock +++ b/composer.lock @@ -1,5 +1,5 @@ { - "hash": "5ad48a363a48c325678f1c0fc47f628a", + "hash": "c53a9b0dd6e704e02bed50b5894b3b56", "packages": [ { "name": "monolog/monolog", @@ -60,24 +60,24 @@ }, { "name": "vates/gallic", - "version": "v0.2.4", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/julien-f/gallic", - "reference": "v0.2.4" + "reference": "030f63592d85929cdd20d07122aa66a906b3aee3" }, "dist": { "type": "zip", - "url": "https://github.com/julien-f/gallic/archive/v0.2.4.zip", - "reference": "v0.2.4", + "url": "https://github.com/julien-f/gallic/archive/030f63592d85929cdd20d07122aa66a906b3aee3.zip", + "reference": "030f63592d85929cdd20d07122aa66a906b3aee3", "shasum": "" }, "require": { "php": ">=5.2.0" }, - "time": "2012-12-17 14:25:36", + "time": "2013-01-04 09:42:19", "type": "library", - "installation-source": "dist", + "installation-source": "source", "autoload": { "files": [ "src/Gallic.php" @@ -148,6 +148,7 @@ ], "minimum-stability": "stable", "stability-flags": { + "vates/gallic": 20, "vates/xo-lib": 20 } } diff --git a/src/config/development.php b/src/config/development.php index 37d836e3cc..ffe68b7788 100644 --- a/src/config/development.php +++ b/src/config/development.php @@ -24,6 +24,10 @@ return array( + 'xo' => array( + 'url' => 'tcp://localhost:1024', + ), + 'log' => array( //'email' => 'your.email@provider.tld', 'file' => '#{root_dir}/log', diff --git a/src/lib/Application.php b/src/lib/Application.php index c688240f7a..43ba9c546f 100644 --- a/src/lib/Application.php +++ b/src/lib/Application.php @@ -34,26 +34,67 @@ final class Application extends Base { parent::__construct(); + session_start(); + $this->_di = $di; } + /** + * + */ + function getCurrentUser() + { + return isset($_SESSION['user']['name']) + ? $_SESSION['user']['name'] + : false; + } + /** * */ function getTemplate($template) { $template = $this->_di->get('template.manager')->build($template); - $template->filters += array( - 'count' => 'count', - 'json' => 'json_encode', - ); - $template->functions += array( - 'url' => array('TemplateUtils', 'url'), - ); + $template->variables['user'] = $this->getCurrentUser(); return $template; } + /** + * + */ + function logIn($name, $password) + { + $xo = $this->_di->get('xo'); + + if (!$xo->logIn($name, $password)) + { + return false; + } + $_SESSION['user']['name'] = $name; + $_SESSION['user']['password'] = $password; + + return true; + } + + /** + * + */ + function logOut() + { + session_destroy(); + + return true; + } + + /** + * + */ + function redirect($url) + { + header("Location: $url"); + } + /** * @var DI */ diff --git a/src/lib/DI.php b/src/lib/DI.php index f14fbfc310..a6cd8b106a 100644 --- a/src/lib/DI.php +++ b/src/lib/DI.php @@ -107,9 +107,23 @@ final class DI extends Base private function _init_template_manager() { - return new Gallic_Template_Manager( + $tm = new Gallic_Template_Manager( __DIR__.'/../templates', 0 ); + $tm->defaultFilters += array( + 'count' => 'count', + 'json' => 'json_encode', + ); + $tm->defaultFunctions += array( + 'url' => array('TemplateUtils', 'url'), + ); + + return $tm; + } + + private function _init_xo() + { + return new XO($this->get('config')->get('xo.url')); } } diff --git a/src/templates/_base.html b/src/templates/_base.html index 46d5f4642d..1c2c768ea9 100644 --- a/src/templates/_base.html +++ b/src/templates/_base.html @@ -42,7 +42,7 @@ {* IE support. *} - + {* HTML5 shim, for IE6-8 support of HTML5 elements *} @@ -52,80 +52,116 @@ {* Our stylesheet and scripts. *} - + {* Children templates may used this block to add extra headers. *} {block extra_html_headers}{/block} - {* /.navbar *} + {block body}{/block} - - + + {* JS Placed at the end of the document so the pages load faster *} diff --git a/src/templates/_generic/error.html b/src/templates/_generic/error.html new file mode 100644 index 0000000000..e89c178d74 --- /dev/null +++ b/src/templates/_generic/error.html @@ -0,0 +1,44 @@ +{** + * This file is a part of Xen Orchestra Web. + * + * Xen Orchestra Web 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. + * + * Xen Orchestra Web 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 Xen Orchestra Web. If not, see + * . + * + * @author Olivier Lambert + * @license http://www.gnu.org/licenses/agpl-3.0-standalone.html GNU AGPLv3 + * + * @package Xen Orchestra Web + *} + +{extends "/_base.html"} + +{block title}Xen Orchestra - Home{/block} + +{block body} +
+
+

{if $error}{$error}{else}Unknown error{/if}

+

+ {if $message} + {$message} + {else} + We are sorry but an unknown error just occured. + {/if} +

+

+ You may {if $referer}go back to the previous page{else}return to the homepage{/if} or contact us for more information. +

+
+
+{/block} diff --git a/src/templates/server.html b/src/templates/server.html index b5abb50795..ab72f35c7c 100644 --- a/src/templates/server.html +++ b/src/templates/server.html @@ -143,7 +143,7 @@ - + {* One server details. TODO: dynamic in PHP *}

Cluster 1

- + {* One server details. TODO: dynamic in PHP *}

Cluster 2