diff --git a/module/gitlab/control.php b/module/gitlab/control.php index b8d2f1a1a4..a33c609923 100644 --- a/module/gitlab/control.php +++ b/module/gitlab/control.php @@ -72,25 +72,57 @@ class gitlab extends control } /** - * bind gitlab user to zentao users. + * Bind gitlab user to zentao users. * * @access public * @return void */ - public function bindUser($id) + public function bindUser($gitlabID) { + $userPairs = $this->loadModel('user')->getPairs(); if($_POST) { + $users = $this->post->zentaoUsers; + $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)) $this->send(array('result' => 'fail', 'message' => sprintf($this->lang->gitlab->bindUserError, join(',', $repeatUsers)))); + + $user = new stdclass; + $user->providerID = $gitlabID; + $user->providerType = 'gitlab'; + + foreach($users as $openID => $account) + { + if(!$account) continue; + $user->account = $account; + $user->openID = $openID; + + $this->dao->delete(TABLE_OAUTH) + ->where('openID')->eq($user->openID) + ->andWhere('providerType')->eq('gitlab') + ->andWhere('providerID')->eq($id) + ->andWhere('account')->eq($user->account) + ->exec(); + + $this->dao->insert(TABLE_OAUTH)->data($user)->exec(); + } + $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->sever->http_referer)); } - $gitlab = $this->gitlab->getByID($id); + $gitlab = $this->gitlab->getByID($gitlabID); $zentaoUsers = $this->dao->select('account,email,realname')->from(TABLE_USER)->fetchAll('account'); $this->view->title = $this->lang->gitlab->bindUser; - $this->view->userPairs = $this->loadModel('user')->getPairs(); + $this->view->userPairs = $userPairs; $this->view->gitlabUsers = $this->gitlab->apiGetUsers($gitlab); - $this->view->matchedResult = $this->gitlab->getMatchedUsers($this->view->gitlabUsers, $zentaoUsers); + $this->view->matchedResult = $this->gitlab->getMatchedUsers($gitlabID, $this->view->gitlabUsers, $zentaoUsers); $this->display(); } diff --git a/module/gitlab/lang/zh-cn.php b/module/gitlab/lang/zh-cn.php index 4389a02c38..bd77ff6bc6 100644 --- a/module/gitlab/lang/zh-cn.php +++ b/module/gitlab/lang/zh-cn.php @@ -27,5 +27,6 @@ $lang->gitlab->gitlabNameTips = '该名称会显示在其他相应模块中 $lang->gitlab->gitlabUrlTips = "填写示例:https://gitlab.zcorp.cc
仅填写gitlab服务地址,不需要填写其他URI"; $lang->gitlab->gitlabTokenTips = "请填写具有admin权限账户的access token
可在:https://<gitlab url>/-/profile/personal_access_tokens 创建"; -$lang->gitlab->tokenError = "当前token非管理员权限。"; -$lang->gitlab->hostError = "无效的gitlab服务地址。"; +$lang->gitlab->tokenError = "当前token非管理员权限。"; +$lang->gitlab->hostError = "无效的gitlab服务地址。"; +$lang->gitlab->bindUserError = "不能重复绑定用户 %s"; diff --git a/module/gitlab/model.php b/module/gitlab/model.php index bcdc639d49..1da694669f 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -97,7 +97,7 @@ class gitlabModel extends model { $api = rtrim($gitlab->url, '/') . '/api/v4/users?private_token=' . $gitlab->token; $response = json_decode(commonModel::http($api)); - + if (!$response) return array(); $users = array(); @@ -123,7 +123,7 @@ class gitlabModel extends model * @access public * @return array */ - public function getMatchedUsers($gitlabUsers, $zentaoUsers) + public function getMatchedUsers($gitlabID, $gitlabUsers, $zentaoUsers) { $matches = new stdclass; foreach($gitlabUsers as $gitlabUser) @@ -135,10 +135,22 @@ class gitlabModel extends model if($gitlabUser->email == $zentaoUser->email) $matches->emails[$gitlabUser->email][] = $zentaoUser->account; } } + + $bindedUsers = $this->dao->select('openID,account')->from(TABLE_OAUTH) + ->where('providerType')->eq('gitlab') + ->andWhere('providerID')->eq($gitlabID) + ->fetchPairs(); $matchedUsers = array(); foreach($gitlabUsers as $gitlabUser) { + if(isset($bindedUsers[$gitlabUser->id])) + { + $gitlabUser->zentaoAccount = $bindedUsers[$gitlabUser->id]; + $matchedUsers[] = $gitlabUser; + continue; + } + $matchedZentaoUsers = array(); if(isset($matches->accounts[$gitlabUser->account])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->accounts[$gitlabUser->account]); if(isset($matches->emails[$gitlabUser->email])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->emails[$gitlabUser->email]); diff --git a/module/gitlab/view/binduser.html.php b/module/gitlab/view/binduser.html.php index 9eb34410a2..a0cf0dd17f 100644 --- a/module/gitlab/view/binduser.html.php +++ b/module/gitlab/view/binduser.html.php @@ -15,7 +15,7 @@

gitlab->bindUser;?>

-
+