Fixing indentation

This commit is contained in:
Floris Westerman 2020-11-10 10:19:07 +01:00 committed by Floris Westerman
parent 427d909783
commit e46c3b10ec
No known key found for this signature in database
GPG key ID: E2AED138B92702B0
3 changed files with 398 additions and 385 deletions

View file

@ -43,10 +43,11 @@ class LdapConfig
public $group_field_mail; public $group_field_mail;
public $group_sender_format; public $group_sender_format;
public static function MakeConfig(Plugin $config) : LdapConfig { public static function MakeConfig(Plugin $config): LdapConfig
{
$ldap = new self(); $ldap = new self();
$ldap->server = trim($config->Get("plugin", self::CONFIG_SERVER)); $ldap->server = trim($config->Get("plugin", self::CONFIG_SERVER));
$ldap->protocol = (int) trim($config->Get("plugin", self::CONFIG_PROTOCOL_VERSION, 3)); $ldap->protocol = (int)trim($config->Get("plugin", self::CONFIG_PROTOCOL_VERSION, 3));
$ldap->bind_user = trim($config->Get("plugin", self::CONFIG_BIND_USER)); $ldap->bind_user = trim($config->Get("plugin", self::CONFIG_BIND_USER));
$ldap->bind_password = trim($config->Get("plugin", self::CONFIG_BIND_PASSWORD)); $ldap->bind_password = trim($config->Get("plugin", self::CONFIG_BIND_PASSWORD));
$ldap->user_base = trim($config->Get("plugin", self::CONFIG_USER_BASE)); $ldap->user_base = trim($config->Get("plugin", self::CONFIG_USER_BASE));
@ -54,7 +55,7 @@ class LdapConfig
$ldap->user_field_name = trim($config->Get("plugin", self::CONFIG_USER_FIELD_NAME)); $ldap->user_field_name = trim($config->Get("plugin", self::CONFIG_USER_FIELD_NAME));
$ldap->user_field_search = trim($config->Get("plugin", self::CONFIG_USER_FIELD_SEARCH)); $ldap->user_field_search = trim($config->Get("plugin", self::CONFIG_USER_FIELD_SEARCH));
$ldap->user_field_mail = trim($config->Get("plugin", self::CONFIG_USER_FIELD_MAIL)); $ldap->user_field_mail = trim($config->Get("plugin", self::CONFIG_USER_FIELD_MAIL));
$ldap->group_get = (bool) trim($config->Get("plugin", self::CONFIG_GROUP_GET)); $ldap->group_get = (bool)trim($config->Get("plugin", self::CONFIG_GROUP_GET));
$ldap->group_base = trim($config->Get("plugin", self::CONFIG_GROUP_BASE)); $ldap->group_base = trim($config->Get("plugin", self::CONFIG_GROUP_BASE));
$ldap->group_objectclass = trim($config->Get("plugin", self::CONFIG_GROUP_OBJECTCLASS)); $ldap->group_objectclass = trim($config->Get("plugin", self::CONFIG_GROUP_OBJECTCLASS));
$ldap->group_field_name = trim($config->Get("plugin", self::CONFIG_GROUP_FIELD_NAME)); $ldap->group_field_name = trim($config->Get("plugin", self::CONFIG_GROUP_FIELD_NAME));

View file

@ -38,7 +38,7 @@ class LdapIdentities implements IIdentities
$this->logger = $logger; $this->logger = $logger;
// Check if LDAP is available // Check if LDAP is available
if(!extension_loaded('ldap') || !function_exists('ldap_connect')) { if (!extension_loaded('ldap') || !function_exists('ldap_connect')) {
$this->ldapAvailable = false; $this->ldapAvailable = false;
$logger->Write("The LDAP plugin is not available!", Type::WARNING, self::LOG_KEY); $logger->Write("The LDAP plugin is not available!", Type::WARNING, self::LOG_KEY);
return; return;
@ -54,7 +54,7 @@ class LdapIdentities implements IIdentities
{ {
try { try {
$this->EnsureBound(); $this->EnsureBound();
} catch(LdapException $e) { } catch (LdapException $e) {
return []; // exceptions are only thrown from the handleerror function that does logging already return []; // exceptions are only thrown from the handleerror function that does logging already
} }
@ -76,26 +76,26 @@ class LdapIdentities implements IIdentities
return []; // exceptions are only thrown from the handleerror function that does logging already return []; // exceptions are only thrown from the handleerror function that does logging already
} }
if(count($userResults) < 1) { if (count($userResults) < 1) {
$this->logger->Write("Could not find user $username", Type::NOTICE, self::LOG_KEY); $this->logger->Write("Could not find user $username", Type::NOTICE, self::LOG_KEY);
return []; return [];
} else if(count($userResults) > 1) { } else if (count($userResults) > 1) {
$this->logger->Write("Found multiple matches for user $username", Type::WARNING, self::LOG_KEY); $this->logger->Write("Found multiple matches for user $username", Type::WARNING, self::LOG_KEY);
} }
$userResult = $userResults[0]; $userResult = $userResults[0];
foreach($userResult->emails as $email) { foreach ($userResult->emails as $email) {
$identity = new Identity($email, $email); $identity = new Identity($email, $email);
$identity->SetName($userResult->name); $identity->SetName($userResult->name);
if($email === $account->Email()) if ($email === $account->Email())
$identity->SetId(""); // primary identity $identity->SetId(""); // primary identity
$identities[] = $identity; $identities[] = $identity;
} }
if(!$this->config->group_get) if (!$this->config->group_get)
return $identities; return $identities;
try { try {
@ -111,8 +111,8 @@ class LdapIdentities implements IIdentities
return []; // exceptions are only thrown from the handleerror function that does logging already return []; // exceptions are only thrown from the handleerror function that does logging already
} }
foreach($groupResults as $group) { foreach ($groupResults as $group) {
foreach($group->emails as $email) { foreach ($group->emails as $email) {
$name = $this->config->group_sender_format; $name = $this->config->group_sender_format;
$name = str_replace("#USER#", $userResult->name, $name); $name = str_replace("#USER#", $userResult->name, $name);
$name = str_replace("#GROUP#", $group->name, $name); $name = str_replace("#GROUP#", $group->name, $name);
@ -154,24 +154,27 @@ class LdapIdentities implements IIdentities
} }
/** @throws LdapException */ /** @throws LdapException */
private function EnsureConnected() : void { private function EnsureConnected(): void
if($this->ldapConnected) return; {
if ($this->ldapConnected) return;
$res = $this->Connect(); $res = $this->Connect();
if(!$res) if (!$res)
$this->HandleLdapError("Connect"); $this->HandleLdapError("Connect");
} }
private function Connect() : bool {
private function Connect(): bool
{
// Set up connection // Set up connection
$ldap = @ldap_connect($this->config->server); $ldap = @ldap_connect($this->config->server);
if($ldap === false) { if ($ldap === false) {
$this->ldapAvailable = false; $this->ldapAvailable = false;
return false; return false;
} }
// Set protocol version // Set protocol version
$option = @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, $this->config->protocol); $option = @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, $this->config->protocol);
if(!$option) { if (!$option) {
$this->ldapAvailable = false; $this->ldapAvailable = false;
return false; return false;
} }
@ -182,18 +185,21 @@ class LdapIdentities implements IIdentities
} }
/** @throws LdapException */ /** @throws LdapException */
private function EnsureBound() : void { private function EnsureBound(): void
if($this->ldapBound) return; {
if ($this->ldapBound) return;
$this->EnsureConnected(); $this->EnsureConnected();
$res = $this->Bind(); $res = $this->Bind();
if(!$res) if (!$res)
$this->HandleLdapError("Bind"); $this->HandleLdapError("Bind");
} }
private function Bind() : bool {
private function Bind(): bool
{
// Bind to LDAP here // Bind to LDAP here
$bindResult = @ldap_bind($this->ldap, $this->config->bind_user, $this->config->bind_password); $bindResult = @ldap_bind($this->ldap, $this->config->bind_user, $this->config->bind_password);
if(!$bindResult) { if (!$bindResult) {
$this->ldapAvailable = false; $this->ldapAvailable = false;
return false; return false;
} }
@ -206,7 +212,8 @@ class LdapIdentities implements IIdentities
* @param string $op * @param string $op
* @throws LdapException * @throws LdapException
*/ */
private function HandleLdapError(string $op = ""): void { private function HandleLdapError(string $op = ""): void
{
// Obtain LDAP error and write logs // Obtain LDAP error and write logs
$errorNo = @ldap_errno($this->ldap); $errorNo = @ldap_errno($this->ldap);
$errorMsg = @ldap_error($this->ldap); $errorMsg = @ldap_error($this->ldap);
@ -226,7 +233,8 @@ class LdapIdentities implements IIdentities
* @return LdapResult[] * @return LdapResult[]
* @throws LdapException * @throws LdapException
*/ */
private function FindLdapResults(string $searchField, string $searchValue, string $searchBase, string $objectClass, string $nameField, string $mailField) : array { private function FindLdapResults(string $searchField, string $searchValue, string $searchBase, string $objectClass, string $nameField, string $mailField): array
{
$this->EnsureBound(); $this->EnsureBound();
$nameField = strtolower($nameField); $nameField = strtolower($nameField);
@ -234,19 +242,19 @@ class LdapIdentities implements IIdentities
$filter = "(&(objectclass=$objectClass)($searchField=$searchValue))"; $filter = "(&(objectclass=$objectClass)($searchField=$searchValue))";
$ldapResult = @ldap_search($this->ldap, $searchBase, $filter, ['dn', $mailField, $nameField]); $ldapResult = @ldap_search($this->ldap, $searchBase, $filter, ['dn', $mailField, $nameField]);
if(!$ldapResult) { if (!$ldapResult) {
$this->HandleLdapError("Fetch $objectClass"); $this->HandleLdapError("Fetch $objectClass");
return []; return [];
} }
$entries = @ldap_get_entries($this->ldap, $ldapResult); $entries = @ldap_get_entries($this->ldap, $ldapResult);
if(!$entries) { if (!$entries) {
$this->HandleLdapError("Fetch $objectClass"); $this->HandleLdapError("Fetch $objectClass");
return []; return [];
} }
$results = []; $results = [];
for($i = 0; $i < $entries["count"]; $i++) { for ($i = 0; $i < $entries["count"]; $i++) {
$entry = $entries[$i]; $entry = $entries[$i];
$result = new LdapResult(); $result = new LdapResult();
@ -267,16 +275,17 @@ class LdapIdentities implements IIdentities
* @param bool $required * @param bool $required
* @return string|string[] * @return string|string[]
*/ */
private function LdapGetAttribute(array $entry, string $attribute, bool $single = true, bool $required = false) { private function LdapGetAttribute(array $entry, string $attribute, bool $single = true, bool $required = false)
if(!isset($entry[$attribute])) { {
if($required) if (!isset($entry[$attribute])) {
if ($required)
$this->logger->Write("Attribute $attribute not found on object {$entry['dn']} while required", Type::NOTICE, self::LOG_KEY); $this->logger->Write("Attribute $attribute not found on object {$entry['dn']} while required", Type::NOTICE, self::LOG_KEY);
return $single ? "" : []; return $single ? "" : [];
} }
if ($single) { if ($single) {
if($entry[$attribute]["count"] > 1) if ($entry[$attribute]["count"] > 1)
$this->logger->Write("Attribute $attribute is multivalues while only a single value is expected", Type::NOTICE, self::LOG_KEY); $this->logger->Write("Attribute $attribute is multivalues while only a single value is expected", Type::NOTICE, self::LOG_KEY);
return $entry[$attribute][0]; return $entry[$attribute][0];
@ -288,7 +297,8 @@ class LdapIdentities implements IIdentities
} }
} }
class LdapResult { class LdapResult
{
/** @var string */ /** @var string */
public $dn; public $dn;

View file

@ -8,19 +8,21 @@ class LdapIdentitiesPlugin extends AbstractPlugin
{ {
public function __construct() public function __construct()
{ {
include_once __DIR__.'/LdapIdentities.php'; include_once __DIR__ . '/LdapIdentities.php';
include_once __DIR__.'/LdapConfig.php'; include_once __DIR__ . '/LdapConfig.php';
include_once __DIR__.'/LdapException.php'; include_once __DIR__ . '/LdapException.php';
} }
public function Init() : void { public function Init(): void
{
$this->addHook("main.fabrica", 'MainFabrica'); $this->addHook("main.fabrica", 'MainFabrica');
} }
public function MainFabrica(string $name, &$result) { public function MainFabrica(string $name, &$result)
if($name !== 'identities') return; {
if ($name !== 'identities') return;
if(!is_array($result)) if (!is_array($result))
$result = []; $result = [];
// Set up config // Set up config