LoginController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
25 
26 /**
27  * LoginController handles the login process.
28  *
29  * The controller supports the following actions:
30  *
31  * <div class="controller-action">
32  * <div> __Action__ login </div>
33  * <div>
34  * Try to login the user with the given user/password parameters.
35  * | Parameter | Description
36  * |------------------------|-------------------------
37  * | _in_ `user` | The login of the user to log in
38  * | _in_ `password` | The password the user is authenticated with
39  * | _out_ `sid` | The newly established session id
40  * | _out_ `roles` | Array of role names assigned to the logged in user
41  * | __Response Actions__ | |
42  * | `ok` | If login succeeded
43  * </div>
44  * </div>
45  *
46  * <div class="controller-action">
47  * <div> __Action__ logout </div>
48  * <div>
49  * Terminate the user session.
50  * </div>
51  * </div>
52  *
53  * @author ingo herwig <ingo@wemove.com>
54  */
55 class LoginController extends Controller {
56 
57  private $_authenticationManager = null;
58 
59  /**
60  * Constructor
61  * @param $session
62  * @param $persistenceFacade
63  * @param $permissionManager
64  * @param $actionMapper
65  * @param $localization
66  * @param $message
67  * @param $configuration
68  * @param $authenticationManager
69  */
70  public function __construct(Session $session,
71  PersistenceFacade $persistenceFacade,
72  PermissionManager $permissionManager,
73  ActionMapper $actionMapper,
74  Localization $localization,
75  Message $message,
76  Configuration $configuration,
77  AuthenticationManager $authenticationManager) {
78  parent::__construct($session, $persistenceFacade, $permissionManager,
79  $actionMapper, $localization, $message, $configuration);
80  $this->_authenticationManager = $authenticationManager;
81  }
82 
83  /**
84  * @see Controller::initialize()
85  */
86  public function initialize(Request $request, Response $response) {
87  // delete all data, if not in login process
88  if ($request->getAction() != 'login') {
89  $request->clearValues();
90  }
91 
92  parent::initialize($request, $response);
93  }
94 
95  /**
96  * @see Controller::validate()
97  */
98  protected function validate() {
99  $request = $this->getRequest();
100  $response = $this->getResponse();
101  if ($request->getAction() == 'login') {
102  $invalidParameters = array();
103  if(!$request->hasValue('user')) {
104  $invalidParameters[] = 'user';
105  }
106  if(!$request->hasValue('password')) {
107  $invalidParameters[] = 'password';
108  }
109 
110  if (sizeof($invalidParameters) > 0) {
111  $response->addError(ApplicationError::get('PARAMETER_INVALID',
112  array('invalidParameters' => $invalidParameters)));
113  return false;
114  }
115  }
116  return true;
117  }
118 
119  /**
120  * @see Controller::doExecute()
121  */
122  protected function doExecute() {
123  $session = $this->getSession();
124  $request = $this->getRequest();
125  $response = $this->getResponse();
126 
127  if ($request->getAction() == 'login') {
128  // try to login
129  try {
130  $authUser = $this->_authenticationManager->login(
131  $request->getValue('user'), $request->getValue('password'));
132  }
133  catch (\Exception $ex) {
134  $authUser = null;
135  $this->getLogger()->error("Could not log in: ".$ex);
136  }
137 
138  if ($authUser) {
139  // login succeeded
140  $session->clear();
141  $session->setAuthUser($authUser);
142 
143  // return role names of the user
144  $roleNames = array();
145  $roles = $authUser->getRoles();
146  for ($i=0, $count=sizeof($roles); $i<$count; $i++) {
147  $roleNames[] = $roles[$i]->getName();
148  }
149  $response->setValue('roles', $roleNames);
150  $response->setValue('sid', $session->getID());
151 
152  $response->setAction('ok');
153  }
154  else {
155  // login failed
156  $response->addError(ApplicationError::get('AUTHENTICATION_FAILED'));
157  }
158  }
159  elseif ($request->getAction() == 'logout') {
160  // clear all session data
161  $session->destroy();
162 
163  // empty response
164  $response->clearValues();
165  }
166  }
167 }
168 ?>
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
getRequest()
Get the Request instance.
Definition: Controller.php:190
Localization defines the interface for storing localized entity instances and retrieving them back...
AuthenticationManager implementations are used to handle all authentication requests.
Controller is the base class of all controllers.
Definition: Controller.php:48
LoginController handles the login process.
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23
getAction()
Get the name of the action.
Session is the interface for session implementations and defines access to session variables...
Definition: Session.php:21
Request holds the request values that are used as input to Controller instances.
Definition: Request.php:20
initialize(Request $request, Response $response)
__construct(Session $session, PersistenceFacade $persistenceFacade, PermissionManager $permissionManager, ActionMapper $actionMapper, Localization $localization, Message $message, Configuration $configuration, AuthenticationManager $authenticationManager)
Constructor.
PermissionManager implementations are used to handle all authorization requests.
Implementations of Configuration give access to the application configuration.
Application controllers.
Definition: namespaces.php:3
ActionMapper implementations are responsible for instantiating and executing Controllers based on the...
static get($code, $data=null)
Factory method for retrieving a predefind error instance.
PersistenceFacade defines the interface for PersistenceFacade implementations.
getLogger()
Get the Logger instance.
Definition: Controller.php:206
getResponse()
Get the Response instance.
Definition: Controller.php:198
getSession()
Get the Session instance.
Definition: Controller.php:214