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