Skip to content

Commit

Permalink
#977 Default module PHP 8 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
j3nsch committed Jan 6, 2023
1 parent 6bf466f commit 84626f0
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 169 deletions.
15 changes: 7 additions & 8 deletions modules/default/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
Expand All @@ -24,17 +25,15 @@
* along with OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @category Application
* @package Module_Default
* @author Ralf Claussnitzer ([email protected])
* @author Simone Finkbeiner ([email protected])
* @copyright Copyright (c) 2008, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

class Default_Bootstrap extends \Zend_Application_Module_Bootstrap
/**
* @phpcs:disable PSR2.Methods.MethodDeclaration
*/
class Default_Bootstrap extends Zend_Application_Module_Bootstrap
{

/**
* Add prefix for custom controller helper.
*
Expand All @@ -43,11 +42,11 @@ class Default_Bootstrap extends \Zend_Application_Module_Bootstrap
*/
public function _initControllerHelpers()
{
\Zend_Controller_Action_HelperBroker::addPrefix('Application_Controller_Action_Helper');
Zend_Controller_Action_HelperBroker::addPrefix('Application_Controller_Action_Helper');
}

public function _initValidationNamespaces()
{
\Zend_Validate::addDefaultNamespaces('Form_Validate');
Zend_Validate::addDefaultNamespaces('Form_Validate');
}
}
107 changes: 53 additions & 54 deletions modules/default/controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@

/**
* Provides actions for basic authenticating login and logout.
*
* @category Application
* @package Module_Default
*/
class AuthController extends Application_Controller_Action
{

public function init()
{
parent::init();
Expand All @@ -60,50 +56,50 @@ protected function checkAccessModulePermissions()
*
* @var array
*/
protected $_loginUrl = ['action' => 'index', 'controller' => 'index', 'module' => 'home', 'params' => []];
protected $loginUrl = ['action' => 'index', 'controller' => 'index', 'module' => 'home', 'params' => []];
/**
* Default URL to goto after successful logout. Maybe overwritten by findRemoteParameters().
*
* @var array
*/
protected $_logoutUrl = [
'action' => 'index', 'controller' => 'index', 'module' => 'default', 'params' => []
protected $logoutUrl = [
'action' => 'index',
'controller' => 'index',
'module' => 'default',
'params' => [],
];

/**
* Index action shows login form or logout link respectively.
*
* @return void
*/
public function indexAction()
{
$identity = \Zend_Auth::getInstance()->getIdentity();
$identity = Zend_Auth::getInstance()->getIdentity();
if (empty($identity) === true) {
return $this->loginAction();
$this->loginAction();
return;
}

$this->view->logout_url = $this->view->url(['action' => 'logout']);
$this->view->identity = htmlspecialchars($identity);
$this->view->identity = htmlspecialchars($identity);
}

/**
* Login action performs login attempt with login form data. After a successful login
* it redirects to the page configured by $_login_url.
*
* @return void
*/
public function loginAction()
{
// Redirect to start page if user is already logged in
$identity = \Zend_Auth::getInstance()->getIdentity();
$identity = Zend_Auth::getInstance()->getIdentity();
if (empty($identity) !== true) {
$this->_helper->_redirector('index', 'index', 'home', []);
return;
}

// Initialize form.
$form = $this->getLoginForm();
/* @var $logger Zend_Log */
/** @var Zend_Log $logger */
$logger = $this->getLogger();

// check for return module, controller, action and parameteres, overwrite $_login_url.
Expand All @@ -117,7 +113,8 @@ public function loginAction()
$form->setAction($url);

$this->view->form = $form;
return $this->render('login');
$this->render('login');
return;
}

// Credentials coming in via POST operation.
Expand All @@ -133,15 +130,16 @@ public function loginAction()
$form->populate($data);

$this->view->form = $form;
return $this->render('login');
$this->render('login');
return;
}

// Form data is valid (including the hash field)
$auth = new AuthAdapter();

// Overwrite auth adapter if config-key is set.
$config = $this->getConfig();
if (isset($config, $config->authenticationModule) and ($config->authenticationModule === 'Ldap')) {
if (isset($config, $config->authenticationModule) && ($config->authenticationModule === 'Ldap')) {
$auth = new LdapAuthAdapter();
}

Expand All @@ -153,75 +151,74 @@ public function loginAction()

if ($authResult->isValid() !== true) {
// Put authentication failure message to the view.
$message = $authResult->getMessages();
$message = $authResult->getMessages();
$this->view->auth_failed_msg = $this->view->translate($message[0]);

// Populate the form again to trigger validator decorators.
$logger->notice("Failed login attempt of user '" . ($login) . "'.");
$logger->notice("Failed login attempt of user '" . $login . "'.");
$form->populate($data);

$this->view->form = $form;
return $this->render('login');
$this->render('login');
return;
}

// Persistent the successful authenticated identity.
$logger->notice("Successful login attempt of user '" . ($login) . "'.");
\Zend_Auth::getInstance()->getStorage()->write(strtolower($login));
$logger->notice("Successful login attempt of user '" . $login . "'.");
Zend_Auth::getInstance()->getStorage()->write(strtolower($login));

// Redirect to post login url.
$action = $this->_loginUrl['action'];
$controller = $this->_loginUrl['controller'];
$module = $this->_loginUrl['module'];
$params = $this->_loginUrl['params'];
$action = $this->loginUrl['action'];
$controller = $this->loginUrl['controller'];
$module = $this->loginUrl['module'];
$params = $this->loginUrl['params'];
$this->_helper->_redirector($action, $controller, $module, $params);
}

/**
* Logout action performs logout and redirects to home module.
*
* @return void
*/
public function logoutAction()
{
\Zend_Auth::getInstance()->clearIdentity();
return $this->_helper->_redirector('index', 'index', 'home');
Zend_Auth::getInstance()->clearIdentity();
$this->_helper->_redirector('index', 'index', 'home');
}

/**
* Assembles and returns a login form.
*
* @return \Zend_Form
* @return Zend_Form
*/
protected function getLoginForm()
{
$form = new \Zend_Form();
$form = new Zend_Form();

// Add hash element to detect counterfeit formular data via validation.
$hash = new \Zend_Form_Element_Hash('hash');
$hash = new Zend_Form_Element_Hash('hash');

// Login name element.
$login = new \Zend_Form_Element_Text('login');
$login->addValidator(new \Zend_Validate_Regex('/^[A-Za-z0-9@._-]+$/'))
$login = new Zend_Form_Element_Text('login');
$login->addValidator(new Zend_Validate_Regex('/^[A-Za-z0-9@._-]+$/'))
->setRequired()
->setLabel('auth_field_login');
$login->addErrorMessages(
[
\Zend_Validate_NotEmpty::IS_EMPTY => 'auth_error_no_username'
Zend_Validate_NotEmpty::IS_EMPTY => 'auth_error_no_username',
]
);

// Password element.
$password = new \Zend_Form_Element_Password('password');
$password = new Zend_Form_Element_Password('password');
$password->setRequired()
->setLabel('auth_field_password');
$password->addErrorMessages(
[
\Zend_Validate_NotEmpty::IS_EMPTY => 'auth_error_no_password'
Zend_Validate_NotEmpty::IS_EMPTY => 'auth_error_no_password',
]
);

// Submit button.
$submit = new \Zend_Form_Element_Submit('SubmitCredentials');
$submit = new Zend_Form_Element_Submit('SubmitCredentials');
$submit->setLabel('Login');

$form->setMethod('POST');
Expand All @@ -236,14 +233,16 @@ protected function getLoginForm()
* Ignores following parameters: module, controller, action, hash, login, password and SubmitCredentials.
*
* returns mixed Associative array containing parameters that should be added to urls referencing this controller.
*
* @return array
*/
protected function findReturnParameters()
{
$params = $this->getRequest()->getUserParams();
$rparams = [];
$rmodule = null;
$params = $this->getRequest()->getUserParams();
$rparams = [];
$rmodule = null;
$rcontroller = null;
$raction = null;
$raction = null;
foreach ($params as $key => $value) {
switch ($key) {
// ignore default parameters
Expand Down Expand Up @@ -273,22 +272,22 @@ protected function findReturnParameters()
}
}

if (is_null($rmodule) || is_null($rcontroller) || is_null($raction)) {
if ($rmodule === null || $rcontroller === null || $raction === null) {
return [];
}

// store return address and parameters
$this->_loginUrl = [
'action' => $raction,
$this->loginUrl = [
'action' => $raction,
'controller' => $rcontroller,
'module' => $rmodule,
'params' => $rparams,
'module' => $rmodule,
'params' => $rparams,
];
$this->_logoutUrl = [
'action' => $raction,
$this->logoutUrl = [
'action' => $raction,
'controller' => $rcontroller,
'module' => $rmodule,
'params' => $rparams,
'module' => $rmodule,
'params' => $rparams,
];
return array_merge(
['rmodule' => $rmodule, 'rcontroller' => $rcontroller, 'raction' => $raction],
Expand Down
Loading

0 comments on commit 84626f0

Please sign in to comment.