/** * Set the field values * * @param array $values * @return Unsubscribe */ public function setFieldValues(array $values = null) { parent::setFieldValues($values); if ($_POST && null !== $this->email) { $member = Table\Users::findBy(['email' => $this->email]); if (!isset($member->id)) { $this->getElement('email')->addValidator(new Validator\NotEqual($this->email, 'That email does not exist.')); } else { if (null !== $member->role_id) { $sess = \Pop\Web\Session::getInstance(); $requireLogin = true; $role = Table\Roles::findById($member->role_id); if (isset($role->id) && null !== $role->permissions) { $permissions = unserialize($role->permissions); if (isset($permissions['deny'])) { foreach ($permissions['deny'] as $deny) { if ($deny['resource'] == 'member-login') { $requireLogin = false; } } } } if ($requireLogin) { if (!isset($sess->member) || isset($sess->member) && $sess->member->id != $member->id) { $memberAdmin = new \Phire\Members\Model\MembersAdmin(); $memberAdmin->getByRoleId($member->role_id); $memberUri = isset($memberAdmin->uri) ? $memberAdmin->uri : APP_URI; $this->getElement('email')->addValidator(new Validator\NotEqual($this->email, 'You must <a href="' . BASE_PATH . $memberUri . '/login">log in</a> to unsubscribe.')); } } } } } return $this; }
/** * Set the field values * * @param array $values * @param Auth $auth * @return Login */ public function setFieldValues(array $values = null, Auth $auth = null) { parent::setFieldValues($values); if ($_POST && null !== $this->username && null !== $this->password && null !== $auth) { $auth->authenticate(html_entity_decode($this->username, ENT_QUOTES, 'UTF-8'), html_entity_decode($this->password, ENT_QUOTES, 'UTF-8')); if (!$auth->isValid()) { $this->getElement('password')->addValidator(new Validator\NotEqual($this->password, 'The login was not correct.')); } else { if (!$auth->adapter()->getUser()->verified) { $this->getElement('password')->addValidator(new Validator\NotEqual($this->password, 'That user is not verified.')); } else { if (!$auth->adapter()->getUser()->active) { $this->getElement('password')->addValidator(new Validator\NotEqual($this->password, 'That user is blocked.')); } else { $role = Table\Roles::findById($auth->adapter()->getUser()->role_id); if (isset($role->id) && null !== $role->permissions) { $permissions = unserialize($role->permissions); if (isset($permissions['deny'])) { foreach ($permissions['deny'] as $deny) { if ($deny['resource'] == 'login') { $this->getElement('password')->addValidator(new Validator\NotEqual($this->password, 'That user is not allowed to login.')); } } } } } } } } return $this; }
/** * Get session config by ID * * @param int $id * @return void */ public function getById($id) { $session = Table\UserSessionConfig::findById((int) $id); if (isset($session->role_id)) { $this->data['role_id'] = $session->role_id; $this->data['multiple_sessions'] = $session->multiple_sessions; $this->data['allowed_attempts'] = $session->allowed_attempts; $this->data['session_expiration'] = $session->session_expiration > 0 ? round($session->session_expiration / 60) : 0; $this->data['timeout_warning'] = (int) $session->timeout_warning; $this->data['ip_allowed'] = $session->ip_allowed; $this->data['ip_blocked'] = $session->ip_blocked; $this->data['log_emails'] = $session->log_emails; $this->data['log_type'] = $session->log_type; $role = \Phire\Table\Roles::findById($session->role_id); $this->data['role'] = $role->name; } }
/** * Login action method * * @return void */ public function login() { $this->prepareView('phire/login.phtml'); $this->view->title = 'Login'; $this->view->form = new Form\Login($this->application->config()['forms']['Phire\\Form\\Login']); if ($this->request->isPost()) { $auth = new Auth\Auth(new Auth\Adapter\Table('Phire\\Table\\Users', Auth\Auth::ENCRYPT_BCRYPT)); $this->view->form->addFilter('strip_tags')->addFilter('htmlentities', [ENT_QUOTES, 'UTF-8'])->setFieldValues($this->request->getPost(), $auth); if ($this->view->form->isValid()) { $this->sess->user = new \ArrayObject(['id' => $auth->adapter()->getUser()->id, 'role_id' => $auth->adapter()->getUser()->role_id, 'role' => Table\Roles::findById($auth->adapter()->getUser()->role_id)->name, 'username' => $auth->adapter()->getUser()->username, 'email' => $auth->adapter()->getUser()->email], \ArrayObject::ARRAY_AS_PROPS); if (php_sapi_name() != 'cli') { $path = BASE_PATH . APP_URI; if ($path == '') { $path = '/'; } $cookie = Cookie::getInstance(['path' => $path]); $cookie->set('phire', ['base_path' => BASE_PATH, 'app_path' => APP_PATH, 'content_path' => CONTENT_PATH, 'modules_path' => MODULES_PATH, 'app_uri' => APP_URI]); } $this->redirect(BASE_PATH . (APP_URI != '' ? APP_URI : '/')); } } $this->send(); }
/** * Login action method * * @return void */ public function login() { $this->prepareView('members/login.phtml'); $this->view->title = $this->memberName . ' : Login'; $fields = $this->application->config()['forms']['Phire\\Members\\Form\\Login']; $fields['role_id']['value'] = $this->memberRoleId; $this->view->form = new Login($fields); if ($this->request->isPost()) { $auth = new Auth\Auth(new Auth\Adapter\Table('Phire\\Table\\Users', Auth\Auth::ENCRYPT_BCRYPT)); $this->view->form->addFilter('strip_tags')->addFilter('htmlentities', [ENT_QUOTES, 'UTF-8'])->setFieldValues($this->request->getPost(), $auth); if ($this->view->form->isValid()) { $this->sess->member = new \ArrayObject(['id' => $auth->adapter()->getUser()->id, 'role_id' => $auth->adapter()->getUser()->role_id, 'role' => Table\Roles::findById($auth->adapter()->getUser()->role_id)->name, 'username' => $auth->adapter()->getUser()->username, 'email' => $auth->adapter()->getUser()->email], \ArrayObject::ARRAY_AS_PROPS); if (!empty($this->memberRedirect)) { $path = BASE_PATH . $this->memberRedirect; } else { if (php_sapi_name() != 'cli') { $path = BASE_PATH . $this->memberUri; if ($path == '') { $path = '/'; } } } $this->redirect($path); } } $this->send(); }
/** * Update an existing user * * @param array $fields * @param \Pop\Web\Session $sess * @return void */ public function update(array $fields, \Pop\Web\Session $sess = null) { $user = Table\Users::findById((int) $fields['id']); if (isset($user->id)) { $oldRoleId = $user->role_id; $oldActive = $user->active; $username = $user->username; $oldRole = Table\Roles::findById($oldRoleId); $role = Table\Roles::findById($fields['role_id']); // Going from username to email if (!$oldRole->email_as_username && $role->email_as_username) { if ((new \Pop\Validator\Email())->evaluate($fields['username'])) { $username = $fields['username']; $fields['email'] = $username; } else { $username = isset($fields['email']) ? $fields['email'] : $user->email; } // Going from email to username } else { if ($oldRole->email_as_username && !$role->email_as_username) { if (!(new \Pop\Validator\Email())->evaluate($fields['username'])) { $username = $fields['email']; unset($fields['email']); } // Staying the same } else { if ($oldRole->email_as_username == $role->email_as_username) { if ($role->email_as_username && isset($fields['email']) && !empty($fields['email'])) { $username = $fields['email']; } else { if (isset($fields['username']) && !empty($fields['username'])) { $username = $fields['username']; } } } } } $user->role_id = $fields['role_id']; $user->username = $username; $user->password = !empty($fields['password1']) ? (new Bcrypt())->create($fields['password1']) : $user->password; $user->first_name = isset($fields['first_name']) ? $fields['first_name'] : $user->first_name; $user->last_name = isset($fields['last_name']) ? $fields['last_name'] : $user->last_name; $user->company = isset($fields['company']) ? $fields['company'] : $user->company; $user->title = isset($fields['title']) ? $fields['title'] : $user->title; $user->email = isset($fields['email']) ? $fields['email'] : $user->email; $user->phone = isset($fields['phone']) ? $fields['phone'] : $user->phone; $user->active = isset($fields['active']) ? (int) $fields['active'] : $user->active; $user->verified = isset($fields['verified']) ? (int) $fields['verified'] : $user->verified; $user->save(); if (null !== $sess && $sess->user->id == $user->id) { $sess->user->username = $user->username; $sess->user->email = $user->email; } $this->data = array_merge($this->data, $user->getColumns()); if ((null === $oldRoleId && null !== $user->role_id || !$oldActive && $user->active) && !empty($user->email)) { $this->sendApproval($user); } } }
/** * Determine if user role has permission to send a password reminder * and reset the password * * @param int $id * @return boolean */ public function canSendReminder($id) { $result = true; $role = Table\Roles::findById((int) $id); if (isset($role->id) && null !== $role->permissions) { $permissions = unserialize($role->permissions); if (isset($permissions['deny'])) { foreach ($permissions['deny'] as $deny) { if ($deny['resource'] == 'forgot') { $result = false; } } } } else { if (!isset($role->id)) { $result = false; } } return $result; }
/** * Initialize the ACL service * * @return Module */ public function initAcl() { $roles = Table\Roles::findAll()->rows(); $resources = $this->application->config()['resources']; foreach ($roles as $role) { $roleName = str_replace(' ', '-', strtolower($role->name)); $resources['role-' . $role->id . '|role-' . $roleName] = ['edit', 'remove']; $resources['users-of-role-' . $role->id . '|users-of-role-' . $roleName] = ['index', 'add', 'edit', 'remove']; } $this->application->mergeConfig(['resources' => $resources]); foreach ($this->application->config()['resources'] as $resource => $permissions) { if (strpos($resource, '|') !== false) { $resource = substr($resource, 0, strpos($resource, '|')); } $this->application->getService('acl')->addResource(new Resource($resource)); } $allRoles = []; foreach ($roles as $role) { $r = new Role($role->name); $allRoles[$role->id] = $r; $this->application->getService('acl')->addRole($r); if (null !== $role->permissions) { $role->permissions = unserialize($role->permissions); } if (null === $role->permissions || is_array($role->permissions) && count($role->permissions) == 0) { $this->application->getService('acl')->allow($role->name); } else { if (count($role->permissions['allow']) > 0) { foreach ($role->permissions['allow'] as $allow) { $this->application->getService('acl')->allow($role->name, $allow['resource'], $allow['permission']); } } else { $this->application->getService('acl')->allow($role->name); } if (count($role->permissions['deny']) > 0) { foreach ($role->permissions['deny'] as $deny) { $this->application->getService('acl')->deny($role->name, $deny['resource'], $deny['permission']); } } } } // Set up parent/child roles foreach ($allRoles as $id => $child) { $r = \Phire\Table\Roles::findById($id); if (isset($r->id) && null !== $r->parent_id && isset($allRoles[$r->parent_id])) { $child->setParent($allRoles[$r->parent_id]); } } // Set the acl in the main nav object $this->application->getService('nav.phire')->setAcl($this->application->getService('acl')); return $this; }
/** * Edit action method * * @param int $id * @return void */ public function edit($id) { $member = new Model\MembersAdmin(); $member->getById($id); if (!isset($member->id)) { $this->redirect(BASE_PATH . APP_URI . '/members'); } $this->prepareView('members/admin/edit.phtml'); $this->view->title = 'Members Admin'; $this->view->member_name = $member->name; $fields = $this->application->config()['forms']['Phire\\Members\\Form\\MembersAdmin']; $role = \Phire\Table\Roles::findById($member->role_id); if (isset($role->id)) { $fields[0]['role_id']['value'][$role->id] = $role->name; } $fields[1]['name']['attributes']['onkeyup'] = 'phire.changeTitle(this.value);'; $this->view->form = new Form\MembersAdmin($fields); $this->view->form->addFilter('htmlentities', [ENT_QUOTES, 'UTF-8'])->setFieldValues($member->toArray()); if ($this->request->isPost()) { $this->view->form->setFieldValues($this->request->getPost()); if ($this->view->form->isValid()) { $this->view->form->clearFilters()->addFilter('html_entity_decode', [ENT_QUOTES, 'UTF-8'])->filter(); $member = new Model\MembersAdmin(); $member->update($this->view->form->getFields(), $this->application->module('phire-members')->config()['history']); $this->view->id = $member->id; $this->sess->setRequestValue('saved', true); $this->redirect(BASE_PATH . APP_URI . '/members/edit/' . $member->id); } } $this->send(); }