This commit is contained in:
dingguodong
2021-06-17 14:52:18 +08:00
4 changed files with 55 additions and 10 deletions
+37 -5
View File
@@ -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();
}
+3 -2
View File
@@ -27,5 +27,6 @@ $lang->gitlab->gitlabNameTips = '该名称会显示在其他相应模块中
$lang->gitlab->gitlabUrlTips = "填写示例:https://gitlab.zcorp.cc <br> 仅填写gitlab服务地址,不需要填写其他URI";
$lang->gitlab->gitlabTokenTips = "请填写具有admin权限账户的access token <br> 可在:https://&lt;gitlab url&gt;/-/profile/personal_access_tokens 创建";
$lang->gitlab->tokenError = "当前token非管理员权限。";
$lang->gitlab->hostError = "无效的gitlab服务地址。";
$lang->gitlab->tokenError = "当前token非管理员权限。";
$lang->gitlab->hostError = "无效的gitlab服务地址。";
$lang->gitlab->bindUserError = "不能重复绑定用户 %s";
+14 -2
View File
@@ -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]);
+1 -1
View File
@@ -15,7 +15,7 @@
<div class="main-header">
<h2><?php echo $lang->gitlab->bindUser;?></h2>
</div>
<form method='post' class='load-indicator main-form' enctype='multipart/form-data' target='hiddenwin' id="batchCreateForm">
<form method='post' class='load-indicator main-form form-ajax' enctype='multipart/form-data'>
<div class="table-responsive">
<table class="table table-borderless w-600px">
<thead>