From 96f59c6150916fe67723c64f325d9de99fed59fa Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Mon, 09 Sep 2013 14:25:52 -0400
Subject: [PATCH] Add option force specified domain in user login - username_domain_forced (#1489264)

---
 CHANGELOG                  |    1 +
 program/include/rcmail.php |   25 ++++++++++++++++---------
 config/defaults.inc.php    |    4 ++++
 3 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 48dbded..35b08a7 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Add option force specified domain in user login - username_domain_forced (#1489264)
 - Fix issue where From address was removed from Cc and Bcc fields when editing a draft (#1489319)
 - Add option to import Vcards with group assignments
 - Save groups membership in Vcard export (#1488509)
diff --git a/config/defaults.inc.php b/config/defaults.inc.php
index bdcbbd3..bf7c4df 100644
--- a/config/defaults.inc.php
+++ b/config/defaults.inc.php
@@ -346,6 +346,10 @@
 // For example %n = mail.domain.tld, %t = domain.tld
 $config['username_domain'] = '';
 
+// Force domain configured in username_domain to be used for login.
+// Any domain in username will be replaced by username_domain.
+$config['username_domain_forced'] = false;
+
 // This domain will be used to form e-mail addresses of new users
 // Specify an array with 'host' => 'domain' values to support multiple hosts
 // Supported replacement variables:
diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index 02287d3..0483f0e 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -481,15 +481,22 @@
         $port = $config['default_port'];
     }
 
-    /* Modify username with domain if required
-       Inspired by Marco <P0L0_notspam_binware.org>
-    */
-    // Check if we need to add domain
-    if (!empty($config['username_domain']) && strpos($username, '@') === false) {
-      if (is_array($config['username_domain']) && isset($config['username_domain'][$host]))
-        $username .= '@'.rcube_utils::parse_host($config['username_domain'][$host], $host);
-      else if (is_string($config['username_domain']))
-        $username .= '@'.rcube_utils::parse_host($config['username_domain'], $host);
+    // Check if we need to add/force domain to username
+    if (!empty($config['username_domain'])) {
+      $domain = is_array($config['username_domain']) ? $config['username_domain'][$host] : $config['username_domain'];
+
+      if ($domain = rcube_utils::parse_host((string)$domain, $host)) {
+        $pos = strpos($username, '@');
+
+        // force configured domains
+        if (!empty($config['username_domain_forced']) && $pos !== false) {
+          $username = substr($username, 0, $pos) . '@' . $domain;
+        }
+        // just add domain if not specified
+        else if ($pos === false) {
+          $username .= '@' . $domain;
+        }
+      }
     }
 
     if (!isset($config['login_lc'])) {

--
Gitblit v1.9.1