Ejemplo n.º 1
0
 /**
  * Creates a User
  */
 public function createAction()
 {
     $form = new UsersForm();
     $form->setDI($this->getDI());
     if ($this->request->isPost()) {
         if ($form->isValid($this->request->getPost()) !== false) {
             $user = new Users();
             $active = $this->request->getPost('active', 'striptags');
             if ($active !== 'Y') {
                 $active = 'N';
             }
             $user->assign(['name' => $this->request->getPost('name', 'striptags'), 'rolesId' => $this->request->getPost('rolesId', 'int'), 'email' => $this->request->getPost('email', 'email'), 'active' => $active]);
             if ($user->save()) {
                 // The user selected to send an email confirmation
                 $emailExtraMsg = $this->request->getPost('emailActivationMsg', 'striptags', '');
                 $emailExtraMsg = trim($emailExtraMsg);
                 if ($emailExtraMsg != '') {
                     $emailConfirmation = new EmailConfirmations();
                     $emailConfirmation->usersId = $user->id;
                     $emailConfirmation->extraMsg = $emailExtraMsg;
                     if ($emailConfirmation->save()) {
                         $this->flash->notice(sprintf($this->translate->gettext('A confirmation mail has been sent to %s'), $user->email));
                     }
                 }
                 $this->flash->success($this->translate->gettext('User was created successfully. You may add another user.'));
                 Tag::resetInput();
             } else {
                 $this->flash->error($user->getMessages());
             }
         }
     }
     $this->view->form = $form;
 }
Ejemplo n.º 2
0
 /**
  *
  */
 public function createAction($argv)
 {
     $params = $this->parseArgs($argv, ['title' => 'Add a user with a permission role.', 'args' => ['required' => ['email', 'role'], 'optional' => []], 'opts' => ['p|password:'******'set user password (otherwise it will need to be on first login).', 'a|activate' => 'activate', 'E|send-email?' => 'send email confirmation with optional message']]);
     list($emailRaw, $roleRef) = $params['args'];
     $opts = $params['opts'];
     $emailParts = mailparse_rfc822_parse_addresses($emailRaw);
     if (empty($emailParts) || $emailParts[0]['display'] == $emailParts[0]['address']) {
         throw new ArgumentValidationException('Email must be in form: display <address>', 1);
     }
     $name = $emailParts[0]['display'];
     $email = $emailParts[0]['address'];
     // Validate the email
     if (($email = filter_var($email, FILTER_VALIDATE_EMAIL)) === false) {
         throw new ArgumentValidationException('Email is invalid', 1);
     }
     $role = $this->getRoleByUniqueRef($roleRef);
     // Validate the password
     if (isset($opts['password'])) {
         $password = $opts['password'];
         $passwordMinLength = $this->config->security->passwordMinLength;
         if (mb_strlen($password) < $passwordMinLength) {
             throw new ArgumentValidationException("Password must be at least {$passwordMinLength} characters", 1);
         }
     } else {
         // The model will check for an empty string and will create a random password
         $password = '';
     }
     // Check for CLI flags
     $active = isset($opts['activate']) ? 'Y' : 'N';
     $sendEmail = array_key_exists('send-email', $opts);
     $emailExtraMsg = isset($opts['send-email']) ? trim($opts['send-email']) : '';
     $user = new Users();
     $user->assign(['name' => $name, 'rolesId' => $role->id, 'email' => $email, 'active' => $active, 'password' => $password]);
     if (!$user->save()) {
         $message = implode("\n", $user->getMessages());
         throw new \Exception("{$message}", 1);
     }
     if ($sendEmail) {
         echo "Sending email confirmation to user\n";
         $emailConfirmation = new EmailConfirmations();
         $emailConfirmation->usersId = $user->id;
         $emailConfirmation->extraMsg = $emailExtraMsg;
         if (!$emailConfirmation->save()) {
             $message = implode("\n", $emailConfirmation->getMessages());
             throw new \Exception($message, 1);
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Confirms an e-mail, if the user must change thier password then changes it
  */
 public function confirmEmailAction()
 {
     $code = $this->dispatcher->getParam('code');
     $t = $this->translate;
     $confirmation = EmailConfirmations::findFirstByCode($code);
     if (!$confirmation) {
         $this->flash->error($t->gettext('The confirmation code was not valid.'));
         return $this->dispatcher->forward(['controller' => 'index', 'action' => 'notification']);
     }
     if ($confirmation->user->isBanned()) {
         $this->flash->error($t->gettext('User is banned'));
         return $this->dispatcher->forward(['controller' => 'index', 'action' => 'notification']);
     }
     if ($confirmation->confirmed != 'N') {
         $this->flash->notice($t->gettext("You have already confirmed your email. Proceed to signin"));
         return $this->dispatcher->forward(['controller' => 'session', 'action' => 'signin']);
     }
     $confirmation->confirmed = 'Y';
     $confirmation->user->active = 'Y';
     /**
      * Change the confirmation to 'confirmed' and update the user to 'active'
      */
     if (!$confirmation->save()) {
         foreach ($confirmation->getMessages() as $message) {
             $this->flash->error($message);
         }
         return $this->dispatcher->forward(['controller' => 'index', 'action' => 'notification']);
     }
     /**
      * Identify the user in the application
      */
     try {
         $this->auth->authUserById($confirmation->user->id, 'email_confirm');
         $this->flash->success($this->translate->gettext('The email was successfully confirmed'));
         return $this->response->redirect($this->config->app->defaultPath);
     } catch (AuthMustChangePasswordException $e) {
         return $this->response->redirect('settings/changePassword');
     }
 }