@@ -0,0 +1,2 | |||
|
1 | <p><%= l(:notice_account_activated) %></p> | |
|
2 | <p><%= l(:label_login) %>: <%= link_to @login_url, @login_url %></p> |
@@ -1,104 +1,108 | |||
|
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 UsersController < ApplicationController |
|
19 | 19 | before_filter :require_admin |
|
20 | 20 | |
|
21 | 21 | helper :sort |
|
22 | 22 | include SortHelper |
|
23 | 23 | helper :custom_fields |
|
24 | 24 | include CustomFieldsHelper |
|
25 | 25 | |
|
26 | 26 | def index |
|
27 | 27 | list |
|
28 | 28 | render :action => 'list' unless request.xhr? |
|
29 | 29 | end |
|
30 | 30 | |
|
31 | 31 | def list |
|
32 | 32 | sort_init 'login', 'asc' |
|
33 | 33 | sort_update %w(login firstname lastname mail admin created_on last_login_on) |
|
34 | 34 | |
|
35 | 35 | @status = params[:status] ? params[:status].to_i : 1 |
|
36 | 36 | c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) |
|
37 | 37 | |
|
38 | 38 | unless params[:name].blank? |
|
39 | 39 | name = "%#{params[:name].strip.downcase}%" |
|
40 | 40 | c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ?", name, name, name] |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | @user_count = User.count(:conditions => c.conditions) |
|
44 | 44 | @user_pages = Paginator.new self, @user_count, |
|
45 | 45 | per_page_option, |
|
46 | 46 | params['page'] |
|
47 | 47 | @users = User.find :all,:order => sort_clause, |
|
48 | 48 | :conditions => c.conditions, |
|
49 | 49 | :limit => @user_pages.items_per_page, |
|
50 | 50 | :offset => @user_pages.current.offset |
|
51 | 51 | |
|
52 | 52 | render :action => "list", :layout => false if request.xhr? |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def add |
|
56 | 56 | if request.get? |
|
57 | 57 | @user = User.new(:language => Setting.default_language) |
|
58 | 58 | else |
|
59 | 59 | @user = User.new(params[:user]) |
|
60 | 60 | @user.admin = params[:user][:admin] || false |
|
61 | 61 | @user.login = params[:user][:login] |
|
62 | 62 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id |
|
63 | 63 | if @user.save |
|
64 | 64 | Mailer.deliver_account_information(@user, params[:password]) if params[:send_information] |
|
65 | 65 | flash[:notice] = l(:notice_successful_create) |
|
66 | 66 | redirect_to :action => 'list' |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | @auth_sources = AuthSource.find(:all) |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | def edit |
|
73 | 73 | @user = User.find(params[:id]) |
|
74 | 74 | if request.post? |
|
75 | 75 | @user.admin = params[:user][:admin] if params[:user][:admin] |
|
76 | 76 | @user.login = params[:user][:login] if params[:user][:login] |
|
77 | 77 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless params[:password].nil? or params[:password].empty? or @user.auth_source_id |
|
78 |
|
|
|
78 | @user.attributes = params[:user] | |
|
79 | # Was the account actived ? (do it before User#save clears the change) | |
|
80 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) | |
|
81 | if @user.save | |
|
82 | Mailer.deliver_account_activated(@user) if was_activated | |
|
79 | 83 | flash[:notice] = l(:notice_successful_update) |
|
80 | 84 | # Give a string to redirect_to otherwise it would use status param as the response code |
|
81 | 85 | redirect_to(url_for(:action => 'list', :status => params[:status], :page => params[:page])) |
|
82 | 86 | end |
|
83 | 87 | end |
|
84 | 88 | @auth_sources = AuthSource.find(:all) |
|
85 | 89 | @roles = Role.find_all_givable |
|
86 | 90 | @projects = Project.active.find(:all, :order => 'lft') |
|
87 | 91 | @membership ||= Member.new |
|
88 | 92 | @memberships = @user.memberships |
|
89 | 93 | end |
|
90 | 94 | |
|
91 | 95 | def edit_membership |
|
92 | 96 | @user = User.find(params[:id]) |
|
93 | 97 | @membership = params[:membership_id] ? Member.find(params[:membership_id]) : Member.new(:user => @user) |
|
94 | 98 | @membership.attributes = params[:membership] |
|
95 | 99 | @membership.save if request.post? |
|
96 | 100 | redirect_to :action => 'edit', :id => @user, :tab => 'memberships' |
|
97 | 101 | end |
|
98 | 102 | |
|
99 | 103 | def destroy_membership |
|
100 | 104 | @user = User.find(params[:id]) |
|
101 | 105 | Member.find(params[:membership_id]).destroy if request.post? |
|
102 | 106 | redirect_to :action => 'edit', :id => @user, :tab => 'memberships' |
|
103 | 107 | end |
|
104 | 108 | end |
@@ -1,297 +1,306 | |||
|
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 Mailer < ActionMailer::Base |
|
19 | 19 | helper :application |
|
20 | 20 | helper :issues |
|
21 | 21 | helper :custom_fields |
|
22 | 22 | |
|
23 | 23 | include ActionController::UrlWriter |
|
24 | 24 | |
|
25 | 25 | def issue_add(issue) |
|
26 | 26 | redmine_headers 'Project' => issue.project.identifier, |
|
27 | 27 | 'Issue-Id' => issue.id, |
|
28 | 28 | 'Issue-Author' => issue.author.login |
|
29 | 29 | redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to |
|
30 | 30 | message_id issue |
|
31 | 31 | recipients issue.recipients |
|
32 | 32 | cc(issue.watcher_recipients - @recipients) |
|
33 | 33 | subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}" |
|
34 | 34 | body :issue => issue, |
|
35 | 35 | :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue) |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | def issue_edit(journal) |
|
39 | 39 | issue = journal.journalized |
|
40 | 40 | redmine_headers 'Project' => issue.project.identifier, |
|
41 | 41 | 'Issue-Id' => issue.id, |
|
42 | 42 | 'Issue-Author' => issue.author.login |
|
43 | 43 | redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to |
|
44 | 44 | message_id journal |
|
45 | 45 | references issue |
|
46 | 46 | @author = journal.user |
|
47 | 47 | recipients issue.recipients |
|
48 | 48 | # Watchers in cc |
|
49 | 49 | cc(issue.watcher_recipients - @recipients) |
|
50 | 50 | s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] " |
|
51 | 51 | s << "(#{issue.status.name}) " if journal.new_value_for('status_id') |
|
52 | 52 | s << issue.subject |
|
53 | 53 | subject s |
|
54 | 54 | body :issue => issue, |
|
55 | 55 | :journal => journal, |
|
56 | 56 | :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue) |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | def reminder(user, issues, days) |
|
60 | 60 | set_language_if_valid user.language |
|
61 | 61 | recipients user.mail |
|
62 | 62 | subject l(:mail_subject_reminder, issues.size) |
|
63 | 63 | body :issues => issues, |
|
64 | 64 | :days => days, |
|
65 | 65 | :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort_key => 'due_date', :sort_order => 'asc') |
|
66 | 66 | end |
|
67 | 67 | |
|
68 | 68 | def document_added(document) |
|
69 | 69 | redmine_headers 'Project' => document.project.identifier |
|
70 | 70 | recipients document.project.recipients |
|
71 | 71 | subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}" |
|
72 | 72 | body :document => document, |
|
73 | 73 | :document_url => url_for(:controller => 'documents', :action => 'show', :id => document) |
|
74 | 74 | end |
|
75 | 75 | |
|
76 | 76 | def attachments_added(attachments) |
|
77 | 77 | container = attachments.first.container |
|
78 | 78 | added_to = '' |
|
79 | 79 | added_to_url = '' |
|
80 | 80 | case container.class.name |
|
81 | 81 | when 'Project' |
|
82 | 82 | added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container) |
|
83 | 83 | added_to = "#{l(:label_project)}: #{container}" |
|
84 | 84 | when 'Version' |
|
85 | 85 | added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id) |
|
86 | 86 | added_to = "#{l(:label_version)}: #{container.name}" |
|
87 | 87 | when 'Document' |
|
88 | 88 | added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) |
|
89 | 89 | added_to = "#{l(:label_document)}: #{container.title}" |
|
90 | 90 | end |
|
91 | 91 | redmine_headers 'Project' => container.project.identifier |
|
92 | 92 | recipients container.project.recipients |
|
93 | 93 | subject "[#{container.project.name}] #{l(:label_attachment_new)}" |
|
94 | 94 | body :attachments => attachments, |
|
95 | 95 | :added_to => added_to, |
|
96 | 96 | :added_to_url => added_to_url |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | def news_added(news) |
|
100 | 100 | redmine_headers 'Project' => news.project.identifier |
|
101 | 101 | message_id news |
|
102 | 102 | recipients news.project.recipients |
|
103 | 103 | subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}" |
|
104 | 104 | body :news => news, |
|
105 | 105 | :news_url => url_for(:controller => 'news', :action => 'show', :id => news) |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | 108 | def message_posted(message, recipients) |
|
109 | 109 | redmine_headers 'Project' => message.project.identifier, |
|
110 | 110 | 'Topic-Id' => (message.parent_id || message.id) |
|
111 | 111 | message_id message |
|
112 | 112 | references message.parent unless message.parent.nil? |
|
113 | 113 | recipients(recipients) |
|
114 | 114 | subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}" |
|
115 | 115 | body :message => message, |
|
116 | 116 | :message_url => url_for(:controller => 'messages', :action => 'show', :board_id => message.board_id, :id => message.root) |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | def account_information(user, password) |
|
120 | 120 | set_language_if_valid user.language |
|
121 | 121 | recipients user.mail |
|
122 | 122 | subject l(:mail_subject_register, Setting.app_title) |
|
123 | 123 | body :user => user, |
|
124 | 124 | :password => password, |
|
125 | 125 | :login_url => url_for(:controller => 'account', :action => 'login') |
|
126 | 126 | end |
|
127 | 127 | |
|
128 | 128 | def account_activation_request(user) |
|
129 | 129 | # Send the email to all active administrators |
|
130 | 130 | recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact |
|
131 | 131 | subject l(:mail_subject_account_activation_request, Setting.app_title) |
|
132 | 132 | body :user => user, |
|
133 | 133 | :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc') |
|
134 | 134 | end |
|
135 | 135 | |
|
136 | # A registered user's account was activated by an administrator | |
|
137 | def account_activated(user) | |
|
138 | set_language_if_valid user.language | |
|
139 | recipients user.mail | |
|
140 | subject l(:mail_subject_register, Setting.app_title) | |
|
141 | body :user => user, | |
|
142 | :login_url => url_for(:controller => 'account', :action => 'login') | |
|
143 | end | |
|
144 | ||
|
136 | 145 | def lost_password(token) |
|
137 | 146 | set_language_if_valid(token.user.language) |
|
138 | 147 | recipients token.user.mail |
|
139 | 148 | subject l(:mail_subject_lost_password, Setting.app_title) |
|
140 | 149 | body :token => token, |
|
141 | 150 | :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value) |
|
142 | 151 | end |
|
143 | 152 | |
|
144 | 153 | def register(token) |
|
145 | 154 | set_language_if_valid(token.user.language) |
|
146 | 155 | recipients token.user.mail |
|
147 | 156 | subject l(:mail_subject_register, Setting.app_title) |
|
148 | 157 | body :token => token, |
|
149 | 158 | :url => url_for(:controller => 'account', :action => 'activate', :token => token.value) |
|
150 | 159 | end |
|
151 | 160 | |
|
152 | 161 | def test(user) |
|
153 | 162 | set_language_if_valid(user.language) |
|
154 | 163 | recipients user.mail |
|
155 | 164 | subject 'Redmine test' |
|
156 | 165 | body :url => url_for(:controller => 'welcome') |
|
157 | 166 | end |
|
158 | 167 | |
|
159 | 168 | # Overrides default deliver! method to prevent from sending an email |
|
160 | 169 | # with no recipient, cc or bcc |
|
161 | 170 | def deliver!(mail = @mail) |
|
162 | 171 | return false if (recipients.nil? || recipients.empty?) && |
|
163 | 172 | (cc.nil? || cc.empty?) && |
|
164 | 173 | (bcc.nil? || bcc.empty?) |
|
165 | 174 | |
|
166 | 175 | # Set Message-Id and References |
|
167 | 176 | if @message_id_object |
|
168 | 177 | mail.message_id = self.class.message_id_for(@message_id_object) |
|
169 | 178 | end |
|
170 | 179 | if @references_objects |
|
171 | 180 | mail.references = @references_objects.collect {|o| self.class.message_id_for(o)} |
|
172 | 181 | end |
|
173 | 182 | super(mail) |
|
174 | 183 | end |
|
175 | 184 | |
|
176 | 185 | # Sends reminders to issue assignees |
|
177 | 186 | # Available options: |
|
178 | 187 | # * :days => how many days in the future to remind about (defaults to 7) |
|
179 | 188 | # * :tracker => id of tracker for filtering issues (defaults to all trackers) |
|
180 | 189 | # * :project => id or identifier of project to process (defaults to all projects) |
|
181 | 190 | def self.reminders(options={}) |
|
182 | 191 | days = options[:days] || 7 |
|
183 | 192 | project = options[:project] ? Project.find(options[:project]) : nil |
|
184 | 193 | tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil |
|
185 | 194 | |
|
186 | 195 | s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date] |
|
187 | 196 | s << "#{Issue.table_name}.assigned_to_id IS NOT NULL" |
|
188 | 197 | s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}" |
|
189 | 198 | s << "#{Issue.table_name}.project_id = #{project.id}" if project |
|
190 | 199 | s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker |
|
191 | 200 | |
|
192 | 201 | issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker], |
|
193 | 202 | :conditions => s.conditions |
|
194 | 203 | ).group_by(&:assigned_to) |
|
195 | 204 | issues_by_assignee.each do |assignee, issues| |
|
196 | 205 | deliver_reminder(assignee, issues, days) unless assignee.nil? |
|
197 | 206 | end |
|
198 | 207 | end |
|
199 | 208 | |
|
200 | 209 | private |
|
201 | 210 | def initialize_defaults(method_name) |
|
202 | 211 | super |
|
203 | 212 | set_language_if_valid Setting.default_language |
|
204 | 213 | from Setting.mail_from |
|
205 | 214 | |
|
206 | 215 | # URL options |
|
207 | 216 | h = Setting.host_name |
|
208 | 217 | h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank? |
|
209 | 218 | default_url_options[:host] = h |
|
210 | 219 | default_url_options[:protocol] = Setting.protocol |
|
211 | 220 | |
|
212 | 221 | # Common headers |
|
213 | 222 | headers 'X-Mailer' => 'Redmine', |
|
214 | 223 | 'X-Redmine-Host' => Setting.host_name, |
|
215 | 224 | 'X-Redmine-Site' => Setting.app_title |
|
216 | 225 | end |
|
217 | 226 | |
|
218 | 227 | # Appends a Redmine header field (name is prepended with 'X-Redmine-') |
|
219 | 228 | def redmine_headers(h) |
|
220 | 229 | h.each { |k,v| headers["X-Redmine-#{k}"] = v } |
|
221 | 230 | end |
|
222 | 231 | |
|
223 | 232 | # Overrides the create_mail method |
|
224 | 233 | def create_mail |
|
225 | 234 | # Removes the current user from the recipients and cc |
|
226 | 235 | # if he doesn't want to receive notifications about what he does |
|
227 | 236 | @author ||= User.current |
|
228 | 237 | if @author.pref[:no_self_notified] |
|
229 | 238 | recipients.delete(@author.mail) if recipients |
|
230 | 239 | cc.delete(@author.mail) if cc |
|
231 | 240 | end |
|
232 | 241 | # Blind carbon copy recipients |
|
233 | 242 | if Setting.bcc_recipients? |
|
234 | 243 | bcc([recipients, cc].flatten.compact.uniq) |
|
235 | 244 | recipients [] |
|
236 | 245 | cc [] |
|
237 | 246 | end |
|
238 | 247 | super |
|
239 | 248 | end |
|
240 | 249 | |
|
241 | 250 | # Renders a message with the corresponding layout |
|
242 | 251 | def render_message(method_name, body) |
|
243 | 252 | layout = method_name.to_s.match(%r{text\.html\.(rhtml|rxml)}) ? 'layout.text.html.rhtml' : 'layout.text.plain.rhtml' |
|
244 | 253 | body[:content_for_layout] = render(:file => method_name, :body => body) |
|
245 | 254 | ActionView::Base.new(template_root, body, self).render(:file => "mailer/#{layout}", :use_full_path => true) |
|
246 | 255 | end |
|
247 | 256 | |
|
248 | 257 | # for the case of plain text only |
|
249 | 258 | def body(*params) |
|
250 | 259 | value = super(*params) |
|
251 | 260 | if Setting.plain_text_mail? |
|
252 | 261 | templates = Dir.glob("#{template_path}/#{@template}.text.plain.{rhtml,erb}") |
|
253 | 262 | unless String === @body or templates.empty? |
|
254 | 263 | template = File.basename(templates.first) |
|
255 | 264 | @body[:content_for_layout] = render(:file => template, :body => @body) |
|
256 | 265 | @body = ActionView::Base.new(template_root, @body, self).render(:file => "mailer/layout.text.plain.rhtml", :use_full_path => true) |
|
257 | 266 | return @body |
|
258 | 267 | end |
|
259 | 268 | end |
|
260 | 269 | return value |
|
261 | 270 | end |
|
262 | 271 | |
|
263 | 272 | # Makes partial rendering work with Rails 1.2 (retro-compatibility) |
|
264 | 273 | def self.controller_path |
|
265 | 274 | '' |
|
266 | 275 | end unless respond_to?('controller_path') |
|
267 | 276 | |
|
268 | 277 | # Returns a predictable Message-Id for the given object |
|
269 | 278 | def self.message_id_for(object) |
|
270 | 279 | # id + timestamp should reduce the odds of a collision |
|
271 | 280 | # as far as we don't send multiple emails for the same object |
|
272 | 281 | hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{object.created_on.strftime("%Y%m%d%H%M%S")}" |
|
273 | 282 | host = Setting.mail_from.to_s.gsub(%r{^.*@}, '') |
|
274 | 283 | host = "#{::Socket.gethostname}.redmine" if host.empty? |
|
275 | 284 | "<#{hash}@#{host}>" |
|
276 | 285 | end |
|
277 | 286 | |
|
278 | 287 | private |
|
279 | 288 | |
|
280 | 289 | def message_id(object) |
|
281 | 290 | @message_id_object = object |
|
282 | 291 | end |
|
283 | 292 | |
|
284 | 293 | def references(object) |
|
285 | 294 | @references_objects ||= [] |
|
286 | 295 | @references_objects << object |
|
287 | 296 | end |
|
288 | 297 | end |
|
289 | 298 | |
|
290 | 299 | # Patch TMail so that message_id is not overwritten |
|
291 | 300 | module TMail |
|
292 | 301 | class Mail |
|
293 | 302 | def add_message_id( fqdn = nil ) |
|
294 | 303 | self.message_id ||= ::TMail::new_message_id(fqdn) |
|
295 | 304 | end |
|
296 | 305 | end |
|
297 | 306 | end |
@@ -1,142 +1,158 | |||
|
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 | require File.dirname(__FILE__) + '/../test_helper' |
|
19 | 19 | require 'users_controller' |
|
20 | 20 | |
|
21 | 21 | # Re-raise errors caught by the controller. |
|
22 | 22 | class UsersController; def rescue_action(e) raise e end; end |
|
23 | 23 | |
|
24 | 24 | class UsersControllerTest < Test::Unit::TestCase |
|
25 | 25 | fixtures :users, :projects, :members |
|
26 | 26 | |
|
27 | 27 | def setup |
|
28 | 28 | @controller = UsersController.new |
|
29 | 29 | @request = ActionController::TestRequest.new |
|
30 | 30 | @response = ActionController::TestResponse.new |
|
31 | 31 | User.current = nil |
|
32 | 32 | @request.session[:user_id] = 1 # admin |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | def test_index_routing |
|
36 | 36 | #TODO: unify with list |
|
37 | 37 | assert_generates( |
|
38 | 38 | '/users', |
|
39 | 39 | :controller => 'users', :action => 'index' |
|
40 | 40 | ) |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | def test_index |
|
44 | 44 | get :index |
|
45 | 45 | assert_response :success |
|
46 | 46 | assert_template 'list' |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | 49 | def test_list_routing |
|
50 | 50 | #TODO: rename action to index |
|
51 | 51 | assert_routing( |
|
52 | 52 | {:method => :get, :path => '/users'}, |
|
53 | 53 | :controller => 'users', :action => 'list' |
|
54 | 54 | ) |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | def test_list |
|
58 | 58 | get :list |
|
59 | 59 | assert_response :success |
|
60 | 60 | assert_template 'list' |
|
61 | 61 | assert_not_nil assigns(:users) |
|
62 | 62 | # active users only |
|
63 | 63 | assert_nil assigns(:users).detect {|u| !u.active?} |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def test_list_with_name_filter |
|
67 | 67 | get :list, :name => 'john' |
|
68 | 68 | assert_response :success |
|
69 | 69 | assert_template 'list' |
|
70 | 70 | users = assigns(:users) |
|
71 | 71 | assert_not_nil users |
|
72 | 72 | assert_equal 1, users.size |
|
73 | 73 | assert_equal 'John', users.first.firstname |
|
74 | 74 | end |
|
75 | 75 | |
|
76 | 76 | def test_add_routing |
|
77 | 77 | assert_routing( |
|
78 | 78 | {:method => :get, :path => '/users/new'}, |
|
79 | 79 | :controller => 'users', :action => 'add' |
|
80 | 80 | ) |
|
81 | 81 | assert_recognizes( |
|
82 | 82 | #TODO: remove this and replace with POST to collection, need to modify form |
|
83 | 83 | {:controller => 'users', :action => 'add'}, |
|
84 | 84 | {:method => :post, :path => '/users/new'} |
|
85 | 85 | ) |
|
86 | 86 | assert_recognizes( |
|
87 | 87 | {:controller => 'users', :action => 'add'}, |
|
88 | 88 | {:method => :post, :path => '/users'} |
|
89 | 89 | ) |
|
90 | 90 | end |
|
91 | 91 | |
|
92 | 92 | def test_edit_routing |
|
93 | 93 | assert_routing( |
|
94 | 94 | {:method => :get, :path => '/users/444/edit'}, |
|
95 | 95 | :controller => 'users', :action => 'edit', :id => '444' |
|
96 | 96 | ) |
|
97 | 97 | assert_routing( |
|
98 | 98 | {:method => :get, :path => '/users/222/edit/membership'}, |
|
99 | 99 | :controller => 'users', :action => 'edit', :id => '222', :tab => 'membership' |
|
100 | 100 | ) |
|
101 | 101 | assert_recognizes( |
|
102 | 102 | #TODO: use PUT on user_path, modify form |
|
103 | 103 | {:controller => 'users', :action => 'edit', :id => '444'}, |
|
104 | 104 | {:method => :post, :path => '/users/444/edit'} |
|
105 | 105 | ) |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | 108 | def test_add_membership_routing |
|
109 | 109 | assert_routing( |
|
110 | 110 | {:method => :post, :path => '/users/123/memberships'}, |
|
111 | 111 | :controller => 'users', :action => 'edit_membership', :id => '123' |
|
112 | 112 | ) |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def test_edit_membership_routing |
|
116 | 116 | assert_routing( |
|
117 | 117 | {:method => :post, :path => '/users/123/memberships/55'}, |
|
118 | 118 | :controller => 'users', :action => 'edit_membership', :id => '123', :membership_id => '55' |
|
119 | 119 | ) |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | def test_edit_membership |
|
123 | 123 | post :edit_membership, :id => 2, :membership_id => 1, |
|
124 | 124 | :membership => { :role_id => 2} |
|
125 | 125 | assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' |
|
126 | 126 | assert_equal 2, Member.find(1).role_id |
|
127 | 127 | end |
|
128 | 128 | |
|
129 | def test_edit_with_activation_should_send_a_notification | |
|
130 | u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr') | |
|
131 | u.login = 'foo' | |
|
132 | u.status = User::STATUS_REGISTERED | |
|
133 | u.save! | |
|
134 | ActionMailer::Base.deliveries.clear | |
|
135 | Setting.bcc_recipients = '1' | |
|
136 | ||
|
137 | post :edit, :id => u.id, :user => {:status => User::STATUS_ACTIVE} | |
|
138 | assert u.reload.active? | |
|
139 | mail = ActionMailer::Base.deliveries.last | |
|
140 | assert_not_nil mail | |
|
141 | assert_equal ['foo.bar@somenet.foo'], mail.bcc | |
|
142 | assert mail.body.include?(ll('fr', :notice_account_activated)) | |
|
143 | end | |
|
144 | ||
|
129 | 145 | def test_destroy_membership |
|
130 | 146 | assert_routing( |
|
131 | 147 | #TODO: use DELETE method on user_membership_path, modify form |
|
132 | 148 | {:method => :post, :path => '/users/567/memberships/12/destroy'}, |
|
133 | 149 | :controller => 'users', :action => 'destroy_membership', :id => '567', :membership_id => '12' |
|
134 | 150 | ) |
|
135 | 151 | end |
|
136 | 152 | |
|
137 | 153 | def test_destroy_membership |
|
138 | 154 | post :destroy_membership, :id => 2, :membership_id => 1 |
|
139 | 155 | assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' |
|
140 | 156 | assert_nil Member.find_by_id(1) |
|
141 | 157 | end |
|
142 | 158 | end |
General Comments 0
You need to be logged in to leave comments.
Login now