##// END OF EJS Templates
Applied this fix http://dev.rubyonrails.org/ticket/4967 to solve namespaced models dependencies problem....
Jean-Philippe Lang -
r558:136a2a614b23
parent child
Show More
@@ -1,172 +1,176
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class ApplicationController < ActionController::Base
18 class ApplicationController < ActionController::Base
19 before_filter :check_if_login_required, :set_localization
19 before_filter :check_if_login_required, :set_localization
20 filter_parameter_logging :password
20 filter_parameter_logging :password
21
21
22 REDMINE_SUPPORTED_SCM.each do |scm|
23 require_dependency "repository/#{scm.underscore}"
24 end
25
22 def logged_in_user=(user)
26 def logged_in_user=(user)
23 @logged_in_user = user
27 @logged_in_user = user
24 session[:user_id] = (user ? user.id : nil)
28 session[:user_id] = (user ? user.id : nil)
25 end
29 end
26
30
27 def logged_in_user
31 def logged_in_user
28 if session[:user_id]
32 if session[:user_id]
29 @logged_in_user ||= User.find(session[:user_id])
33 @logged_in_user ||= User.find(session[:user_id])
30 else
34 else
31 nil
35 nil
32 end
36 end
33 end
37 end
34
38
35 # Returns the role that the logged in user has on the current project
39 # Returns the role that the logged in user has on the current project
36 # or nil if current user is not a member of the project
40 # or nil if current user is not a member of the project
37 def logged_in_user_membership
41 def logged_in_user_membership
38 @user_membership ||= logged_in_user.role_for_project(@project)
42 @user_membership ||= logged_in_user.role_for_project(@project)
39 end
43 end
40
44
41 # check if login is globally required to access the application
45 # check if login is globally required to access the application
42 def check_if_login_required
46 def check_if_login_required
43 # no check needed if user is already logged in
47 # no check needed if user is already logged in
44 return true if logged_in_user
48 return true if logged_in_user
45 # auto-login feature
49 # auto-login feature
46 autologin_key = cookies[:autologin]
50 autologin_key = cookies[:autologin]
47 if autologin_key && Setting.autologin?
51 if autologin_key && Setting.autologin?
48 self.logged_in_user = User.find_by_autologin_key(autologin_key)
52 self.logged_in_user = User.find_by_autologin_key(autologin_key)
49 end
53 end
50 require_login if Setting.login_required?
54 require_login if Setting.login_required?
51 end
55 end
52
56
53 def set_localization
57 def set_localization
54 lang = begin
58 lang = begin
55 if self.logged_in_user and self.logged_in_user.language and !self.logged_in_user.language.empty? and GLoc.valid_languages.include? self.logged_in_user.language.to_sym
59 if self.logged_in_user and self.logged_in_user.language and !self.logged_in_user.language.empty? and GLoc.valid_languages.include? self.logged_in_user.language.to_sym
56 self.logged_in_user.language
60 self.logged_in_user.language
57 elsif request.env['HTTP_ACCEPT_LANGUAGE']
61 elsif request.env['HTTP_ACCEPT_LANGUAGE']
58 accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.split('-').first
62 accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.split('-').first
59 if accept_lang and !accept_lang.empty? and GLoc.valid_languages.include? accept_lang.to_sym
63 if accept_lang and !accept_lang.empty? and GLoc.valid_languages.include? accept_lang.to_sym
60 accept_lang
64 accept_lang
61 end
65 end
62 end
66 end
63 rescue
67 rescue
64 nil
68 nil
65 end || Setting.default_language
69 end || Setting.default_language
66 set_language_if_valid(lang)
70 set_language_if_valid(lang)
67 end
71 end
68
72
69 def require_login
73 def require_login
70 unless self.logged_in_user
74 unless self.logged_in_user
71 store_location
75 store_location
72 redirect_to :controller => "account", :action => "login"
76 redirect_to :controller => "account", :action => "login"
73 return false
77 return false
74 end
78 end
75 true
79 true
76 end
80 end
77
81
78 def require_admin
82 def require_admin
79 return unless require_login
83 return unless require_login
80 unless self.logged_in_user.admin?
84 unless self.logged_in_user.admin?
81 render_403
85 render_403
82 return false
86 return false
83 end
87 end
84 true
88 true
85 end
89 end
86
90
87 # authorizes the user for the requested action.
91 # authorizes the user for the requested action.
88 def authorize(ctrl = params[:controller], action = params[:action])
92 def authorize(ctrl = params[:controller], action = params[:action])
89 unless @project.active?
93 unless @project.active?
90 @project = nil
94 @project = nil
91 render_404
95 render_404
92 return false
96 return false
93 end
97 end
94 # check if action is allowed on public projects
98 # check if action is allowed on public projects
95 if @project.is_public? and Permission.allowed_to_public "%s/%s" % [ ctrl, action ]
99 if @project.is_public? and Permission.allowed_to_public "%s/%s" % [ ctrl, action ]
96 return true
100 return true
97 end
101 end
98 # if action is not public, force login
102 # if action is not public, force login
99 return unless require_login
103 return unless require_login
100 # admin is always authorized
104 # admin is always authorized
101 return true if self.logged_in_user.admin?
105 return true if self.logged_in_user.admin?
102 # if not admin, check membership permission
106 # if not admin, check membership permission
103 if logged_in_user_membership and Permission.allowed_to_role( "%s/%s" % [ ctrl, action ], logged_in_user_membership )
107 if logged_in_user_membership and Permission.allowed_to_role( "%s/%s" % [ ctrl, action ], logged_in_user_membership )
104 return true
108 return true
105 end
109 end
106 render_403
110 render_403
107 false
111 false
108 end
112 end
109
113
110 # make sure that the user is a member of the project (or admin) if project is private
114 # make sure that the user is a member of the project (or admin) if project is private
111 # used as a before_filter for actions that do not require any particular permission on the project
115 # used as a before_filter for actions that do not require any particular permission on the project
112 def check_project_privacy
116 def check_project_privacy
113 unless @project.active?
117 unless @project.active?
114 @project = nil
118 @project = nil
115 render_404
119 render_404
116 return false
120 return false
117 end
121 end
118 return true if @project.is_public?
122 return true if @project.is_public?
119 return false unless logged_in_user
123 return false unless logged_in_user
120 return true if logged_in_user.admin? || logged_in_user_membership
124 return true if logged_in_user.admin? || logged_in_user_membership
121 render_403
125 render_403
122 false
126 false
123 end
127 end
124
128
125 # store current uri in session.
129 # store current uri in session.
126 # return to this location by calling redirect_back_or_default
130 # return to this location by calling redirect_back_or_default
127 def store_location
131 def store_location
128 session[:return_to_params] = params
132 session[:return_to_params] = params
129 end
133 end
130
134
131 # move to the last store_location call or to the passed default one
135 # move to the last store_location call or to the passed default one
132 def redirect_back_or_default(default)
136 def redirect_back_or_default(default)
133 if session[:return_to_params].nil?
137 if session[:return_to_params].nil?
134 redirect_to default
138 redirect_to default
135 else
139 else
136 redirect_to session[:return_to_params]
140 redirect_to session[:return_to_params]
137 session[:return_to_params] = nil
141 session[:return_to_params] = nil
138 end
142 end
139 end
143 end
140
144
141 def render_403
145 def render_403
142 @html_title = "403"
146 @html_title = "403"
143 @project = nil
147 @project = nil
144 render :template => "common/403", :layout => true, :status => 403
148 render :template => "common/403", :layout => true, :status => 403
145 return false
149 return false
146 end
150 end
147
151
148 def render_404
152 def render_404
149 @html_title = "404"
153 @html_title = "404"
150 render :template => "common/404", :layout => true, :status => 404
154 render :template => "common/404", :layout => true, :status => 404
151 return false
155 return false
152 end
156 end
153
157
154 # qvalues http header parser
158 # qvalues http header parser
155 # code taken from webrick
159 # code taken from webrick
156 def parse_qvalues(value)
160 def parse_qvalues(value)
157 tmp = []
161 tmp = []
158 if value
162 if value
159 parts = value.split(/,\s*/)
163 parts = value.split(/,\s*/)
160 parts.each {|part|
164 parts.each {|part|
161 if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
165 if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
162 val = m[1]
166 val = m[1]
163 q = (m[2] or 1).to_f
167 q = (m[2] or 1).to_f
164 tmp.push([val, q])
168 tmp.push([val, q])
165 end
169 end
166 }
170 }
167 tmp = tmp.sort_by{|val, q| -q}
171 tmp = tmp.sort_by{|val, q| -q}
168 tmp.collect!{|val, q| val}
172 tmp.collect!{|val, q| val}
169 end
173 end
170 return tmp
174 return tmp
171 end
175 end
172 end No newline at end of file
176 end
General Comments 0
You need to be logged in to leave comments. Login now