Beispiel #1
0
 /**
  * Person add action creates a new person object.
  * 
  * @param KCommandContext $context Commaind chain context
  * 
  * @return AnDomainEntityAbstract
  */
 protected function _actionAdd(KCommandContext $context)
 {
     //we are not saving this person but just validating it
     $person = parent::_actionAdd($context);
     $data = $context->data;
     $person->userId = PHP_INT_MAX;
     //is assiged automatically
     //manually set the password to make sure there's a password
     $person->setPassword($data->password);
     //add the validations here
     $this->getRepository()->getValidator()->addValidation('username', 'uniqueness')->addValidation('email', 'uniqueness');
     if ($person->validate() === false) {
         throw new AnErrorException($person->getErrors(), KHttpResponse::BAD_REQUEST);
     }
     $person->reset();
     $firsttime = !(bool) $this->getService('repos://site/users')->getQuery(true)->fetchValue('id');
     $user = clone JFactory::getUser();
     $authorize =& JFactory::getACL();
     if ($firsttime) {
         //for now lets make the com_notes assigable to always
         $component = $this->getService('repos://site/components')->find(array('component' => 'com_notes'));
         if ($component) {
             $component->setAssignmentForIdentifier('person', ComComponentsDomainBehaviorAssignable::ACCESS_ALWAYS);
         }
         $datbase = $this->getService('koowa:database.adapter.mysqli');
         //joomla legacy. don't know what happens if it's set to 1
         $query = "INSERT INTO #__users VALUES (62, 'admin', 'admin', '*****@*****.**', '', 'Super Administrator', 0, 1, 25, '', '', '', '')";
         $datbase->execute($query);
         $query = "INSERT INTO #__core_acl_aro VALUES (10,'users','62',0,'Administrator',0)";
         $datbase->execute($query);
         $query = "INSERT INTO #__core_acl_groups_aro_map VALUES (25,'',10)";
         $datbase->execute($query);
         $user =& JFactory::getUser();
         $user = JUser::getInstance(62);
         $this->unregisterCallback('after.add', array($this, 'notifyAdminsNewUser'));
     } else {
         $user->set('id', 0);
         $config =& JComponentHelper::getParams('com_users');
         $user->set('usertype', $config->get('new_usertype'));
         $user->set('gid', $authorize->get_group_id('', $config->get('new_usertype'), 'ARO'));
         if ($this->activationRequired()) {
             jimport('joomla.user.helper');
             $user->set('activation', JUtility::getHash(JUserHelper::genRandomPassword()));
             $user->set('block', '1');
         }
     }
     $user->set('name', $person->name);
     $user->set('username', $person->username);
     $user->set('email', $person->email);
     $user->set('password', $person->getPassword(true));
     $date =& JFactory::getDate();
     $user->set('registerDate', $date->toMySQL());
     $user->save();
     $person = $this->getRepository()->find(array('userId' => $user->id));
     //if person is null then user has not been saved
     if (!$person) {
         throw new RuntimeException('Unexpected error when saving user');
     }
     //set the portrait image
     if ($file = KRequest::get('files.portrait', 'raw')) {
         $person->setPortraitImage(array('url' => $file['tmp_name'], 'mimetype' => $file['type']));
     }
     //set the status
     $this->getResponse()->status = KHttpResponse::CREATED;
     $this->setItem($person);
     if (!$person->enabled) {
         $this->registerCallback('after.add', array($this, 'mailActivationLink'));
     } elseif ($this->isDispatched()) {
         if ($context->request->getFormat() == 'html') {
             $context->response->status = 200;
             $this->registerCallback('after.add', array($this, 'login'));
         }
     }
     return $person;
 }
Beispiel #2
0
 /**
  * Person add action creates a new person object.
  *
  * @param KCommandContext $context Commaind chain context
  *
  * @return AnDomainEntityAbstract
  */
 protected function _actionAdd(KCommandContext $context)
 {
     $data = $context->data;
     $viewer = get_viewer();
     $isFirstUser = !(bool) $this->getService('repos://site/users')->getQuery(true)->fetchValue('id');
     $person = parent::_actionAdd($context);
     if ($data->password) {
         $person->setPassword($data->password);
     }
     $redirectUrl = 'option=com_people';
     $this->getRepository()->getValidator()->addValidation('username', 'uniqueness')->addValidation('email', 'uniqueness');
     if ($person->validate() === false) {
         throw new AnErrorException($person->getErrors(), KHttpResponse::BAD_REQUEST);
         return false;
     }
     if ($viewer->admin() && in_array($data->userType, $this->_allowed_user_types)) {
         $person->userType = $data->userType;
     } else {
         $person->userType = ComPeopleDomainEntityPerson::USERTYPE_REGISTERED;
     }
     if ($isFirstUser) {
         $this->registerCallback('after.add', array($this, 'activateFirstAdmin'));
     } elseif ($viewer->admin()) {
         $redirectUrl .= '&view=people';
         if ($person->admin()) {
             $this->registerCallback('after.add', array($this, 'mailAdminsNewAdmin'));
         }
     } else {
         $context->response->setHeader('X-User-Activation-Required', true);
         $this->setMessage(JText::sprintf('COM-PEOPLE-PROMPT-ACTIVATION-LINK-SENT', $person->name), 'success');
         $redirectUrl .= '&view=session';
     }
     $context->response->setRedirect(JRoute::_($redirectUrl));
     $context->response->status = 200;
     return $person;
 }