v. 0.6.3 from http://dev.piwik.org/trac/ticket/598
[piwik-CASLogin.git] / Controller.php
1 <?php
2 /**
3  * Piwik - Open source web analytics
4  *
5  * @link http://piwik.org
6  * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
7  * @version $Id: Controller.php 943 2009-03-01 23:36:36Z matt $
8  *
9  * @package Piwik_CASLogin
10  */
11
12 require PIWIK_INCLUDE_PATH . '/plugins/UsersManager/API.php';
13 require PIWIK_INCLUDE_PATH . '/core/View.php';
14
15 /**
16  * @package Piwik_CASLogin
17  */
18 class Piwik_CASLogin_Controller extends Piwik_Controller
19 {
20         public function index()
21         {
22                 Piwik::redirectToModule('CoreHome');
23         }
24         
25         /**
26          * Configure common view properties
27          *
28          * @param Piwik_View $view
29          */
30         private function configureView($view)
31         {
32                 
33                 $this->setBasicVariablesView($view);
34                 $view->linkTitle = Piwik::getRandomTitle();
35
36                 $enableFramedLogins = Zend_Registry::get('config')->General->enable_framed_logins;
37                 $view->enableFramedLogins = $enableFramedLogins;
38                 if(!$enableFramedLogins)
39                 {
40                         $view->setXFrameOptions('sameorigin');
41                 }
42                 $view->forceSslLogin = Zend_Registry::get('config')->General->force_ssl_login;
43                 // crsf token: don't trust the submitted value; generate/fetch it from session data
44                 $view->nonce = Piwik_Nonce::getNonce('Piwik_Login.login');
45         }
46     
47         /**
48          * Login form
49          *
50          * @param string $messageNoAccess Access error message
51          * @param string $currentUrl Current URL
52          * @return void
53          */
54         function login($messageNoAccess = null)
55         {
56                 $view = Piwik_View::factory('login');
57                 $view->AccessErrorString = $messageNoAccess;
58                 $view->linkTitle = Piwik::getRandomTitle();
59                 $view->subTemplate = 'genericForm.tpl';
60                 $this->configureView($view);
61                 echo $view->render();
62         }
63     
64     public function redirectToCAS() {
65                 // This is simply if we are coming back from CAS.
66         // the actual redirect happens in the authentication class.
67         if(Piwik::getCurrentUserLogin() != 'anonymous') {
68             Piwik::redirectToModule('CoreHome');
69         }
70     }
71
72         private function clearSession()
73         {       
74                 /* Note: some browsers don't respect server revokation */
75                 $auth = Zend_Registry::get('auth');
76                 $auth->setLogin(null);
77                 $auth->setTokenAuth(null);
78
79                 $access = Zend_Registry::get('access');
80                 $access->reloadAccess($auth);
81
82         $authCookieName = Zend_Registry::get('config')->General->login_cookie_name;
83         $cookie = new Piwik_Cookie($authCookieName);
84         $cookie->delete();
85
86                 @Zend_Session::destroy(true);
87         }
88         
89         public function logout()
90         {
91         phpCAS::logoutWithUrl(Piwik_Url::getCurrentUrlWithoutQueryString() );
92         }
93 }