hasRequestParameter("max_per_page")) $this->getUser()->setResultsPerPage($this->getRequestParameter("max_per_page")); return parent::executeList(); } public function executeCreate() { $this->setTemplate("edit"); $result = $this->executeEdit(); if($this->getRequest()->getMethod() == sfRequest::GET) { if(!$this->hasRequestParameter("id")) { $this->updateAddressFromRequest(); } $this->address->setActive(true); $this->address->setSaveInMailbox(true); } return $result; } public function handleErrorCreate() { $this->setTemplate("edit"); return $this->handleErrorEdit(); } public function validateCreate() { return $this->validateEdit(); } public function validateEdit() { $i18n = sfI18N::getInstance(); if($this->getRequest()->getMethod() == sfRequest::GET) return true; $newaddress = $this->getRequestParameter('address'); if($newaddress["domain_id"]=='') { $this->getRequest()->setError('address{alias}', 'A domain must be selected'); return false; } $address = new Address(); if($this->getRequest()->hasParameter('id')) $address = AddressPeer::retrieveByPk($this->getRequestParameter('id')); // if we got different values than the current record already has if($this->getActionName()=="create" || $address->getLocalpart() != $newaddress["localpart"] || $address->getDomainId() != $newaddress["domain_id"] ) { // check if localpart@domain is already used $c = new Criteria(); $c->add(AddressPeer::LOCALPART, $newaddress["localpart"]); $c->add(AddressPeer::DOMAIN_ID, $newaddress["domain_id"]); $checkaddress = AddressPeer::doSelectOne($c); if($checkaddress) { $this->getRequest()->setError('address{alias}', $i18n->__('The address "%1%" already exists', array("%1%"=>$checkaddress))); return false; } } // check if either save to mailbox option or destination is set to satisfy message routing if(!isset($newaddress["save_in_mailbox"]) && $newaddress["destination"]=='') { $this->getRequest()->setError('address{save_in_mailbox}', 'If messages are not saved in the mailbox, the forward destination has to be set'); $this->getRequest()->setError('address{destination}', 'Destination can not be empty if the message is not saved in the mailbox'); return false; } // verify destination list $ev = new sfEmailValidator(); $destinations = Address::getDestinationAsArray($newaddress["destination"]); foreach($destinations as $dest) { if($dest=='') continue; // check email if(strpos($dest, "@") !== false) { $error = ''; $ev->initialize($this->getContext(), array( 'email_error' => $i18n->__('Invalid E-Mail address "%1%"', array("%1%" => $dest)) )); if(!$ev->execute($dest, $error)) { $this->getRequest()->setError('address{destination}', $error); return false; } } else // check if mailbox name is valid { // superadmin has all freedom if($this->getUser()->hasCredential("superadmin")) continue; $c = new Criteria(); $c->add(MailboxPeer::NAME, $dest); $mailbox = MailboxPeer::doSelectOne($c); if(!$mailbox) { // no such target $this->getRequest()->setError('address{destination}', $i18n->__('Mailbox "%1%" does not exist', array("%1%" => $dest))); return false; } // check permissions $c = new Criteria(); $c->add(DomainPermissionPeer::DOMAIN_ID, $mailbox->getDomainId()); $permissions = $this->getUser()->getDomainPermissions($c); if(!$permissions) { $this->getRequest()->setError('address{destination}', $i18n->__('No permissions to use Mailbox "%1%" as destination', array("%1%" => $dest))); return false; } } } return true; } protected function updateAddressFromRequest() { $address = $this->getRequestParameter('address'); if (isset($address['localpart'])) { $this->address->setLocalpart($address['localpart']); } if (isset($address['domain_id'])) { $this->address->setDomainId($address['domain_id']); } return parent::updateAddressFromRequest(); } }