<邮件加密自动登录功能,邮件附件下载功能>

Signed-off-by: alan <547533434@qq.com>
This commit is contained in:
alan 2015-01-15 18:48:19 +08:00
parent 2bba0ddd70
commit 5191411508
12 changed files with 97 additions and 42 deletions

View File

@ -156,7 +156,16 @@ class ApplicationController < ActionController::Base
user user
end end
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 # Sets the logged in user
def logged_user=(user) def logged_user=(user)
reset_session reset_session
@ -248,10 +257,11 @@ class ApplicationController < ActionController::Base
end end
end 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?) if(!User.current.logged? && !token.nil?)
password_authentication
User.current =try_to_autologin1
end end
allowed = authorize_allowed(params[:controller], params[:action],global) allowed = authorize_allowed(params[:controller], params[:action],global)
@ -307,9 +317,9 @@ class ApplicationController < ActionController::Base
end end
end end
def authorize_attachment_download1(ctrl = params[:controller], action = params[:action],login = params[:user_name],password = params[:password], global = false) def authorize_attachment_download1(ctrl = params[:controller], action = params[:action],token = params[:token], global = false)
if(!User.current.logged? && !login.nil?) if(!User.current.logged? && !token.nil?)
password_authentication User.current = try_to_autologin1
end end
case @attachment.container_type case @attachment.container_type
when "Memo" when "Memo"
@ -850,6 +860,7 @@ class ApplicationController < ActionController::Base
end end
def successful_authentication(user, last_login_on) def successful_authentication(user, last_login_on)
logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
# Valid user # Valid user

View File

@ -140,10 +140,12 @@ module ApplicationHelper
# * :text - Link text (default to attachment filename) # * :text - Link text (default to attachment filename)
# * :download - Force download (default: false) # * :download - Force download (default: false)
def link_to_attachment(attachment, options={}) def link_to_attachment(attachment, options={})
token = options[:token] if options[:token]
text = options.delete(:text) || attachment.filename text = options.delete(:text) || attachment.filename
route_method = options.delete(:download) ? :download_named_attachment_path : :named_attachment_path route_method = options.delete(:download) ? :download_named_attachment_path : :named_attachment_path
html_options = options.slice!(:only_path) html_options = options.slice!(:only_path)
url = send(route_method, attachment, attachment.filename, options) url = send(route_method, attachment, attachment.filename, options)
url << "?token=#{token}" unless token.nil?
link_to text, url, html_options link_to text, url, html_options
end end

View File

@ -224,6 +224,7 @@ module IssuesHelper
# as an array of strings # as an array of strings
def details_to_strings(details, no_html=false, options={}) def details_to_strings(details, no_html=false, options={})
options[:only_path] = (options[:only_path] == false ? false : true) options[:only_path] = (options[:only_path] == false ? false : true)
options[:token] = options[:token] if options[:token]
strings = [] strings = []
values_by_field = {} values_by_field = {}
details.each do |detail| 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? 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) if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key)
# Link to the attachment if it has not been removed # 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? if options[:only_path] != false && atta.is_text?
value += link_to( value += link_to(
image_tag('magnifier.png'), image_tag('magnifier.png'),

View File

@ -18,8 +18,12 @@
class IssueObserver < ActiveRecord::Observer class IssueObserver < ActiveRecord::Observer
def after_create(issue) def after_create(issue)
thread1=Thread.new do Thread.start do
Mailer.issue_add(issue).deliver if Setting.notified_events.include?('issue_added') recipients = issue.recipients
recipients.each do |rec|
Mailer.issue_add(issue,rec).deliver if Setting.notified_events.include?('issue_added')
end
end end
end end
end end

View File

@ -20,7 +20,11 @@ class IssueOverdue < ActiveRecord::Base
#发邮件 #发邮件
#puts "11" + issue.id.to_s #puts "11" + issue.id.to_s
#Mailer.issue_expire(issue).deliver #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 break
end end
end end

View File

@ -23,8 +23,12 @@ class JournalObserver < ActiveRecord::Observer
(Setting.notified_events.include?('issue_status_updated') && journal.new_status.present?) || (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?) (Setting.notified_events.include?('issue_priority_updated') && journal.new_value_for('priority_id').present?)
) )
Thread.new do Thread.start do
Mailer.issue_edit(journal).deliver recipients = journal.recipients
recipients.each do |rec|
Mailer.issue_edit(journal,rec).deliver
end
end end
end end
end end

View File

@ -95,38 +95,45 @@ class Mailer < ActionMailer::Base
# Example: # Example:
# issue_add(issue) => Mail::Message object # issue_add(issue) => Mail::Message object
# Mailer.issue_add(issue).deliver => sends an email to issue recipients # 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 issue_id = issue.project_index
redmine_headers 'Project' => issue.project.identifier, redmine_headers 'Project' => issue.project.identifier,
'Issue-Id' => issue_id, 'Issue-Id' => issue_id,
'Issue-Author' => issue.author.login 'Issue-Author' => issue.author.login
redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
message_id issue message_id issue
@author = issue.author @author = issue.author
@issue = issue @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| # issue.attachments.each do |attach|
# attachments["#{attach.filename}"] = File.read("#{attach.disk_filename}") # attachments["#{attach.filename}"] = File.read("#{attach.disk_filename}")
# end # 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.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. # Builds a Mail::Message object used to email recipients of the edited issue.
# #
# Example: # Example:
# issue_edit(journal) => Mail::Message object # issue_edit(journal) => Mail::Message object
# Mailer.issue_edit(journal).deliver => sends an email to issue recipients # 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 = journal.journalized.reload
issue_id = issue.project_index issue_id = issue.project_index
redmine_headers 'Project' => issue.project.identifier, redmine_headers 'Project' => issue.project.identifier,
@ -136,24 +143,34 @@ class Mailer < ActionMailer::Base
message_id journal message_id journal
references issue references issue
@author = journal.user @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') token = Token.new(:user => User.find_by_mail(recipients), :action => 'autologin')
else token.save
@issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :anchor => "change-#{journal.id}") @token = token
end @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :anchor => "change-#{journal.id}", :token => @token.value)
# Watchers in cc # 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.project.name} - #{issue.tracker.name} ##{issue_id}] "
s << "(#{issue.status.name}) " if journal.new_value_for('status_id') s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
s << issue.subject s << issue.subject
@issue = issue @issue = issue
@journal = journal @journal = journal
# @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}") # @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
mail :to => recipients, mail(:to => recipients,
:cc => cc, :cc => cc,
:subject => s :subject => s)
end
def self.deliver_mailer(to,cc, subject)
mail :to => to,
:cc => cc,
:subject => subject
end end
# 用户申请加入项目邮件通知 # 用户申请加入项目邮件通知

View File

@ -380,7 +380,7 @@ class User < Principal
raise text raise text
end end
# Returns the user who matches the given autologin +key+ or nil
def self.try_to_autologin(key) def self.try_to_autologin(key)
user = Token.find_active_user('autologin', key, Setting.autologin.to_i) user = Token.find_active_user('autologin', key, Setting.autologin.to_i)
if user if user
@ -466,7 +466,11 @@ class User < Principal
User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password
end end
end end
def check_password1?(clear_password)
clear_password == hashed_password
end
# Generates a random salt and computes hashed_password for +clear_password+ # Generates a random salt and computes hashed_password for +clear_password+
# The hashed password is stored in the following form: SHA1(salt + SHA1(password)) # The hashed password is stored in the following form: SHA1(salt + SHA1(password))
def salt_password(clear_password) def salt_password(clear_password)

View File

@ -5,10 +5,11 @@
<% unless @issue.attachments.nil? %> <% unless @issue.attachments.nil? %>
<% @issue.attachments.each do |attach| %> <% @issue.attachments.each do |attach| %>
<li> <%= link_to_attachment(attach, :download => true, :only_path => false) %></li> <li> <%= l(:label_attachment) %><%= link_to_attachment(attach, :download => true, :token => @token.value, :only_path => false) %><%= l(:label_added) %></li>
<% end %> <% end %>
<% end %> <% end %>
</ul> </ul>
<hr /> <hr />
<%= render :partial => 'issue', :formats => [:html], :locals => { :issue => @issue, :issue_url => @issue_url } %> <%= render :partial => 'issue', :formats => [:html], :locals => { :issue => @issue, :issue_url => @issue_url } %>

View File

@ -1,8 +1,11 @@
<%= l(:text_issue_added, :id => "##{@issue.project_index}", :author => @issue.author) %> <%= l(:text_issue_added, :id => "##{@issue.project_index}", :author => @issue.author) %>
<% @issue.attachments.each do |attach| %> <% @issue.attachments.each do |attach| %>
<%= link_to_attachment(attach, :download => true, :only_path => false) %> <%= l(:label_attachment) %>
<%= link_to_attachment(attach, :download => true, :token => @token.value, :only_path => false) %> <%= l(:label_added) %>
<% end %> <% end %>
---------------------------------------- ----------------------------------------
<%= render :partial => 'issue', :formats => [:text], :locals => { :issue => @issue, :issue_url => @issue_url } %> <%= render :partial => 'issue', :formats => [:text], :locals => { :issue => @issue, :issue_url => @issue_url } %>

View File

@ -1,7 +1,7 @@
<%= l(:text_issue_updated, :id => "##{@issue.project_index}", :author => h(@journal.user)) %> <%= l(:text_issue_updated, :id => "##{@issue.project_index}", :author => h(@journal.user)) %>
<ul> <ul>
<% details_to_strings(@journal.details, false, :only_path => false).each do |string| %> <% details_to_strings(@journal.details, false, :only_path => false, :token => @token.value).each do |string| %>
<li><%= string %></li> <li><%= string %></li>
<% end %> <% end %>
</ul> </ul>

View File

@ -1,6 +1,6 @@
<%= l(:text_issue_updated, :id => "##{@issue.project_index}", :author => @journal.user) %> <%= l(:text_issue_updated, :id => "##{@issue.project_index}", :author => @journal.user) %>
<% details_to_strings(@journal.details, true).each do |string| -%> <% details_to_strings(@journal.details, true, :token => @token.value).each do |string| -%>
<%= string %> <%= string %>
<% end -%> <% end -%>