From 11d1cac40da69e01582a0bf691becd96c3db67c3 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 21 Oct 2015 17:13:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E9=80=9A=E8=BF=87gitlab?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/application_controller.rb | 3 +- app/controllers/repositories_controller.rb | 96 ++++++------------- app/models/repository.rb | 5 + .../settings/_new_repositories.html.erb | 12 --- app/views/repositories/_form.html.erb | 7 +- app/views/repositories/_form_create.html.erb | 3 - lib/trustie/gitlab/helper.rb | 37 +++++++ lib/trustie/gitlab/manage_member.rb | 30 ++++-- lib/trustie/gitlab/manage_user.rb | 37 +++++++ lib/trustie/gitlab/sync.rb | 29 ++---- 10 files changed, 140 insertions(+), 119 deletions(-) create mode 100644 lib/trustie/gitlab/manage_user.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 86c220bd2..a7b6ac114 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -38,7 +38,8 @@ class ApplicationController < ActionController::Base protect_from_forgery def handle_unverified_request super - cookies.delete(autologin_cookie_name) + raise(ActionController::InvalidAuthenticityToken) + # cookies.delete(autologin_cookie_name) end before_filter :find_first_page diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 442f6d22c..9d1284fde 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -115,74 +115,34 @@ update } def create - if params[:repository_scm].to_s == 'Gitlab' - # add by nwb - # 增加对gitlab版本库的支持 - attrs = pickup_extra_info - @repository = Repository.factory('Git') - @repository.safe_attributes = params[:repository] - if attrs[:attrs_extra].keys.any? - @repository.merge_extra_info(attrs[:attrs_extra]) - end - @repository.project = @project - if request.post? && @repository.save - redirect_to settings_project_url(@project, :tab => 'repositories') - else - redirect_to settings_project_url(@project, :tab => 'repositories') - end - else # 原逻辑 - ##xianbo - @root_path=RepositoriesHelper::ROOT_PATH - @repository_name=User.current.login.to_s+"/"+params[:repository][:identifier]+".git" - @project_path=@root_path+"htdocs/"+@repository_name - @repository_tag=params[:repository][:upassword] || params[:repository][:password] - @repo_name=User.current.login.to_s+"_"+params[:repository][:identifier] - logger.info "htpasswd -mb "+@root_path+"htdocs/user.passwd "+@repo_name+": "+@repository_tag - logger.info "the value of create repository"+@root_path+": "+@repository_name+": "+@project_path+": "+@repo_name - attrs = pickup_extra_info - if((@repository_tag!="")&¶ms[:repository_scm]=="Git") - params[:repository][:url]=@project_path - end - ###xianbo - @repository = Repository.factory(params[:repository_scm]) - @repository.safe_attributes = params[:repository] - if attrs[:attrs_extra].keys.any? - @repository.merge_extra_info(attrs[:attrs_extra]) - end - - @repository.project = @project - if request.post? && @repository.save - if(params[:repository_scm]=="Git") - system "htpasswd -mb "+@root_path+"htdocs/user.passwd "+@repo_name+" "+@repository_tag - system "echo -e '"+@repo_name+"-write:"+ - " "+@repo_name+"' >> "+@root_path+"htdocs/group.passwd" - system "mkdir "+@root_path+"htdocs/"+User.current.login.to_s - system "git init --bare "+@project_path - system "mv "+@project_path+"/hooks/post-update{.sample,}" - system "chmod a+x "+@project_path+"/hooks/post-update" - system "echo -e 'Allow from all \n Order Deny,Allow \n "+ - " \n"+ - "Require group "+@repo_name+"-write \n "+ - " \n ' >> "+ - @root_path+"htdocs/"+ @repository_name+"/.htaccess" - system "cd "+@project_path+" ;git update-server-info" - - File.open(@project_path+"/hooks/post-update", "w+") do |f| - f.write(HOOK_TEMPLATE) - end - - @repository.update_attributes(:login => User.current.login.to_s) - end - redirect_to settings_project_url(@project, :tab => 'repositories',:repository_error_message=>@repository.errors.full_messages) - else if(@repository_tag.blank?) - #render :action => 'newrepo', :layout =>'base_projects' - redirect_to settings_project_url(@project, :tab => 'repositories',:repository => "pswd_is_null",:repository_error_message=>@repository.errors.full_messages) - else - redirect_to settings_project_url(@project, :tab => 'repositories',:repository => @repository,:repository_error_message=>@repository.errors.full_messages) - end - end - + attrs = pickup_extra_info + @repository = Repository.factory('Git') + @repository.safe_attributes = params[:repository] + if attrs[:attrs_extra].keys.any? + @repository.merge_extra_info(attrs[:attrs_extra]) + end + @repository.project = @project + @repository.type = 'Repository::Gitlab' + @repository.url = @repository.identifier + if request.post? && @repository.save + g = ::Gitlab.client + gid = @project.owner.gid + gproject = g.create_project(@repository.identifier, + path: @repository.identifier, + description: @project.description, + wiki_enabled: false, + wall_enabled: false, + issues_enabled: false, + snippets_enabled: false, + public: false, + user_id: gid + ) + @project.gpid = gproject.id + @project.save! + redirect_to settings_project_url(@project, :tab => 'repositories') + else + redirect_to settings_project_url(@project, :tab => 'repositories',:repository_error_message=>@repository.errors.full_messages) end end @@ -266,7 +226,7 @@ update end end - unless @repository && @repository.type == 'Repository::Gitlab' + unless @repository.gitlab? # redirect_to to_gitlab_project_repository_path(@project, @repository) render :to_gitlab return diff --git a/app/models/repository.rb b/app/models/repository.rb index 5680f05a7..94b7905c6 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -57,6 +57,11 @@ class Repository < ActiveRecord::Base safe_attributes 'url', :if => lambda {|repository, user| repository.new_record?} + + def gitlab? + self.type == 'Repository::Gitlab' + end + def repo_create_validation unless Setting.enabled_scm.include?(self.class.name.demodulize) errors.add(:type, :invalid) diff --git a/app/views/projects/settings/_new_repositories.html.erb b/app/views/projects/settings/_new_repositories.html.erb index 0859e5e19..bfb87b16a 100644 --- a/app/views/projects/settings/_new_repositories.html.erb +++ b/app/views/projects/settings/_new_repositories.html.erb @@ -73,13 +73,6 @@ <%= l(:text_scm_command_not_available) %> <% end %> - - <% unless judge_main_repository(@project) %> -
  • - - <%= f.check_box :is_default, :label => "", :no_label => true %>

    -
  • - <% end %>
  • @@ -89,11 +82,6 @@ <%=l(:text_length_between,:min=>1,:max=>254)< <% end %>
  • -
  • - - <%= f.password_field :upassword, :label=> "", :no_label => true%> - <%= l(:label_upassword_info)%> -
  • <%=l(:button_save)%> diff --git a/app/views/repositories/_form.html.erb b/app/views/repositories/_form.html.erb index bc5a060db..8c169d7be 100644 --- a/app/views/repositories/_form.html.erb +++ b/app/views/repositories/_form.html.erb @@ -31,12 +31,7 @@ <%= f.text_field :login, :size => 30 %>

    -

    - <%= f.password_field :password, :size => 30, :name => 'ignore', - :value => ((@repository.new_record? || @repository.password.blank?) ? '' : ('x'*15)), - :onfocus => "this.value=''; this.name='repository[password]';", - :onchange => "this.name='repository[password]';" %> -

    +

    diff --git a/app/views/repositories/_form_create.html.erb b/app/views/repositories/_form_create.html.erb index d8c30143e..11dce9cd4 100644 --- a/app/views/repositories/_form_create.html.erb +++ b/app/views/repositories/_form_create.html.erb @@ -26,7 +26,6 @@ border:none <%= l(:text_scm_command_not_available) %> <% end %>

    -

    <%= f.check_box :is_default, :label => :field_repository_is_default %>

    @@ -36,8 +35,6 @@ border:none <%= l(:text_repository_identifier_info).html_safe %> <% end %>

    -

    <%= f.password_field :upassword, :required =>true, :label=> :field_password %> - <%= l(:label_upassword_info)%>

    <%= submit_tag(@repository.new_record? ? l(:button_create) : l(:button_save)) %> diff --git a/lib/trustie/gitlab/helper.rb b/lib/trustie/gitlab/helper.rb index e69de29bb..5ea4c13e1 100644 --- a/lib/trustie/gitlab/helper.rb +++ b/lib/trustie/gitlab/helper.rb @@ -0,0 +1,37 @@ +#coding=utf-8 + +module Trustie + module Gitlab + module Helper + def change_password(uid, en_pwd, salt) + options = {:encrypted_password=>en_pwd, :password_salt=>salt} + self.g.put("/users/ext/#{uid}", :body => options) + # g.edit_user(uid, :encrypted_password=>en_pwd, :password_salt=>salt) + end + + def add_user(user) + u = nil + begin + u = self.g.get("/users?search=#{user.mail}").first + unless u + u = self.g.create_user(user.mail, + user.hashed_password, + name: user.show_name, + username: user.login, + confirm: "true") + user.gid = u.id + end + change_password(u.id, user.hashed_password, user.salt) + rescue => e + puts e + end + return u + end + + def del_user(user) + ## gitlab unimplement + end + + end + end +end \ No newline at end of file diff --git a/lib/trustie/gitlab/manage_member.rb b/lib/trustie/gitlab/manage_member.rb index 8201d1759..d0f74ad5e 100644 --- a/lib/trustie/gitlab/manage_member.rb +++ b/lib/trustie/gitlab/manage_member.rb @@ -14,27 +14,41 @@ module Trustie end def change_gitlab_member - if self.member.project_id == 2 + if isGitlabProject? @g ||= ::Gitlab.client - @g.edit_team_member(11, self.member.user.gid, self.role.to_gitlab_role ) + @g.edit_team_member(project.gpid, self.member.user.gid, self.role.to_gitlab_role ) end end def add_gitlab_member - if self.member.project_id == 2 + if isGitlabProject? @g ||= ::Gitlab.client - @g.add_team_member(11, self.member.user.gid, self.role.to_gitlab_role ) + @g.add_team_member(project.gpid, self.member.user.gid, self.role.to_gitlab_role ) end end def delete_gitlab_member - if member.roles.count <=1 - if self.member.project_id == 2 - @g ||= ::Gitlab.client - @g.remove_team_member(11, self.member.user.gid) + if isGitlabProject? + if member.roles.count <=1 + @g ||= ::Gitlab.client + @g.remove_team_member(project.gpid, self.member.user.gid) end end end + + private + def project + self.member.project + end + + def repository + project.repository + end + + def isGitlabProject? + repository && repository.gitlab? + end + end end diff --git a/lib/trustie/gitlab/manage_user.rb b/lib/trustie/gitlab/manage_user.rb new file mode 100644 index 000000000..76528739c --- /dev/null +++ b/lib/trustie/gitlab/manage_user.rb @@ -0,0 +1,37 @@ +#coding=utf-8 +# +# +require_relative 'helper' +module Trustie + module Gitlab + module ManageUser + include Helper + + def self.included(base) + base.class_eval { + before_create :add_gitlab_user + before_destroy :delete_gitlab_user + before_save :change_gitlab_user + } + end + + def add_gitlab_user + add_user(self) + end + + def delete_gitlab_user + del_user(self) + end + + def change_gitlab_user + change_password(self.gid, self.hashed_password, self.salt) + end + + private + def g + @g ||= ::Gitlab.client + end + + end + end +end diff --git a/lib/trustie/gitlab/sync.rb b/lib/trustie/gitlab/sync.rb index 597e01a36..d941795ee 100644 --- a/lib/trustie/gitlab/sync.rb +++ b/lib/trustie/gitlab/sync.rb @@ -1,3 +1,7 @@ +#coding=utf-8 + +require_relative 'helper' + module Trustie module Gitlab module UserLevel @@ -10,33 +14,17 @@ module Trustie class Sync attr :g + include Helper def initialize @g = ::Gitlab.client end - def change_password(uid, en_pwd, salt) - options = {:encrypted_password=>en_pwd, :password_salt=>salt} - self.g.put("/users/ext/#{uid}", :body => options) - # g.edit_user(uid, :encrypted_password=>en_pwd, :password_salt=>salt) - end - def sync_user(user) - begin - u = self.g.get("/users?search=#{user.mail}").first - unless u - u = self.g.create_user(user.mail, user.hashed_password, name: user.show_name, username: user.login, confirm: "true") - user.gid = u.id - user.save! - puts "create user #{user.login}" - end - change_password(u.id, user.hashed_password, user.salt) - rescue => e - puts e - end + u = add_user(user) + user.save! if u end - def sync_project(project, opt={}) gid = project.owner.gid raise "unknow gid" unless gid @@ -48,7 +36,6 @@ module Trustie if opt[:password] import_url.sub('@', ":#{opt[:password]}@") end - # import url http://xianbo_trustie2:1234@repository.trustie.net/xianbo/trustie2.git # can use password @@ -80,7 +67,7 @@ module Trustie def remove_project end - end + end end \ No newline at end of file