LoginController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 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 = [];
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  ['invalidParameters' => $invalidParameters]));
113  return false;
114  }
115  }
116  return true;
117  }
118 
119  /**
120  * @see Controller::doExecute()
121  */
122  protected function doExecute($method=null) {
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  'login' => $request->getValue('user'),
132  'password' => $request->getValue('password')
133  ]);
134  }
135  catch (\Exception $ex) {
136  $authUser = null;
137  $this->getLogger()->error("Could not log in: ".$ex);
138  }
139 
140  if ($authUser) {
141  // login succeeded
142  $session->clear();
143  $session->setAuthUser($authUser->getLogin());
144 
145  // return role names of the user
146  $roleNames = [];
147  $roles = $authUser->getRoles();
148  for ($i=0, $count=sizeof($roles); $i<$count; $i++) {
149  $roleNames[] = $roles[$i]->getName();
150  }
151  $response->setValue('roles', $roleNames);
152  $response->setValue('sid', $session->getID());
153 
154  $response->setAction('ok');
155  }
156  else {
157  // login failed
158  $response->addError(ApplicationError::get('AUTHENTICATION_FAILED'));
159  }
160  }
161  elseif ($request->getAction() == 'logout') {
162  // clear all session data
163  $session->destroy();
164 
165  // empty response
166  $response->clearValues();
167  }
168  }
169 }
170 ?>
Session is the interface for session implementations and defines access to session variables.
Definition: Session.php:19
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
Request holds the request values that are used as input to Controller instances.
Definition: Request.php:18
initialize(Request $request, Response $response)
__construct(Session $session, PersistenceFacade $persistenceFacade, PermissionManager $permissionManager, ActionMapper $actionMapper, Localization $localization, Message $message, Configuration $configuration, AuthenticationManager $authenticationManager)
Constructor.
AuthenticationManager implementations are used to handle all authentication requests.
getAction()
Get the name of the action.
Implementations of Configuration give access to the application configuration.
getLogger()
Get the Logger instance.
Definition: Controller.php:267
ApplicationError is used to signal errors that occur while processing a request.
static get($code, $data=null)
Factory method for retrieving a predefined error instance.
PersistenceFacade defines the interface for PersistenceFacade implementations.
getRequest()
Get the Request instance.
Definition: Controller.php:251
Application controllers.
Definition: namespaces.php:3
Controller is the base class of all controllers.
Definition: Controller.php:49
ActionMapper implementations are responsible for instantiating and executing Controllers based on the...
getResponse()
Get the Response instance.
Definition: Controller.php:259
PermissionManager implementations are used to handle all authorization requests.
getSession()
Get the Session instance.
Definition: Controller.php:275
LoginController handles the login process.
Localization defines the interface for storing localized entity instances and retrieving them back.
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23