From b5588cef1066b280d2a22c664f16fbc7dfd441d4 Mon Sep 17 00:00:00 2001 From: Ben Klang Date: Wed, 30 Dec 2009 22:40:30 +0000 Subject: [PATCH] Continue working on extensions Getting closer to being able to save extension data to the backend again. git-svn-id: https://svn.alkaloid.net/gpl/shout/trunk@506 06cd67b6-e706-0410-b29e-9de616bca6e9 --- extensions.php | 116 +++++++++++++++++++++----------------------- lib/Driver/Ldap.php | 47 +++++++++--------- lib/Forms/ExtensionForm.php | 32 +++++++++++- 3 files changed, 107 insertions(+), 88 deletions(-) diff --git a/extensions.php b/extensions.php index 29f3a8247..295d1658a 100644 --- a/extensions.php +++ b/extensions.php @@ -26,73 +26,65 @@ $section = 'extensions'; $title = _("Extensions: "); switch ($action) { - case 'add': - case 'edit': - if ($action == 'add') { - $title .= _("New Extension"); - // Treat adds just like an empty edit - $action = 'edit'; - } else { - $title .= sprintf(_("Edit Extension %s"), $extension); +case 'save': + $title .= sprintf(_("Save Extension %s"), $extension); + $FormName = $vars->get('formname'); - } - - $FormName = 'UserDetailsForm'; - $vars = new Horde_Variables($extensions[$extension]); - $Form = &Horde_Form::singleton($FormName, $vars); - - $Form->open($RENDERER, $vars, Horde::applicationUrl('extensions.php'), 'post'); - - break; - case 'save': - $title .= sprintf(_("Save Extension %s"), $extension); - $FormName = $vars->get('formname'); - - $Form = &Horde_Form::singleton($FormName, $vars); - - $FormValid = $Form->validate($vars, true); - - if (!$FormValid || !$Form->isSubmitted()) { - $notification->push("FIXME: Redirect to re-edit"); - } else { - // Form is Valid and Submitted - $extension = $vars->get('extension'); - - # FIXME: Input Validation (Text::??) - $details = array( - "newextension" => $vars->get('newextension'), - "name" => $vars->get('name'), - "mailboxpin" => $vars->get('mailboxpin'), - "email" => $vars->get('email'), - ); - - try { - $res = $shout_extensions->saveUser($context, - $extension, $details); - } catch (Exception $e) { - $notification->push($e); - } - $notification->push(_("User information updated."), - 'horde.success'); - } - - break; - case 'delete': - $title .= sprintf(_("Delete Extension %s"), $extension); - $extension = Horde_Util::getFormData('extension'); + $Form = &Horde_Form::singleton($FormName, $vars); - $res = $shout->deleteUser($context, $extension); + $FormValid = $Form->validate($vars, true); - if (!$res) { - echo "Failed!"; - print_r($res); + if ($Form->isSubmitted() && $FormValid) { + // Form is Valid and Submitted + try { + $Form->execute(); + } catch (Exception $e) { + $notification->push($e); } - $notification->push("User Deleted."); + $notification->push(_("User information updated."), + 'horde.success'); break; - case 'list': - default: - $action = 'list'; - $title .= _("List Users"); + } else { + $action = 'edit'; + // Fall-through to the "edit" action + } + +case 'add': +case 'edit': + if ($action == 'add') { + $title .= _("New Extension"); + // Treat adds just like an empty edit + $action = 'edit'; + } else { + $title .= sprintf(_("Edit Extension %s"), $extension); + + } + + $FormName = 'ExtensionDetailsForm'; + $vars = new Horde_Variables($extensions[$extension]); + $Form = &Horde_Form::singleton($FormName, $vars); + + $Form->open($RENDERER, $vars, Horde::applicationUrl('extensions.php'), 'post'); + + break; + +case 'delete': + $title .= sprintf(_("Delete Extension %s"), $extension); + $extension = Horde_Util::getFormData('extension'); + + $res = $shout->deleteUser($context, $extension); + + if (!$res) { + echo "Failed!"; + print_r($res); + } + $notification->push("User Deleted."); + break; + +case 'list': +default: + $action = 'list'; + $title .= _("List Users"); } require SHOUT_TEMPLATES . '/common-header.inc'; diff --git a/lib/Driver/Ldap.php b/lib/Driver/Ldap.php index e268caf7e..ee0358fde 100644 --- a/lib/Driver/Ldap.php +++ b/lib/Driver/Ldap.php @@ -483,35 +483,32 @@ class Shout_Driver_Ldap extends Shout_Driver } /** - * Save a user to the LDAP tree + * Save an extension to the LDAP tree * * @param string $context Context to which the user should be added * * @param string $extension Extension to be saved * - * @param array $userdetails Phone numbers, PIN, options, etc to be saved + * @param array $details Phone numbers, PIN, options, etc to be saved * * @return TRUE on success, PEAR::Error object on error */ - public function saveUser($context, $extension, $userdetails) + public function saveExtension($context, $extension, $details) { $ldapKey = &$this->_ldapKey; $appKey = &$this->_appKey; # FIXME Access Control/Authorization - if ( - !(Shout::checkRights("shout:contexts:$context:users", PERMS_EDIT, 1)) - && - !($userdetails[$appKey] == Auth::getAuth()) - ) { - return PEAR::raiseError("No permission to modify users in this " . - "context."); + if (!Shout::checkRights("shout:contexts:$context:extensions", PERMS_EDIT, 1)) { + // FIXME: Allow users to edit themselves + //&& !($details[$appKey] == Auth::getAuth())) { + throw new Shout_Exception(_("Permission denied to save extensions in this context.")); } $contexts = &$this->getContexts(); // $domain = $contexts[$context]['domain']; # Check to ensure the extension is unique within this context - $filter = "(&(objectClass=asteriskVoiceMailbox)(context=$context))"; + $filter = "(&(objectClass=AstVoicemailMailbox)(context=$context))"; $reqattrs = array('dn', $ldapKey); $res = @ldap_search($this->_LDAP, SHOUT_USERS_BRANCH . ',' . $this->_params['basedn'], @@ -522,30 +519,30 @@ class Shout_Driver_Ldap extends Shout_Driver } if (($res['count'] > 1) || ($res['count'] != 0 && - !in_array($res[0][$ldapKey], $userdetails[$appKey]))) { + !in_array($res[0][$ldapKey], $details[$appKey]))) { return PEAR::raiseError('Duplicate extension found. Not saving changes.'); } $entry = array( - 'cn' => $userdetails['name'], - 'sn' => $userdetails['name'], - 'mail' => $userdetails['email'], - 'uid' => $userdetails['email'], - 'voiceMailbox' => $userdetails['newextension'], - 'voiceMailboxPin' => $userdetails['mailboxpin'], + 'cn' => $details['name'], + 'sn' => $details['name'], + 'mail' => $details['email'], + 'uid' => $details['email'], + 'voiceMailbox' => $details['newextension'], + 'voiceMailboxPin' => $details['mailboxpin'], 'context' => $context, - 'asteriskUserDialOptions' => $userdetails['dialopts'], + 'asteriskUserDialOptions' => $details['dialopts'], ); - if (!empty ($userdetails['telephonenumber'])) { - $entry['telephoneNumber'] = $userdetails['telephonenumber']; + if (!empty ($details['telephonenumber'])) { + $entry['telephoneNumber'] = $details['telephonenumber']; } $validusers = &$this->getUsers($context); if (!isset($validusers[$extension])) { # Test to see if we're modifying an existing user that has # no telephone system objectClasses and update that object/user - $rdn = $ldapKey.'='.$userdetails[$appKey].','; + $rdn = $ldapKey.'='.$details[$appKey].','; $branch = SHOUT_USERS_BRANCH.','.$this->_params['basedn']; # This test is something of a hack. I want a cheap way to check @@ -555,7 +552,7 @@ class Shout_Driver_Ldap extends Shout_Driver # it'll return error. If it ever returns false something wierd # is going on. $res = @ldap_compare($this->_LDAP, $rdn.$branch, - $ldapKey, $userdetails[$appKey]); + $ldapKey, $details[$appKey]); if ($res === false) { # We should never get here: a DN should ALWAYS match itself return PEAR::raiseError("Internal Error: " . __FILE__ . " at " . @@ -563,7 +560,7 @@ class Shout_Driver_Ldap extends Shout_Driver } elseif ($res === true) { # The object/user exists but doesn't have the Asterisk # objectClasses - $extension = $userdetails['newextension']; + $extension = $details['newextension']; # $tmp is the minimal information required to establish # an account in LDAP as required by the objectClasses. @@ -584,7 +581,7 @@ class Shout_Driver_Ldap extends Shout_Driver # Populate the $validusers array to make the edit go smoothly # below $validusers[$extension] = array(); - $validusers[$extension][$appKey] = $userdetails[$appKey]; + $validusers[$extension][$appKey] = $details[$appKey]; # The remainder of the work is done at the outside of the # parent if() like a normal edit. diff --git a/lib/Forms/ExtensionForm.php b/lib/Forms/ExtensionForm.php index 00a9bc7f9..444319dda 100644 --- a/lib/Forms/ExtensionForm.php +++ b/lib/Forms/ExtensionForm.php @@ -10,8 +10,15 @@ * @package Shout */ -class UserDetailsForm extends Horde_Form { +class ExtensionDetailsForm extends Horde_Form { + /** + * ExtensionDetailsForm constructor. + * + * @global $shout_extensions + * @param $vars + * @return + */ function __construct(&$vars) { global $shout_extensions; @@ -37,4 +44,27 @@ class UserDetailsForm extends Horde_Form { return true; } + /** + * Process this form, saving its information to the backend. + * + * @param string $context Context in which to execute this save + * FIXME: is there a better way to get the $context and $shout_extensions? + */ + function execute($context) + { + global $shout_extensions; + + $extension = $this->vars->get('extension'); + + # FIXME: Input Validation (Text::??) + $details = array( + 'newextension' => $vars->get('newextension'), + 'name' => $vars->get('name'), + 'mailboxpin' => $vars->get('mailboxpin'), + 'email' => $vars->get('email'), + ); + + $res = $shout_extensions->saveExtension($context, $extension, $details); + } + } \ No newline at end of file -- 2.11.0