mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +03:00
Fixing indentation
This commit is contained in:
parent
427d909783
commit
e46c3b10ec
3 changed files with 398 additions and 385 deletions
|
|
@ -5,63 +5,64 @@ use RainLoop\Config\Plugin;
|
||||||
|
|
||||||
class LdapConfig
|
class LdapConfig
|
||||||
{
|
{
|
||||||
public const CONFIG_SERVER = "server";
|
public const CONFIG_SERVER = "server";
|
||||||
public const CONFIG_PROTOCOL_VERSION = "server_version";
|
public const CONFIG_PROTOCOL_VERSION = "server_version";
|
||||||
|
|
||||||
public const CONFIG_BIND_USER = "bind_user";
|
public const CONFIG_BIND_USER = "bind_user";
|
||||||
public const CONFIG_BIND_PASSWORD = "bind_password";
|
public const CONFIG_BIND_PASSWORD = "bind_password";
|
||||||
|
|
||||||
public const CONFIG_USER_BASE = "user_base";
|
public const CONFIG_USER_BASE = "user_base";
|
||||||
public const CONFIG_USER_OBJECTCLASS = "user_objectclass";
|
public const CONFIG_USER_OBJECTCLASS = "user_objectclass";
|
||||||
public const CONFIG_USER_FIELD_NAME = "user_field_name";
|
public const CONFIG_USER_FIELD_NAME = "user_field_name";
|
||||||
public const CONFIG_USER_FIELD_SEARCH = "user_field_search";
|
public const CONFIG_USER_FIELD_SEARCH = "user_field_search";
|
||||||
public const CONFIG_USER_FIELD_MAIL = "user_field_mail";
|
public const CONFIG_USER_FIELD_MAIL = "user_field_mail";
|
||||||
|
|
||||||
public const CONFIG_GROUP_GET = "group_get";
|
public const CONFIG_GROUP_GET = "group_get";
|
||||||
public const CONFIG_GROUP_BASE = "group_base";
|
public const CONFIG_GROUP_BASE = "group_base";
|
||||||
public const CONFIG_GROUP_OBJECTCLASS = "group_objectclass";
|
public const CONFIG_GROUP_OBJECTCLASS = "group_objectclass";
|
||||||
public const CONFIG_GROUP_FIELD_NAME = "group_field_name";
|
public const CONFIG_GROUP_FIELD_NAME = "group_field_name";
|
||||||
public const CONFIG_GROUP_FIELD_MEMBER = "group_field_member";
|
public const CONFIG_GROUP_FIELD_MEMBER = "group_field_member";
|
||||||
public const CONFIG_GROUP_FIELD_MAIL = "group_field_mail";
|
public const CONFIG_GROUP_FIELD_MAIL = "group_field_mail";
|
||||||
public const CONFIG_GROUP_SENDER_FORMAT = "group_sender_format";
|
public const CONFIG_GROUP_SENDER_FORMAT = "group_sender_format";
|
||||||
|
|
||||||
|
|
||||||
public $server;
|
public $server;
|
||||||
public $protocol;
|
public $protocol;
|
||||||
public $bind_user;
|
public $bind_user;
|
||||||
public $bind_password;
|
public $bind_password;
|
||||||
public $user_base;
|
public $user_base;
|
||||||
public $user_objectclass;
|
public $user_objectclass;
|
||||||
public $user_field_name;
|
public $user_field_name;
|
||||||
public $user_field_search;
|
public $user_field_search;
|
||||||
public $user_field_mail;
|
public $user_field_mail;
|
||||||
public $group_get;
|
public $group_get;
|
||||||
public $group_base;
|
public $group_base;
|
||||||
public $group_objectclass;
|
public $group_objectclass;
|
||||||
public $group_field_name;
|
public $group_field_name;
|
||||||
public $group_field_member;
|
public $group_field_member;
|
||||||
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->server = trim($config->Get("plugin", self::CONFIG_SERVER));
|
$ldap = new self();
|
||||||
$ldap->protocol = (int) trim($config->Get("plugin", self::CONFIG_PROTOCOL_VERSION, 3));
|
$ldap->server = trim($config->Get("plugin", self::CONFIG_SERVER));
|
||||||
$ldap->bind_user = trim($config->Get("plugin", self::CONFIG_BIND_USER));
|
$ldap->protocol = (int)trim($config->Get("plugin", self::CONFIG_PROTOCOL_VERSION, 3));
|
||||||
$ldap->bind_password = trim($config->Get("plugin", self::CONFIG_BIND_PASSWORD));
|
$ldap->bind_user = trim($config->Get("plugin", self::CONFIG_BIND_USER));
|
||||||
$ldap->user_base = trim($config->Get("plugin", self::CONFIG_USER_BASE));
|
$ldap->bind_password = trim($config->Get("plugin", self::CONFIG_BIND_PASSWORD));
|
||||||
$ldap->user_objectclass = trim($config->Get("plugin", self::CONFIG_USER_OBJECTCLASS));
|
$ldap->user_base = trim($config->Get("plugin", self::CONFIG_USER_BASE));
|
||||||
$ldap->user_field_name = trim($config->Get("plugin", self::CONFIG_USER_FIELD_NAME));
|
$ldap->user_objectclass = trim($config->Get("plugin", self::CONFIG_USER_OBJECTCLASS));
|
||||||
$ldap->user_field_search = trim($config->Get("plugin", self::CONFIG_USER_FIELD_SEARCH));
|
$ldap->user_field_name = trim($config->Get("plugin", self::CONFIG_USER_FIELD_NAME));
|
||||||
$ldap->user_field_mail = trim($config->Get("plugin", self::CONFIG_USER_FIELD_MAIL));
|
$ldap->user_field_search = trim($config->Get("plugin", self::CONFIG_USER_FIELD_SEARCH));
|
||||||
$ldap->group_get = (bool) trim($config->Get("plugin", self::CONFIG_GROUP_GET));
|
$ldap->user_field_mail = trim($config->Get("plugin", self::CONFIG_USER_FIELD_MAIL));
|
||||||
$ldap->group_base = trim($config->Get("plugin", self::CONFIG_GROUP_BASE));
|
$ldap->group_get = (bool)trim($config->Get("plugin", self::CONFIG_GROUP_GET));
|
||||||
$ldap->group_objectclass = trim($config->Get("plugin", self::CONFIG_GROUP_OBJECTCLASS));
|
$ldap->group_base = trim($config->Get("plugin", self::CONFIG_GROUP_BASE));
|
||||||
$ldap->group_field_name = trim($config->Get("plugin", self::CONFIG_GROUP_FIELD_NAME));
|
$ldap->group_objectclass = trim($config->Get("plugin", self::CONFIG_GROUP_OBJECTCLASS));
|
||||||
$ldap->group_field_member = trim($config->Get("plugin", self::CONFIG_GROUP_FIELD_MEMBER));
|
$ldap->group_field_name = trim($config->Get("plugin", self::CONFIG_GROUP_FIELD_NAME));
|
||||||
$ldap->group_field_mail = trim($config->Get("plugin", self::CONFIG_GROUP_FIELD_MAIL));
|
$ldap->group_field_member = trim($config->Get("plugin", self::CONFIG_GROUP_FIELD_MEMBER));
|
||||||
$ldap->group_sender_format = trim($config->Get("plugin", self::CONFIG_GROUP_SENDER_FORMAT));
|
$ldap->group_field_mail = trim($config->Get("plugin", self::CONFIG_GROUP_FIELD_MAIL));
|
||||||
|
$ldap->group_sender_format = trim($config->Get("plugin", self::CONFIG_GROUP_SENDER_FORMAT));
|
||||||
|
|
||||||
return $ldap;
|
return $ldap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -8,293 +8,303 @@ use RainLoop\Providers\Identities\IIdentities;
|
||||||
|
|
||||||
class LdapIdentities implements IIdentities
|
class LdapIdentities implements IIdentities
|
||||||
{
|
{
|
||||||
/** @var resource */
|
/** @var resource */
|
||||||
private $ldap;
|
private $ldap;
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
private $ldapAvailable = true;
|
private $ldapAvailable = true;
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
private $ldapConnected = false;
|
private $ldapConnected = false;
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
private $ldapBound = false;
|
private $ldapBound = false;
|
||||||
|
|
||||||
/** @var LdapConfig */
|
/** @var LdapConfig */
|
||||||
private $config;
|
private $config;
|
||||||
|
|
||||||
/** @var Logger */
|
/** @var Logger */
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
private const LOG_KEY = "Ldap";
|
private const LOG_KEY = "Ldap";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* LdapIdentities constructor.
|
* LdapIdentities constructor.
|
||||||
*
|
*
|
||||||
* @param LdapConfig $config
|
* @param LdapConfig $config
|
||||||
* @param Logger $logger
|
* @param Logger $logger
|
||||||
*/
|
*/
|
||||||
public function __construct(LdapConfig $config, Logger $logger)
|
public function __construct(LdapConfig $config, Logger $logger)
|
||||||
{
|
{
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$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;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Connect();
|
$this->Connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function GetIdentities(Account $account): array
|
public function GetIdentities(Account $account): array
|
||||||
{
|
{
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
$identities = [];
|
$identities = [];
|
||||||
|
|
||||||
// Try and get identity information
|
// Try and get identity information
|
||||||
$username = @ldap_escape($account->Email(), "", LDAP_ESCAPE_FILTER);
|
$username = @ldap_escape($account->Email(), "", LDAP_ESCAPE_FILTER);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$userResults = $this->FindLdapResults(
|
$userResults = $this->FindLdapResults(
|
||||||
$this->config->user_field_search,
|
$this->config->user_field_search,
|
||||||
$username,
|
$username,
|
||||||
$this->config->user_base,
|
$this->config->user_base,
|
||||||
$this->config->user_objectclass,
|
$this->config->user_objectclass,
|
||||||
$this->config->user_field_name,
|
$this->config->user_field_name,
|
||||||
$this->config->user_field_mail
|
$this->config->user_field_mail
|
||||||
);
|
);
|
||||||
} 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
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
$groupResults = $this->FindLdapResults(
|
$groupResults = $this->FindLdapResults(
|
||||||
$this->config->group_field_member,
|
$this->config->group_field_member,
|
||||||
$userResult->dn,
|
$userResult->dn,
|
||||||
$this->config->group_base,
|
$this->config->group_base,
|
||||||
$this->config->group_objectclass,
|
$this->config->group_objectclass,
|
||||||
$this->config->group_field_name,
|
$this->config->group_field_name,
|
||||||
$this->config->group_field_mail
|
$this->config->group_field_mail
|
||||||
);
|
);
|
||||||
} 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
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
$identity = new Identity($email, $email);
|
$identity = new Identity($email, $email);
|
||||||
$identity->SetName($name);
|
$identity->SetName($name);
|
||||||
$identity->SetBcc($email);
|
$identity->SetBcc($email);
|
||||||
|
|
||||||
$identities[] = $identity;
|
$identities[] = $identity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $identities;
|
return $identities;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
* @throws \RainLoop\Exceptions\Exception
|
* @throws \RainLoop\Exceptions\Exception
|
||||||
*/
|
*/
|
||||||
public function SetIdentities(Account $account, array $identities)
|
public function SetIdentities(Account $account, array $identities)
|
||||||
{
|
{
|
||||||
throw new \RainLoop\Exceptions\Exception("Ldap identities provider does not support storage");
|
throw new \RainLoop\Exceptions\Exception("Ldap identities provider does not support storage");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function SupportsStore(): bool
|
public function SupportsStore(): bool
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function Name(): string
|
public function Name(): string
|
||||||
{
|
{
|
||||||
return "Ldap";
|
return "Ldap";
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @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 {
|
|
||||||
// Set up connection
|
|
||||||
$ldap = @ldap_connect($this->config->server);
|
|
||||||
if($ldap === false) {
|
|
||||||
$this->ldapAvailable = false;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set protocol version
|
private function Connect(): bool
|
||||||
$option = @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, $this->config->protocol);
|
{
|
||||||
if(!$option) {
|
// Set up connection
|
||||||
$this->ldapAvailable = false;
|
$ldap = @ldap_connect($this->config->server);
|
||||||
return false;
|
if ($ldap === false) {
|
||||||
}
|
$this->ldapAvailable = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$this->ldap = $ldap;
|
// Set protocol version
|
||||||
$this->ldapConnected = true;
|
$option = @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, $this->config->protocol);
|
||||||
return true;
|
if (!$option) {
|
||||||
}
|
$this->ldapAvailable = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/** @throws LdapException */
|
$this->ldap = $ldap;
|
||||||
private function EnsureBound() : void {
|
$this->ldapConnected = true;
|
||||||
if($this->ldapBound) return;
|
return true;
|
||||||
$this->EnsureConnected();
|
}
|
||||||
|
|
||||||
$res = $this->Bind();
|
/** @throws LdapException */
|
||||||
if(!$res)
|
private function EnsureBound(): void
|
||||||
$this->HandleLdapError("Bind");
|
{
|
||||||
}
|
if ($this->ldapBound) return;
|
||||||
private function Bind() : bool {
|
$this->EnsureConnected();
|
||||||
// Bind to LDAP here
|
|
||||||
$bindResult = @ldap_bind($this->ldap, $this->config->bind_user, $this->config->bind_password);
|
|
||||||
if(!$bindResult) {
|
|
||||||
$this->ldapAvailable = false;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->ldapBound = true;
|
$res = $this->Bind();
|
||||||
return true;
|
if (!$res)
|
||||||
}
|
$this->HandleLdapError("Bind");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
private function Bind(): bool
|
||||||
* @param string $op
|
{
|
||||||
* @throws LdapException
|
// Bind to LDAP here
|
||||||
*/
|
$bindResult = @ldap_bind($this->ldap, $this->config->bind_user, $this->config->bind_password);
|
||||||
private function HandleLdapError(string $op = ""): void {
|
if (!$bindResult) {
|
||||||
// Obtain LDAP error and write logs
|
$this->ldapAvailable = false;
|
||||||
$errorNo = @ldap_errno($this->ldap);
|
return false;
|
||||||
$errorMsg = @ldap_error($this->ldap);
|
}
|
||||||
|
|
||||||
$message = empty($op) ? "LDAP Error: {$errorMsg} ({$errorNo})" : "LDAP Error during {$op}: {$errorMsg} ({$errorNo})";
|
$this->ldapBound = true;
|
||||||
$this->logger->Write($message, Type::ERROR, self::LOG_KEY);
|
return true;
|
||||||
throw new LdapException($message, $errorNo);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $searchField
|
* @param string $op
|
||||||
* @param string $searchValue
|
* @throws LdapException
|
||||||
* @param string $searchBase
|
*/
|
||||||
* @param string $objectClass
|
private function HandleLdapError(string $op = ""): void
|
||||||
* @param string $nameField
|
{
|
||||||
* @param string $mailField
|
// Obtain LDAP error and write logs
|
||||||
* @return LdapResult[]
|
$errorNo = @ldap_errno($this->ldap);
|
||||||
* @throws LdapException
|
$errorMsg = @ldap_error($this->ldap);
|
||||||
*/
|
|
||||||
private function FindLdapResults(string $searchField, string $searchValue, string $searchBase, string $objectClass, string $nameField, string $mailField) : array {
|
|
||||||
$this->EnsureBound();
|
|
||||||
|
|
||||||
$nameField = strtolower($nameField);
|
$message = empty($op) ? "LDAP Error: {$errorMsg} ({$errorNo})" : "LDAP Error during {$op}: {$errorMsg} ({$errorNo})";
|
||||||
$mailField = strtolower($mailField);
|
$this->logger->Write($message, Type::ERROR, self::LOG_KEY);
|
||||||
|
throw new LdapException($message, $errorNo);
|
||||||
|
}
|
||||||
|
|
||||||
$filter = "(&(objectclass=$objectClass)($searchField=$searchValue))";
|
/**
|
||||||
$ldapResult = @ldap_search($this->ldap, $searchBase, $filter, ['dn', $mailField, $nameField]);
|
* @param string $searchField
|
||||||
if(!$ldapResult) {
|
* @param string $searchValue
|
||||||
$this->HandleLdapError("Fetch $objectClass");
|
* @param string $searchBase
|
||||||
return [];
|
* @param string $objectClass
|
||||||
}
|
* @param string $nameField
|
||||||
|
* @param string $mailField
|
||||||
|
* @return LdapResult[]
|
||||||
|
* @throws LdapException
|
||||||
|
*/
|
||||||
|
private function FindLdapResults(string $searchField, string $searchValue, string $searchBase, string $objectClass, string $nameField, string $mailField): array
|
||||||
|
{
|
||||||
|
$this->EnsureBound();
|
||||||
|
|
||||||
$entries = @ldap_get_entries($this->ldap, $ldapResult);
|
$nameField = strtolower($nameField);
|
||||||
if(!$entries) {
|
$mailField = strtolower($mailField);
|
||||||
$this->HandleLdapError("Fetch $objectClass");
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$results = [];
|
$filter = "(&(objectclass=$objectClass)($searchField=$searchValue))";
|
||||||
for($i = 0; $i < $entries["count"]; $i++) {
|
$ldapResult = @ldap_search($this->ldap, $searchBase, $filter, ['dn', $mailField, $nameField]);
|
||||||
$entry = $entries[$i];
|
if (!$ldapResult) {
|
||||||
|
$this->HandleLdapError("Fetch $objectClass");
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
$result = new LdapResult();
|
$entries = @ldap_get_entries($this->ldap, $ldapResult);
|
||||||
$result->dn = $entry["dn"];
|
if (!$entries) {
|
||||||
$result->name = $this->LdapGetAttribute($entry, $nameField, true, true);
|
$this->HandleLdapError("Fetch $objectClass");
|
||||||
$result->emails = $this->LdapGetAttribute($entry, $mailField, false, false);
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
$results[] = $result;
|
$results = [];
|
||||||
}
|
for ($i = 0; $i < $entries["count"]; $i++) {
|
||||||
|
$entry = $entries[$i];
|
||||||
|
|
||||||
return $results;
|
$result = new LdapResult();
|
||||||
}
|
$result->dn = $entry["dn"];
|
||||||
|
$result->name = $this->LdapGetAttribute($entry, $nameField, true, true);
|
||||||
|
$result->emails = $this->LdapGetAttribute($entry, $mailField, false, false);
|
||||||
|
|
||||||
/**
|
$results[] = $result;
|
||||||
* @param array $entry
|
}
|
||||||
* @param string $attribute
|
|
||||||
* @param bool $single
|
|
||||||
* @param bool $required
|
|
||||||
* @return string|string[]
|
|
||||||
*/
|
|
||||||
private function LdapGetAttribute(array $entry, string $attribute, bool $single = true, bool $required = false) {
|
|
||||||
if(!isset($entry[$attribute])) {
|
|
||||||
if($required)
|
|
||||||
$this->logger->Write("Attribute $attribute not found on object {$entry['dn']} while required", Type::NOTICE, self::LOG_KEY);
|
|
||||||
|
|
||||||
return $single ? "" : [];
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($single) {
|
/**
|
||||||
if($entry[$attribute]["count"] > 1)
|
* @param array $entry
|
||||||
$this->logger->Write("Attribute $attribute is multivalues while only a single value is expected", Type::NOTICE, self::LOG_KEY);
|
* @param string $attribute
|
||||||
|
* @param bool $single
|
||||||
|
* @param bool $required
|
||||||
|
* @return string|string[]
|
||||||
|
*/
|
||||||
|
private function LdapGetAttribute(array $entry, string $attribute, bool $single = true, bool $required = false)
|
||||||
|
{
|
||||||
|
if (!isset($entry[$attribute])) {
|
||||||
|
if ($required)
|
||||||
|
$this->logger->Write("Attribute $attribute not found on object {$entry['dn']} while required", Type::NOTICE, self::LOG_KEY);
|
||||||
|
|
||||||
return $entry[$attribute][0];
|
return $single ? "" : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $entry[$attribute];
|
if ($single) {
|
||||||
unset($result["count"]);
|
if ($entry[$attribute]["count"] > 1)
|
||||||
return array_values($result);
|
$this->logger->Write("Attribute $attribute is multivalues while only a single value is expected", Type::NOTICE, self::LOG_KEY);
|
||||||
}
|
|
||||||
|
return $entry[$attribute][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $entry[$attribute];
|
||||||
|
unset($result["count"]);
|
||||||
|
return array_values($result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class LdapResult {
|
class LdapResult
|
||||||
/** @var string */
|
{
|
||||||
public $dn;
|
/** @var string */
|
||||||
|
public $dn;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
public $name;
|
public $name;
|
||||||
|
|
||||||
/** @var string[] */
|
/** @var string[] */
|
||||||
public $emails;
|
public $emails;
|
||||||
}
|
}
|
||||||
|
|
@ -6,121 +6,123 @@ use RainLoop\Plugins\Property;
|
||||||
|
|
||||||
class LdapIdentitiesPlugin extends AbstractPlugin
|
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
|
||||||
$config = LdapConfig::MakeConfig($this->Config());
|
$config = LdapConfig::MakeConfig($this->Config());
|
||||||
|
|
||||||
$ldap = new LdapIdentities($config, $this->Manager()->Actions()->Logger());
|
$ldap = new LdapIdentities($config, $this->Manager()->Actions()->Logger());
|
||||||
|
|
||||||
$result[] = $ldap;
|
$result[] = $ldap;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configMapping(): array
|
protected function configMapping(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
Property::NewInstance(LdapConfig::CONFIG_SERVER)
|
Property::NewInstance(LdapConfig::CONFIG_SERVER)
|
||||||
->SetLabel("LDAP Server URL")
|
->SetLabel("LDAP Server URL")
|
||||||
->SetPlaceholder("ldap://server:port")
|
->SetPlaceholder("ldap://server:port")
|
||||||
->SetType(PluginPropertyType::STRING),
|
->SetType(PluginPropertyType::STRING),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_PROTOCOL_VERSION)
|
Property::NewInstance(LdapConfig::CONFIG_PROTOCOL_VERSION)
|
||||||
->SetLabel("LDAP Protocol Version")
|
->SetLabel("LDAP Protocol Version")
|
||||||
->SetType(PluginPropertyType::SELECTION)
|
->SetType(PluginPropertyType::SELECTION)
|
||||||
->SetDefaultValue([2, 3]),
|
->SetDefaultValue([2, 3]),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_BIND_USER)
|
Property::NewInstance(LdapConfig::CONFIG_BIND_USER)
|
||||||
->SetLabel("Bind User DN")
|
->SetLabel("Bind User DN")
|
||||||
->SetDescription("The user to use for binding to the LDAP server. Should be a DN or RDN. Leave empty for anonymous bind")
|
->SetDescription("The user to use for binding to the LDAP server. Should be a DN or RDN. Leave empty for anonymous bind")
|
||||||
->SetType(PluginPropertyType::STRING),
|
->SetType(PluginPropertyType::STRING),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_BIND_PASSWORD)
|
Property::NewInstance(LdapConfig::CONFIG_BIND_PASSWORD)
|
||||||
->SetLabel("Bind User Password")
|
->SetLabel("Bind User Password")
|
||||||
->SetDescription("Leave empty for anonymous bind")
|
->SetDescription("Leave empty for anonymous bind")
|
||||||
->SetType(PluginPropertyType::PASSWORD),
|
->SetType(PluginPropertyType::PASSWORD),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_USER_OBJECTCLASS)
|
Property::NewInstance(LdapConfig::CONFIG_USER_OBJECTCLASS)
|
||||||
->SetLabel("User object class")
|
->SetLabel("User object class")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDefaultValue("user"),
|
->SetDefaultValue("user"),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_USER_FIELD_SEARCH)
|
Property::NewInstance(LdapConfig::CONFIG_USER_FIELD_SEARCH)
|
||||||
->SetLabel("User search field")
|
->SetLabel("User search field")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDescription("The field in the user object to search using the email the user logged in with")
|
->SetDescription("The field in the user object to search using the email the user logged in with")
|
||||||
->SetDefaultValue("mail"),
|
->SetDefaultValue("mail"),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_USER_FIELD_MAIL)
|
Property::NewInstance(LdapConfig::CONFIG_USER_FIELD_MAIL)
|
||||||
->SetLabel("User mail field")
|
->SetLabel("User mail field")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDescription("The field in the user object listing all identities (email addresses) of the user")
|
->SetDescription("The field in the user object listing all identities (email addresses) of the user")
|
||||||
->SetDefaultValue("mail"),
|
->SetDefaultValue("mail"),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_USER_FIELD_NAME)
|
Property::NewInstance(LdapConfig::CONFIG_USER_FIELD_NAME)
|
||||||
->SetLabel("User name field")
|
->SetLabel("User name field")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDescription("The field in the user object with their default sender name")
|
->SetDescription("The field in the user object with their default sender name")
|
||||||
->SetDefaultValue("cn"),
|
->SetDefaultValue("cn"),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_USER_BASE)
|
Property::NewInstance(LdapConfig::CONFIG_USER_BASE)
|
||||||
->SetLabel("User base DN")
|
->SetLabel("User base DN")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDescription("The base DN to search in for users"),
|
->SetDescription("The base DN to search in for users"),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_GROUP_GET)
|
Property::NewInstance(LdapConfig::CONFIG_GROUP_GET)
|
||||||
->SetLabel("Find groups?")
|
->SetLabel("Find groups?")
|
||||||
->SetType(PluginPropertyType::BOOL)
|
->SetType(PluginPropertyType::BOOL)
|
||||||
->SetDescription("Whether or not to search for groups")
|
->SetDescription("Whether or not to search for groups")
|
||||||
->SetDefaultValue(true),
|
->SetDefaultValue(true),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_GROUP_OBJECTCLASS)
|
Property::NewInstance(LdapConfig::CONFIG_GROUP_OBJECTCLASS)
|
||||||
->SetLabel("Group object class")
|
->SetLabel("Group object class")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDefaultValue("group"),
|
->SetDefaultValue("group"),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_GROUP_FIELD_MAIL)
|
Property::NewInstance(LdapConfig::CONFIG_GROUP_FIELD_MAIL)
|
||||||
->SetLabel("Group mail field")
|
->SetLabel("Group mail field")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDescription("The field in the group object listing all identities (email addresses) of the group")
|
->SetDescription("The field in the group object listing all identities (email addresses) of the group")
|
||||||
->SetDefaultValue("mail"),
|
->SetDefaultValue("mail"),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_GROUP_FIELD_NAME)
|
Property::NewInstance(LdapConfig::CONFIG_GROUP_FIELD_NAME)
|
||||||
->SetLabel("Group name field")
|
->SetLabel("Group name field")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDescription("The field in the group object with the name")
|
->SetDescription("The field in the group object with the name")
|
||||||
->SetDefaultValue("cn"),
|
->SetDefaultValue("cn"),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_GROUP_FIELD_MEMBER)
|
Property::NewInstance(LdapConfig::CONFIG_GROUP_FIELD_MEMBER)
|
||||||
->SetLabel("Group member field")
|
->SetLabel("Group member field")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDescription("The field in the group object with all member DNs")
|
->SetDescription("The field in the group object with all member DNs")
|
||||||
->SetDefaultValue("member"),
|
->SetDefaultValue("member"),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_GROUP_SENDER_FORMAT)
|
Property::NewInstance(LdapConfig::CONFIG_GROUP_SENDER_FORMAT)
|
||||||
->SetLabel("Group mail sender format")
|
->SetLabel("Group mail sender format")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDescription("The sender name format for group addresses. Available template values: #USER# for the user name and #GROUP# for the group name")
|
->SetDescription("The sender name format for group addresses. Available template values: #USER# for the user name and #GROUP# for the group name")
|
||||||
->SetDefaultValue("#USER# || #GROUP#"),
|
->SetDefaultValue("#USER# || #GROUP#"),
|
||||||
|
|
||||||
Property::NewInstance(LdapConfig::CONFIG_GROUP_BASE)
|
Property::NewInstance(LdapConfig::CONFIG_GROUP_BASE)
|
||||||
->SetLabel("Group base DN")
|
->SetLabel("Group base DN")
|
||||||
->SetType(PluginPropertyType::STRING)
|
->SetType(PluginPropertyType::STRING)
|
||||||
->SetDescription("The base DN to search in for groups")
|
->SetDescription("The base DN to search in for groups")
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue