From 5191411508a85fc96de05095f066d18cfddac193 Mon Sep 17 00:00:00 2001 From: alan <547533434@qq.com> Date: Thu, 15 Jan 2015 18:48:19 +0800 Subject: [PATCH] =?UTF-8?q?<=E9=82=AE=E4=BB=B6=E5=8A=A0=E5=AF=86=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E7=99=BB=E5=BD=95=E5=8A=9F=E8=83=BD=EF=BC=8C=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E9=99=84=E4=BB=B6=E4=B8=8B=E8=BD=BD=E5=8A=9F=E8=83=BD?= =?UTF-8?q?>=20Signed-off-by:=20alan=20<547533434@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/application_controller.rb | 23 ++++++--- app/helpers/application_helper.rb | 2 + app/helpers/issues_helper.rb | 7 ++- app/models/issue_observer.rb | 10 ++-- app/models/issue_overdue.rb | 6 ++- app/models/journal_observer.rb | 8 ++- app/models/mailer.rb | 63 ++++++++++++++--------- app/models/user.rb | 6 ++- app/views/mailer/issue_add.html.erb | 5 +- app/views/mailer/issue_add.text.erb | 5 +- app/views/mailer/issue_edit.html.erb | 2 +- app/views/mailer/issue_edit.text.erb | 2 +- 12 files changed, 97 insertions(+), 42 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a017753f6..fe9f6c1b6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -156,7 +156,16 @@ class ApplicationController < ActionController::Base user end end + def try_to_autologin1 + # auto-login feature starts a new session + user = User.try_to_autologin(params[:token]) + if user + start_user_session(user) + end + user + + end # Sets the logged in user def logged_user=(user) reset_session @@ -248,10 +257,11 @@ class ApplicationController < ActionController::Base end end end - def authorize1(ctrl = params[:controller], action = params[:action],login = params[:user_name],password = params[:password], global = false) + def authorize1(ctrl = params[:controller], action = params[:action],token = params[:token], global = false) - if(!User.current.logged? && !login.nil?) - password_authentication + if(!User.current.logged? && !token.nil?) + + User.current =try_to_autologin1 end allowed = authorize_allowed(params[:controller], params[:action],global) @@ -307,9 +317,9 @@ class ApplicationController < ActionController::Base end end - def authorize_attachment_download1(ctrl = params[:controller], action = params[:action],login = params[:user_name],password = params[:password], global = false) - if(!User.current.logged? && !login.nil?) - password_authentication + def authorize_attachment_download1(ctrl = params[:controller], action = params[:action],token = params[:token], global = false) + if(!User.current.logged? && !token.nil?) + User.current = try_to_autologin1 end case @attachment.container_type when "Memo" @@ -850,6 +860,7 @@ class ApplicationController < ActionController::Base end + def successful_authentication(user, last_login_on) logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" # Valid user diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index cb88742ad..7efa02503 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -140,10 +140,12 @@ module ApplicationHelper # * :text - Link text (default to attachment filename) # * :download - Force download (default: false) def link_to_attachment(attachment, options={}) + token = options[:token] if options[:token] text = options.delete(:text) || attachment.filename route_method = options.delete(:download) ? :download_named_attachment_path : :named_attachment_path html_options = options.slice!(:only_path) url = send(route_method, attachment, attachment.filename, options) + url << "?token=#{token}" unless token.nil? link_to text, url, html_options end diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 8390fc61c..6a708051a 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -224,6 +224,7 @@ module IssuesHelper # as an array of strings def details_to_strings(details, no_html=false, options={}) options[:only_path] = (options[:only_path] == false ? false : true) + options[:token] = options[:token] if options[:token] strings = [] values_by_field = {} details.each do |detail| @@ -312,7 +313,11 @@ module IssuesHelper old_value = content_tag("del", old_value) if detail.old_value and detail.value.blank? if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key) # Link to the attachment if it has not been removed - value = link_to_attachment(atta, :download => true, :only_path => options[:only_path]) + if options[:token].nil? + value = link_to_attachment(atta, :download => true, :only_path => options[:only_path]) + else + value = link_to_attachment(atta, :download => true, :only_path => options[:only_path], :token => options[:token]) + end if options[:only_path] != false && atta.is_text? value += link_to( image_tag('magnifier.png'), diff --git a/app/models/issue_observer.rb b/app/models/issue_observer.rb index 3369387ee..50cbf2a42 100644 --- a/app/models/issue_observer.rb +++ b/app/models/issue_observer.rb @@ -18,8 +18,12 @@ class IssueObserver < ActiveRecord::Observer def after_create(issue) - thread1=Thread.new do - Mailer.issue_add(issue).deliver if Setting.notified_events.include?('issue_added') - end + Thread.start do + recipients = issue.recipients + recipients.each do |rec| + Mailer.issue_add(issue,rec).deliver if Setting.notified_events.include?('issue_added') + end + end + end end diff --git a/app/models/issue_overdue.rb b/app/models/issue_overdue.rb index 5caec94c9..3002b74ef 100644 --- a/app/models/issue_overdue.rb +++ b/app/models/issue_overdue.rb @@ -20,7 +20,11 @@ class IssueOverdue < ActiveRecord::Base #发邮件 #puts "11" + issue.id.to_s #Mailer.issue_expire(issue).deliver - Mailer.issue_add(issue).deliver + recipients = issue.recipients + recipients.each do |rec| + + Mailer.issue_edit(issue,rec).deliver + end break end end diff --git a/app/models/journal_observer.rb b/app/models/journal_observer.rb index 0357fb74d..10d3f7b4b 100644 --- a/app/models/journal_observer.rb +++ b/app/models/journal_observer.rb @@ -23,8 +23,12 @@ class JournalObserver < ActiveRecord::Observer (Setting.notified_events.include?('issue_status_updated') && journal.new_status.present?) || (Setting.notified_events.include?('issue_priority_updated') && journal.new_value_for('priority_id').present?) ) - Thread.new do - Mailer.issue_edit(journal).deliver + Thread.start do + recipients = journal.recipients + recipients.each do |rec| + + Mailer.issue_edit(journal,rec).deliver + end end end end diff --git a/app/models/mailer.rb b/app/models/mailer.rb index 327dd6b4a..b432461c7 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -95,38 +95,45 @@ class Mailer < ActionMailer::Base # Example: # issue_add(issue) => Mail::Message object # Mailer.issue_add(issue).deliver => sends an email to issue recipients - def issue_add(issue) + def issue_add(issue, recipients) issue_id = issue.project_index redmine_headers 'Project' => issue.project.identifier, 'Issue-Id' => issue_id, 'Issue-Author' => issue.author.login redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to message_id issue + @author = issue.author @issue = issue - recipients = issue.recipients - if recipients.include? "547533434@qq.com" - @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :user_name => 'alan', :password => 'alanlong') - else - @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id) - end + + token = Token.new(:user => User.find_by_mail(recipients), :action => 'autologin') + token.save + @token = token + @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :token => @token.value) + + + cc = issue.watcher_recipients - issue.recipients + subject = "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}" + mail(:to => recipients, + :cc => cc, + :subject => subject) + end # issue.attachments.each do |attach| # attachments["#{attach.filename}"] = File.read("#{attach.disk_filename}") # end - cc = issue.watcher_recipients - recipients + # cc = issue.watcher_recipients - recipients #mail.attachments['test'] = File.read("#{RAILS.root}/files/2015/01/150114094010_libegl.dll") - mail :to => recipients, - :cc => cc, - :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}" - end + + + # Builds a Mail::Message object used to email recipients of the edited issue. # # Example: # issue_edit(journal) => Mail::Message object # Mailer.issue_edit(journal).deliver => sends an email to issue recipients - def issue_edit(journal) + def issue_edit(journal,recipients) issue = journal.journalized.reload issue_id = issue.project_index redmine_headers 'Project' => issue.project.identifier, @@ -136,24 +143,34 @@ class Mailer < ActionMailer::Base message_id journal references issue @author = journal.user - recipients = journal.recipients - if recipients.include? "547533434@qq.com" - @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :anchor => "change-#{journal.id}", :user_name => 'alan', :password => 'alanlong') - else - @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :anchor => "change-#{journal.id}") - end + + + token = Token.new(:user => User.find_by_mail(recipients), :action => 'autologin') + token.save + @token = token + @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :anchor => "change-#{journal.id}", :token => @token.value) + + + # Watchers in cc - cc = journal.watcher_recipients - recipients + + cc = journal.watcher_recipients - journal.recipients s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] " s << "(#{issue.status.name}) " if journal.new_value_for('status_id') s << issue.subject @issue = issue @journal = journal # @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}") - mail :to => recipients, - :cc => cc, - :subject => s + mail(:to => recipients, + :cc => cc, + :subject => s) + end + + def self.deliver_mailer(to,cc, subject) + mail :to => to, + :cc => cc, + :subject => subject end # 用户申请加入项目邮件通知 diff --git a/app/models/user.rb b/app/models/user.rb index 735b80762..52619b038 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -380,7 +380,7 @@ class User < Principal raise text end - # Returns the user who matches the given autologin +key+ or nil + def self.try_to_autologin(key) user = Token.find_active_user('autologin', key, Setting.autologin.to_i) if user @@ -466,7 +466,11 @@ class User < Principal User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password end end + def check_password1?(clear_password) + clear_password == hashed_password + + end # Generates a random salt and computes hashed_password for +clear_password+ # The hashed password is stored in the following form: SHA1(salt + SHA1(password)) def salt_password(clear_password) diff --git a/app/views/mailer/issue_add.html.erb b/app/views/mailer/issue_add.html.erb index da7f8ac65..3f0fce917 100644 --- a/app/views/mailer/issue_add.html.erb +++ b/app/views/mailer/issue_add.html.erb @@ -5,10 +5,11 @@ <% unless @issue.attachments.nil? %> <% @issue.attachments.each do |attach| %> -