Beispiel #1
0
 /**
  * Checks whether or not the current user is a "regular" or "guest" user
  *
  * @return boolean
  */
 public function isGuest()
 {
     return (bool) (!$this->isLoggedIn() || $this->getID() == TBGSettings::getDefaultUserID() && TBGSettings::isDefaultUserGuest());
 }
Beispiel #2
0
 public function runDeleteUser(TBGRequest $request)
 {
     try {
         try {
             $return_options = array();
             $user = TBGContext::factory()->TBGUser($request['user_id']);
             if ($user->getGroup() instanceof TBGGroup) {
                 $return_options['update_groups'] = array('ids' => array(), 'membercounts' => array());
                 $group_id = $user->getGroup()->getID();
                 $return_options['update_groups']['ids'][] = $group_id;
                 $return_options['update_groups']['membercounts'][$group_id] = $user->getGroup()->getNumberOfMembers();
             }
             if (count($user->getTeams())) {
                 $return_options['update_teams'] = array('ids' => array(), 'membercounts' => array());
                 foreach ($user->getTeams() as $team) {
                     $team_id = $team->getID();
                     $return_options['update_teams']['ids'][] = $team_id;
                     $return_options['update_teams']['membercounts'][$team_id] = $team->getNumberOfMembers();
                 }
             }
             if (in_array($user->getID(), array(1, TBGSettings::getDefaultUserID()))) {
                 throw new Exception(TBGContext::getI18n()->__("You cannot delete this system user"));
             }
         } catch (Exception $e) {
         }
         if (!$user instanceof TBGUser) {
             throw new Exception(TBGContext::getI18n()->__("You cannot delete this user"));
         }
         if (TBGContext::getScope()->isDefault()) {
             $user->markAsDeleted();
             $user->save();
             $return_options['message'] = TBGContext::getI18n()->__('The user was deleted');
         } else {
             $user->removeScope(TBGContext::getScope()->getID());
             $return_options['message'] = TBGContext::getI18n()->__('The user has been removed from this scope');
         }
         $return_options['total_count'] = TBGUser::getUsersCount();
         $return_options['more_available'] = TBGContext::getScope()->hasUsersAvailable();
         return $this->renderJSON($return_options);
     } catch (Exception $e) {
         $this->getResponse()->setHttpStatus(400);
         return $this->renderJSON(array('error' => $e->getMessage()));
     }
 }
Beispiel #3
0
 /**
  * Prune users from users table who aren't in LDAP
  *
  * @param TBGRequest $request
  */
 public function runPruneUsers(TBGRequest $request)
 {
     $validgroups = TBGContext::getModule('auth_ldap')->getSetting('groups');
     $base_dn = TBGContext::getModule('auth_ldap')->getSetting('b_dn');
     $dn_attr = TBGContext::getModule('auth_ldap')->getSetting('dn_attr');
     $username_attr = TBGContext::getModule('auth_ldap')->getSetting('u_attr');
     $fullname_attr = TBGContext::getModule('auth_ldap')->getSetting('f_attr');
     $email_attr = TBGContext::getModule('auth_ldap')->getSetting('e_attr');
     $groups_members_attr = TBGContext::getModule('auth_ldap')->getSetting('g_attr');
     $user_class = TBGContext::getModule('auth_ldap')->getSetting('u_type');
     $group_class = TBGContext::getModule('auth_ldap')->getSetting('g_type');
     $users = TBGUser::getAll();
     $deletecount = 0;
     try {
         $connection = TBGContext::getModule('auth_ldap')->connect();
         TBGContext::getModule('auth_ldap')->bind($connection, TBGContext::getModule('auth_ldap')->getSetting('control_user'), TBGContext::getModule('auth_ldap')->getSetting('control_pass'));
         $default = TBGSettings::getDefaultUserID();
         foreach ($users as $user) {
             if ($user->getID() == $default) {
                 continue;
             }
             $username = $user->getUsername();
             $fields = array($fullname_attr, $email_attr, 'cn', $dn_attr);
             $filter = '(&(objectClass=' . TBGLDAPAuthentication::getModule()->escape($user_class) . ')(' . $username_attr . '=' . TBGLDAPAuthentication::getModule()->escape($username) . '))';
             $results = ldap_search($connection, $base_dn, $filter, $fields);
             if (!$results) {
                 TBGLogging::log('failed to search for user: '******'ldap', TBGLogging::LEVEL_FATAL);
                 throw new Exception(TBGContext::geti18n()->__('Search failed: ') . ldap_error($connection));
             }
             $data = ldap_get_entries($connection, $results);
             /*
              * If a user is not found, delete it
              */
             if ($data['count'] != 1) {
                 $user->delete();
                 $deletecount++;
                 continue;
             }
             if ($validgroups != '') {
                 if (strstr($validgroups, ',')) {
                     $groups = explode(',', $validgroups);
                 } else {
                     $groups = array();
                     $groups[] = $validgroups;
                 }
                 $allowed = false;
                 foreach ($groups as $group) {
                     $fields2 = array($groups_members_attr);
                     $filter2 = '(&(objectClass=' . TBGLDAPAuthentication::getModule()->escape($group_class) . ')(cn=' . TBGLDAPAuthentication::getModule()->escape($group) . '))';
                     $results2 = ldap_search($connection, $base_dn, $filter2, $fields2);
                     if (!$results2) {
                         TBGLogging::log('failed to search for user: '******'ldap', TBGLogging::LEVEL_FATAL);
                         throw new Exception(TBGContext::geti18n()->__('Search failed: ') . ldap_error($connection));
                     }
                     $data2 = ldap_get_entries($connection, $results2);
                     if ($data2['count'] != 1) {
                         continue;
                     }
                     foreach ($data2[0][$groups_members_attr] as $member) {
                         $member = preg_replace('/(?<=,) +(?=[a-zA-Z])/', '', $member);
                         $user_dn = preg_replace('/(?<=,) +(?=[a-zA-Z])/', '', $data[0][strtolower($dn_attr)][0]);
                         if (!is_numeric($member) && strtolower($member) == strtolower($user_dn)) {
                             $allowed = true;
                         }
                     }
                 }
                 /*
                  * If a user is not allowed access, delete it
                  */
                 if ($allowed == false) {
                     $user->delete();
                     $deletecount++;
                     continue;
                 }
             }
         }
     } catch (Exception $e) {
         ldap_unbind($connection);
         TBGContext::setMessage('module_error', TBGContext::getI18n()->__('Pruning failed'));
         TBGContext::setMessage('module_error_details', $e->getMessage());
         $this->forward(TBGContext::getRouting()->generate('configure_module', array('config_module' => 'auth_ldap')));
     }
     ldap_unbind($connection);
     TBGContext::setMessage('module_message', TBGContext::getI18n()->__('Pruning successful! %del users deleted', array('%del' => $deletecount)));
     $this->forward(TBGContext::getRouting()->generate('configure_module', array('config_module' => 'auth_ldap')));
 }
 protected static function initializeUser()
 {
     TBGLogging::log('Loading user');
     try {
         TBGLogging::log('is this logout?');
         if (self::getRequest()->getParameter('logout')) {
             TBGLogging::log('yes');
             self::logout();
         } else {
             TBGLogging::log('no');
             TBGLogging::log('sets up user object');
             $event = TBGEvent::createNew('core', 'pre_login');
             $event->trigger();
             if ($event->isProcessed()) {
                 self::loadUser($event->getReturnValue());
             } else {
                 self::loadUser();
             }
             TBGEvent::createNew('core', 'post_login', self::getUser())->trigger();
             TBGLogging::log('loaded');
             self::cacheAllPermissions();
         }
     } catch (Exception $e) {
         TBGLogging::log("Something happened while setting up user: " . $e->getMessage(), 'main', TBGLogging::LEVEL_WARNING);
         if (!self::isCLI() && (self::getRouting()->getCurrentRouteModule() != 'main' || self::getRouting()->getCurrentRouteAction() != 'register1' && self::getRouting()->getCurrentRouteAction() != 'register2' && self::getRouting()->getCurrentRouteAction() != 'activate' && self::getRouting()->getCurrentRouteAction() != 'reset_password' && self::getRouting()->getCurrentRouteAction() != 'captcha' && self::getRouting()->getCurrentRouteAction() != 'login' && self::getRouting()->getCurrentRouteAction() != 'getBackdropPartial' && self::getRouting()->getCurrentRouteAction() != 'serve')) {
             self::$_redirect_login = true;
         } else {
             self::$_user = self::factory()->TBGUser(TBGSettings::getDefaultUserID());
         }
     }
     TBGLogging::log('...done');
 }
 protected function _upgrade()
 {
     switch ($this->_version) {
         case "1.0":
             // Upgrade tables
             \b2db\Core::getTable('TBGVCSIntegrationCommitsTable')->create();
             \b2db\Core::getTable('TBGVCSIntegrationFilesTable')->create();
             \b2db\Core::getTable('TBGVCSIntegrationIssueLinksTable')->create();
             TBGVCSIntegrationCommitsTable::getTable()->createIndexes();
             TBGVCSIntegrationFilesTable::getTable()->createIndexes();
             TBGVCSIntegrationIssueLinksTable::getTable()->createIndexes();
             // Migrate data from old table to new tables
             $crit = new \b2db\Criteria();
             $crit->addOrderBy(TBGVCSIntegrationTable::DATE, \b2db\Criteria::SORT_DESC);
             $results = TBGVCSIntegrationTable::getTable()->doSelect($crit);
             if ($results instanceof \b2db\Resultset && $results->count() > 0) {
                 $commits = array();
                 while ($row = $results->getNextRow()) {
                     $rev = $row->get(TBGVCSIntegrationTable::NEW_REV);
                     if (array_key_exists($rev, $commits)) {
                         // Add a new file or issue to the commit data
                         $commits[$rev]['files'][$row->get(TBGVCSIntegrationTable::FILE_NAME)] = array('file_name' => $row->get(TBGVCSIntegrationTable::FILE_NAME), 'action' => $row->get(TBGVCSIntegrationTable::ACTION));
                         $commits[$rev]['issues'][$row->get(TBGVCSIntegrationTable::ISSUE_NO)] = $row->get(TBGVCSIntegrationTable::ISSUE_NO);
                     } else {
                         // All issues will be of the same project, so use one issue
                         $issue = TBGContext::factory()->TBGIssue($results->get(TBGVCSIntegrationTable::ISSUE_NO));
                         // Add details of a new commit
                         $commits[$rev] = array('commit' => array(), 'files' => array(), 'issues' => array());
                         $commits[$rev]['commit'] = array('new_rev' => $rev, 'old_rev' => $row->get(TBGVCSIntegrationTable::OLD_REV), 'author' => $row->get(TBGVCSIntegrationTable::AUTHOR), 'date' => $row->get(TBGVCSIntegrationTable::DATE), 'log' => $row->get(TBGVCSIntegrationTable::LOG), 'scope' => $row->get(TBGVCSIntegrationTable::SCOPE), 'project' => $issue->getProject());
                         $commits[$rev]['files'][$row->get(TBGVCSIntegrationTable::FILE_NAME)] = array('file_name' => $row->get(TBGVCSIntegrationTable::FILE_NAME), 'action' => $row->get(TBGVCSIntegrationTable::ACTION));
                         $commits[$rev]['issues'][$row->get(TBGVCSIntegrationTable::ISSUE_NO)] = $row->get(TBGVCSIntegrationTable::ISSUE_NO);
                     }
                 }
                 foreach ($commits as $commit) {
                     $files = array();
                     $issues = array();
                     $scope = TBGContext::factory()->TBGScope($commit['commit']['scope']);
                     try {
                         $author = TBGContext::factory()->TBGUser($commit['commit']['author']);
                     } catch (Exception $e) {
                         $author = TBGContext::factory()->TBGUser(TBGSettings::getDefaultUserID());
                     }
                     if (!$author instanceof TBGUser) {
                         $author = TBGContext::factory()->TBGUser(TBGSettings::getDefaultUserID());
                     }
                     // Add the commit
                     $inst = new TBGVCSIntegrationCommit();
                     $inst->setAuthor($author);
                     $inst->setDate($commit['commit']['date']);
                     $inst->setLog($commit['commit']['log']);
                     $inst->setPreviousRevision($commit['commit']['old_rev']);
                     $inst->setRevision($commit['commit']['new_rev']);
                     $inst->setProject($commit['commit']['project']);
                     $inst->setScope($scope);
                     $inst->save();
                     // Process issue list, remove duplicates
                     $issues = $commit['issues'];
                     $files = $commit['files'];
                     $commit = $inst;
                     foreach ($files as $file) {
                         // Add affected files
                         $inst = new TBGVCSIntegrationFile();
                         $inst->setCommit($commit);
                         $inst->setFile($file['file_name']);
                         $inst->setAction($file['action']);
                         $inst->setScope($scope);
                         $inst->save();
                     }
                     foreach ($issues as $issue) {
                         // Add affected issues
                         $issue = TBGContext::factory()->TBGIssue($issue);
                         $inst = new TBGVCSIntegrationIssueLink();
                         $inst->setIssue($issue);
                         $inst->setCommit($commit);
                         $inst->setScope($scope);
                         $inst->save();
                     }
                 }
             }
             // Migrate settings to new format
             $access_method = $this->getSetting('use_web_interface');
             $passkey = $this->getSetting('vcs_passkey');
             foreach (TBGProject::getAll() as $project) {
                 $projectId = $project->getID();
                 $web_path = $this->getSetting('web_path_' . $projectId);
                 $web_repo = $this->getSetting('web_repo_' . $projectId);
                 // Check if enabled
                 if ($web_path == '') {
                     continue;
                 }
                 switch ($this->getSetting('web_type_' . $projectId)) {
                     case 'viewvc':
                         $base_url = $web_path . '/' . '?root=' . $web_repo;
                         $link_rev = '&amp;view=rev&amp;revision=%revno';
                         $link_file = '&amp;view=log';
                         $link_diff = '&amp;r1=%revno&amp;r2=%oldrev';
                         $link_view = '&amp;revision=%revno&amp;view=markup';
                         break;
                     case 'viewvc_repo':
                         $base_url = $web_path;
                         $link_rev = '/?view=rev&amp;revision=%revno';
                         $link_file = '/%file?view=log';
                         $link_diff = '/%file?r1=%revno&amp;r2=%oldrev';
                         $link_view = '/%file?revision=%revno&amp;view=markup';
                         break;
                     case 'websvn':
                         $base_url = $web_path;
                         $link_rev = '/revision.php?repname=' . $web_repo . '&amp;isdir=1&amp;rev=%revno';
                         $link_file = '/log.php?repname=' . $web_repo . '&amp;path=/$%file';
                         $link_diff = '/comp.php?repname=' . $web_repo . '&amp;compare[]=/%file@%revno&amp;compare[]=/%file@%oldrev';
                         $link_view = '/filedetails.php?repname=' . $web_repo . '&path=/%file&amp;rev=%revno';
                         break;
                     case 'websvn_mv':
                         $base_url = $web_path;
                         $link_rev = '/' . '?repname=' . $web_repo . '&amp;op=log&isdir=1&amp;rev=%revno';
                         $link_file = '/%file?repname=' . $web_repo;
                         $link_diff = '/%file?repname=' . $web_repo . '&amp;compare[]=/%file@%revno&amp;compare[]=/%file@%oldrev';
                         $link_view = '/%file?repname=' . $web_repo . '&amp;rev=%revno';
                         break;
                     case 'loggerhead':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/revision/%revno';
                         $link_file = '/changes';
                         $link_diff = '/revision/%revno?compare_revid=%oldrev';
                         $link_view = '/annotate/head:/%file';
                         break;
                     case 'gitweb':
                         $base_url = $web_path . '/' . '?p=' . $web_repo;
                         $link_rev = ';a=commitdiff;h=%revno';
                         $link_file = ';a=history;f=%file;hb=HEAD';
                         $link_diff = ';a=blobdiff;f=%file;hb=%revno;hpb=%oldrev';
                         $link_view = ';a=blob;f=%file;hb=%revno';
                         break;
                     case 'cgit':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/commit/?id=%revno';
                         $link_file = '/log';
                         $link_diff = '/diff/%file?id=%revno?id2=%oldrev';
                         $link_view = '/tree/%file?id=%revno';
                         break;
                     case 'hgweb':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/rev/%revno';
                         $link_file = '/log/tip/%file';
                         $link_diff = '/diff/%revno/%file';
                         $link_view = '/file/%revno/%file';
                         break;
                     case 'github':
                         $base_url = 'http://github.com/' . $web_repo;
                         $link_rev = '/commit/%revno';
                         $link_file = '/commits/master/%file';
                         $link_diff = '/commit/%revno';
                         $link_view = '/blob/%revno/%file';
                         break;
                     case 'gitlab':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/commit/%revno';
                         $link_file = '/commits/%branch/%file';
                         $link_diff = '/commit/%revno';
                         $link_view = '/blob/%revno/%file';
                         break;
                     case 'bitbucket':
                         $base_url = 'https://bitbucket.org/' . $web_repo;
                         $link_rev = '/changeset/%revno';
                         $link_file = '/history/%file';
                         $link_diff = '/changeset/%revno#chg-%file';
                         $link_view = '/src/%revno/%file';
                         break;
                     case 'gitorious':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/commit/%revno';
                         $link_file = '/blobs/history/master/%file';
                         $link_diff = '/commit/%revno';
                         $link_view = '/blobs/%revno/%file';
                         break;
                     case 'rhodecode':
                         $base_url = $web_path . '/' . $web_repo;
                         $link_rev = '/changeset/%revno';
                         $link_file = '/changelog/%revno/%file';
                         $link_diff = '/diff/%file?diff2=%revno&amp;diff1=%oldrev&amp;fulldiff=1&amp;diff=diff';
                         $link_view = '/files/%revno/%file';
                         break;
                 }
                 $this->saveSetting('browser_url_' . $projectId, $base_url);
                 $this->saveSetting('log_url_' . $projectId, $link_file);
                 $this->saveSetting('blob_url_' . $projectId, $link_diff);
                 $this->saveSetting('diff_url_' . $projectId, $link_view);
                 $this->saveSetting('commit_url_' . $projectId, $link_rev);
                 // Access method
                 $this->saveSetting('access_method_' . $projectId, $access_method);
                 if ($access_method == self::ACCESS_HTTP) {
                     $this->saveSetting('access_passkey_' . $projectId, $passkey);
                 }
                 // Enable VCS Integration
                 $this->saveSetting('vcs_mode_' . $projectId, self::MODE_ISSUECOMMITS);
                 // Remove old settings
                 $this->deleteSetting('web_type_' . $projectId);
                 $this->deleteSetting('web_path_' . $projectId);
                 $this->deleteSetting('web_repo_' . $projectId);
             }
             // Remove old settings
             $this->deleteSetting('use_web_interface');
             $this->deleteSetting('vcs_passkey');
             // Upgrade module version
             $this->_version = $this->_module_version;
             $this->save();
             break;
     }
 }
 protected static function initializeUser()
 {
     TBGLogging::log('Loading user');
     try {
         TBGLogging::log('is this logout?');
         if (self::getRequest()->getParameter('logout')) {
             TBGLogging::log('yes');
             self::logout();
         } else {
             TBGLogging::log('no');
             TBGLogging::log('sets up user object');
             $event = TBGEvent::createNew('core', 'pre_login');
             $event->trigger();
             if ($event->isProcessed()) {
                 self::loadUser($event->getReturnValue());
             } elseif (!self::isCLI()) {
                 self::loadUser();
             } else {
                 self::$_user = new TBGUser();
             }
             TBGEvent::createNew('core', 'post_login', self::getUser())->trigger();
             TBGLogging::log('loaded');
             TBGLogging::log('caching permissions');
             self::cacheAllPermissions();
             TBGLogging::log('done (caching permissions)');
         }
     } catch (TBGElevatedLoginException $e) {
         TBGLogging::log("Could not reauthenticate elevated permissions: " . $e->getMessage(), 'main', TBGLogging::LEVEL_INFO);
         TBGContext::setMessage('elevated_login_message_err', $e->getMessage());
         self::$_redirect_login = '******';
     } catch (Exception $e) {
         TBGLogging::log("Something happened while setting up user: " . $e->getMessage(), 'main', TBGLogging::LEVEL_WARNING);
         $allow_anonymous_routes = array('register', 'register_check_username', 'register1', 'register2', 'activate', 'reset_password', 'captcha', 'login', 'login_page', 'getBackdropPartial', 'serve', 'doLogin');
         if (!self::isCLI() && (!in_array(self::getRouting()->getCurrentRouteModule(), array('main', 'remote')) || !in_array(self::getRouting()->getCurrentRouteName(), $allow_anonymous_routes))) {
             TBGContext::setMessage('login_message_err', $e->getMessage());
             self::$_redirect_login = '******';
         } else {
             self::$_user = self::factory()->TBGUser(TBGSettings::getDefaultUserID());
         }
     }
     TBGLogging::log('...done');
 }
Beispiel #7
0
 /**
  * Whether or not the current user has voted
  *
  * @return boolean
  */
 public function hasUserVoted($user_id, $up)
 {
     $user_id = is_object($user_id) ? $user_id->getID() : $user_id;
     $this->_setupVotes();
     if ($user_id == TBGSettings::getDefaultUserID() && TBGSettings::isDefaultUserGuest() || !$this->getProject()->canVoteOnIssues()) {
         return true;
     }
     if (array_key_exists($user_id, $this->_votes)) {
         return $up ? (int) $this->_votes[$user_id] > 0 : (int) $this->_votes[$user_id] < 0;
     } else {
         return false;
     }
 }