/** * Add action method * * @return void */ public function add($rid = null) { $this->prepareView('users/add.phtml'); $this->view->title = 'Add User'; if (null !== $rid) { $role = new Model\Role(); $role->getById($rid); $this->view->title .= ' : ' . $role->name; $fields = $this->application->config()['forms']['App\\Form\\User']; $fields[1]['password1']['required'] = true; $fields[1]['password2']['required'] = true; $fields[0]['role_id']['value'] = $rid; unset($fields[0]['clear_logins']); unset($fields[0]['failed_attempts']); $this->view->form = new Form\User($fields); if ($this->request->isPost()) { $this->view->form->addFilter('strip_tags')->addFilter('htmlentities', [ENT_QUOTES, 'UTF-8'])->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->save($this->view->form->getFields(), $this->application->config()['application_title']); $this->view->id = $user->id; $this->sess->setRequestValue('saved', true); $this->redirect('/users/edit/' . $user->id); } } } else { $this->view->roles = (new Model\Role())->getAll(); } $this->send(); }
/** * 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('Enter Email: '); } $email = $username; } else { while ($username == '') { $username = $this->console->prompt('Enter Username: '******''; while (!(new Email())->evaluate($email)) { $email = $this->console->prompt('Enter Email: '); } } } $dupeUser = Table\Users::findBy(['username' => $username]); } $password = ''; while ($password == '') { $password = $this->console->prompt('Enter Password: '******''; while (strtolower($active) != 'y' && strtolower($active) != 'n') { $active = $this->console->prompt('Active? (Y/N): '); } $verified = ''; while (strtolower($verified) != 'y' && strtolower($verified) != 'n') { $verified = $this->console->prompt('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->application->config()['application_title']); $this->console->write(); $this->console->write($this->console->colorize('User Added!', Console::BOLD_GREEN)); }
/** * JSON action method * * @return void */ public function json($id) { $json = []; if (is_numeric($id)) { $role = new Model\Role(); $role->getById($id); if (isset($role->id) && null !== $role->permissions) { $permissions = unserialize($role->permissions); if (is_array($permissions['allow']) && count($permissions['allow']) > 0) { foreach ($permissions['allow'] as $allow) { $json[] = ['resource' => $allow['resource'], 'action' => $allow['permission'], 'permission' => 'allow']; } } if (is_array($permissions['deny']) && count($permissions['deny']) > 0) { foreach ($permissions['deny'] as $deny) { $json[] = ['resource' => $deny['resource'], 'action' => $deny['permission'], 'permission' => 'deny']; } } } } else { $config = $this->application->config(); if (isset($config['resources'][$id])) { $json['permissions'] = $config['resources'][$id]; } else { foreach ($config['resources'] as $resource => $perms) { if (strpos($resource, '|') !== false && substr($resource, 0, strlen($id) + 1) == $id . '|') { $json['permissions'] = $perms; } } } } $this->response->setBody(json_encode($json, JSON_PRETTY_PRINT)); $this->send(200, ['Content-Type' => 'application/json']); }