/** * 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; }
/** * */ 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); } } }