/** * Constructor method to instantiate the form object * * @param string $action * @param string $method * @param int $tid * @param boolean $profile * @param int $uid * @param \Phire\Auth\Acl $acl * @param boolean $register * @return self */ public function __construct($action = null, $method = 'post', $tid = 0, $profile = false, $uid = 0, $acl = null, $register = false) { parent::__construct($action, $method, null, ' '); // Create user type fields/form first if ($tid == 0) { $typesAry = array(); $types = Table\UserTypes::findAll('id ASC'); foreach ($types->rows as $type) { if ($acl->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'add_' . $type->id)) { $typesAry[$type->id] = $type->type; } } $this->initFieldsValues = array('type_id' => array('type' => 'select', 'required' => true, 'label' => $this->i18n->__('Select User Type'), 'value' => $typesAry, 'attributes' => array('style' => 'margin: 0 10px 0 0; padding: 6px 5px 7px 5px; height: 32px;')), 'submit' => array('type' => 'submit', 'value' => $this->i18n->__('SELECT'), 'attributes' => array('class' => 'save-btn', 'style' => 'margin: 0; padding: 5px 6px 6px 6px; width: 100px; height: 32px;'))); $id = 'user-select-form'; // Else, create initial user fields } else { $this->initFieldsValues = $this->getInitFields($tid, $profile, $uid, $action, $register); if (strpos($action, '/install/user') !== false) { $id = 'user-install-form'; } else { if ($profile) { $id = 'user-install-form'; } else { $id = 'user-form'; } } } $this->setAttributes('id', $id); }
/** * Get all roles method * * @param string $sort * @param string $page * @return void */ public function getAll($sort = null, $page = null) { $order = $this->getSortOrder($sort, $page); $order['field'] = $order['field'] == 'id' ? DB_PREFIX . 'user_sessions.id' : $order['field']; // Create SQL object to get session data $sql = Table\UserSessions::getSql(); $sql->select(array(0 => DB_PREFIX . 'user_sessions.id', 1 => DB_PREFIX . 'user_types.type', 2 => DB_PREFIX . 'users.username', 3 => DB_PREFIX . 'user_sessions.ip', 4 => DB_PREFIX . 'user_sessions.user_id', 5 => DB_PREFIX . 'user_sessions.ua', 6 => DB_PREFIX . 'user_sessions.start', 7 => DB_PREFIX . 'users.type_id'))->join(DB_PREFIX . 'users', array('user_id', 'id'), 'LEFT JOIN')->join(DB_PREFIX . 'user_types', array(DB_PREFIX . 'users.type_id', 'id'), 'LEFT JOIN')->orderBy($order['field'], $order['order']); if (null !== $order['limit']) { $sql->select()->limit($order['limit'])->offset($order['offset']); } $searchByMarked = null; $searchByAry = array(); $types = Table\UserTypes::findAll(); foreach ($types->rows as $type) { $searchByAry[$type->id] = $type->type; } if (isset($_GET['search_by'])) { $count = Table\UserSessions::getCountOfType((int) $_GET['search_by']); $searchByMarked = (int) $_GET['search_by']; $sql->select()->where()->equalTo('type_id', (int) $_GET['search_by']); } else { $count = Table\UserSessions::getCount(); } // Execute SQL query $sessions = Table\UserSessions::execute($sql->render(true)); if ($this->data['acl']->isAuth('Phire\\Controller\\Phire\\User\\SessionsController', 'remove')) { $removeCheckbox = '<input type="checkbox" name="remove_sessions[]" id="remove_sessions[{i}]" value="[{id}]" />'; $removeCheckAll = '<input type="checkbox" id="checkall" name="checkall" value="remove_sessions" />'; $submit = array('class' => 'remove-btn', 'value' => $this->i18n->__('Remove')); } else { $removeCheckbox = ' '; $removeCheckAll = ' '; $submit = array('class' => 'remove-btn', 'value' => $this->i18n->__('Remove'), 'style' => 'display: none;'); } if ($this->data['acl']->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'edit')) { $username = '******' . BASE_PATH . APP_URI . '/users/edit/[{user_id}]">[{username}]</a>'; } else { $username = '******'; } $options = array('form' => array('id' => 'session-remove-form', 'action' => BASE_PATH . APP_URI . '/users/sessions/remove', 'method' => 'post', 'process' => $removeCheckbox, 'submit' => $submit), 'table' => array('headers' => array('id' => '<a href="' . BASE_PATH . APP_URI . '/users/sessions?sort=id">#</a>', 'type' => '<a href="' . BASE_PATH . APP_URI . '/users/sessions?sort=type">' . $this->i18n->__('Type') . '</a>', 'username' => '<a href="' . BASE_PATH . APP_URI . '/users/sessions?sort=type">' . $this->i18n->__('Username') . '</a>', 'ip' => $this->i18n->__('IP'), 'ua' => $this->i18n->__('User Agent'), 'started' => '<a href="' . BASE_PATH . APP_URI . '/users/sessions?sort=start">' . $this->i18n->__('Started') . '</a>', 'process' => $removeCheckAll), 'class' => 'data-table', 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0), 'separator' => '', 'date' => $this->config->datetime_format, 'exclude' => array('type_id', 'user_id', 'start', 'process' => array('id' => $this->data['user']->sess_id)), 'username' => $username, 'indent' => ' '); $sessAry = array(); foreach ($sessions->rows as $session) { $session->started = date($this->config->datetime_format, strtotime($session->start)) . ' (' . \Pop\Feed\Format\AbstractFormat::calculateTime($session->start) . ')'; $sessAry[] = $session; } if (isset($sessAry[0])) { $this->data['table'] = Html::encode($sessAry, $options, $this->config->pagination_limit, $this->config->pagination_range, $count); $this->data['searchBy'] = new \Pop\Form\Element\Select('search_by', $searchByAry, $searchByMarked); } }
/** * Prepare view method * * @param string $template * @param array $data * @return void */ public function prepareView($template = null, array $data = array()) { $site = \Phire\Table\Sites::getSite(); if (null !== $template) { $template = $this->getCustomView($template, $site); } $sess = \Pop\Web\Session::getInstance(); $config = \Phire\Table\Config::getSystemConfig(); $i18n = \Phire\Table\Config::getI18n(); $this->live = (bool) $config->live; $jsVars = null; $this->view = View::factory($template, $data); $this->view->set('base_path', $site->base_path)->set('content_path', CONTENT_PATH); // Check for an override Phire theme for the header/footer if (file_exists($site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/header.phtml') && file_exists($site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/footer.phtml')) { $this->view->set('phireHeader', $site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/header.phtml')->set('phireFooter', $site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/footer.phtml'); // Else, just use the default header/footer } else { $this->view->set('phireHeader', $site->document_root . $site->base_path . APP_PATH . '/vendor/Phire/view/phire/header.phtml')->set('phireFooter', $site->document_root . $site->base_path . APP_PATH . '/vendor/Phire/view/phire/footer.phtml'); } if (isset($this->view->assets)) { $jsVars = '?lang=' . $config->default_language; } if (isset($sess->user)) { if (isset($this->sess->user->last_action)) { $this->sess->user->last_action = date('Y-m-d H:i:s'); } // Set the timeout warning, giving a 30 second buffer to act if (isset($this->view->assets)) { if (isset($this->view->acl) && $this->view->acl->getType()->session_expiration > 0 && $this->view->acl->getType()->timeout_warning) { $exp = $this->view->acl->getType()->session_expiration * 60 - 30; $uri = $site->base_path . (strtolower($this->view->acl->getType()->type) != 'user' ? '/' . strtolower($this->view->acl->getType()->type) : APP_URI); $jsVars .= '&_exp=' . $exp . '&_base=' . urlencode($uri); } } $this->view->set('user', $sess->user)->set('role', \Phire\Table\UserRoles::getRole($sess->user->role_id))->set('globalAccess', $sess->user->global_access); if (isset($this->view->phireNav) && isset($this->view->acl) && $this->view->acl->hasRole($this->view->role->getName())) { $this->view->phireNav->setConfig(array('top' => array('node' => 'ul', 'id' => 'phire-nav'))); $this->view->phireNav->setAcl($this->view->acl); $this->view->phireNav->setRole($this->view->role); $tree = $this->view->phireNav->getTree(); // If the sub-children haven't been added yet if (isset($tree[0])) { // And any user types to the main phire nav $userTypes = \Phire\Table\UserTypes::findAll('id ASC'); if (isset($userTypes->rows)) { foreach ($userTypes->rows as $type) { $perm = 'index_' . $type->id; if ($this->view->acl->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'index') && $this->view->acl->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'index_' . $type->id)) { $perm = 'index'; } $this->view->phireNav->addLeaf('Users', array('name' => ucwords(str_replace('-', ' ', $type->type)), 'href' => 'index/' . $type->id, 'acl' => array('resource' => 'Phire\\Controller\\Phire\\User\\IndexController', 'permission' => $perm)), 1); } } // Set the language $tree = $this->view->phireNav->getTree(); foreach ($tree as $key => $value) { if (isset($value['name'])) { $tree[$key]['name'] = $i18n->__($value['name']); if (isset($value['children']) && count($value['children']) > 0) { foreach ($value['children'] as $k => $v) { if ($v['name'] == 'Fields' && isset($tree[$key]['children'][$k]['children'][0]['name'])) { $tree[$key]['children'][$k]['children'][0]['name'] = $i18n->__($tree[$key]['children'][$k]['children'][0]['name']); } $tree[$key]['children'][$k]['name'] = $i18n->__($v['name']); } } } } $this->view->phireNav->setTree($tree); } $this->view->phireNav->rebuild(); $this->view->phireNav->nav()->setIndent(' '); } } if (isset($this->view->assets)) { $this->view->assets = str_replace('jax.3.2.0.min.js', 'jax.3.2.0.min.js' . $jsVars, $this->view->assets); } if (isset($sess->errors)) { $this->view->set('errors', $sess->errors); } // Set config object and system/site default data $this->view->set('i18n', $i18n)->set('system_title', $config->system_title)->set('system_email', $config->system_email)->set('site_title', $config->site_title)->set('base_path', $config->base_path)->set('separator', $config->separator)->set('default_language', $config->default_language)->set('datetime_format', $config->datetime_format); }
/** * Get all user types method * * @return array */ public function getUserTypes() { $types = Table\UserTypes::findAll('id ASC'); $typeRows = array(); foreach ($types->rows as $type) { $type->type = ucwords(str_replace('-', ' ', $type->type)); $typeRows[] = $type; } return $typeRows; }
/** * Get the init field values * * @param int $rid * @param \Pop\Config $config * @return array */ protected function getInitFields($rid = 0, $config = null) { // Get types for the user role $typesAry = array(); $types = UserTypes::findAll('id ASC'); foreach ($types->rows as $type) { $typesAry[$type->id] = $type->type; } // Create initial fields $fields1 = array('name' => array('type' => 'text', 'label' => $this->i18n->__('Name'), 'required' => true, 'attributes' => array('size' => 75, 'style' => 'width: 600px;'))); if ($rid != 0) { $fields1['name']['attributes']['onkeyup'] = "phire.updateTitle('#user-role-title', this);"; } // Get any existing field values $fields2 = array(); $fieldGroups = array(); $model = str_replace('Form', 'Model', get_class($this)); $newFields = \Phire\Model\Field::getByModel($model, 0, $rid); if ($newFields['hasFile']) { $this->hasFile = true; } foreach ($newFields as $key => $value) { if (is_numeric($key)) { $fieldGroups[] = $value; } } // Get available resources with their corresponding permissions $resources = \Phire\Model\UserRole::getResources($config); $classes = array('0' => '(' . $this->i18n->__('All') . ')'); $classTypes = array(); $classActions = array(); foreach ($resources as $key => $resource) { $classes[$key] = $resource['name']; $classTypes[$key] = array('0' => '(' . $this->i18n->__('All') . ')'); $classActions[$key] = array('0' => '(' . $this->i18n->__('All') . ')'); foreach ($resource['types'] as $id => $type) { if ((int) $id != 0) { $classTypes[$key][$id] = $type; } } foreach ($resource['actions'] as $permAction) { $classActions[$key][$permAction] = $permAction; } } asort($classes); // Get any current resource/permission fields if ($rid != 0) { $role = UserRoles::findById($rid); $permissions = null !== $role->permissions ? unserialize($role->permissions) : array(); $i = 1; foreach ($permissions as $permission) { if (strpos($permission['permission'], '_') !== false) { $permAry = explode('_', $permission['permission']); $p = $permAry[0]; $t = $permAry[1]; } else { $p = $permission['permission']; $t = '0'; } $fields2['resource_cur_' . $i] = array('type' => 'select', 'label' => " ", 'value' => $classes, 'marked' => $permission['resource'], 'attributes' => array('onchange' => 'phire.changePermissions(this);', 'style' => 'display: block;')); $fields2['permission_cur_' . $i] = array('type' => 'select', 'value' => $classActions[$permission['resource']], 'marked' => $p, 'attributes' => array('style' => 'display: block; width: 150px;')); $fields2['type_cur_' . $i] = array('type' => 'select', 'value' => $classTypes[$permission['resource']], 'marked' => $t, 'attributes' => array('style' => 'display: block; width: 150px;')); $fields2['allow_cur_' . $i] = array('type' => 'select', 'value' => array('1' => $this->i18n->__('allow'), '0' => $this->i18n->__('deny')), 'marked' => $permission['allow'], 'attributes' => array('style' => 'display: block; width: 150px;')); $fields2['rm_resource_' . $i] = array('type' => 'checkbox', 'value' => array($rid . '_' . $permission['resource'] . '_' . $permission['permission'] => $this->i18n->__('Remove') . '?')); $i++; } } // Create new resource/permission fields $fields3 = array('resource_new_1' => array('type' => 'select', 'label' => '<span class="label-pad-2"><a href="#" onclick="phire.addResource(); return false;">[+]</a> ' . $this->i18n->__('Resource') . '</span><span class="label-pad-2">' . $this->i18n->__('Action') . '</span><span class="label-pad-2">' . $this->i18n->__('Type') . '</span><span class="label-pad-2">' . $this->i18n->__('Permission') . '</span>', 'attributes' => array('onchange' => 'phire.changePermissions(this);', 'style' => 'display: block; margin: 3px 0 3px 0;'), 'value' => $classes), 'permission_new_1' => array('type' => 'select', 'attributes' => array('style' => 'display: block; width: 150px; margin: 3px 0 3px 0;'), 'value' => array('0' => '(' . $this->i18n->__('All') . ')')), 'type_new_1' => array('type' => 'select', 'attributes' => array('style' => 'display: block; width: 150px; margin: 3px 0 3px 0;'), 'value' => array('0' => '(' . $this->i18n->__('All') . ')')), 'allow_new_1' => array('type' => 'select', 'attributes' => array('style' => 'display: block; width: 150px; margin: 3px 0 3px 0;'), 'value' => array('1' => $this->i18n->__('allow'), '0' => $this->i18n->__('deny')))); $fields4 = array('submit' => array('type' => 'submit', 'value' => $this->i18n->__('SAVE'), 'attributes' => array('class' => 'save-btn')), 'update' => array('type' => 'button', 'value' => $this->i18n->__('UPDATE'), 'attributes' => array('onclick' => "return phire.updateForm('#user-role-form', true);", 'class' => 'update-btn')), 'type_id' => array('type' => 'select', 'required' => true, 'label' => $this->i18n->__('User Type'), 'value' => $typesAry, 'attributes' => array('style' => 'width: 200px;')), 'id' => array('type' => 'hidden', 'value' => 0), 'update_value' => array('type' => 'hidden', 'value' => 0)); $allFields = array($fields4, $fields1); if (count($fieldGroups) > 0) { foreach ($fieldGroups as $fg) { $allFields[] = $fg; } } $allFields[] = $fields3; $allFields[] = $fields2; return $allFields; }
/** * Load other user types' routes * * @return void */ protected function loadUserRoutes() { // Get any other user types and declare their URI / Controller mapping $types = \Phire\Table\UserTypes::findAll(); foreach ($types->rows as $type) { if (strtolower($type->type) != 'user') { // If the user type has a defined controller if ($type->controller != '') { // If the user type has defined sub-controllers if ($type->sub_controllers != '') { $controller = array('/' => $type->controller); $namespace = substr($type->controller, 0, strrpos($type->controller, '\\') + 1); $subs = explode(',', $type->sub_controllers); foreach ($subs as $sub) { $sub = trim($sub); $controller['/' . $sub] = $namespace . ucfirst($sub) . 'Controller'; } } else { $controller = $type->controller; } // Else, just map to the base Phire controller } else { $controller = 'Phire\\Controller\\Phire\\IndexController'; } $this->router->addControllers(array('/' . $type->type => $controller)); } } }