summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-03-15 12:12:06 +0100
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-03-15 12:12:06 +0100
commit78d62915289072290ac0a3c1644ceb6127694699 (patch)
treedf08b3398fe410b377f7088361e9c4b50ee5cad2
parent8cc0247145953855e4cf071203409f9f9ef9fe2e (diff)
downloadwebadmin-78d62915289072290ac0a3c1644ceb6127694699.tar.gz
Add kolab_recipient_policy capabilities
-rw-r--r--lib/api/kolab_api_service_form_value.php111
-rw-r--r--lib/kolab_recipient_policy.php205
2 files changed, 269 insertions, 47 deletions
diff --git a/lib/api/kolab_api_service_form_value.php b/lib/api/kolab_api_service_form_value.php
index c778e5e..edec4d8 100644
--- a/lib/api/kolab_api_service_form_value.php
+++ b/lib/api/kolab_api_service_form_value.php
@@ -71,6 +71,10 @@ class kolab_api_service_form_value extends kolab_api_service
return $this->{$method_name}($postdata, $attribs);
}
+ private function generate_alias($postdata, $attribs = array())
+ {
+ return $this->generate_secondary_mail($postdata, $attribs);
+ }
private function generate_cn($postdata, $attribs = array())
{
@@ -82,6 +86,7 @@ class kolab_api_service_form_value extends kolab_api_service
}
}
+ // TODO: Generate using policy from configuration
$cn = trim($postdata['givenname'] . " " . $postdata['sn']);
return array("cn" => $cn);
@@ -98,11 +103,20 @@ class kolab_api_service_form_value extends kolab_api_service
}
}
+ // TODO: Generate using policy from configuration
$displayname = $postdata['givenname'];
if ($postdata['sn']) {
$displayname = $postdata['sn'] . ", " . $displayname;
}
+ // TODO: Figure out what may be sent as an additional comment;
+ //
+ // Examples:
+ //
+ // - van Meeuwen, Jeroen (Kolab Systems)
+ // - Doe, John (Contractor)
+ //
+
return array("displayname" => $displayname);
}
}
@@ -128,61 +142,22 @@ class kolab_api_service_form_value extends kolab_api_service
}
}
- $uid = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);
- $uid = strtolower($uid);
- $uid = preg_replace('/[^a-z-_]/i', '', $uid);
-
- $orig_uid = $uid;
-
- $auth = Auth::get_instance($_SESSION['user']->get_domain());
-
- $x = 2;
- while ($auth->user_find_by_attribute(array('uid' => $uid))) {
- $uid = $orig_uid . $x;
- $x++;
- }
+ // TODO: Home directory attribute to use
+ $uid = $this->generate_uid($postdata, $attribs);
// TODO: Home directory base path from configuration?
-
return array('homedirectory' => '/home/'.$uid);
}
}
private function generate_mail($postdata, $attribs = array())
{
- if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['mail'])) {
- // Use Data Please
- foreach ($attribs['auto_form_fields']['mail']['data'] as $key) {
- if (!isset($postdata[$key])) {
- throw new Exception("Key not set: " . $key, 12356);
- }
- }
-
- $givenname = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['givenname']);
- $sn = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);
-
- $givenname = strtolower($givenname);
- $sn = strtolower($sn);
-
- $givenname = preg_replace('/[^a-z-_]/i', '', $givenname);
- $sn = preg_replace('/[^a-z-_]/i', '', $sn);
-
- $local = trim($givenname . '.' . $sn, '.');
- $mail = $local . '@' . $_SESSION['user']->get_domain();
-
- $orig_mail = $mail;
-
- $auth = Auth::get_instance($_SESSION['user']->get_domain());
-
- $x = 2;
- while ($auth->user_find_by_attribute(array('mail' => $mail))) {
- list($mail_local, $mail_domain) = explode('@', $orig_mail);
- $mail = $mail_local . $x . '@' . $mail_domain;
- $x++;
- }
+ return $this->generate_primary_mail($postdata, $attribs);
+ }
- return array('mail' => $mail);
- }
+ private function generate_mailalternateaddress($postdata, $attribs = array())
+ {
+ return $this->generate_secondary_mail($postdata, $attribs);
}
private function generate_mailhost($postdata, $attribs = array())
@@ -195,11 +170,53 @@ class kolab_api_service_form_value extends kolab_api_service
private function generate_password($postdata, $attribs = array())
{
+ // TODO: Password complexity policy.
exec("head -c 200 /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c15", $userpassword_plain);
$userpassword_plain = $userpassword_plain[0];
return array('password' => $userpassword_plain);
}
+ private function generate_primary_mail($postdata, $attribs = array())
+ {
+ if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['mail'])) {
+ // Use Data Please
+ foreach ($attribs['auto_form_fields']['mail']['data'] as $key) {
+ if (!isset($postdata[$key])) {
+ throw new Exception("Key not set: " . $key, 12356);
+ }
+ }
+
+ $primary_mail = kolab_recipient_policy::primary_mail($postdata);
+
+ return array('mail' => $primary_mail);
+ }
+ }
+
+ private function generate_secondary_mail($postdata, $attribs = array())
+ {
+ $secondary_mail_address = Array();
+
+ if (isset($attribs['auto_form_fields'])) {
+ if (isset($attribs['auto_form_fields']['alias'])) {
+ $secondary_mail_key = 'alias';
+ } elseif (isset($attribs['auto_form_fields']['mailalternateaddress'])) {
+ $secondary_mail_key = 'mailalternateaddress';
+ } else {
+ throw new Exception("No valid input for secondary mail address(es)", 478);
+ }
+
+ foreach ($attribs['auto_form_fields'][$secondary_mail_key]['data'] as $key) {
+ if (!isset($postdata[$key])) {
+ throw new Exception("Key not set: " . $key, 456789);
+ }
+ }
+
+ $secondary_mail = kolab_recipient_policy::secondary_mail($postdata);
+
+ return Array($secondary_mail_key => $secondary_mail);
+ }
+ }
+
private function generate_uid($postdata, $attribs = array())
{
if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['uid'])) {
@@ -210,6 +227,7 @@ class kolab_api_service_form_value extends kolab_api_service
}
}
+ // TODO: Use preferredlanguage
$uid = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);
$uid = strtolower($uid);
$uid = preg_replace('/[^a-z-_]/i', '', $uid);
@@ -234,7 +252,6 @@ class kolab_api_service_form_value extends kolab_api_service
$auth = Auth::get_instance($_SESSION['user']->get_domain());
// TODO: Actually poll $auth for users with a uidNumber set, and take the next one.
-
return array('uidnumber' => 500);
}
}
diff --git a/lib/kolab_recipient_policy.php b/lib/kolab_recipient_policy.php
new file mode 100644
index 0000000..2bd647c
--- /dev/null
+++ b/lib/kolab_recipient_policy.php
@@ -0,0 +1,205 @@
+<?php
+/*
+ +--------------------------------------------------------------------------+
+ | This file is part of the Kolab Web Admin Panel |
+ | |
+ | Copyright (C) 2011-2012, Kolab Systems AG |
+ | |
+ | This program is free software: you can redistribute it and/or modify |
+ | it under the terms of the GNU Affero General Public License as published |
+ | by the Free Software Foundation, either version 3 of the License, or |
+ | (at your option) any later version. |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public License |
+ | along with this program. If not, see <http://www.gnu.org/licenses/> |
+ +--------------------------------------------------------------------------+
+ | Author: Aleksander Machniak <machniak@kolabsys.com> |
+ | Author: Jeroen van Meeuwen <vanmeeuwen@kolabsys.com> |
+ +--------------------------------------------------------------------------+
+*/
+
+class kolab_recipient_policy {
+
+ static function format() {
+ $_args = func_get_args();
+
+ $args = Array();
+
+ for ($i = 0; $i < func_num_args(); $i++) {
+ #$args[$i] = preg_replace('/\./', '\.', $_args[$i]);
+ $args[$i] = $_args[$i];
+ }
+
+ return $args;
+ }
+
+ static function normalize_userdata($userdata)
+ {
+ $keymap = Array(
+ 'sn' => 'surname',
+ );
+
+ foreach ($userdata as $key => $value) {
+ if (isset($keymap[$key])) {
+ $_key = $keymap[$key];
+ } else {
+ $_key = $key;
+ }
+
+ $userdata[$_key] = str_replace(' ', '', $userdata[$key]);
+ }
+
+ return $userdata;
+ }
+
+ static function primary_mail($userdata)
+ {
+ $userdata = self::normalize_userdata($userdata);
+
+ $conf = Conf::get_instance();
+
+ if (isset($userdata['domain'])) {
+ $primary_mail = $conf->get_raw($userdata['domain'], 'primary_mail');
+ } else {
+ $primary_mail = $conf->get_raw($_SESSION['user']->get_domain(), 'primary_mail');
+ // Also append the domain to the userdata
+ $userdata['domain'] = $_SESSION['user']->get_domain();
+ }
+
+ preg_match_all('/%\((\w+)\)s/', $primary_mail, $substrings);
+
+ // Update userdata array
+ for ($x = 0; $x < count($substrings[0]); $x++) {
+ if (array_key_exists($substrings[1][$x], $userdata)) {
+ if (!empty($substrings[2][$x])) {
+ if (!empty($substrings[3][$x])) {
+ $primary_mail = preg_replace(
+ '/%\(' . $substrings[1][$x]. '\)s/',
+ substr(
+ $userdata[$substrings[1][$x]],
+ $substrings[2][$x],
+ $substrings[3][$x]
+ ),
+ $primary_mail
+ );
+ } else {
+ $primary_mail = preg_replace(
+ '/%\(' . $substrings[1][$x]. '\)s/',
+ substr(
+ $userdata[$substrings[1][$x]],
+ $substrings[2][$x]
+ ),
+ $primary_mail
+ );
+ }
+ } elseif (!empty($substrings[3][$x])) {
+ $primary_mail = preg_replace(
+ '/%\(' . $substrings[1][$x]. '\)s/',
+ substr(
+ $userdata[$substrings[1][$x]],
+ 0,
+ $substrings[3][$x]
+ ),
+ $primary_mail
+ );
+ } else {
+ $primary_mail = preg_replace(
+ '/%\(' . $substrings[1][$x]. '\)s/',
+ $userdata[$substrings[1][$x]],
+ $primary_mail
+ );
+ }
+ } else {
+ console("Key " . $substrings[1][$x] . " does not exist in \$userdata");
+ }
+ }
+
+ return $primary_mail;
+
+ }
+
+ static function secondary_mail($userdata)
+ {
+ $secondary_mail_addresses = Array();
+
+ $functions = Array(
+ '\'%\((\w+)\)s\'\.capitalize\(\)' => 'strtoupper(substr("%(${1})s", 0, 1)) . strtolower(substr("%(${1})s", 1))',
+ '\'%\((\w+)\)s\'\.lower\(\)' => 'strtolower("%(${1})s")',
+ '\'%\((\w+)\)s\'\.upper\(\)' => 'strtoupper("%(${1})s")',
+ );
+
+ $userdata = self::normalize_userdata($userdata);
+
+ $conf = Conf::get_instance();
+
+ if (isset($userdata['domain'])) {
+ $secondary_mail = $conf->get_raw($userdata['domain'], 'secondary_mail');
+ } else {
+ $secondary_mail = $conf->get_raw($_SESSION['user']->get_domain(), 'secondary_mail');
+ $userdata['domain'] = $_SESSION['user']->get_domain();
+ }
+
+ $secondary_mail = preg_replace('/^{\d:\s*/','',$secondary_mail);
+ $secondary_mail = preg_replace('/\s*}$/','',$secondary_mail);
+ $secondary_mail = preg_replace('/,\d+:\s*/',',',$secondary_mail);
+ $secondary_mail = "[" . $secondary_mail . "]";
+ $secondary_mail = json_decode($secondary_mail, true);
+
+ $orig_userdata = $userdata;
+
+ foreach ($secondary_mail as $policy_rule) {
+ foreach ($policy_rule as $format => $rule) {
+ $userdata = $orig_userdata;
+
+ $format = preg_replace('/(\{\d+\})/', '%s', $format);
+
+ preg_match_all('/\'%\((\w+)\)s\'\[(\d+):(\d+)\]/', $rule, $substrings);
+
+ // Update userdata array
+ for ($x = 0; $x < count($substrings[0]); $x++) {
+ if (array_key_exists($substrings[1][$x], $userdata)) {
+ $userdata[$substrings[1][$x]] = substr($userdata[$substrings[1][$x]], $substrings[2][$x], $substrings[3][$x]);
+ } else {
+ console("Key " . $substrings[1][$x] . " does not exist in \$userdata");
+ }
+
+ $rule = preg_replace(
+ '/\'%\(' . $substrings[1][$x] . '\)s\'\[' . $substrings[2][$x] . ':' . $substrings[3][$x] . '\]/',
+ '\'%(' . $substrings[1][$x] . ')s\'',
+ $rule
+ );
+
+ }
+
+ foreach ($functions as $match => $replace) {
+ if (preg_match('/' . $match . '/', $rule, $strings)) {
+
+ if (array_key_exists($strings[1], $userdata)) {
+ $rule = preg_replace('/' . $match . '/', $replace, $rule);
+ }
+
+ }
+ }
+
+ $expanded = $conf->expand($rule, $userdata);
+
+ eval("\$result = self::" . $expanded . ";");
+
+ eval("\$result = sprintf('" . $format . "', '" . implode("', '", array_values($result)) . "');");
+
+ $secondary_mail_addresses[] = $result;
+
+ }
+
+ }
+
+ return $secondary_mail_addresses;
+
+ }
+}
+?>