Esempio n. 1
0
 /**
  * Get the init field values
  *
  * @param  int     $tid
  * @return array
  */
 protected function getInitFields($tid = 0)
 {
     $yesNo = array('1' => $this->i18n->__('Yes'), '0' => $this->i18n->__('No'));
     // Get roles for the user type
     $roles = Table\UserRoles::findAll('id ASC', array('type_id' => $tid));
     $rolesAry = array('0' => '(' . $this->i18n->__('Blocked') . ')');
     foreach ($roles->rows as $role) {
         $rolesAry[$role->id] = $role->name;
     }
     // Set up initial fields
     $fields1 = array('type' => array('type' => 'text', 'label' => $this->i18n->__('Type'), 'required' => true, 'attributes' => array('size' => 40)), 'ip_allowed' => array('type' => 'text', 'label' => $this->i18n->__('IPs Allowed'), 'attributes' => array('size' => 40)), 'ip_blocked' => array('type' => 'text', 'label' => $this->i18n->__('IPs Blocked'), 'attributes' => array('size' => 40)), 'log_emails' => array('type' => 'text', 'label' => $this->i18n->__('Log Emails'), 'attributes' => array('size' => 40)), 'log_exclude' => array('type' => 'text', 'label' => $this->i18n->__('Log Exclude'), 'attributes' => array('size' => 40)), 'controller' => array('type' => 'text', 'label' => $this->i18n->__('Controller'), 'attributes' => array('size' => 40)), 'sub_controllers' => array('type' => 'text', 'label' => $this->i18n->__('Sub Controllers'), 'attributes' => array('size' => 40)));
     if ($tid != 0) {
         $fields1['type']['attributes']['onkeyup'] = "phire.updateTitle('#user-type-title', this);";
     }
     $fields2a = array('log_in' => array('type' => 'radio', 'label' => $this->i18n->__('Allow Login'), 'value' => $yesNo, 'marked' => '1'), 'registration' => array('type' => 'radio', 'label' => $this->i18n->__('Allow Registration'), 'value' => $yesNo, 'marked' => '1'), 'registration_notification' => array('type' => 'radio', 'label' => $this->i18n->__('Registration Notification'), 'value' => $yesNo, 'marked' => '0'), 'use_captcha' => array('type' => 'radio', 'label' => $this->i18n->__('Use CAPTCHA'), 'value' => $yesNo, 'marked' => '1'), 'use_csrf' => array('type' => 'radio', 'label' => $this->i18n->__('Use CSRF'), 'value' => $yesNo, 'marked' => '1'), 'multiple_sessions' => array('type' => 'radio', 'label' => $this->i18n->__('Allow Multiple Sessions'), 'value' => $yesNo, 'marked' => '1'), 'timeout_warning' => array('type' => 'radio', 'label' => $this->i18n->__('Session Timeout Warning'), 'value' => $yesNo, 'marked' => '0'), 'mobile_access' => array('type' => 'radio', 'label' => $this->i18n->__('Allow Mobile Access'), 'value' => $yesNo, 'marked' => '1'));
     $fields2b = array('email_as_username' => array('type' => 'radio', 'label' => $this->i18n->__('Allow Email as Username'), 'value' => $yesNo, 'marked' => '0'), 'email_verification' => array('type' => 'radio', 'label' => $this->i18n->__('User Email Verification'), 'value' => $yesNo, 'marked' => '0'), 'global_access' => array('type' => 'radio', 'label' => $this->i18n->__('Allow Global Access'), 'value' => $yesNo, 'marked' => '0'), 'force_ssl' => array('type' => 'radio', 'label' => $this->i18n->__('Force SSL'), 'value' => $yesNo, 'marked' => '0'), 'track_sessions' => array('type' => 'radio', 'label' => $this->i18n->__('Track Sessions'), 'value' => $yesNo, 'marked' => '1'), 'verification' => array('type' => 'radio', 'label' => $this->i18n->__('System Email Verification'), 'value' => $yesNo, 'marked' => '1'), 'approval' => array('type' => 'radio', 'label' => $this->i18n->__('Require Approval'), 'value' => $yesNo, 'marked' => '1'), 'unsubscribe_login' => array('type' => 'radio', 'label' => $this->i18n->__('Require Login for Unsubscribe'), 'value' => $yesNo, 'marked' => '1'));
     $fieldGroups = array();
     $dynamicFields = false;
     $model = str_replace('Form', 'Model', get_class($this));
     $newFields = \Phire\Model\Field::getByModel($model, 0, $tid);
     if ($newFields['dynamic']) {
         $dynamicFields = true;
     }
     if ($newFields['hasFile']) {
         $this->hasFile = true;
     }
     foreach ($newFields as $key => $value) {
         if (is_numeric($key)) {
             $fieldGroups[] = $value;
         }
     }
     $fields4 = array();
     $fields4['submit'] = array('type' => 'submit', 'value' => $this->i18n->__('SAVE'), 'attributes' => array('class' => 'save-btn'));
     $fields4['update'] = array('type' => 'button', 'value' => $this->i18n->__('UPDATE'), 'attributes' => array('onclick' => "return phire.updateForm('#user-type-form', " . ($this->hasFile || $dynamicFields ? 'true' : 'false') . ");", 'class' => 'update-btn'));
     $fields4['id'] = array('type' => 'hidden', 'value' => 0);
     $fields4['update_value'] = array('type' => 'hidden', 'value' => 0);
     $fields4['default_role_id'] = array('type' => 'select', 'label' => $this->i18n->__('Default Role'), 'value' => $rolesAry, 'attributes' => array('style' => 'width: 200px;'));
     $fields4['password_encryption'] = array('type' => 'select', 'label' => $this->i18n->__('Password Encryption'), 'value' => array('1' => 'MD5', '2' => 'SHA1', '3' => 'Crypt', '4' => 'Bcrypt', '5' => 'Mcrypt (2-Way)', '6' => 'Crypt_MD5', '7' => 'Crypt_SHA256', '8' => 'Crypt_SHA512', '0' => $this->i18n->__('None')), 'marked' => '4', 'attributes' => array('style' => 'width: 200px;'));
     $fields4['reset_password'] = array('type' => 'select', 'label' => $this->i18n->__('Password Reset'), 'value' => $yesNo, 'marked' => '0');
     $fields4['reset_password_interval'] = array('type' => 'select', 'label' => $this->i18n->__('Password Reset Interval'), 'value' => array('--' => '--', '1st' => '1st Login', 'Every' => 'Every'), 'marked' => '0');
     $fields4['reset_password_interval_value'] = array('type' => 'text', 'attributes' => array('size' => 2, 'style' => 'margin: 0; padding: 3px 5px 3px 5px; height: 16px; font-size: 0.9em;'));
     $fields4['reset_password_interval_unit'] = array('type' => 'select', 'value' => array('--' => '--', 'Days' => 'Day(s)', 'Months' => 'Month(s)', 'Years' => 'Year(s)'));
     $fields4['allowed_attempts'] = array('type' => 'text', 'label' => $this->i18n->__('Allowed Attempts'), 'attributes' => array('size' => 3), 'value' => '0');
     $fields4['session_expiration'] = array('type' => 'text', 'label' => $this->i18n->__('Session Expiration') . ' <span style="font-size: 0.9em; font-weight: normal;">(' . $this->i18n->__('Minutes') . ')</span>', 'attributes' => array('size' => 3), 'value' => '0');
     $allFields = array($fields4, $fields1, $fields2a, $fields2b);
     if (count($fieldGroups) > 0) {
         foreach ($fieldGroups as $fg) {
             $allFields[] = $fg;
         }
     }
     return $allFields;
 }
 /**
  * Instantiate the model object.
  *
  * @param  array $data
  * @return self
  */
 public function __construct(array $data = null)
 {
     if (null !== $data) {
         $this->data = $data;
     }
     $this->config = \Phire\Table\Config::getSystemConfig();
     if (!headers_sent()) {
         $sess = \Pop\Web\Session::getInstance();
     }
     $this->i18n = Table\Config::getI18n();
     if (isset($sess->user)) {
         $this->data['user'] = $sess->user;
         $this->data['role'] = \Phire\Table\UserRoles::getRole($sess->user->role_id);
         $this->data['globalAccess'] = $sess->user->global_access;
     }
 }
 /**
  * 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);
 }
Esempio n. 4
0
 /**
  * Login method
  *
  * @param string                 $username
  * @param \Phire\Table\UserTypes $type
  * @param boolean                $success
  * @return void
  */
 public function login($username, $type, $success = true)
 {
     $user = Table\Users::findBy(array('username' => $username));
     $sess = Session::getInstance();
     $typeUri = strtolower($type->type) != 'user' ? '/' . strtolower($type->type) : APP_URI;
     // If login success
     if ($success && isset($user->id)) {
         // Create and save new session database entry
         if ($type->track_sessions) {
             Table\UserSessions::clearSessions($user->id);
             $session = new Table\UserSessions(array('user_id' => $user->id, 'ip' => $_SERVER['REMOTE_ADDR'], 'ua' => $_SERVER['HTTP_USER_AGENT'], 'start' => date('Y-m-d H:i:s')));
             $session->save();
             $sessionId = $session->id;
             $otherSession = Table\UserSessions::findBy(array('user_id' => $user->id));
             if (isset($otherSession->rows[0])) {
                 foreach ($otherSession->rows as $other) {
                     if ($other->id != $sessionId) {
                         $sess->sessionError = $this->i18n->__('Another user is currently logged in as %1 from %2.', array('<strong>' . $username . '</strong>', $other->ip));
                     }
                 }
             }
         } else {
             $sessionId = null;
         }
         $type = Table\UserTypes::findById($user->type_id);
         $role = Table\UserRoles::findById($user->role_id);
         // Get user login data
         $lastLogin = null;
         $lastUa = null;
         $lastIp = null;
         $lastLoginString = '(N/A)';
         $timestamp = time();
         $ua = $_SERVER['HTTP_USER_AGENT'];
         $ip = $_SERVER['REMOTE_ADDR'];
         if ($type->reset_password) {
             if ($type->reset_password_interval == '1st') {
                 if ($user->logins == '') {
                     $sess->reset_pwd = true;
                 }
             } else {
                 $interval = 86400;
                 $resetAry = explode(' ', $type->reset_password_interval);
                 if ($resetAry[1] == 'Months') {
                     $interval = 2628000;
                 } else {
                     if ($resetAry[1] == 'Years') {
                         $interval = 31536000;
                     }
                 }
                 $interval = $resetAry[0] * $interval;
                 if ($user->logins != '') {
                     $lastL = key(unserialize($user->logins));
                     if (time() - $lastL > $interval) {
                         $sess->reset_pwd = true;
                     }
                 }
             }
         }
         if ($user->logins == '') {
             $logins = array($timestamp => array('ua' => $ua, 'ip' => $ip));
         } else {
             $logins = unserialize($user->logins);
             $last = end($logins);
             $lastLogin = date('Y-m-d H:i:s', key($logins));
             $lastIp = $last['ip'];
             $lastUa = $last['ua'];
             $logins[$timestamp] = array('ua' => $ua, 'ip' => $ip);
             $lastLoginString = date('D M j, Y g:i A', strtotime($lastLogin)) . ' (' . ('' !== $lastIp ? $lastIp : 'N/A') . ')';
         }
         // Create new session object
         $sess->user = new \ArrayObject(array('id' => $user->id, 'site_ids' => unserialize($user->site_ids), 'type_id' => $user->type_id, 'type' => $type->type, 'typeUri' => $typeUri, 'global_access' => $type->global_access, 'role_id' => isset($role->id) ? $role->id : 0, 'role' => isset($role->id) ? $role->name : null, 'username' => $username, 'email' => $user->email, 'last_login' => $lastLogin, 'last_ua' => $lastUa, 'last_ip' => $lastIp, 'sess_id' => $sessionId, 'last' => $lastLoginString, 'last_action' => date('Y-m-d H:i:s')), \ArrayObject::ARRAY_AS_PROPS);
         // Store timestamp and login data
         $user->logins = serialize($logins);
         $user->failed_attempts = 0;
         $user->save();
         // If set, log the login
         if ($type->log_emails != '') {
             $this->log($type, $user);
         }
         // Else, log failed attempt
     } else {
         if (isset($user->id)) {
             $user->failed_attempts++;
             $user->save();
         }
     }
 }
Esempio n. 5
0
 /**
  * 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' => "&nbsp;", '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;
 }
Esempio n. 6
0
 /**
  * Is auth method
  *
  * @param  string $resource
  * @param  string $permission
  * @return boolean
  */
 public function isAuth($resource = null, $permission = null)
 {
     $auth = false;
     // If tracking sessions is on
     if ($this->type->track_sessions && (isset($this->sess->user->sess_id) && null !== $this->sess->user->sess_id)) {
         $session = Table\UserSessions::findById($this->sess->user->sess_id);
         if (!isset($session->id) || $this->type->session_expiration != 0 && $session->hasExpired($this->type->session_expiration, $this->sess->user->last_action)) {
             $this->sess->lastUrl = strpos($_SERVER['REQUEST_URI'], '/users/sessions/json') === false ? $_SERVER['REQUEST_URI'] : BASE_PATH . APP_URI . '/';
             $this->sess->expired = true;
             $this->logout();
         } else {
             if (isset($this->sess->user->id)) {
                 // If the user is not the right type, check for global access
                 if ($this->type->id != $this->sess->user->type_id) {
                     if ($this->sess->user->global_access) {
                         $auth = true;
                     } else {
                         $this->sess->authError = true;
                         $this->logout();
                         $auth = false;
                     }
                     // Else, authorize the user role
                 } else {
                     if ($this->sess->user->role_id != 0) {
                         $role = Table\UserRoles::getRole($this->sess->user->role_id);
                         if (null !== $resource && !$this->hasResource($resource)) {
                             $this->addResource($resource);
                         }
                         $auth = $this->isAllowed($role, $resource, $permission);
                         // Else, validate the session and record the action
                     } else {
                         $auth = true;
                     }
                 }
             }
         }
         // Else, just check for a regular session
     } else {
         if (isset($this->sess->user->id)) {
             // If the user is not the right type, check for global access
             if ($this->type->id != $this->sess->user->type_id) {
                 $auth = $this->sess->user->global_access ? true : false;
                 // Else, authorize the user role
             } else {
                 if ($this->sess->user->role_id != 0) {
                     $role = Table\UserRoles::getRole($this->sess->user->role_id);
                     if (null !== $resource && !$this->hasResource($resource)) {
                         $this->addResource($resource);
                     }
                     $auth = $this->isAllowed($role, $resource, $permission);
                 } else {
                     $auth = true;
                 }
             }
         }
     }
     return $auth;
 }
Esempio n. 7
0
 /**
  * Initialize the ACL object, checking for user types and user roles
  *
  * @return void
  */
 protected function initAcl()
 {
     // Get the user type from either session or the URI
     $sess = \Pop\Web\Session::getInstance();
     $type = str_replace(BASE_PATH, '', $_SERVER['REQUEST_URI']);
     // If the URI matches the system user URI
     if (substr($type, 0, strlen(APP_URI)) == APP_URI) {
         $type = 'user';
         // Else, set user type
     } else {
         $type = substr($type, 1);
         if (strpos($type, '/') !== false) {
             $type = substr($type, 0, strpos($type, '/'));
         }
     }
     // Create the type object and pass it to the Acl object
     if (isset($sess->user->type_id)) {
         $typeObj = \Phire\Table\UserTypes::findById($sess->user->type_id);
     } else {
         $typeObj = \Phire\Table\UserTypes::findBy(array('type' => $type));
     }
     $this->getService('acl')->setType($typeObj);
     // Set the roles for this user type in the Acl object
     $perms = \Phire\Table\UserRoles::getAllRoles($typeObj->id);
     if (count($perms['roles']) > 0) {
         foreach ($perms['roles'] as $role) {
             $this->getService('acl')->addRole($role);
         }
     }
     // Set up the ACL object's resources and permissions
     if (count($perms['resources']) > 0) {
         foreach ($perms['resources'] as $role => $perm) {
             if (count($perm['allow']) > 0) {
                 foreach ($perm['allow'] as $resource => $p) {
                     $this->getService('acl')->addResource($resource);
                     if (count($p) > 0) {
                         $this->getService('acl')->allow($role, $resource, $p);
                     } else {
                         $this->getService('acl')->allow($role, $resource);
                     }
                 }
             } else {
                 $this->getService('acl')->allow($role);
             }
             if (count($perm['deny']) > 0) {
                 foreach ($perm['deny'] as $resource => $p) {
                     $this->getService('acl')->addResource($resource);
                     if (count($p) > 0) {
                         $this->getService('acl')->deny($role, $resource, $p);
                     } else {
                         $this->getService('acl')->deny($role, $resource);
                     }
                 }
             }
         }
     }
 }
Esempio n. 8
0
 /**
  * Remove user role
  *
  * @param  array   $post
  * @return void
  */
 public function remove(array $post)
 {
     if (isset($post['remove_roles'])) {
         foreach ($post['remove_roles'] as $id) {
             $role = Table\UserRoles::findById($id);
             if (isset($role->id)) {
                 $role->delete();
             }
             $sql = Table\UserTypes::getSql();
             if ($sql->getDbType() == \Pop\Db\Sql::SQLITE) {
                 $sql->update(array('default_role_id' => null))->where()->equalTo('default_role_id', $role->id);
                 Table\UserTypes::execute($sql->render(true));
             }
             FieldValue::remove($id);
         }
     }
 }
Esempio n. 9
0
 /**
  * Get the init field values
  *
  * @param  int     $tid
  * @param  boolean $profile
  * @param  int     $uid
  * @param  string  $action
  * @param  boolean $register
  * @return array
  */
 protected function getInitFields($tid = 0, $profile = false, $uid = 0, $action, $register = false)
 {
     $type = Table\UserTypes::findById($tid);
     $fields1 = array();
     // Continue setting up initial user fields
     $fields1['email1'] = array('type' => 'text', 'label' => $this->i18n->__('Email'), 'required' => true, 'attributes' => array('size' => 30), 'validators' => new Validator\Email());
     if ($type->email_verification) {
         $fields1['email2'] = array('type' => 'text', 'label' => $this->i18n->__('Re-Type Email'), 'required' => true, 'attributes' => array('size' => 30), 'validators' => new Validator\Email());
     }
     // If not email as username, create username field
     if (!$type->email_as_username) {
         $fields2 = array('username' => array('type' => 'text', 'label' => $this->i18n->__('Username'), 'required' => true, 'attributes' => array('size' => 30), 'validators' => array(new Validator\AlphaNumeric(), new Validator\LengthGte(4))));
         if ($uid != 0) {
             $fields2['username']['attributes']['onkeyup'] = "phire.updateTitle('#username-title', this);";
         }
     } else {
         $fields2 = array();
         if ($uid != 0) {
             $fields1['email1']['attributes']['onkeyup'] = "phire.updateTitle('#username-title', this);";
         }
     }
     // Continue setting up initial user fields
     if ($type->login) {
         $fields3 = array('password1' => array('type' => 'password', 'label' => $this->i18n->__('Enter Password'), 'required' => true, 'attributes' => array('size' => 30), 'validators' => new Validator\LengthGte(6)), 'password2' => array('type' => 'password', 'label' => $this->i18n->__('Re-Type Password'), 'required' => true, 'attributes' => array('size' => 30), 'validators' => new Validator\LengthGte(6)));
     } else {
         $fields3 = array();
     }
     $fieldGroups = array();
     $dynamicFields = false;
     $model = str_replace('Form', 'Model', get_class($this));
     $newFields = \Phire\Model\Field::getByModel($model, $tid, $uid);
     if ($newFields['dynamic']) {
         $dynamicFields = true;
     }
     if ($newFields['hasFile']) {
         $this->hasFile = true;
     }
     foreach ($newFields as $key => $value) {
         if (is_numeric($key)) {
             $fieldGroups[] = $value;
         }
     }
     $fields4 = array();
     if ($register) {
         $site = Table\Sites::getSite();
         if ($type->use_csrf) {
             $fields4['csrf'] = array('type' => 'csrf', 'value' => \Pop\Filter\String::random(8));
         }
         if ($type->use_captcha) {
             $fields4['captcha'] = array('type' => 'captcha', 'label' => $this->i18n->__('Enter Code'), 'captcha' => '<br /><img id="captcha-image" src="' . $site->base_path . '/captcha" /><br /><a class="reload-link" href="#" onclick="document.getElementById(\'captcha-image\').src = \'' . $site->base_path . '/captcha?reload=1\';return false;">' . $this->i18n->__('Reload') . '</a>', 'attributes' => array('size' => 5));
         }
     }
     // Finish the initial fields
     $fields4['submit'] = array('type' => 'submit', 'value' => strpos($action, '/register') !== false ? $this->i18n->__('REGISTER') : $this->i18n->__('SAVE'), 'attributes' => array('class' => strpos($action, '/install/user') !== false || $profile ? 'update-btn' : 'save-btn'));
     if ($profile) {
         $fields4['submit']['label'] = '&nbsp;';
         $fields4['submit']['attributes']['style'] = 'width: 250px;';
         $fields4['profile'] = array('type' => 'hidden', 'value' => 1);
         $sess = \Pop\Web\Session::getInstance();
         if (isset($sess->reset_pwd)) {
             $fields4['reset_pwd'] = array('type' => 'hidden', 'value' => 1);
         }
     }
     if (!$profile) {
         $fields4['update'] = array('type' => 'button', 'value' => $this->i18n->__('Update'), 'attributes' => array('onclick' => "return phire.updateForm('#user-form', " . ($this->hasFile || $dynamicFields ? 'true' : 'false') . ");", 'class' => 'update-btn'));
     }
     $fields4['type_id'] = array('type' => 'hidden', 'value' => $tid);
     $fields4['id'] = array('type' => 'hidden', 'value' => 0);
     if (!$profile) {
         $fields4['update_value'] = array('type' => 'hidden', 'value' => 0);
     }
     // If not profile
     if (!$profile) {
         // Get roles for user type
         $rolesAry = array('0' => '(' . $this->i18n->__('Blocked') . ')');
         if ($tid != 0) {
             $roles = Table\UserRoles::findBy(array('type_id' => $tid), 'id ASC');
             foreach ($roles->rows as $role) {
                 $rolesAry[$role->id] = $role->name;
             }
         }
         $siteIds = array('0' => $_SERVER['HTTP_HOST']);
         $sites = Table\Sites::findAll();
         foreach ($sites->rows as $site) {
             $siteIds[(string) $site->id] = $site->domain;
         }
         $fields4['role_id'] = array('type' => 'select', 'required' => true, 'label' => $this->i18n->__('User Role'), 'value' => $rolesAry, 'marked' => $type->default_role_id);
         $fields4['verified'] = array('type' => 'select', 'label' => $this->i18n->__('Verified'), 'value' => array('1' => $this->i18n->__('Yes'), '0' => $this->i18n->__('No')), 'marked' => '0');
         $fields4['failed_attempts'] = array('type' => 'text', 'label' => $this->i18n->__('Failed Attempts'), 'attributes' => array('size' => 3));
         $fields4['site_ids'] = array('type' => 'checkbox', 'label' => $this->i18n->__('Allowed Sites'), 'value' => $siteIds);
     }
     if (strpos($action, '/install/user') !== false || $profile) {
         $allFields = array($fields1, $fields2, $fields3);
         if (count($fieldGroups) > 0) {
             foreach ($fieldGroups as $fg) {
                 $allFields[] = $fg;
             }
         }
         $allFields[] = $fields4;
     } else {
         $allFields = array($fields4, $fields1, $fields2, $fields3);
         if (count($fieldGroups) > 0) {
             foreach ($fieldGroups as $fg) {
                 $allFields[] = $fg;
             }
         }
     }
     return $allFields;
 }