Author: tchemit Date: 2014-04-15 20:51:13 +0200 (Tue, 15 Apr 2014) New Revision: 431 Url: http://forge.nuiton.org/projects/jredmine/repository/revisions/431 Log: fixes #3151 Modified: trunk/ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java trunk/jredmine-client/src/main/redmine/jredmine-2.x/app/controllers/jredmine_controller.rb trunk/jredmine-client/src/main/redmine/jredmine-2.x/config/routes.rb trunk/pom.xml Property changes on: trunk ___________________________________________________________________ Modified: svn:ignore - target *.iml *.ipr *.iws + target *.iml *.ipr *.iws .idea Modified: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java 2014-03-02 14:43:10 UTC (rev 430) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java 2014-04-15 18:51:13 UTC (rev 431) @@ -106,12 +106,12 @@ client = new DefaultHttpClient(); clientContext = new BasicHttpContext(); - // Create a local instance of cookie store - CookieStore cookieStore = new BasicCookieStore(); +// // Create a local instance of cookie store +// CookieStore cookieStore = new BasicCookieStore(); +// +// // Bind custom cookie store to the local context +// clientContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); - // Bind custom cookie store to the local context - clientContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); - // Get server uri try { serverURI = configuration.getUrl().toURI(); @@ -167,28 +167,28 @@ throw e; } - if (!configuration.isAnonymous()) { +// if (!configuration.isAnonymous()) { +// +// // log in +// RedmineClientAuthConfiguration conf = +// getConfiguration().getAuthConfiguration(); +// RedmineRequest<Void> loginRequest; +// +// if (conf.isUseApiKey()) { +// +// // use api key to login +// loginRequest = RedmineRequestHelper.postAction("login", void.class). +// parameter("apiKey", conf.getApiKey()); +// } else { +// // use normal login / password +// loginRequest = RedmineRequestHelper.postAction("login", void.class). +// parameter("username", conf.getUsername()). +// parameter("password", conf.getPassword()); +// } +// +// executeRequest(loginRequest); +// } - // log in - RedmineClientAuthConfiguration conf = - getConfiguration().getAuthConfiguration(); - RedmineRequest<Void> loginRequest; - - if (conf.isUseApiKey()) { - - // use api key to login - loginRequest = RedmineRequestHelper.postAction("login", void.class). - parameter("apiKey", conf.getApiKey()); - } else { - // use normal login / password - loginRequest = RedmineRequestHelper.postAction("login", void.class). - parameter("username", conf.getUsername()). - parameter("password", conf.getPassword()); - } - - executeRequest(loginRequest); - } - open = true; } } @@ -231,9 +231,6 @@ } try { - if (!open) { - throw new IllegalStateException("the session is not opened"); - } HttpRequestBase gm = prepareRequest(request); @@ -256,23 +253,31 @@ try { - RedmineRequest<Void> request = - RedmineRequestHelper.action("logout", void.class); - executeRequest(request); - } catch (Exception e) { - throw new IOException( - "could not close client for reason " + - e.getMessage(), e); + client.getConnectionManager().shutdown(); } finally { - try { + open = false; + } - client.getConnectionManager().shutdown(); - } finally { - - open = false; - } - } +// try { +// +// RedmineRequest<Void> request = +// RedmineRequestHelper.action("logout", void.class); +// executeRequest(request); +// } catch (Exception e) { +// throw new IOException( +// "could not close client for reason " + +// e.getMessage(), e); +// } finally { +// +// try { +// +// client.getConnectionManager().shutdown(); +// } finally { +// +// open = false; +// } +// } } } Modified: trunk/jredmine-client/src/main/redmine/jredmine-2.x/app/controllers/jredmine_controller.rb =================================================================== --- trunk/jredmine-client/src/main/redmine/jredmine-2.x/app/controllers/jredmine_controller.rb 2014-03-02 14:43:10 UTC (rev 430) +++ trunk/jredmine-client/src/main/redmine/jredmine-2.x/app/controllers/jredmine_controller.rb 2014-04-15 18:51:13 UTC (rev 431) @@ -26,12 +26,15 @@ # post request before_filter :check_post, :only => [:add_version, :update_version, :next_version, :add_news, :add_attachment, :add_version_attachment, :add_issue_time, :update_issue_time] - - # find user (after login) - before_filter :find_user, :only => [:get_user_projects, :add_version, :update_version, :next_version, :add_news, :add_attachment, :add_version_attachment, :add_issue_time, :update_issue_time] - + + # try to get user from request (if found, then put it in User.current_user) + before_filter :try_find_current_user + + # check user exists + before_filter :check_current_user, :only => [:get_user_projects, :add_version, :update_version, :next_version, :add_news, :add_attachment, :add_version_attachment, :add_issue_time, :update_issue_time] + # find project and check permission - before_filter :find_project, :except => [:ping, :login, :logout, :get_projects, :get_user_projects, :get_issue_statuses, :get_issue_priorities, :get_enumeration] + before_filter :find_project, :except => [:ping, :get_projects, :get_user_projects, :get_issue_statuses, :get_issue_priorities, :get_enumeration] # find project's version before_filter :find_version, :only => [:get_version, :get_version_issues, :get_version_opened_issues, :get_version_attachments, :add_version_attachment] @@ -42,47 +45,14 @@ # find project's issue before_filter :find_issue, :only => [:get_issue_times, :add_issue_time, :update_time_issue] + # remove User.current + after_filter :remove_current_user + # ping service (just to test if service is reachable) def ping render :text => "ping", :status => 200 end - # Login request and validation - def login - if !request.post? - # Logout user : get method not possible - self.logged_user = nil - render_status 405, "POST method required" - else - # Authenticate user - #user = User.try_to_login(params[:username], params[:password]) - user = find_current_user - if user.nil? - # Invalid credentials - render_status 401, "Invalid credentials" - #elsif user.new_record? - # # Onthefly creation failed, - # render_status 401, "User not activated" - else - # Valid user - self.logged_user = user - # always generate a key and set autologin cookie - token = Token.create(:user => user, :action => 'autologin') - cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now } - render_status 200, "User logged in" - end - end - end - - # Log out current user and redirect to welcome page - def logout - cookies.delete :autologin - Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) if User.current.logged? - # user is no more connected - self.logged_user = nil - render_status 200, "User logged out" - end - # recuperation des projets def get_projects @projects = Project.find(:all) @@ -441,28 +411,26 @@ end end end - - def find_user - # Check the settings cache for each request - Setting.check_cache - # Find the current user - User.current = find_current_user - if !session[:user_id] + + # Check if User.current exists + def check_current_user + if User.current.nil? render_status 401, "Not connected" end end + # Clean current user + def remove_current_user + User.current = nil + end + # Returns the current user or nil if no user is logged in - def find_current_user + def try_find_current_user + # Check the settings cache for each request + Setting.check_cache + # Find the current user user = nil - if session[:user_id] - # existing session - user = (User.active.find(session[:user_id]) rescue nil) - elsif cookies[:autologin] - # auto-login feature - user = User.find_by_autologin_key(cookies[:autologin]) - end - if user.nil? && Setting.rest_api_enabled? + if Setting.rest_api_enabled? if (key = api_key_from_request) # Use API key user = User.find_by_api_key(key) @@ -477,7 +445,7 @@ # Last chance using request params user = User.try_to_login(params[:username], params[:password]) end - user + User.current = user end # Returns the API key present in the request Modified: trunk/jredmine-client/src/main/redmine/jredmine-2.x/config/routes.rb =================================================================== --- trunk/jredmine-client/src/main/redmine/jredmine-2.x/config/routes.rb 2014-03-02 14:43:10 UTC (rev 430) +++ trunk/jredmine-client/src/main/redmine/jredmine-2.x/config/routes.rb 2014-04-15 18:51:13 UTC (rev 431) @@ -25,10 +25,9 @@ RedmineApp::Application.routes.draw do # jredmine : actions with no project context - match 'jredmine/:action.xml', :to => 'jredmine#:action', :action => ['get_projects', 'get_user_projects', 'get_issue_statuses', 'get_issue_priorities', 'get_enumeration'], :format => 'xml', :via => ['get'] - match 'jredmine/:action.json', :to => 'jredmine#:action', :action => ['get_projects', 'get_user_projects', 'get_issue_statuses', 'get_issue_priorities', 'get_enumeration'], :format => 'json', :via => ['get'] - match 'jredmine/:action', :to => 'jredmine#:action', :action => ['login'], :format => 'xml', :via => ['post'] - match 'jredmine/:action', :to => 'jredmine#:action', :action => ['ping', 'logout', 'get_projects', 'get_user_projects', 'get_issue_statuses', 'get_issue_priorities', 'get_enumeration'], :format => 'xml', :via => ['get'] + match 'jredmine/:action.xml', :to => 'jredmine#:action', :action => ['get_projects', 'get_user_projects', 'get_issue_statuses', 'get_issue_priorities', 'get_enumeration'], :format => 'xml', :via => ['get'] + match 'jredmine/:action.json', :to => 'jredmine#:action', :action => ['get_projects', 'get_user_projects', 'get_issue_statuses', 'get_issue_priorities', 'get_enumeration'], :format => 'json', :via => ['get'] + match 'jredmine/:action', :to => 'jredmine#:action', :action => ['ping', 'get_projects', 'get_user_projects', 'get_issue_statuses', 'get_issue_priorities', 'get_enumeration'], :format => 'xml', :via => ['get'] # jredmine/action/:pid : actions with project context match 'jredmine/:action.xml/:pid', :to=> 'jredmine#:action', :pid => /.+/, :format => 'xml', :via => ['get', 'post'] Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2014-03-02 14:43:10 UTC (rev 430) +++ trunk/pom.xml 2014-04-15 18:51:13 UTC (rev 431) @@ -26,7 +26,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>mavenpom4redmineAndCentral</artifactId> - <version>5.0</version> + <version>5.0.5</version> </parent> <artifactId>jredmine</artifactId>