示例#1
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' => " ", '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;
 }
示例#2
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();
         }
     }
 }
示例#3
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);
         }
     }
 }