Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mailbox auto aliases #119

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion application/Repositories/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function filterForAliasList( $filter, $admin, $domain = null, $ima = fal
->setParameter( 1, $admin );

if( $domain )
$qb->andWhere( 'm.Domain = ?2' )
$qb->andWhere( 'a.Domain = ?2' )
->setParameter( 2, $domain );

if( !$ima )
Expand Down
11 changes: 11 additions & 0 deletions application/configs/application.ini.dist
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,19 @@ vimbadmin_plugins.AccessPermissions.type.SIEVE = "SIEVE"



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Allow admins to force that for a mailbox/domain basic aliases are existing
; If a new mailbox is created the system will check if the aliases are existing, if not they are created.

vimbadmin_plugins.MailboxAutomaticAliases.disabled = false

; These aliases should always exist, it is not recommened to delete it
vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "postmaster"
vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "abuse"

; These aliases are optional, but it recommended to not remove them
vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "hostmaster"
vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "webmaster"



Expand Down
51 changes: 28 additions & 23 deletions application/controllers/AliasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,21 @@ public function ajaxToggleActiveAction()
if( !$this->getAlias() )
print 'ko';

$this->getAlias()->setActive( !$this->getAlias()->getActive() );
$this->getAlias()->setModified( new \DateTime() );

$this->log(
$this->getAlias()->getActive() ? \Entities\Log::ACTION_ALIAS_ACTIVATE : \Entities\Log::ACTION_ALIAS_DEACTIVATE,
"{$this->getAdmin()->getFormattedName()} " . ( $this->getAlias()->getActive() ? 'activated' : 'deactivated' ) . " alias {$this->getAlias()->getAddress()}"
);
$this->notify( 'alias', 'toggleActive', 'preflush', $this, [ 'active' => $this->getAlias()->getActive() ] );
$this->getD2EM()->flush();
$this->notify( 'alias', 'toggleActive', 'postflush', $this, [ 'active' => $this->getAlias()->getActive() ] );
print 'ok';
$status = $this->notify( 'alias', 'toggleActive', 'preToggle', $this, [ 'active' => $this->getAlias()->getActive() ] );

if( $status == 'ok') {
$this->getAlias()->setActive( !$this->getAlias()->getActive() );
$this->getAlias()->setModified( new \DateTime() );

$this->log(
$this->getAlias()->getActive() ? \Entities\Log::ACTION_ALIAS_ACTIVATE : \Entities\Log::ACTION_ALIAS_DEACTIVATE,
"{$this->getAdmin()->getFormattedName()} " . ( $this->getAlias()->getActive() ? 'activated' : 'deactivated' ) . " alias {$this->getAlias()->getAddress()}"
);
$this->notify( 'alias', 'toggleActive', 'preflush', $this, [ 'active' => $this->getAlias()->getActive() ] );
$this->getD2EM()->flush();
$this->notify( 'alias', 'toggleActive', 'postflush', $this, [ 'active' => $this->getAlias()->getActive() ] );
}
print $status;
}


Expand All @@ -341,21 +345,22 @@ public function deleteAction()
foreach( $this->getAlias()->getPreferences() as $pref )
$this->getD2EM()->remove( $pref );

$this->notify( 'alias', 'delete', 'preRemove', $this );
$this->getD2EM()->remove( $this->getAlias() );
if( $this->getAlias()->getAddress() != $this->getAlias()->getGoto() )
$this->getDomain()->setAliasCount( $this->getDomain()->getAliasCount() - 1 );
if($this->notify( 'alias', 'delete', 'preRemove', $this )) {
$this->getD2EM()->remove( $this->getAlias() );
if( $this->getAlias()->getAddress() != $this->getAlias()->getGoto() )
$this->getDomain()->setAliasCount( $this->getDomain()->getAliasCount() - 1 );

$this->log(
\Entities\Log::ACTION_ALIAS_DELETE,
"{$this->getAdmin()->getFormattedName()} removed alias {$this->getAlias()->getAddress()}"
);
$this->log(
\Entities\Log::ACTION_ALIAS_DELETE,
"{$this->getAdmin()->getFormattedName()} removed alias {$this->getAlias()->getAddress()}"
);

$this->notify( 'alias', 'delete', 'preFlush', $this );
$this->getD2EM()->flush();
$this->notify( 'alias', 'delete', 'postFlush', $this );
$this->notify( 'alias', 'delete', 'preFlush', $this );
$this->getD2EM()->flush();
$this->notify( 'alias', 'delete', 'postFlush', $this );

$this->addMessage( 'Alias has bean removed successfully', OSS_Message::SUCCESS );
$this->addMessage( 'Alias has bean removed successfully', OSS_Message::SUCCESS );
}
$this->redirect( 'alias/list' );
}

Expand Down
156 changes: 156 additions & 0 deletions application/plugins/MailboxAutomaticAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

/**
* @copyright Copyright (c) 2014 Matthias Fechner
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
* @author Matthias Fechner <matthias _at_ fechner.net>
*/

/**
* The Mailbox Automatic Aliases Plugin
*
* The plugin ensures that a required set of aliases for a domain are existent.
* Required aliases are:
* postmaster@domain.tld
* abuse@domain.tld
* Optional aliases are:
* webmaster@domain.tld
* hostmaster@domains.tld
*
* See https://github.com/idefix6/vimbadmin-mailbox-automatic-aliases
*
* Add the following lines to configs/application.ini:
* vimbadmin_plugins.MailboxAutomaticAliases.disabled = false
* vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "postmaster"
* vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "abuse"
* vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "hostmaster"
* vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "webmaster"
*
* @package ViMbAdmin
* @subpackage Plugins
*/
class ViMbAdminPlugin_MailboxAutomaticAliases extends ViMbAdmin_Plugin implements OSS_Plugin_Observer {
private $defaultAliases;

public function __construct(OSS_Controller_Action $controller) {
parent::__construct($controller, get_class() );

// read config parameters
$this->defaultAliases = $controller->getOptions()['vimbadmin_plugins']['MailboxAutomaticAliases']['defaultAliases'];
}

/**
* Is called after a mailbox is created. It ensures that required aliases are created.
*
* @param $controller
* @param $options
*/
public function mailbox_add_addPostflush($controller, $options) {
// get domain
$domainId = $controller->getDomain()->getId();
$domain = $controller->getDomain()->getDomain();

// get mailbox
$mailbox = $controller->getMailbox()->getUsername();

// check if domain has enforced aliases or do we have to create them?
if($this->defaultAliases) {
foreach($this->defaultAliases as $key => $item) {
$aliasList = $controller->getD2EM()->getRepository( "\\Entities\\Alias" )->filterForAliasList( $item . '@' . $domain, $controller->getAdmin(), $domainId, true );
if(count($aliasList) == 0) {
$alias = new \Entities\Alias();
$alias->setAddress($item.'@'.$domain);
$alias->setGoto($mailbox);
$alias->setDomain($controller->getDomain());
$alias->setActive(1);
$alias->setCreated(new \DateTime());
$controller->getD2EM()->persist($alias);
// Increase alias count for domain
$controller->getDomain()->increaseAliasCount();
$controller->getD2EM()->flush();
$controller->addMessage( sprintf(_("Auto-Created alias %s@%s -> %s."), $item, $domain, $mailbox));
}
}
}
}

/**
* Check if the aliases is allowed to be removed. If not return false, else return true.
*
* @param $controller
* @param $options
* @return bool
*/
public function alias_delete_preRemove($controller, $options) {
// get alias that should be deleted
$alias = $controller->getAlias()->getAddress();
$domain = $controller->getDomain()->getDomain();

// check if the alias to delete is not enforced by the plugin
if($this->defaultAliases) {
foreach($this->defaultAliases as $key => $item) {
if($alias == $item.'@'.$domain) {
// not allowed to delete, show error message and stop delete
$controller->addMessage( sprintf( _("Alias %s is required and cannot be deleted."), $alias), OSS_Message::ERROR);
return false;
}
}
}
return true;
}

public function alias_toggleActive_preToggle($controller, $options) {
// get alias that should be deleted
$alias = $controller->getAlias()->getAddress();
$domain = $controller->getDomain()->getDomain();

if($options['active'] == 'true') {
// we have to check if it is allowed to disable this alias
if($this->defaultAliases) {
foreach($this->defaultAliases as $key => $item) {
if($alias == $item.'@'.$domain) {
// not allowed to delete, show error message and stop delete
return( sprintf( _("Alias %s is required and cannot be disabled."), $alias));
}
}
}

}
return 'ok';
}

/**
* Is called after an alias is created. It ensures that required aliases are created.
*
* @param $controller
* @param $options
*/
public function alias_add_addPreflush($controller, $options) {
// get domain
$domainId = $controller->getDomain()->getId();
$domain = $controller->getDomain()->getDomain();

// get alias
$aliasGoto = $controller->getalias()->getGoto();

// check if domain has enforced aliases or do we have to create them?
if($this->defaultAliases) {
foreach($this->defaultAliases as $key => $item) {
$aliasList = $controller->getD2EM()->getRepository( "\\Entities\\Alias" )->filterForAliasList( $item . '@' . $domain, $controller->getAdmin(), $domainId, true );
if(count($aliasList) == 0) {
$alias = new \Entities\Alias();
$alias->setAddress($item.'@'.$domain);
$alias->setGoto($aliasGoto);
$alias->setDomain($controller->getDomain());
$alias->setActive(1);
$alias->setCreated(new \DateTime());
$controller->getD2EM()->persist($alias);
// Increase alias count for domain
$controller->getDomain()->increaseAliasCount();
$controller->getD2EM()->flush();
$controller->addMessage( sprintf(_("Auto-Created alias %s@%s -> %s."), $item, $domain, $aliasGoto));
}
}
}
}
}
13 changes: 11 additions & 2 deletions library/ViMbAdmin/Controller/PluginAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,17 @@ public function detach( OSS_Plugin_Observer $observer )
*/
public function notify( $controller, $action, $hook, OSS_Controller_Action $controllerObject, $params = null )
{
foreach( $this->observers as $o )
$o->update( $controller, $action, $hook, $controllerObject, $params );
$returnValue=true;
foreach( $this->observers as $o ) {
$status=$o->update( $controller, $action, $hook, $controllerObject, $params );
if(!$status) {
$returnValue=false;
}
if($hook == 'preToggle') {
$returnValue=$status;
}
}
return($returnValue);
}

/**
Expand Down
1 change: 1 addition & 0 deletions library/ViMbAdmin/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function update( $controller, $action, $hook, OSS_Controller_Action $cont
$hookfn = "{$controller}_{$action}_{$hook}";
if( method_exists( $this, $hookfn ) )
return $this->$hookfn( $controllerObject, $params );
return true;
}


Expand Down