OSDN Git Service

UAC support
authorAkihiro Ono <a-ono@users.sourceforge.jp>
Thu, 3 Feb 2011 15:03:33 +0000 (00:03 +0900)
committerAkihiro Ono <a-ono@users.sourceforge.jp>
Thu, 3 Feb 2011 15:03:33 +0000 (00:03 +0900)
script/install
script/lib/shortcut.rb [deleted file]
script/lib/windows.rb [new file with mode: 0644]
script/lib/windows/authorization.rb [new file with mode: 0644]
script/lib/windows/shortcut.rb [new file with mode: 0644]
script/service.bat
script/setenv.bat
script/uninstall

index 86fe5ab..8d5b0f1 100644 (file)
@@ -9,6 +9,8 @@ Dir.glob(ScriptDir + "/lib/*.rb").each {|rb|
        require rb\r
 }\r
 \r
+Windows::Authorization.runas_admin\r
+\r
 home = File.expand_path(ENV["PACKAGE_HOME"])\r
 host = Socket.gethostbyname(Socket.gethostname).first\r
 options = {\r
@@ -219,4 +221,4 @@ Template.install(options)
 OpenDS.install(options) unless options[:ldap_setting]\r
 Redmine.install(options)\r
 system(%Q["#{ScriptDir}/service.bat" install])\r
-Shortcut.install(options)\r
+Windows::Shortcut.install(options)\r
diff --git a/script/lib/shortcut.rb b/script/lib/shortcut.rb
deleted file mode 100644 (file)
index a751012..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-require 'win32ole'\r
-require 'win32/shortcut'\r
-\r
-module Shortcut\r
-       MENU_DIR = File.join(WIN32OLE.new("WScript.Shell").SpecialFolders("AllUsersPrograms"), "RedmineLE")\r
-\r
-       def self.install(options)\r
-               service_dir = File.join(MENU_DIR, "service")\r
-               Dir.mkdir(MENU_DIR) unless File.directory?(MENU_DIR)\r
-               Dir.mkdir(service_dir) unless File.directory?(service_dir)\r
-\r
-               Win32::Shortcut.new(File.join(MENU_DIR, "home.url")) {|s|\r
-                       port = options[:apache_port]\r
-                       port = port.to_s == "80" ? "" : ":#{port}"\r
-                       s.path = "http://localhost#{port}/"\r
-               }\r
-               Win32::Shortcut.new(File.join(MENU_DIR, "uninstall.lnk")) {|s|\r
-                       s.path = "javaw"\r
-                       s.arguments = %Q[-jar "#{File.join(options[:home], "Uninstaller/uninstaller.jar")}"]\r
-               }\r
-\r
-               %w[start stop restart status install uninstall].each {|command|\r
-                       Win32::Shortcut.new(File.join(service_dir, "#{command}.lnk")) {|s|\r
-                               s.path = File.join(options[:home], "script/service.bat")\r
-                               s.arguments = "#{command} & pause"\r
-                       }\r
-               }\r
-       end\r
-\r
-       def self.uninstall\r
-               FileUtils.rm_rf(MENU_DIR)\r
-       end\r
-end\r
diff --git a/script/lib/windows.rb b/script/lib/windows.rb
new file mode 100644 (file)
index 0000000..4cccf97
--- /dev/null
@@ -0,0 +1,2 @@
+require 'windows/authorization'
+require 'windows/shortcut'
diff --git a/script/lib/windows/authorization.rb b/script/lib/windows/authorization.rb
new file mode 100644 (file)
index 0000000..f0ed5c2
--- /dev/null
@@ -0,0 +1,27 @@
+require 'Win32API'
+require 'win32ole'
+
+module Windows
+  module Authorization
+    def self.admin?
+      Win32API.new("shell32", "IsUserAnAdmin", nil, "i").call != 0
+    end
+
+    def self.runas_admin
+      return if admin?
+
+      buf = [148,0,0,0,0,"\0"*128].pack("LLLLLa128")
+      Win32API.new("kernel32", "GetVersionEx", "p", "i").call(buf)
+      (major, minor) = buf[4,12].unpack("LL")
+
+      # after vista
+      if major >= 6
+        args = [$0, ARGV.join(" "), Dir.pwd, "runas"]
+        WIN32OLE.new("Shell.Application").ShellExecute(*args)
+        exit
+      else
+        raise "it requires administrator authorization."
+      end
+    end
+  end
+end
diff --git a/script/lib/windows/shortcut.rb b/script/lib/windows/shortcut.rb
new file mode 100644 (file)
index 0000000..8101d31
--- /dev/null
@@ -0,0 +1,35 @@
+require 'win32ole'\r
+require 'win32/shortcut'\r
+\r
+module Windows\r
+  module Shortcut\r
+    MENU_DIR = File.join(WIN32OLE.new("WScript.Shell").SpecialFolders("AllUsersPrograms"), "RedmineLE")\r
+\r
+    def self.install(options)\r
+      service_dir = File.join(MENU_DIR, "service")\r
+      Dir.mkdir(MENU_DIR) unless File.directory?(MENU_DIR)\r
+      Dir.mkdir(service_dir) unless File.directory?(service_dir)\r
+\r
+      Win32::Shortcut.new(File.join(MENU_DIR, "home.url")) {|s|\r
+        port = options[:apache_port]\r
+        port = port.to_s == "80" ? "" : ":#{port}"\r
+        s.path = "http://localhost#{port}/"\r
+      }\r
+      Win32::Shortcut.new(File.join(MENU_DIR, "uninstall.lnk")) {|s|\r
+        s.path = "javaw"\r
+        s.arguments = %Q[-jar "#{File.join(options[:home], "Uninstaller/uninstaller.jar")}"]\r
+      }\r
+\r
+      %w[start stop restart status install uninstall].each {|command|\r
+        Win32::Shortcut.new(File.join(service_dir, "#{command}.lnk")) {|s|\r
+          s.path = File.join(options[:home], "script/service.bat")\r
+          s.arguments = "#{command} pause"\r
+        }\r
+      }\r
+    end\r
+\r
+    def self.uninstall\r
+      FileUtils.rm_rf(MENU_DIR)\r
+    end\r
+  end\r
+end\r
index 64f2832..565be3b 100644 (file)
@@ -7,6 +7,7 @@ goto :end
 #!ruby\r
 require 'yaml'\r
 require 'win32/service'\r
+require 'windows/authorization'\r
 \r
 PACKAGE_HOME = File.expand_path(ENV["PACKAGE_HOME"])\r
 \r
@@ -164,8 +165,17 @@ EOT
        end\r
 end\r
 \r
-Service.usage unless command = ARGV.shift\r
+(command, pause) = ARGV[0..1]\r
+Service.usage unless command\r
+if %w[install uninstall start stop restart].include?(command)\r
+  Windows::Authorization.runas_admin\r
+end\r
 Service.send(command)\r
 \r
+if pause\r
+  print "Press any key to exit..."\r
+  STDIN.gets\r
+end\r
+\r
 __END__\r
 :end\r
index da4f28f..0001dc9 100644 (file)
@@ -5,3 +5,4 @@ set PATH=%PACKAGE_HOME%apache\bin;%PACKAGE_HOME%ruby\bin;%PACKAGE_HOME%sqlite;%P
 set APR_ICONV_PATH=%PACKAGE_HOME%apache\bin\iconv\r
 \r
 set RUBYOPT=rubygems\r
+set RUBYLIB=%PACKAGE_HOME%script\lib\r
index 65b8c57..2dcb47d 100644 (file)
@@ -1,7 +1,9 @@
 #!/usr/bin/env ruby\r
 \r
+require 'windows'\r
+\r
 ScriptDir = File.dirname(__FILE__)\r
-require ScriptDir + "/lib/shortcut.rb"\r
+Windows::Authorization.runas_admin\r
 \r
 system(%Q["#{ScriptDir}/service.bat" uninstall])\r
-Shortcut.uninstall\r
+Windows::Shortcut.uninstall\r