Commit 1cb8ef59 authored by Stefan Busemann's avatar Stefan Busemann
Browse files

[REFACTOR] Add error handling

parent e40b2da0
Pipeline #6592 failed with stages
in 27 seconds
...@@ -93,6 +93,7 @@ class Ldap ...@@ -93,6 +93,7 @@ class Ldap
/** /**
* LDAP constructor. * LDAP constructor.
* @throws \Exception
*/ */
public function __construct() public function __construct()
{ {
...@@ -109,8 +110,21 @@ class Ldap ...@@ -109,8 +110,21 @@ class Ldap
$this->ldapBaseDnForPasswordChanges = trim($this->extensionConfiguration['ldapBaseDnForPasswordChanges']); $this->ldapBaseDnForPasswordChanges = trim($this->extensionConfiguration['ldapBaseDnForPasswordChanges']);
// Connect and bind // Connect and bind
$this->createLdapConnection(); try {
$this->ldapBind($this->ldapConnection, $this->ldapBindDn, $this->ldapBindPassword); if ($this->createLdapConnection()) {
$this->ldapBind($this->ldapConnection, $this->ldapBindDn, $this->ldapBindPassword);
} else {
throw new \Exception(
'createLdapConnection connection failed',
1553856512
);
}
} catch (\Exception $e) {
throw $e;
}
} }
/** /**
...@@ -249,13 +263,21 @@ class Ldap ...@@ -249,13 +263,21 @@ class Ldap
* Errors are logged to syslog. * Errors are logged to syslog.
* *
* @return bool * @return bool
* @throws \Exception
*/ */
private function createLdapConnection() private function createLdapConnection()
{ {
$ret = false; $ret = false;
$port = intval($this->ldapServerPort); $port = intval($this->ldapServerPort);
try { try {
$this->ldapConnection = @ldap_connect($this->ldapServer, ($port > 0 ? $port : null)); if (function_exists('ldap_connect')) {
$this->ldapConnection = @ldap_connect($this->ldapServer, ($port > 0 ? $port : null));
} else {
throw new \Exception(
'LDAP PHP Extension is not available',
1553856513
);
}
if ($this->ldapConnection) { if ($this->ldapConnection) {
// Set protocol version // Set protocol version
if (ldap_set_option($this->ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $this->ldapProtocolVersion)) { if (ldap_set_option($this->ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $this->ldapProtocolVersion)) {
......
...@@ -32,13 +32,19 @@ class UserCreateUpdateDelete ...@@ -32,13 +32,19 @@ class UserCreateUpdateDelete
* @param bool $createIfNotExists Create the user if it does not exist * @param bool $createIfNotExists Create the user if it does not exist
* @param string $updatePassword * @param string $updatePassword
* @return bool * @return bool
* @throws \Exception
*/ */
public function updateUser(\In2code\Femanager\Domain\Model\User $user, $createIfNotExists = true, $updatePassword = '') public function updateUser(\In2code\Femanager\Domain\Model\User $user, $createIfNotExists = true, $updatePassword = '')
{ {
$ret = false; $ret = false;
/** @var \T3o\T3oLdap\Connectors\Ldap $ldap */ /** @var \T3o\T3oLdap\Connectors\Ldap $ldap */
$ldap = GeneralUtility::makeInstance(\T3o\T3oLdap\Connectors\Ldap::class); try {
$ldap =new \T3o\T3oLdap\Connectors\Ldap();
} catch (\Exception $e) {
throw $e;
}
$feUserUid = $user->getUid(); $feUserUid = $user->getUid();
if ($ldap->userExists($user->getUsername())) { if ($ldap->userExists($user->getUsername())) {
......
<?php <?php
namespace T3o\T3omy\Hooks; namespace T3o\T3omy\Hooks;
/*************************************************************** /***************************************************************
...@@ -183,13 +184,20 @@ class FeManagerHooks ...@@ -183,13 +184,20 @@ class FeManagerHooks
* *
* @param \In2code\Femanager\Domain\Model\User $user * @param \In2code\Femanager\Domain\Model\User $user
* @return bool * @return bool
* @throws \Exception
*/ */
public function createUser(User $user) public function createUser(User $user)
{ {
$this->persistenceManager->persistAll(); $this->persistenceManager->persistAll();
// TODO: Create log entry if user has been successfully added or throw an exception otherwise // TODO: Create log entry if user has been successfully added or throw an exception otherwise
return $this->ldapUserCreateUpdateDelete->updateUser($user, true); try {
$this->ldapUserCreateUpdateDelete->updateUser($user, true);
} catch (\Exception $e) {
throw $e;
}
return true;
} }
/** /**
...@@ -349,6 +357,7 @@ class FeManagerHooks ...@@ -349,6 +357,7 @@ class FeManagerHooks
return $location; return $location;
} }
return 'No Access key is set in typoscript constants - tx_t3omy.ipapi.access_key'; return 'No Access key is set in typoscript constants - tx_t3omy.ipapi.access_key';
} }
} }
...@@ -59,8 +59,19 @@ class ReactivateUserHook implements \TYPO3\CMS\Backend\RecordList\RecordListGetT ...@@ -59,8 +59,19 @@ class ReactivateUserHook implements \TYPO3\CMS\Backend\RecordList\RecordListGetT
$this->persistenceManager->persistAll(); $this->persistenceManager->persistAll();
$ldapOperation = $this->objectManager->get(FeManagerHooks::class); $ldapOperation = $this->objectManager->get(FeManagerHooks::class);
try {
$ldapOperation->createUser($frontendUser);
} catch (\Exception $e) {
$this->addFlashMessage(
$e->getMessage(),
\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
'Error ' . $e->getCode()
);
return false;
}
return $ldapOperation->createUser($frontendUser); return true;
} }
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment