diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d272a7678..a72444f0a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -364,11 +364,13 @@ class UsersController < ApplicationController def show # 测试代码 - #login_gitlab("admin@local.host","19840410") + 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") + add_user_to_project("1","2",GitlabHelper::GUEST) + delete_user_from_project("1","2") # 测试结束 pre_count = 10 #limit diff --git a/app/helpers/gitlab_helper.rb b/app/helpers/gitlab_helper.rb index 1b2906523..20faf551c 100644 --- a/app/helpers/gitlab_helper.rb +++ b/app/helpers/gitlab_helper.rb @@ -12,6 +12,12 @@ module GitlabHelper REPO_IP_ADDRESS = "http://192.168.137.100" GITLAB_API = "/api/v3" + # 用户在项目中的权限级别 + GUEST = 10 + REPORTER = 20 + DEVELOPER = 30 + MASTER = 40 + def self.gitlab_token=(token) Thread.current[:gitlab_token] = token end @@ -116,6 +122,8 @@ module GitlabHelper end # 删除用户 + # user_id:用户在gitlab中的id + # add by nwb def delete_user(user_id) url = REPO_IP_ADDRESS + GITLAB_API + "/users/" + user_id uri = URI.parse(url) @@ -138,9 +146,56 @@ module GitlabHelper end # 给用户添加一个可操作的项目 - def add_project_to_user(project_name,user) + # 若用户已在该项目中,权限级别取新权限与现有权限的最大值 + # project_id:项目在gitlab中的id user_id:用户在gitlab中的id + # access_level为用户在项目中的权限级别,对应如下: + #GUEST = 10 + #REPORTER = 20 + #DEVELOPER = 30 + #MASTER = 40 + # add by nwb + def add_user_to_project(project_id,user_id,access_level) + url = REPO_IP_ADDRESS + GITLAB_API + "/projects/" + project_id +"/members" + uri = URI.parse(url) + data = {id:project_id,user_id:user_id,access_level:access_level, private_token:GitlabHelper.gitlab_token} + begin + res = Net::HTTP.post_form(uri, data) + if res.code == '201' + temp = ActiveSupport::JSON.decode(res.body) + return true + else + return false + end + rescue =>err + return false + end end + # 从项目中删除用户 + # project_id:项目在gitlab中的ids user_id:用户在gitlab中的id + # add by nwb + def delete_user_from_project(project_id,user_id) + url = REPO_IP_ADDRESS + GITLAB_API + "/projects/" + project_id +"/members/" + user_id + uri = URI.parse(url) + data = {id:project_id,user_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) + return true + else + return false + end + rescue =>err + return false + end + end + + # 发送post消息 + # add by nwb def post(url, params) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) @@ -163,7 +218,8 @@ module GitlabHelper end end - + # 发送get消息 + # add by nwb def get(url, params) uri = URI.parse(url) uri.query = URI.encode_www_form(params)