From 36c97752b487eee8b6df045d6c5a01860d82d10c Mon Sep 17 00:00:00 2001 From: zenggang Date: Thu, 20 Jan 2022 09:25:44 +0000 Subject: [PATCH 1/3] * Finish task#48104 --- module/gitlab/model.php | 2 +- module/mr/control.php | 9 +++++- module/mr/model.php | 56 +++++++++++++++++++++++++++++++++- module/mr/view/browse.html.php | 8 ++--- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index c27939213e..eebe678608 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -656,7 +656,7 @@ class gitlabModel extends model $allResults = array(); for($page = 1; true; $page++) { - $results = json_decode(commonModel::http($host . "&simple=true&page={$page}&per_page=100")); + $results = json_decode(commonModel::http($url . "&page={$page}&per_page=100")); if(!is_array($results)) break; if(!empty($results)) $allResults = array_merge($allResults, $results); if(count($results)<100 or $page > 10) break; diff --git a/module/mr/control.php b/module/mr/control.php index b70439a853..34e3308f10 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -32,7 +32,13 @@ class mr extends control { $this->app->loadClass('pager', $static = true); $pager = new pager($recTotal, $recPerPage, $pageID); - $MRList = $this->mr->getList($mode, $param, $orderBy, $pager); + + $projects = array(); + if(!$this->app->user->admin) + { + $projects = $this->mr->getAllGitlabProjects(); + } + $MRList = $this->mr->getList($mode, $param, $orderBy, $pager, $projects); /* Save current URI to session. */ $this->session->set('mrList', $this->app->getURI(true), 'repo'); @@ -53,6 +59,7 @@ class mr extends control $this->view->title = $this->lang->mr->common . $this->lang->colon . $this->lang->mr->browse; $this->view->MRList = $MRList; + $this->view->projects = $projects; $this->view->pager = $pager; $this->view->mode = $mode; $this->view->param = $param; diff --git a/module/mr/model.php b/module/mr/model.php index 5565966381..85534fef89 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -42,17 +42,35 @@ class mrModel extends model * @param string $param * @param string $orderBy * @param object $pager + * @param array $filterProjects * @access public * @return array */ - public function getList($mode = 'all', $param = 'all', $orderBy = 'id_desc', $pager = null) + public function getList($mode = 'all', $param = 'all', $orderBy = 'id_desc', $pager = null, $filterProjects = array()) { + $filterProjectSql = ''; + if(!empty($filterProjects)) + { + foreach($filterProjects as $gitlabID => $projects) + { + $projectIDs = array_keys($projects); + if(!empty($projectIDs)) $filterProjectSql .= "(gitlabID = {$gitlabID} and sourceProject ".helper::dbIN($projectIDs).") or "; + } + + if($filterProjectSql) + { + $filterProjectSql = substr($filterProjectSql, 0, -3); // Remove last or. + $filterProjectSql = '(' . $filterProjectSql . ')'; + } + } + $MRList = $this->dao->select('*') ->from(TABLE_MR) ->where('deleted')->eq('0') ->beginIF($mode == 'status' and $param != 'all')->andWhere('status')->eq($param)->fi() ->beginIF($mode == 'assignee' and $param != 'all')->andWhere('assignee')->eq($param)->fi() ->beginIF($mode == 'creator' and $param != 'all')->andWhere('createdBy')->eq($param)->fi() + ->beginIF($filterProjectSql)->andWhere($filterProjectSql)->fi() ->orderBy($orderBy) ->page($pager) ->fetchAll('id'); @@ -76,6 +94,42 @@ class mrModel extends model return array('' => '') + $MR; } + /** + * Get all gitlab server project,private projects that do not include guest permissions. + * + * @access public + * @return void + */ + public function getAllGitlabProjects() + { + $gitlabIDs = $this->dao->select('distinct gitlabID')->from(TABLE_MR) + ->where('deleted')->eq('0') + ->fetchPairs('gitlabID'); + + $allProjects = array(); + $gitlabUsers = $this->dao->select('openID,providerID')->from(TABLE_OAUTH) + ->where('providerType')->eq('gitlab') + ->andWhere('account')->eq($this->app->user->account) + ->fetchPairs('providerID', 'openID'); + foreach($gitlabIDs as $gitlabID) + { + if(!isset($gitlabUsers[$gitlabID])) continue; + $allProjects[$gitlabID] = $this->gitlab->apiGetProjects($gitlabID); + } + + $allProjectPairs = array(); + foreach($allProjects as $gitlabID => $projects) + { + foreach($projects as $key => $project) + { + if(empty($project->permissions->project_access->access_level) or $project->permissions->project_access->access_level <= 10) continue; + $allProjectPairs[$gitlabID][$project->id] = $project; + } + } + + return $allProjectPairs; + } + /** * Create MR function. * diff --git a/module/mr/view/browse.html.php b/module/mr/view/browse.html.php index 4a27fcd709..17a41bb9a3 100644 --- a/module/mr/view/browse.html.php +++ b/module/mr/view/browse.html.php @@ -64,13 +64,13 @@ - sourceProject])) $projects[$MR->sourceProject] = $this->loadModel('gitlab')->apiGetSingleProject($MR->gitlabID, $MR->sourceProject); ?> - targetProject])) $projects[$MR->targetProject] = $this->loadModel('gitlab')->apiGetSingleProject($MR->gitlabID, $MR->targetProject); ?> + gitlabID][$MR->sourceProject])) $projects[$MR->gitlabID][$MR->sourceProject] = $this->loadModel('gitlab')->apiGetSingleProject($MR->gitlabID, $MR->sourceProject); ?> + gitlabID][$MR->targetProject])) $projects[$MR->gitlabID][$MR->targetProject] = $this->loadModel('gitlab')->apiGetSingleProject($MR->gitlabID, $MR->targetProject); ?> id;?> id}"), $MR->title);?> - sourceBranch;?>'>sourceProject]->name_with_namespace . ':' . $MR->sourceBranch;?> - targetBranch;?>'>targetProject]->name_with_namespace . ':' . $MR->targetBranch;?> + sourceBranch;?>'>gitlabID][$MR->sourceProject]->name_with_namespace . ':' . $MR->sourceBranch;?> + targetBranch;?>'>gitlabID][$MR->targetProject]->name_with_namespace . ':' . $MR->targetBranch;?> status == 'closed'):?> mr->statusList, $MR->status);?> From e315a624400466747ee3fb19283ec3ba71e171c0 Mon Sep 17 00:00:00 2001 From: zenggang Date: Fri, 21 Jan 2022 02:38:51 +0000 Subject: [PATCH 2/3] * Adjust code --- module/mr/control.php | 8 ++------ module/mr/model.php | 13 +++++++------ module/mr/view/browse.html.php | 2 -- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/module/mr/control.php b/module/mr/control.php index 34e3308f10..f6f241039c 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -33,12 +33,8 @@ class mr extends control $this->app->loadClass('pager', $static = true); $pager = new pager($recTotal, $recPerPage, $pageID); - $projects = array(); - if(!$this->app->user->admin) - { - $projects = $this->mr->getAllGitlabProjects(); - } - $MRList = $this->mr->getList($mode, $param, $orderBy, $pager, $projects); + $projects = $this->mr->getAllGitlabProjects(); + $MRList = $this->mr->getList($mode, $param, $orderBy, $pager, empty($projects) ? false : $projects); /* Save current URI to session. */ $this->session->set('mrList', $this->app->getURI(true), 'repo'); diff --git a/module/mr/model.php b/module/mr/model.php index 85534fef89..cb669e0902 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -48,13 +48,14 @@ class mrModel extends model */ public function getList($mode = 'all', $param = 'all', $orderBy = 'id_desc', $pager = null, $filterProjects = array()) { + if($filterProjects === false) return array(); // If filterProjects equals false,it means no permission. $filterProjectSql = ''; if(!empty($filterProjects)) { foreach($filterProjects as $gitlabID => $projects) { - $projectIDs = array_keys($projects); - if(!empty($projectIDs)) $filterProjectSql .= "(gitlabID = {$gitlabID} and sourceProject ".helper::dbIN($projectIDs).") or "; + $projectIDList = array_keys($projects); + if(!empty($projectIDList)) $filterProjectSql .= "(gitlabID = {$gitlabID} and sourceProject ".helper::dbIN($projectIDList).") or "; } if($filterProjectSql) @@ -98,11 +99,11 @@ class mrModel extends model * Get all gitlab server project,private projects that do not include guest permissions. * * @access public - * @return void + * @return array */ public function getAllGitlabProjects() { - $gitlabIDs = $this->dao->select('distinct gitlabID')->from(TABLE_MR) + $gitlabIDList = $this->dao->select('distinct gitlabID')->from(TABLE_MR) ->where('deleted')->eq('0') ->fetchPairs('gitlabID'); @@ -111,9 +112,9 @@ class mrModel extends model ->where('providerType')->eq('gitlab') ->andWhere('account')->eq($this->app->user->account) ->fetchPairs('providerID', 'openID'); - foreach($gitlabIDs as $gitlabID) + foreach($gitlabIDList as $gitlabID) { - if(!isset($gitlabUsers[$gitlabID])) continue; + if(!$this->app->user->admin and !isset($gitlabUsers[$gitlabID])) continue; $allProjects[$gitlabID] = $this->gitlab->apiGetProjects($gitlabID); } diff --git a/module/mr/view/browse.html.php b/module/mr/view/browse.html.php index 17a41bb9a3..d017f89fa3 100644 --- a/module/mr/view/browse.html.php +++ b/module/mr/view/browse.html.php @@ -64,8 +64,6 @@ - gitlabID][$MR->sourceProject])) $projects[$MR->gitlabID][$MR->sourceProject] = $this->loadModel('gitlab')->apiGetSingleProject($MR->gitlabID, $MR->sourceProject); ?> - gitlabID][$MR->targetProject])) $projects[$MR->gitlabID][$MR->targetProject] = $this->loadModel('gitlab')->apiGetSingleProject($MR->gitlabID, $MR->targetProject); ?> id;?> id}"), $MR->title);?> From 47e13757a89b7cf378980d6c403c14a45bcdea9f Mon Sep 17 00:00:00 2001 From: zenggang Date: Fri, 21 Jan 2022 05:11:52 +0000 Subject: [PATCH 3/3] * Adjust code --- module/mr/model.php | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/module/mr/model.php b/module/mr/model.php index cb669e0902..f1b69d83c8 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -38,17 +38,19 @@ class mrModel extends model /** * Get MR list of gitlab project. * - * @param string $mode - * @param string $param - * @param string $orderBy - * @param object $pager - * @param array $filterProjects + * @param string $mode + * @param string $param + * @param string $orderBy + * @param object $pager + * @param array|bool $filterProjects * @access public * @return array */ public function getList($mode = 'all', $param = 'all', $orderBy = 'id_desc', $pager = null, $filterProjects = array()) { - if($filterProjects === false) return array(); // If filterProjects equals false,it means no permission. + /* If filterProjects equals false,it means no permission. */ + if($filterProjects === false) return array(); + $filterProjectSql = ''; if(!empty($filterProjects)) { @@ -58,11 +60,7 @@ class mrModel extends model if(!empty($projectIDList)) $filterProjectSql .= "(gitlabID = {$gitlabID} and sourceProject ".helper::dbIN($projectIDList).") or "; } - if($filterProjectSql) - { - $filterProjectSql = substr($filterProjectSql, 0, -3); // Remove last or. - $filterProjectSql = '(' . $filterProjectSql . ')'; - } + if($filterProjectSql) $filterProjectSql = '(' . substr($filterProjectSql, 0, -3) . ')'; // Remove last or. } $MRList = $this->dao->select('*') @@ -108,10 +106,10 @@ class mrModel extends model ->fetchPairs('gitlabID'); $allProjects = array(); - $gitlabUsers = $this->dao->select('openID,providerID')->from(TABLE_OAUTH) + $gitlabUsers = $this->dao->select('providerID,openID')->from(TABLE_OAUTH) ->where('providerType')->eq('gitlab') ->andWhere('account')->eq($this->app->user->account) - ->fetchPairs('providerID', 'openID'); + ->fetchPairs(); foreach($gitlabIDList as $gitlabID) { if(!$this->app->user->admin and !isset($gitlabUsers[$gitlabID])) continue;