From 3d8571b59e2e1fa1325e12a90b3df4876deda3bf Mon Sep 17 00:00:00 2001 From: nwb Date: Wed, 16 Jul 2014 16:50:28 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=AE=9E=E7=8E=B0=E6=96=B0=E5=A2=9Egitlab?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=8E=A5=E5=8F=A3=202.=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=88=A0=E9=99=A4gitlab=E7=94=A8=E6=88=B7=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 2 ++ app/helpers/gitlab_helper.rb | 41 ++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2051eb4c9..d272a7678 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -367,6 +367,8 @@ class UsersController < ApplicationController #login_gitlab("admin@local.host","19840410") #create_project("testproject1") #create_project_for_user("testproject1","2") + #create_user("test","just test","123456","test@sina.com") + #delete_user("3") # 测试结束 pre_count = 10 #limit diff --git a/app/helpers/gitlab_helper.rb b/app/helpers/gitlab_helper.rb index ea7013c31..1b2906523 100644 --- a/app/helpers/gitlab_helper.rb +++ b/app/helpers/gitlab_helper.rb @@ -94,14 +94,47 @@ module GitlabHelper end # 创建用户 - # loginname:登录名称 username:用户姓名 - def create_user (loginname,username,password,email) - + # loginname:登录名称(版本库路径包含) name:用户姓名 + # add by nwb + def create_user (loginname,name,password,email) + url = REPO_IP_ADDRESS + GITLAB_API + "/users" + uri = URI.parse(url) + data = {email:email,password:password,username:loginname, name:name, private_token:GitlabHelper.gitlab_token} + begin + res = Net::HTTP.post_form(uri, data) + if res.code == '201' + temp = ActiveSupport::JSON.decode(res.body) + #新创建的gitlab用户编号(需保存) + user_id = temp['id'] + return true + else + return false + end + rescue =>err + return false + end end # 删除用户 def delete_user(user_id) - + url = REPO_IP_ADDRESS + GITLAB_API + "/users/" + user_id + uri = URI.parse(url) + data = {id:user_id,private_token:GitlabHelper.gitlab_token} + http = Net::HTTP.new uri.host, uri.port + begin + req = Net::HTTP::Delete.new(uri.request_uri) + req.form_data = data + res= http.start { |http| http.request req } + if res.code == '200' + temp = ActiveSupport::JSON.decode(res.body) + # 删除成功,对应更新trustie用户的gitlab用户编号 + return true + else + return false + end + rescue =>err + return false + end end # 给用户添加一个可操作的项目