From 65d9c524bb8fb31cf56119b7e9ab7cac323c8ee7 Mon Sep 17 00:00:00 2001 From: zenggang Date: Mon, 8 Nov 2021 09:38:15 +0000 Subject: [PATCH] * Finish task#43846 --- module/gitlab/control.php | 29 +++++ module/gitlab/css/edituser.css | 4 + module/gitlab/js/common.js | 20 ++++ module/gitlab/js/createuser.js | 15 +-- module/gitlab/js/edituser.js | 4 + module/gitlab/model.php | 136 ++++++++++++++++++++-- module/gitlab/view/createproject.html.php | 1 - module/gitlab/view/createuser.html.php | 1 - module/gitlab/view/editproject.html.php | 1 - module/gitlab/view/edituser.html.php | 115 ++++++++++++++++++ 10 files changed, 297 insertions(+), 29 deletions(-) create mode 100644 module/gitlab/css/edituser.css create mode 100644 module/gitlab/js/common.js create mode 100644 module/gitlab/js/edituser.js create mode 100644 module/gitlab/view/edituser.html.php diff --git a/module/gitlab/control.php b/module/gitlab/control.php index bc81d7b53a..5c2862594f 100644 --- a/module/gitlab/control.php +++ b/module/gitlab/control.php @@ -330,6 +330,35 @@ class gitlab extends control $this->display(); } + /** + * Edit a gitlab user. + * + * @param int $gitlabID + * @param int $projectID + * @access public + * @return void + */ + public function editUser($gitlabID, $userID) + { + if($_POST) + { + $this->gitlab->editUser($gitlabID); + + if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError())); + return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => inlink('userBrowse', "gitlabID=$gitlabID"))); + } + + $userPairs = $this->loadModel('user')->getPairs('noclosed|noletter'); + $user = $this->gitlab->apiGetSingleUser($gitlabID, $userID); + $zentaoBindAccount = $this->dao->select('account')->from(TABLE_OAUTH)->where('providerType')->eq('gitlab')->andWhere('providerID')->eq($gitlabID)->andWhere('openID')->eq($user->id)->fetch(); + + $this->view->user = $user; + $this->view->userPairs = $userPairs; + $this->view->zentaoBindAccount = $zentaoBindAccount; + $this->view->gitlabID = $gitlabID; + $this->display(); + } + /** * Browse gitlab project. * diff --git a/module/gitlab/css/edituser.css b/module/gitlab/css/edituser.css new file mode 100644 index 0000000000..2d047c2e0a --- /dev/null +++ b/module/gitlab/css/edituser.css @@ -0,0 +1,4 @@ +#avatarUpload {display: inline-block; width: 50px; height: 50px; position: relative;} +#avatarUploadBtn {display: block; position: absolute; top: 0; left: 0; right: 0; bottom: 0;} +#avatarUploadBtn {opacity: 0; color: #fff; border-radius: 50%; line-height: 50px;} +#avatarUploadBtn:hover {opacity: 1; background-color: rgba(0,0,0,.5);} \ No newline at end of file diff --git a/module/gitlab/js/common.js b/module/gitlab/js/common.js new file mode 100644 index 0000000000..d4c0d829bb --- /dev/null +++ b/module/gitlab/js/common.js @@ -0,0 +1,20 @@ +/** + * set avatar event + * + * @access public + * @return void + */ +function setAvatar() +{ + $('#avatarUploadBtn').on('click', function() + { + $('#files').click(); + }); + $("#files").change(function(){ + var files = this.files; + if(!files.length) return; + + $(".avatar img").attr("src", window.URL.createObjectURL(files[0])); + $(".avatar").removeClass('hidden'); + }); +} \ No newline at end of file diff --git a/module/gitlab/js/createuser.js b/module/gitlab/js/createuser.js index a9181027e8..b7407093da 100644 --- a/module/gitlab/js/createuser.js +++ b/module/gitlab/js/createuser.js @@ -1,17 +1,4 @@ $(document).ready(function() { - $('#avatarUploadBtn').on('click', function() - { - $('#files').click(); - }); - $("#files").change(function(){ - var files = this.files; - if(!files.length) - { - return; - } - - $(".avatar img").attr("src", window.URL.createObjectURL(files[0])); - $(".avatar").removeClass('hidden'); - }); + setAvatar(); }); \ No newline at end of file diff --git a/module/gitlab/js/edituser.js b/module/gitlab/js/edituser.js new file mode 100644 index 0000000000..b7407093da --- /dev/null +++ b/module/gitlab/js/edituser.js @@ -0,0 +1,4 @@ +$(document).ready(function() +{ + setAvatar(); +}); \ No newline at end of file diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 9d18684664..5803cf19b2 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -531,6 +531,23 @@ class gitlabModel extends model return json_decode(commonModel::http($url, $user)); } + /** + * Create a gitab user by api. + * + * @param int $gitlabID + * @param object $user + * @access public + * @return object + */ + public function apiUpdateUser($gitlabID, $user) + { + if(empty($user->id)) return false; + + $apiRoot = $this->getApiRoot($gitlabID); + $url = sprintf($apiRoot, "/users/{$user->id}"); + return json_decode(commonModel::http($url, $user, $options = array(CURLOPT_CUSTOMREQUEST => 'PUT'))); + } + /** * Update a gitab project by api. * @@ -579,6 +596,20 @@ class gitlabModel extends model return json_decode(commonModel::http($url)); } + /** + * Get single user by API. + * + * @param int $gitlabID + * @param int $userID + * @access public + * @return object + */ + public function apiGetSingleUser($gitlabID, $userID) + { + $url = sprintf($this->getApiRoot($gitlabID), "/users/$userID"); + return json_decode(commonModel::http($url)); + } + /** * Get project users. * @@ -1682,7 +1713,7 @@ class gitlabModel extends model } /** - * Create gitlab user. + * Create a gitlab user. * * @param int $gitlabID * @access public @@ -1690,8 +1721,8 @@ class gitlabModel extends model */ public function createUser($gitlabID) { - $user = fixer::input('post')->get(); - if($_FILES['avatar']) $user->avatar = curl_file_create($_FILES['avatar']['tmp_name'], $_FILES['avatar']['type'], $_FILES['avatar']['name']); + $user = fixer::input('post')->remove('avatar')->get(); + if(!empty($_FILES['avatar'])) $user->avatar = curl_file_create($_FILES['avatar']['tmp_name'], $_FILES['avatar']['type'], $_FILES['avatar']['name']); if(empty($user->name)) dao::$errors['name'][] = $this->lang->gitlab->user->name . $this->lang->gitlab->user->emptyError; if(empty($user->username)) dao::$errors['username'][] = $this->lang->gitlab->user->username . $this->lang->gitlab->user->emptyError; @@ -1716,31 +1747,112 @@ class gitlabModel extends model $reponse = $this->apiCreateUser($gitlabID, $user); - if(!$reponse) dao::$errors[] = false; if(!empty($reponse->id)) { /* Bind user. */ if($user->bind) { $userBind = new stdclass; - $userBind->providerID = $gitlabID; + $userBind->providerID = $gitlabID; $userBind->providerType = 'gitlab'; - $userBind->account = $user->bind; - $userBind->openID = $reponse->id; + $userBind->account = $user->bind; + $userBind->openID = $reponse->id; $this->dao->insert(TABLE_OAUTH)->data($userBind)->exec(); } return TRUE; } - if(is_string($reponse->message)) dao::$errors[] = $reponse->message; - else + /* Error handling. */ + if(!empty($reponse->error)) { - foreach($reponse->message as $field => $fieldErrors) + dao::$errors[] = $reponse->error; + return FALSE; + } + if(!empty($reponse->message)) + { + if(is_string($reponse->message)) dao::$errors[] = $reponse->message; + else { - foreach($fieldErrors as $error) + foreach($reponse->message as $field => $fieldErrors) { - if($error) dao::$errors[$field][] = $error; + foreach($fieldErrors as $error) + { + if($error) dao::$errors[$field][] = $error; + } } } } + if(!$reponse) dao::$errors[] = false; + } + + /** + * Edit a gitlab user. + * + * @param int $gitlabID + * @access public + * @return bool + */ + public function editUser($gitlabID) + { + $user = fixer::input('post')->removeIF(!$this->post->password, 'password,password_repeat')->remove('avatar')->get(); + if(!empty($_FILES['avatar'])) $user->avatar = curl_file_create($_FILES['avatar']['tmp_name'], $_FILES['avatar']['type'], $_FILES['avatar']['name']); + + if(empty($user->name)) dao::$errors['name'][] = $this->lang->gitlab->user->name . $this->lang->gitlab->user->emptyError; + if(empty($user->username)) dao::$errors['username'][] = $this->lang->gitlab->user->username . $this->lang->gitlab->user->emptyError; + if(empty($user->email)) dao::$errors['email'][] = $this->lang->gitlab->user->email . $this->lang->gitlab->user->emptyError; + if(dao::isError()) return false; + if(!empty($user->password) and $user->password != $user->password_repeat) + { + dao::$errors[] = $this->lang->gitlab->user->passwordError; + return false; + } + /* Check whether the user has been bind. */ + if($user->bind) + { + $zentaoBindUser = $this->dao->select('account')->from(TABLE_OAUTH)->where('providerType')->eq('gitlab')->andWhere('providerID')->eq($gitlabID)->andWhere('account')->eq($user->bind)->fetch(); + $changeBind = (!$zentaoBindUser or $zentaoBindUser->openID != $user->id) ? true : false; + if($zentaoBindUser && $changeBind) + { + dao::$errors['bind'][] = $this->lang->gitlab->user->bindError; + return false; + } + } + + $reponse = $this->apiUpdateUser($gitlabID, $user); + + if(!empty($reponse->id)) + { + /* Bind user. */ + if($user->bind && $changeBind) + { + $userBind = new stdclass; + $userBind->providerID = $gitlabID; + $userBind->providerType = 'gitlab'; + $userBind->account = $user->bind; + $userBind->openID = $reponse->id; + $this->dao->replace(TABLE_OAUTH)->data($userBind)->exec(); + } + return TRUE; + } + /* Error handling. */ + if(!empty($reponse->error)) + { + dao::$errors[] = $reponse->error; + return FALSE; + } + if(!empty($reponse->message)) + { + if(is_string($reponse->message)) dao::$errors[] = $reponse->message; + else + { + foreach($reponse->message as $field => $fieldErrors) + { + foreach($fieldErrors as $error) + { + if($error) dao::$errors[$field][] = $error; + } + } + } + } + if(!$reponse) dao::$errors[] = false; } } diff --git a/module/gitlab/view/createproject.html.php b/module/gitlab/view/createproject.html.php index ce15a9dd0e..f9cd5212f1 100644 --- a/module/gitlab/view/createproject.html.php +++ b/module/gitlab/view/createproject.html.php @@ -11,7 +11,6 @@ */ ?> -
diff --git a/module/gitlab/view/createuser.html.php b/module/gitlab/view/createuser.html.php index f2a09473fa..611336a970 100644 --- a/module/gitlab/view/createuser.html.php +++ b/module/gitlab/view/createuser.html.php @@ -11,7 +11,6 @@ */ ?> -
diff --git a/module/gitlab/view/editproject.html.php b/module/gitlab/view/editproject.html.php index 6bc13f3a5c..6adc09d24f 100644 --- a/module/gitlab/view/editproject.html.php +++ b/module/gitlab/view/editproject.html.php @@ -11,7 +11,6 @@ */ ?> -
diff --git a/module/gitlab/view/edituser.html.php b/module/gitlab/view/edituser.html.php new file mode 100644 index 0000000000..4fe69e3880 --- /dev/null +++ b/module/gitlab/view/edituser.html.php @@ -0,0 +1,115 @@ + + * @package gitlab + * @version $Id$ + * @link http://www.zentao.net + */ +?> + +
+
+
+
+

gitlab->user->edit;?>

+
+
+ id);?> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
gitlab->user->name;?>name, "class='form-control' placeholder='{$lang->gitlab->user->name}'");?>
gitlab->user->username;?>username, "class='form-control' placeholder='{$lang->gitlab->user->username}'");?>
gitlab->user->email;?>email, "class='form-control' placeholder='{$lang->gitlab->user->email}'");?>
gitlab->user->password;?>gitlab->user->password}'");?>
gitlab->user->passwordRepeat;?>gitlab->user->passwordRepeat}'");?>
gitlab->user->projectsLimit;?>projects_limit, "class='form-control' placeholder='{$lang->gitlab->user->projectsLimit}'");?>
gitlab->user->canCreateGroup;?> +
+ can_create_group) echo 'checked';?> /> +
+
gitlab->user->external;?> +
+ external) echo 'checked';?> /> +
+
gitlab->user->bind;?>
gitlab->user->avatar;?> +
+ $user->avatar_url, 'account'=>''), 50); ?> + + ', '', "class='btn-avatar' id='avatarUploadBtn' data-toggle='tooltip' data-container='body' data-placement='bottom' title='{$lang->gitlab->user->avatar}'");?> +
+
gitlab->user->skype;?>skype, "class='form-control' placeholder='{$lang->gitlab->user->skype}'");?>
gitlab->user->linkedin;?>linkedin, "class='form-control' placeholder='{$lang->gitlab->user->linkedin}'");?>
gitlab->user->twitter;?>twitter, "class='form-control' placeholder='{$lang->gitlab->user->twitter}'");?>
gitlab->user->websiteUrl;?>website_url, "class='form-control' placeholder='{$lang->gitlab->user->websiteUrl}'");?>
gitlab->user->note;?>note, "rows='10' class='form-control' placeholder='{$lang->gitlab->user->note}'");?>
+ + goback, '', 'class="btn btn-wide"');?> +
+
+
+
+
+