diff --git a/module/action/lang/de.php b/module/action/lang/de.php index 988570d7ce..3e6cb30e3f 100644 --- a/module/action/lang/de.php +++ b/module/action/lang/de.php @@ -133,6 +133,7 @@ $lang->action->objectTypes['gitlabbranch'] = 'GitLab Branch'; $lang->action->objectTypes['gitlabbranchpriv'] = 'GitLab Protected Branches'; $lang->action->objectTypes['gitlabtag'] = 'GitLab Tag'; $lang->action->objectTypes['gitlabtagpriv'] = 'GitLab Tag Protected'; +$lang->action->objectTypes['giteauser'] = 'Gitea User'; $lang->action->objectTypes['kanbanspace'] = 'Kanban Space'; $lang->action->objectTypes['kanban'] = 'Kanban'; $lang->action->objectTypes['kanbanregion'] = 'Kanban Region'; diff --git a/module/action/lang/en.php b/module/action/lang/en.php index e195c73ad7..2ca3bbdd58 100755 --- a/module/action/lang/en.php +++ b/module/action/lang/en.php @@ -133,6 +133,7 @@ $lang->action->objectTypes['gitlabbranch'] = 'GitLab Branch'; $lang->action->objectTypes['gitlabbranchpriv'] = 'GitLab Protected Branches'; $lang->action->objectTypes['gitlabtag'] = 'GitLab Tag'; $lang->action->objectTypes['gitlabtagpriv'] = 'GitLab Tag Protected'; +$lang->action->objectTypes['giteauser'] = 'Gitea User'; $lang->action->objectTypes['kanbanspace'] = 'Kanban Space'; $lang->action->objectTypes['kanban'] = 'Kanban'; $lang->action->objectTypes['kanbanregion'] = 'Kanban Region'; diff --git a/module/action/lang/fr.php b/module/action/lang/fr.php index fb1c45ebc5..75212cf95b 100644 --- a/module/action/lang/fr.php +++ b/module/action/lang/fr.php @@ -133,6 +133,7 @@ $lang->action->objectTypes['gitlabbranch'] = 'GitLab Branch'; $lang->action->objectTypes['gitlabbranchpriv'] = 'GitLab Protected Branches'; $lang->action->objectTypes['gitlabtag'] = 'GitLab Tag'; $lang->action->objectTypes['gitlabtagpriv'] = 'GitLab Tag Protected'; +$lang->action->objectTypes['giteauser'] = 'Gitea User'; $lang->action->objectTypes['kanbanspace'] = 'Kanban Space'; $lang->action->objectTypes['kanban'] = 'Kanban'; $lang->action->objectTypes['kanbanregion'] = 'Kanban Region'; diff --git a/module/action/lang/vi.php b/module/action/lang/vi.php index 681dc1b71f..7e7aa0909a 100644 --- a/module/action/lang/vi.php +++ b/module/action/lang/vi.php @@ -111,6 +111,7 @@ $lang->action->objectTypes['gitlabbranch'] = 'GitLab Branch'; $lang->action->objectTypes['gitlabbranchpriv'] = 'GitLab Protected Branches'; $lang->action->objectTypes['gitlabtag'] = 'GitLab Tag'; $lang->action->objectTypes['gitlabtagpriv'] = 'GitLab Tag Protected'; +$lang->action->objectTypes['giteauser'] = 'Gitea User'; $lang->action->objectTypes['kanbanspace'] = 'Kanban Space'; $lang->action->objectTypes['kanban'] = 'Kanban'; $lang->action->objectTypes['kanbanregion'] = 'Kanban Region'; diff --git a/module/action/lang/zh-cn.php b/module/action/lang/zh-cn.php index 4b332524af..84a25b2372 100755 --- a/module/action/lang/zh-cn.php +++ b/module/action/lang/zh-cn.php @@ -133,6 +133,7 @@ $lang->action->objectTypes['gitlabbranch'] = 'GitLab分支'; $lang->action->objectTypes['gitlabbranchpriv'] = 'GitLab保护分支'; $lang->action->objectTypes['gitlabtag'] = 'GitLab标签'; $lang->action->objectTypes['gitlabtagpriv'] = 'GitLab标签保护'; +$lang->action->objectTypes['giteauser'] = 'Gitea用户'; $lang->action->objectTypes['kanbanspace'] = '看板空间'; $lang->action->objectTypes['kanban'] = '看板'; $lang->action->objectTypes['kanbanregion'] = '看板区域'; diff --git a/module/action/lang/zh-tw.php b/module/action/lang/zh-tw.php index 9a66017d80..04ebfaedf6 100755 --- a/module/action/lang/zh-tw.php +++ b/module/action/lang/zh-tw.php @@ -127,6 +127,7 @@ $lang->action->objectTypes['gitlabgroup'] = 'GitLab群組'; $lang->action->objectTypes['gitlabbranch'] = 'GitLab分支'; $lang->action->objectTypes['gitlabbranchpriv'] = 'GitLab保護分支'; $lang->action->objectTypes['gitlabtag'] = 'GitLab標籤'; +$lang->action->objectTypes['giteauser'] = 'Gitea用戶'; $lang->action->objectTypes['kanbanspace'] = '看板空間'; $lang->action->objectTypes['kanban'] = '看板'; $lang->action->objectTypes['kanbanregion'] = '看板區域'; diff --git a/module/action/model.php b/module/action/model.php index ca7525cfd4..9512d4cbd8 100755 --- a/module/action/model.php +++ b/module/action/model.php @@ -1254,8 +1254,8 @@ class actionModel extends model /* If action type is login or logout, needn't link. */ if($actionType == 'svncommited' or $actionType == 'gitcommited') $action->actor = zget($commiters, $action->actor); - /* Get gitlab objectname. */ - if(empty($action->objectName) and substr($objectType, 0, 6) == 'gitlab') $action->objectName = $action->extra; + /* Get gitlab or gitea objectname. */ + if(empty($action->objectName) and (substr($objectType, 0, 6) == 'gitlab' or substr($objectType, 0, 5) == 'gitea')) $action->objectName = $action->extra; /* Other actions, create a link. */ if(!$this->setObjectLink($action, $deptUsers)) diff --git a/module/gitea/control.php b/module/gitea/control.php index 413f2c2e52..2c6cc08a2a 100644 --- a/module/gitea/control.php +++ b/module/gitea/control.php @@ -43,6 +43,11 @@ class gitea extends control /* Admin user don't need bind. */ $giteaList = $this->gitea->getList($orderBy, $pager); + foreach($giteaList as $gitea) + { + $gitea->isBindUser = true; + if(!$this->app->user->admin and !isset($myGiteas[$gitea->id])) $gitea->isBindUser = false; + } $this->view->title = $this->lang->gitea->common . $this->lang->colon . $this->lang->gitea->browse; $this->view->giteaList = $giteaList; @@ -164,4 +169,31 @@ class gitea extends control return true; } + + /** + * Bind gitea user to zentao users. + * + * @param int $giteaID + * @access public + * @return void + */ + public function bindUser($giteaID) + { + $zentaoUsers = $this->dao->select('account,email,realname')->from(TABLE_USER)->fetchAll('account'); + $userPairs = $this->loadModel('user')->getPairs('noclosed|noletter'); + + if($_POST) + { + $this->gitea->bindUser($giteaID); + if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError())); + return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->server->http_referer)); + } + + $this->view->title = $this->lang->gitea->bindUser; + $this->view->userPairs = $userPairs; + $this->view->giteaUsers = $this->gitea->apiGetUsers($giteaID); + $this->view->bindedUsers = $this->gitea->getUserAccountIdPairs($giteaID); + $this->view->matchedResult = $this->gitea->getMatchedUsers($giteaID, $this->view->giteaUsers, $zentaoUsers); + $this->display(); + } } diff --git a/module/gitea/lang/de.php b/module/gitea/lang/de.php index 5d0779ba55..b4716e7bed 100644 --- a/module/gitea/lang/de.php +++ b/module/gitea/lang/de.php @@ -8,6 +8,13 @@ $lang->gitea->edit = 'Edit Gitea'; $lang->gitea->view = 'View Gitea'; $lang->gitea->delete = 'Delete Gitea'; $lang->gitea->confirmDelete = 'Do you want to delete this Gitea server?'; +$lang->gitea->bindUser = 'Bind User'; +$lang->gitea->giteaAccount = 'Gitea Account'; +$lang->gitea->zentaoAccount = 'Zentao Account'; +$lang->gitea->bindingStatus = 'Binding Status'; +$lang->gitea->notBind = 'Not bind'; +$lang->gitea->binded = 'Binded'; +$lang->gitea->bindDynamic = '%s and Zentao user %s'; $lang->gitea->browseAction = 'Gitea List'; $lang->gitea->deleteAction = 'Delete Gitea'; diff --git a/module/gitea/lang/en.php b/module/gitea/lang/en.php index 5d0779ba55..b4716e7bed 100644 --- a/module/gitea/lang/en.php +++ b/module/gitea/lang/en.php @@ -8,6 +8,13 @@ $lang->gitea->edit = 'Edit Gitea'; $lang->gitea->view = 'View Gitea'; $lang->gitea->delete = 'Delete Gitea'; $lang->gitea->confirmDelete = 'Do you want to delete this Gitea server?'; +$lang->gitea->bindUser = 'Bind User'; +$lang->gitea->giteaAccount = 'Gitea Account'; +$lang->gitea->zentaoAccount = 'Zentao Account'; +$lang->gitea->bindingStatus = 'Binding Status'; +$lang->gitea->notBind = 'Not bind'; +$lang->gitea->binded = 'Binded'; +$lang->gitea->bindDynamic = '%s and Zentao user %s'; $lang->gitea->browseAction = 'Gitea List'; $lang->gitea->deleteAction = 'Delete Gitea'; diff --git a/module/gitea/lang/fr.php b/module/gitea/lang/fr.php index 5d0779ba55..b4716e7bed 100644 --- a/module/gitea/lang/fr.php +++ b/module/gitea/lang/fr.php @@ -8,6 +8,13 @@ $lang->gitea->edit = 'Edit Gitea'; $lang->gitea->view = 'View Gitea'; $lang->gitea->delete = 'Delete Gitea'; $lang->gitea->confirmDelete = 'Do you want to delete this Gitea server?'; +$lang->gitea->bindUser = 'Bind User'; +$lang->gitea->giteaAccount = 'Gitea Account'; +$lang->gitea->zentaoAccount = 'Zentao Account'; +$lang->gitea->bindingStatus = 'Binding Status'; +$lang->gitea->notBind = 'Not bind'; +$lang->gitea->binded = 'Binded'; +$lang->gitea->bindDynamic = '%s and Zentao user %s'; $lang->gitea->browseAction = 'Gitea List'; $lang->gitea->deleteAction = 'Delete Gitea'; diff --git a/module/gitea/lang/vi.php b/module/gitea/lang/vi.php index 5d0779ba55..b4716e7bed 100644 --- a/module/gitea/lang/vi.php +++ b/module/gitea/lang/vi.php @@ -8,6 +8,13 @@ $lang->gitea->edit = 'Edit Gitea'; $lang->gitea->view = 'View Gitea'; $lang->gitea->delete = 'Delete Gitea'; $lang->gitea->confirmDelete = 'Do you want to delete this Gitea server?'; +$lang->gitea->bindUser = 'Bind User'; +$lang->gitea->giteaAccount = 'Gitea Account'; +$lang->gitea->zentaoAccount = 'Zentao Account'; +$lang->gitea->bindingStatus = 'Binding Status'; +$lang->gitea->notBind = 'Not bind'; +$lang->gitea->binded = 'Binded'; +$lang->gitea->bindDynamic = '%s and Zentao user %s'; $lang->gitea->browseAction = 'Gitea List'; $lang->gitea->deleteAction = 'Delete Gitea'; diff --git a/module/gitea/lang/zh-cn.php b/module/gitea/lang/zh-cn.php index dd8da67d29..7e273df350 100644 --- a/module/gitea/lang/zh-cn.php +++ b/module/gitea/lang/zh-cn.php @@ -8,6 +8,13 @@ $lang->gitea->edit = '编辑Gitea'; $lang->gitea->view = '查看Gitea'; $lang->gitea->delete = '删除Gitea'; $lang->gitea->confirmDelete = '确认删除该Gitea吗?'; +$lang->gitea->bindUser = '绑定用户'; +$lang->gitea->giteaAccount = 'Gitea用户'; +$lang->gitea->zentaoAccount = '禅道用户'; +$lang->gitea->bindingStatus = '绑定状态'; +$lang->gitea->notBind = '未绑定'; +$lang->gitea->binded = '已绑定'; +$lang->gitea->bindDynamic = '%s与禅道用户%s'; $lang->gitea->browseAction = 'Gitea列表'; $lang->gitea->deleteAction = '删除Gitea'; diff --git a/module/gitea/lang/zh-tw.php b/module/gitea/lang/zh-tw.php index dd8da67d29..7e273df350 100644 --- a/module/gitea/lang/zh-tw.php +++ b/module/gitea/lang/zh-tw.php @@ -8,6 +8,13 @@ $lang->gitea->edit = '编辑Gitea'; $lang->gitea->view = '查看Gitea'; $lang->gitea->delete = '删除Gitea'; $lang->gitea->confirmDelete = '确认删除该Gitea吗?'; +$lang->gitea->bindUser = '绑定用户'; +$lang->gitea->giteaAccount = 'Gitea用户'; +$lang->gitea->zentaoAccount = '禅道用户'; +$lang->gitea->bindingStatus = '绑定状态'; +$lang->gitea->notBind = '未绑定'; +$lang->gitea->binded = '已绑定'; +$lang->gitea->bindDynamic = '%s与禅道用户%s'; $lang->gitea->browseAction = 'Gitea列表'; $lang->gitea->deleteAction = '删除Gitea'; diff --git a/module/gitea/model.php b/module/gitea/model.php index f1fabf2ffe..6a9ad9ef99 100644 --- a/module/gitea/model.php +++ b/module/gitea/model.php @@ -104,6 +104,62 @@ class giteaModel extends model return $this->loadModel('pipeline')->update($id); } + /** + * Bind users. + * + * @param int $giteaID + * @access public + * @return array + */ + public function bindUser($giteaID) + { + $users = $this->post->zentaoUsers; + $giteaNames = $this->post->giteaUserNames; + $accountList = array(); + $repeatUsers = array(); + foreach($users as $openID => $user) + { + if(empty($user)) continue; + if(isset($accountList[$user])) $repeatUsers[] = zget($userPairs, $user); + $accountList[$user] = $openID; + } + + if(count($repeatUsers)) + { + dao::$errors[] = sprintf($this->lang->gitea->bindUserError, join(',', $repeatUsers)); + return false; + } + + $user = new stdclass; + $user->providerID = $giteaID; + $user->providerType = 'gitea'; + + $oldUsers = $this->dao->select('*')->from(TABLE_OAUTH)->where('providerType')->eq($user->providerType)->andWhere('providerID')->eq($user->providerID)->fetchAll('openID'); + foreach($users as $openID => $account) + { + $existAccount = isset($oldUsers[$openID]) ? $oldUsers[$openID] : ''; + + if($existAccount and $existAccount->account != $account) + { + $this->dao->delete() + ->from(TABLE_OAUTH) + ->where('openID')->eq($openID) + ->andWhere('providerType')->eq($user->providerType) + ->andWhere('providerID')->eq($user->providerID) + ->exec(); + $this->loadModel('action')->create('giteauser', $openID, 'unbind', '', sprintf($this->lang->gitea->bindDynamic, $giteaNames[$openID], $zentaoUsers[$existAccount->account]->realname)); + } + if(!$existAccount or $existAccount->account != $account) + { + if(!$account) continue; + $user->account = $account; + $user->openID = $openID; + $this->dao->insert(TABLE_OAUTH)->data($user)->exec(); + $this->loadModel('action')->create('giteauser', $openID, 'bind', '', sprintf($this->lang->gitea->bindDynamic, $giteaNames[$openID], $zentaoUsers[$account]->realname)); + } + } + } + /** * Api error handling. * @@ -225,6 +281,70 @@ class giteaModel extends model ->fetchPairs('providerID'); } + /** + * Get zentao account gitea user id pairs of one gitea. + * + * @param int $giteaID + * @access public + * @return array + */ + public function getUserAccountIdPairs($giteaID, $fields = 'account,openID') + { + return $this->dao->select($fields)->from(TABLE_OAUTH) + ->where('providerType')->eq('gitea') + ->andWhere('providerID')->eq($giteaID) + ->fetchPairs(); + } + + /** + * Get matched gitea users. + * + * @param int $giteaID + * @param array $giteaUsers + * @param array $zentaoUsers + * @access public + * @return array + */ + public function getMatchedUsers($giteaID, $giteaUsers, $zentaoUsers) + { + $matches = new stdclass; + foreach($giteaUsers as $giteaUser) + { + foreach($zentaoUsers as $zentaoUser) + { + if($giteaUser->account == $zentaoUser->account) $matches->accounts[$giteaUser->account][] = $zentaoUser->account; + if($giteaUser->realname == $zentaoUser->realname) $matches->names[$giteaUser->realname][] = $zentaoUser->account; + if($giteaUser->email == $zentaoUser->email) $matches->emails[$giteaUser->email][] = $zentaoUser->account; + } + } + + $bindedUsers = $this->getUserAccountIdPairs($giteaID, 'openID,account'); + $matchedUsers = array(); + foreach($giteaUsers as $giteaUser) + { + if(isset($bindedUsers[$giteaUser->id])) + { + $giteaUser->zentaoAccount = $bindedUsers[$giteaUser->id]; + $matchedUsers[] = $giteaUser; + continue; + } + + $matchedZentaoUsers = array(); + if(isset($matches->accounts[$giteaUser->account])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->accounts[$giteaUser->account]); + if(isset($matches->emails[$giteaUser->email])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->emails[$giteaUser->email]); + if(isset($matches->names[$giteaUser->realname])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->names[$giteaUser->realname]); + + $matchedZentaoUsers = array_unique($matchedZentaoUsers); + if(count($matchedZentaoUsers) == 1) + { + $giteaUser->zentaoAccount = current($matchedZentaoUsers); + $matchedUsers[] = $giteaUser; + } + } + + return $matchedUsers; + } + /** * Get project by api. * @@ -250,14 +370,70 @@ class giteaModel extends model * @access public * @return array */ - public function apiGetProjects($giteaID, $sudo = 'true') + public function apiGetProjects($giteaID, $sudo = true) { $apiRoot = $this->getApiRoot($giteaID, $sudo); if(!$apiRoot) return array(); - $url = sprintf($apiRoot, "/repos/search"); - $results = json_decode(commonModel::http($url)); + $url = sprintf($apiRoot, "/repos/search"); + $allResults = array(); + for($page = 1; true; $page++) + { + $results = json_decode(commonModel::http($url . "&page={$page}&limit=50")); + if(!is_array($results->data)) break; + if(!empty($results->data)) $allResults = array_merge($allResults, $results->data); + if(count($results->data) < 50) break; + } - return $results->data; + return $allResults; + } + + /** + * Get gitea user list. + * + * @param int $giteaID + * @param bool $onlyLinked + * @access public + * @return array + */ + public function apiGetUsers($giteaID, $onlyLinked = false) + { + $response = array(); + $apiRoot = $this->getApiRoot($giteaID); + + for($page = 1; true; $page++) + { + $url = sprintf($apiRoot, "/users/search") . "&page={$page}&limit=50"; + $result = json_decode(commonModel::http($url)); + if(empty($result->data)) break; + + $response = array_merge($response, $result->data); + $page += 1; + } + + if(empty($response)) return array(); + + /* Get linked users. */ + $linkedUsers = array(); + if($onlyLinked) $linkedUsers = $this->getUserAccountIdPairs($giteaID, 'openID,account'); + + $users = array(); + foreach($response as $giteaUser) + { + if($onlyLinked and !isset($linkedUsers[$giteaUser->id])) continue; + + $user = new stdclass; + $user->id = $giteaUser->id; + $user->realname = $giteaUser->full_name ? $giteaUser->full_name : $giteaUser->username; + $user->account = $giteaUser->username; + $user->email = zget($giteaUser, 'email', ''); + $user->avatar = $giteaUser->avatar_url; + $user->createdAt = zget($giteaUser, 'created', ''); + $user->lastActivityOn = zget($giteaUser, 'last_login', ''); + + $users[] = $user; + } + + return $users; } } diff --git a/module/gitea/view/browse.html.php b/module/gitea/view/browse.html.php index d8a23425ac..b92dd8cab0 100644 --- a/module/gitea/view/browse.html.php +++ b/module/gitea/view/browse.html.php @@ -55,7 +55,9 @@