/** * Add action method * * @return void */ public function add() { $roleId = $this->getRoleId(); $username = ''; $email = null; $role = new Model\Role(); $role->getById($roleId); $this->console->write(); $dupeUser = Table\Users::findBy(['username' => $username]); while ($username == '' || isset($dupeUser->id)) { if (isset($dupeUser->id)) { $this->console->write($this->console->colorize('That username already exists.', Console::BOLD_RED)); $username = ''; } if ($role->email_as_username) { while (!(new Email())->evaluate($username)) { $username = $this->console->prompt($this->console->getIndent() . 'Enter Email: '); } $email = $username; } else { while ($username == '') { $username = $this->console->prompt($this->console->getIndent() . 'Enter Username: '******''; while (!(new Email())->evaluate($email)) { $email = $this->console->prompt($this->console->getIndent() . 'Enter Email: '); } } } $dupeUser = Table\Users::findBy(['username' => $username]); } $password = ''; while ($password == '') { $password = $this->console->prompt($this->console->getIndent() . 'Enter Password: '******''; while (strtolower($active) != 'y' && strtolower($active) != 'n') { $active = $this->console->prompt($this->console->getIndent() . 'Active? (Y/N): '); } $verified = ''; while (strtolower($verified) != 'y' && strtolower($verified) != 'n') { $verified = $this->console->prompt($this->console->getIndent() . 'Verified? (Y/N): '); } $fields = ['role_id' => $roleId, 'username' => $username, 'password1' => $password, 'email' => $email, 'active' => strtolower($active) == 'y' ? 1 : 0, 'verified' => strtolower($verified) == 'y' ? 1 : 0]; $user = new Model\User(); $user->save($fields); $this->console->write(); $this->console->write($this->console->colorize('User Added!', Console::BOLD_GREEN)); }
/** * Profile action method * * @return void */ public function profile() { $this->prepareView('phire/profile.phtml'); $this->view->title = 'Profile'; $user = new Model\User(); $user->getById($this->sess->user->id); $role = new Model\Role(); $role->getById($this->sess->user->role_id); if ($role->email_as_username) { $fields = $this->application->config()['forms']['Phire\\Form\\ProfileEmail']; $fields[2]['role_id']['value'] = $this->sess->user->role_id; $this->view->form = new Form\ProfileEmail($fields); } else { $fields = $this->application->config()['forms']['Phire\\Form\\Profile']; $fields[2]['role_id']['value'] = $this->sess->user->role_id; if ($role->email_required) { $fields[1]['email']['required'] = true; } $this->view->form = new Form\Profile($fields); } $this->view->form->addFilter('htmlentities', [ENT_QUOTES, 'UTF-8'])->setFieldValues($user->toArray()); if ($this->request->isPost()) { $this->view->form->addFilter('strip_tags')->setFieldValues($this->request->getPost()); if ($this->view->form->isValid()) { $this->view->form->clearFilters()->addFilter('html_entity_decode', [ENT_QUOTES, 'UTF-8'])->filter(); $fields = $this->view->form->getFields(); $role = new Model\Role(); $role->getById($this->sess->user->role_id); $fields['verified'] = (int) (!$role->verification); $user = new Model\User(); $user->update($fields, $this->sess); $this->view->id = $user->id; $this->sess->setRequestValue('saved', true); $this->redirect(BASE_PATH . APP_URI . '/profile'); } } $this->send(); }
/** * JSON action method * * @param int $id * @return void */ public function jsonEmail($id) { $json = []; if (is_numeric($id)) { $role = new Model\Role(); $role->getById($id); $json['email_as_username'] = $role->email_as_username; } $this->response->setBody(json_encode($json, JSON_PRETTY_PRINT)); $this->send(200, ['Content-Type' => 'application/json']); }
/** * Edit action method * * @param int $id * @return void */ public function edit($id) { $user = new Model\User(); $user->getById($id); if (!isset($user->id)) { $this->redirect(BASE_PATH . APP_URI . '/users'); } if ($this->services['acl']->isAllowed($this->sess->user->role, 'users-of-role-' . $user->role_id, 'edit')) { $this->prepareView('phire/users/edit.phtml'); $this->view->title = 'Edit User'; $this->view->username = $user->username; $role = new Model\Role(); $role->getById($user->role_id); if ($role->email_as_username) { $fields = $this->application->config()['forms']['Phire\\Form\\UserEmail']; $fields[1]['email']['attributes']['onkeyup'] = 'phire.changeTitle(this.value);'; } else { $fields = $this->application->config()['forms']['Phire\\Form\\User']; $fields[1]['username']['attributes']['onkeyup'] = 'phire.changeTitle(this.value);'; if ($role->email_required) { $fields[2]['email']['required'] = true; } } $roles = $role->getAll(); $roleValues = []; foreach ($roles as $r) { $roleValues[$r->id] = $r->name; } $fields[1]['password1']['required'] = false; $fields[1]['password2']['required'] = false; $fields[0]['role_id']['type'] = 'select'; $fields[0]['role_id']['label'] = 'Role'; $fields[0]['role_id']['value'] = $roleValues; $fields[0]['role_id']['marked'] = $user->role_id; $fields[0]['role_id']['attributes'] = ['onchange' => 'phire.checkUserRole(this);']; $this->view->form = $role->email_as_username ? new Form\UserEmail($fields) : new Form\User($fields); $this->view->form->addFilter('strip_tags', null, 'textarea')->addFilter('htmlentities', [ENT_QUOTES, 'UTF-8'])->setFieldValues($user->toArray()); if ($this->request->isPost()) { $this->view->form->addFilter('strip_tags', null, 'textarea')->setFieldValues($this->request->getPost()); if ($this->view->form->isValid()) { $this->view->form->clearFilters()->addFilter('html_entity_decode', [ENT_QUOTES, 'UTF-8'])->filter(); $user = new Model\User(); $user->update($this->view->form->getFields(), $this->sess); $this->view->id = $user->id; $this->sess->setRequestValue('saved', true); $this->redirect(BASE_PATH . APP_URI . '/users/edit/' . $user->id); } } $this->send(); } else { $this->redirect(BASE_PATH . APP_URI . '/users'); } }