wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.LoginController.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.LoginController.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/presentation/class.Controller.php");
20 require_once(BASE."wcmf/lib/util/class.InifileParser.php");
21 require_once(BASE."wcmf/lib/security/class.AuthUser.php");
22 require_once(BASE."wcmf/lib/security/class.UserManager.php");
23 require_once(BASE."wcmf/lib/persistence/class.LockManager.php");
24 require_once(BASE."wcmf/lib/util/class.SessionData.php");
25 
26 /**
27  * @class LoginController
28  * @ingroup Controller
29  * @brief LoginController is a controller that handles the login process.
30  *
31  * <b>Input actions:</b>
32  * - @em login Present the login dialog
33  * - @em dologin Try to login the user with the given credentials
34  * - @em logout Terminate the user session
35  *
36  * <b>Output actions:</b>
37  * - @em ok If login succeeded
38  * - @em login If login failed
39  *
40  * @param[in] login The users login name
41  * @param[in] password The user's password
42  * @param[in] remember_me If given with any value a login cookie will be created in the browser
43  * @param[in] password_is_encrypted True/False wether the given password is encrypted on not (default: false)
44  * @param[out] loginmessage A message if login failed
45  *
46  * @author ingo herwig <ingo@wemove.com>
47  */
49 {
50  var $_anonymous = 0; // in anonymous mode all authorization requests answered positive
51  // and AuthUser is an instance of AnonymousUser
52  // The mode is set in configuration section 'cms' key 'anonymous'
53  var $NUM_LOGINTRIES_VARNAME = 'LoginController.logintries';
54  var $LOGINMESSAGE_VARNAME = 'LoginController.loginmessage';
55 
56  /**
57  * @see Controller::initialize()
58  */
59  function initialize(&$request, &$response)
60  {
61  // delete all data, if not in login process
62  if ($request->getAction() != 'dologin')
63  $request->clearValues();
64 
65  $parser = &InifileParser::getInstance();
66  $this->_anonymous = $parser->getValue('anonymous', 'cms');
67 
68  parent::initialize($request, $response);
69  }
70  /**
71  * @see Controller::validate()
72  */
73  function validate()
74  {
75  if ($this->_request->getAction() == 'dologin' && !$this->_anonymous)
76  {
77  if(!$this->_request->hasValue('login'))
78  {
79  $this->setErrorMsg("No 'login' given in data.");
80  return false;
81  }
82  if(!$this->_request->hasValue('password'))
83  {
84  $this->setErrorMsg("No 'password' given in data.");
85  return false;
86  }
87  }
88  return true;
89  }
90  /**
91  * @see Controller::hasView()
92  */
93  function hasView()
94  {
95  if ($this->_request->getAction() == 'dologin' || $this->_anonymous || $this->isCookieLogin())
96  return false;
97  else
98  return true;
99  }
100  /**
101  * If called with any usr_action except 'dologin' this Controller presents the login dialog else
102  * if usr_action is 'dologin' it checks the login data ('login' & 'password') and creates AuthUser object in the Session on
103  * success.
104  * @return Array of given context and action 'ok' on success, action 'failure' on failure.
105  * False if the login dialog is presented (Stop action processing chain).
106  * In case of 'failure' a detailed description is provided by getErrorMsg().
107  * @see Controller::executeKernel()
108  */
109  function executeKernel()
110  {
111  $session = &SessionData::getInstance();
112 
113  // return immediately if anonymous
114  if ($this->_anonymous)
115  {
116  $this->_response->setAction('ok');
117  return true;
118  }
119 
120  if ($this->_request->getAction() == 'login')
121  {
122  // preserve login failure details
123  $loginTries = $session->get($this->NUM_LOGINTRIES_VARNAME);
124  $loginMessage = $session->get($this->LOGINMESSAGE_VARNAME);
125  $session->clear();
126  $session->set($this->NUM_LOGINTRIES_VARNAME, $loginTries);
127  $session->set($this->LOGINMESSAGE_VARNAME, $loginMessage);
128  }
129 
130  if ($this->_request->getAction() == 'dologin')
131  {
132  // create AuthUser instance
133  $authUser = new AuthUser();
134 
135  $isPasswordEncrypted = false;
136  if ($this->_request->hasValue('password_is_encrypted'))
137  $isPasswordEncrypted = $this->_request->getValue('password_is_encrypted');
138 
139  if ($authUser->login($this->_request->getValue('login'), $this->_request->getValue('password'), $isPasswordEncrypted))
140  {
141  // login succeeded
142  $session->clear();
143  $session->set(RightsManager::getAuthUserVarname(), $authUser);
144 
145  // did this user check the 'remember me' checkbox?
146  if($this->_request->getValue('remember_me'))
147  {
148  // if yes store the password login combination in a cookie
149  $expire = time() + 1728000; // expire in 20 days
150  $cookiePassword = UserManager::encryptPassword($this->_request->getValue('password'));
151 
152  setcookie('login', $this->_request->getValue('login'), $expire);
153  setcookie('password', $cookiePassword, $expire);
154  }
155  $this->_response->setAction('ok');
156  return true;
157  }
158  else
159  {
160  // login failed
161  $logintries = $session->get($this->NUM_LOGINTRIES_VARNAME)+1;
162  $session->set($this->NUM_LOGINTRIES_VARNAME, $logintries);
163  $this->setErrorMsg(Message::get("Login failed. Try again."));
164  $session->set($this->LOGINMESSAGE_VARNAME, $this->getErrorMsg());
165 
166  $this->_response->setAction('login');
167  return true;
168  }
169  }
170  elseif ($this->_request->getAction() == 'logout')
171  {
172  // release all locks
173  $lockManager = &LockManager::getInstance();
174  $lockManager->releaseAllLocks();
175 
176  // delete cookies (also clientside)
177  setcookie('login', '', time()-3600, '/');
178  setcookie('password', '', time()-3600, '/');
179  setcookie(session_name(), '', time()-3600, '/');
180  if ($this->_response->getFormat() == MSG_FORMAT_HTML) {
181  print '<script type="text/javascript">
182  document.cookie = "login=; expires=Wed, 1 Mar 2006 00:00:00";
183  document.cookie = "password=; expires=Wed, 1 Mar 2006 00:00:00";
184  </script>';
185  }
186  // clear all session data
187  $session->destroy();
188 
189  // empty response
190  $this->_response->clearValues();
191  return false;
192  }
193  else
194  {
195  // check if the login and password is stored in a cookie
196  if ($this->isCookieLogin())
197  {
198  // if yes redirect to login process
199  $this->_response->setValue('login', $_COOKIE['login']);
200  $this->_response->setValue('password', $_COOKIE['password']);
201  $this->_response->setValue('password_is_encrypted', true);
202 
203  $this->_response->setAction('dologin');
204  return true;
205  }
206 
207  // present login dialog
208  $loginMessage = $session->get($this->LOGINMESSAGE_VARNAME);
209  if (strlen($loginMessage) > 0)
210  {
211  $msg = $loginMessage;
212  if ($session->exist($this->NUM_LOGINTRIES_VARNAME))
213  $msg .= " (".Message::get("Attempt")." #".($session->get($this->NUM_LOGINTRIES_VARNAME)+1).")";
214  $this->_response->setValue('loginmessage', $msg);
215  $this->setErrorMsg($loginMessage);
216  }
217  return false;
218  }
219  }
220 
221  /**
222  * Check if the user logs in via cookies
223  * @return True/False
224  */
225  function isCookieLogin()
226  {
227  return ($this->_request->getAction() == 'login' && isset($_COOKIE['login'], $_COOKIE['password']));
228  }
229 }
230 ?>
AuthUser provides a storage and methods for user data used for authentication/authorization purposes...
get($message, $parameters=null, $domain='', $lang='')
initialize(&$request, &$response)
LoginController is a controller that handles the login process.
Controller is the base class of all controllers. If a Controller has a view it is expected to reside ...
encryptPassword($password)
const MSG_FORMAT_HTML