Skip to content

Commit

Permalink
improved reporting of installation and configuration issues
Browse files Browse the repository at this point in the history
satisfies #5
  • Loading branch information
RocketMan committed Dec 1, 2019
1 parent 0f4ccfa commit 9d8cfe0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
25 changes: 17 additions & 8 deletions Auth/IMAPUserAuthProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class IMAPUserAuthProvider implements PasswordAuthenticationProviderInterface {

protected $mailbox;
protected $domain;
protected $db;
protected $plugin;

const DEFAULT_MAILBOX='{%DOMAIN%:993/imap/ssl/novalidate-cert/notls}';

public function __construct($configModel, $db) {
$this->mailbox = $configModel->get('plugin_imap_user_auth_mailbox', '');
$this->domain = $configModel->get('plugin_imap_user_auth_domain', '');
public function __construct($plugin) {
$this->mailbox = $plugin->configModel->get('plugin_imap_user_auth_mailbox', '');
$this->domain = $plugin->configModel->get('plugin_imap_user_auth_domain', '');

$this->db = $db;
$this->plugin = $plugin;
}

public function getUser() {
Expand All @@ -57,8 +57,17 @@ public function authenticate() {
if(empty($mailbox) && !empty($this->domain))
$mailbox = str_replace('%DOMAIN%', $this->domain, self::DEFAULT_MAILBOX);

if(!function_exists('imap_open') || empty($mailbox))
return false;
if(!function_exists('imap_open')) {
$this->plugin->raiseException([
t("The PHP IMAP module is missing."),
t("See the [Documentation]({PluginHomepage}) for setup instructions.")
]);
} else if(empty($mailbox)) {
$this->plugin->raiseException([
t("The plugin's configuration settings are missing."),
t("See the [Documentation]({PluginHomepage}) for setup instructions.")
]);
}

// supply curly braces if not already present
if(strpos('x' . trim($mailbox), '{', 1) != 1)
Expand All @@ -74,7 +83,7 @@ public function authenticate() {
}

// lookup user in the local database
$user = $this->db
$user = $this->plugin->db
->table(UserModel::TABLE)
->columns('id')
->eq('username', $this->username)
Expand Down
32 changes: 30 additions & 2 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Plugin extends Base
public function initialize()
{
$this->authenticationManager->register(
new IMAPUserAuthProvider($this->configModel, $this->db));
new IMAPUserAuthProvider($this));
$this->template->hook->attach('template:config:integrations',
'IMAPUserAuth:config');
}
Expand All @@ -46,12 +46,40 @@ public function getPluginAuthor()

public function getPluginVersion()
{
return '1.0.2';
return '1.0.3';
}

public function getPluginHomepage()
{
return 'https://github.com/RocketMan/plugin-imap-user-auth';
}

/**
* replace all instances of {PluginXXX} with the results of the
* evaluation $this->getPluginXXX()
*/
public function resolveMetadata($text)
{
return preg_replace_callback('/\{Plugin(.*?)\}/', function($matched) {
return call_user_func([$this, "getPlugin".$matched[1]]); }, $text);
}

/**
* resolve all metadata and Markdown hyperlinks in the supplied text
*/
public function resolveMarkdown($text)
{
return preg_replace('/\[(.*?)\]\((.*?)\)/', "<A HREF='$2'>$1</A>",
$this->resolveMetadata($text));
}

public function raiseException($messages)
{
$output = "";
foreach($messages as $message)
$output .= "<P>".$this->resolveMarkdown($message)."</P>";

throw new \LogicException($output);
}
}

0 comments on commit 9d8cfe0

Please sign in to comment.