Handle not ready repos
[gitorious.git] / ldap-wizard / wizard.rb
blobd4f1fb00beba49f5660a82b1bb273072f3dee14c
1 require "pathname"
3 WIZARD_ROOT = File.dirname(__FILE__)
4 RAILS_ROOT = Pathname(WIZARD_ROOT + "/../")
5 ENV["BUNDLE_GEMFILE"] = (RAILS_ROOT + "Gemfile").to_s
7 $LOAD_PATH << WIZARD_ROOT + "/lib"
8 $LOAD_PATH << (RAILS_ROOT + "lib").to_s
9 $LOAD_PATH << (RAILS_ROOT + "app").to_s
11 require "bundler"
12 Bundler.setup(:default, :ldap_wizard)
14 require "net/ldap"
16 # Out libs
17 require "credentials"
18 require "ldap_configuration_presenter"
19 require "ldap_tester"
20 require "gitorious/configuration_reader"
21 require "auth_config_loader"
23 require "sinatra"
24 require "sinatra/reloader"
25 require "yaml"
26 require "gitorious/authentication"
27 require "gitorious/authentication/ldap_configurator"
28 require "gitorious/authentication/ldap_authentication"
29 require "makeup/markup"
31 set :run, true
32 set :port, 1337
33 set :bind, '0.0.0.0'
35 class ConfigurationError < StandardError
36 end
38 get "/" do
39   readme_file = Pathname(WIZARD_ROOT + "/README.md")
40   @readme = Makeup::Markup.new.render("README.md", readme_file.read)
41   erb :readme
42 end
44 get "/begin" do
45   begin
46     @configurator = AuthConfigLoader.load_auth_file
47     @credentials = Credentials.new
48     erb :wizard
49   rescue ConfigurationError => e
50     render_error e.message
51   end
52 end
54 post "/test" do
55   params["bind_user"] = {"username" => params[:bind_username], "password" => params[:bind_password]}
56   configuration = Gitorious::Authentication::LDAPConfigurator.new(params)
57   @configurator = LdapConfigurationPresenter.new(configuration)
58   @credentials = Credentials.from_params(params)
59   ldap_tester = LdapTester.new(configuration, @credentials)
60   @test_results = ldap_tester.execute
61   erb :wizard
62 end
64 # Curl'able interface. Do a HTTP POST with username and password parameters for a quick sanity check
65 post "/check" do
66   configurator = AuthConfigLoader.load_auth_file.config
67   credentials = Credentials.from_params(params)
68   tester = LdapTester.new(configurator, credentials)
69   if tester.success?
70     status 200
71     body "Authentication succeeded"
72   else
73     status 403
74     body "Authentication failed"
75   end
76 end
78 helpers do
79   def field(obj, name, description, help_text=nil, options=nil)
80     value = obj.public_send(name.to_sym)
81     result = "<div class=\"control-group\">"
82     result << "<label class=\"control-label\">#{description}</label>\n"
83     result << "<div class=\"controls\">"
84     if options
85       result << select_field(name, value.to_s, options)
86     else
87       result << text_field(name, value)
88     end
89     result << "</div>"
90     result << "<span class=\"help-block\">#{help_text}</span>" if help_text
91     result << "</div>"
92     result
93   end
95   def text_field(name, value)
96     field_type = (name == :password) ? "password" : "text"
97     "<input type =\"#{field_type}\" name=\"#{name}\" value=\"#{value}\">"
98   end
100   def select_field(name, value, options)
101     result = "<select name=\"#{name}\">"
102     options.each do |option|
103       selected = (option == value) ? " selected" : ""
104       result << "<option value=\"#{option}\" #{selected}>#{option}</option>"
105     end
106     result << "</select>"
107   end
114 def render_error(message)
115   @error = message
116   erb :readme