diff --git a/Gemfile b/Gemfile index 660a7ff49..cd2607a4f 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ unless RUBY_PLATFORM =~ /w32/ gem 'iconv' end +gem 'grack', path:'./lib/grack' gem 'rest-client' gem "mysql2", "= 0.3.18" gem 'redis-rails' diff --git a/config/configuration.yml b/config/configuration.yml index 390754a87..4c786ad28 100644 --- a/config/configuration.yml +++ b/config/configuration.yml @@ -197,9 +197,12 @@ default: #max_concurrent_ajax_uploads: 2 #pic_types: "bmp,jpeg,jpg,png,gif" + repository_root_path: '/Users/guange/repository' + # specific configuration options for production environment # that overrides the default ones production: + repository_root_path: '/home/pdl/redmine-2.3.2-0/apache2/htdocs' cookie_domain: ".trustie.net" rmagick_font_path: /usr/share/fonts/ipa-mincho/ipam.ttf email_delivery: diff --git a/config/routes.rb b/config/routes.rb index b5a244345..16d8da882 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,6 +28,9 @@ RedmineApp::Application.routes.draw do mount Mobile::API => '/api' + # Enable Grack support + mount Trustie::Grack.new, at: '/', constraints: lambda { |request| /[-\/\w\.]+\.git\//.match(request.path_info) }, via: [:get, :post] + resources :homework_users resources :no_uses delete 'no_uses', :to => 'no_uses#delete' diff --git a/lib/grack b/lib/grack new file mode 160000 index 000000000..949be9116 --- /dev/null +++ b/lib/grack @@ -0,0 +1 @@ +Subproject commit 949be9116a76b50a6f86baf507dec4c4d7fb34c6 diff --git a/lib/trustie.rb b/lib/trustie.rb index b6cec3c86..3636efd95 100644 --- a/lib/trustie.rb +++ b/lib/trustie.rb @@ -1,2 +1,3 @@ require 'trustie/utils' require 'trustie/utils/image' +require 'trustie/grack/grack' diff --git a/lib/trustie/grack/auth.rb b/lib/trustie/grack/auth.rb new file mode 100644 index 000000000..c27477be2 --- /dev/null +++ b/lib/trustie/grack/auth.rb @@ -0,0 +1,55 @@ +require 'rack/auth/basic' +require 'rack/auth/abstract/handler' +require 'rack/auth/abstract/request' + +module Grack + class Auth < Rack::Auth::Basic + def call(env) + @env = env + @request = Rack::Request.new(env) + @auth = Request.new(env) + + if not @auth.provided? + unauthorized + elsif not @auth.basic? + bad_request + else + result = if (access = valid?(@auth) and access == true) + @env['REMOTE_USER'] = @auth.username + @app.call(env) + else + if access == '404' + render_not_found + elsif access == '403' + #render_no_access + unauthorized + else + unauthorized + end + end + result + end + end# method call + + + def render_not_found + [404, {"Content-Type" => "text/plain"}, ["Not Found"]] + end + + def valid?(auth) + match = @request.path_info.match(/(\/.+\.git)\//) + if match + rep = Repository.where("root_url like ?", "%#{match[1]}") + return "404" if rep.empty? + username, password = auth.credentials + user, last_login_on = User.try_to_login(username, password) + return '403' unless user + if user.member_of?(rep.first.project) || user.admin? + return true + end + end + false + end + end# class Auth +end# module Grack + diff --git a/lib/trustie/grack/grack.rb b/lib/trustie/grack/grack.rb new file mode 100644 index 000000000..1dc4bdc0d --- /dev/null +++ b/lib/trustie/grack/grack.rb @@ -0,0 +1,18 @@ +require_relative 'auth' + +module Trustie + module Grack + + def self.new + Rack::Builder.new do + use ::Grack::Auth + run ::Grack::Server.new( + project_root: Redmine::Configuration['repository_root_path'] || "/home/pdl/redmine-2.3.2-0/apache2/htdocs", + upload_pack: true, + receive_pack:true + ) + end + end + + end +end