Merge branch 'guange_dev' into szzh

This commit is contained in:
sw 2015-05-08 15:45:24 +08:00
commit 1b6ecbf520
19 changed files with 1474 additions and 1132 deletions

10
Gemfile
View File

@ -24,6 +24,8 @@ gem 'acts-as-taggable-on', '2.4.1'
gem 'spreadsheet' gem 'spreadsheet'
gem 'ruby-ole' gem 'ruby-ole'
gem 'rails_kindeditor',path:'lib/rails_kindeditor' gem 'rails_kindeditor',path:'lib/rails_kindeditor'
gem "rmagick", ">= 2.0.0"
group :development do group :development do
gem 'grape-swagger' gem 'grape-swagger'
#gem 'grape-swagger-ui', git: 'https://github.com/guange2015/grape-swagger-ui.git' #gem 'grape-swagger-ui', git: 'https://github.com/guange2015/grape-swagger-ui.git'
@ -48,14 +50,6 @@ group :test do
gem 'selenium-webdriver', '~> 2.42.0' gem 'selenium-webdriver', '~> 2.42.0'
gem "faker" gem "faker"
# platforms :mri, :mingw do
# group :rmagick do
# # RMagick 2 supports ruby 1.9
# # RMagick 1 would be fine for ruby 1.8 but Bundler does not support
# # different requirements for the same gem on different platforms
# gem "rmagick", ">= 2.0.0"
# end
#end
end end
# Gems used only for assets and not required # Gems used only for assets and not required

View File

@ -31,9 +31,6 @@ class AccountController < ApplicationController
else else
authenticate_user authenticate_user
end end
rescue AuthSourceException => e
logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
render_error :message => e.message
end end
# Log out current user and redirect to welcome page # Log out current user and redirect to welcome page
@ -47,6 +44,10 @@ class AccountController < ApplicationController
# display the logout form # display the logout form
end end
def heartbeat
render :json => session[:user_id]
end
# Lets user choose a new password # Lets user choose a new password
def lost_password def lost_password
(redirect_to(home_url); return) unless Setting.lost_password? (redirect_to(home_url); return) unless Setting.lost_password?
@ -329,7 +330,7 @@ class AccountController < ApplicationController
end end
def set_autologin_cookie(user) def set_autologin_cookie(user)
token = Token.create(:user => user, :action => 'autologin') token = Token.get_or_create_permanent_login_token(user)
cookie_options = { cookie_options = {
:value => token.value, :value => token.value,
:expires => 7.days.from_now, :expires => 7.days.from_now,

View File

@ -156,16 +156,16 @@ class ApplicationController < ActionController::Base
user user
end end
end end
def try_to_autologin1
# auto-login feature starts a new session def try_to_autologin1
user = User.try_to_autologin(params[:token]) user = User.try_to_autologin(params[:token])
if user if user
logout_user if User.current.id != user.id
start_user_session(user) start_user_session(user)
end end
user user
end end
# Sets the logged in user # Sets the logged in user
def logged_user=(user) def logged_user=(user)
reset_session reset_session
@ -200,7 +200,7 @@ class ApplicationController < ActionController::Base
def logout_user def logout_user
if User.current.logged? if User.current.logged?
cookies.delete(autologin_cookie_name) cookies.delete(autologin_cookie_name)
Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) # Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin'])
self.logged_user = nil self.logged_user = nil
end end
end end

View File

@ -56,25 +56,9 @@ class AvatarController < ApplicationController
# self.digest = md5.hexdigest # self.digest = md5.hexdigest
end end
@temp_file = nil @temp_file = nil
# @avatar = Avatar.new(:receive_file => request.raw_post)
# @avatar.source_id = User.current.id
# @avatar.image_file = params[:filename].presence || Redmine::Utils.random_hex(16)
# saved = @avatar.save
begin
f = Magick::ImageList.new(diskfile)
# gif格式不再做大小处理
if f.format != 'GIF'
width = 300.0
proportion = (width/f[0].columns)
height = (f[0].rows*proportion)
f.resize_to_fill!(width,height)
f.write(diskfile)
end
rescue Exception => e
logger.error "[Error] avatar : avatar_controller#upload ===> #{e}"
end
image = Trustie::Utils::Image.new(diskfile,true)
image.compress(300)
respond_to do |format| respond_to do |format|
format.js format.js

View File

@ -95,6 +95,7 @@ class MyController < ApplicationController
@pref = @user.pref @pref = @user.pref
diskfile = disk_filename('User', @user.id) diskfile = disk_filename('User', @user.id)
diskfile1 = diskfile + 'temp' diskfile1 = diskfile + 'temp'
begin
if request.post? if request.post?
@user.safe_attributes = params[:user] @user.safe_attributes = params[:user]
@user.pref.attributes = params[:pref] @user.pref.attributes = params[:pref]
@ -121,32 +122,7 @@ class MyController < ApplicationController
if @user.save && @se.save if @user.save && @se.save
# 头像保存 # 头像保存
if File.exist?(diskfile1) FileUtils.mv diskfile1, diskfile, force: true
if File.exist?(diskfile)
File.delete(diskfile)
end
File.open(diskfile1, "rb") do |f|
buffer = f.read(10)
if buffer != "DELETE"
File.open(diskfile1, "rb") do |f1|
File.open(diskfile, "wb") do |f|
buffer = ""
while (buffer = f1.read(8192))
f.write(buffer)
end
end
end
# File.rename(diskfile + 'temp',diskfile);
end
end
end
# 确保文件被删除
if File.exist?(diskfile1)
File.delete(diskfile1)
end
@user.pref.save @user.pref.save
@user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
set_language_if_valid @user.language set_language_if_valid @user.language
@ -154,17 +130,11 @@ class MyController < ApplicationController
redirect_to user_url(@user) redirect_to user_url(@user)
return return
else else
# 确保文件被删除
if File.exist?(diskfile1)
File.delete(diskfile1)
end
@user.login = lg @user.login = lg
end end
else
# 确保文件被删除
if File.exist?(diskfile1)
File.delete(diskfile1)
end end
ensure
File.delete(diskfile1) if File.exist?(diskfile1)
end end
end end
@ -200,31 +170,20 @@ class MyController < ApplicationController
@user = us.change_password params.merge(:current_user_id => @user.id) @user = us.change_password params.merge(:current_user_id => @user.id)
if @user.errors.full_messages.count <= 0 if @user.errors.full_messages.count <= 0
flash.now[:notice] = l(:notice_account_password_updated) flash.now[:notice] = l(:notice_account_password_updated)
redirect_to my_account_url # 修改完密码让其重新登录并更新Token
Token.delete_user_all_tokens(@user)
logout_user
redirect_to signin_url(back_url: my_account_path)
else
flash.now[:error] = l(:notice_account_wrong_password)
end end
end end
rescue Exception => e rescue Exception => e
if e.message == 'wrong password' if e.message == 'wrong password'
flash.now[:error] = l(:notice_account_wrong_password) flash.now[:error] = l(:notice_account_wrong_password)
else
flash.now[:error] = e.message
end end
# @user = User.current
# unless @user.change_password_allowed?
# flash.now[:error] = l(:notice_can_t_change_password)
# redirect_to my_account_url
# return
# end
# if request.post?
# if @user.check_password?(params[:password])
# @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
#
# if @user.save
# flash.now[:notice] = l(:notice_account_password_updated)
# redirect_to my_account_url
# end
# else
# flash.now[:error] = l(:notice_account_wrong_password)
# end
# end
end end
# Create a new feeds key # Create a new feeds key

View File

@ -1,3 +1,4 @@
#coding=utf-8
# Redmine - project management software # Redmine - project management software
# Copyright (C) 2006-2013 Jean-Philippe Lang # Copyright (C) 2006-2013 Jean-Philippe Lang
# #
@ -14,7 +15,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
class Token < ActiveRecord::Base class Token < ActiveRecord::Base
belongs_to :user belongs_to :user
validates_uniqueness_of :value validates_uniqueness_of :value
@ -27,6 +28,14 @@ class Token < ActiveRecord::Base
self.value = Token.generate_token_value self.value = Token.generate_token_value
end end
def self.get_or_create_permanent_login_token(user)
token = Token.get_token_from_user(user, 'autologin')
unless token
token = Token.create(:user => user, :action => 'autologin')
end
token
end
def self.get_token_from_user(user, action) def self.get_token_from_user(user, action)
token = Token.where(:action => action, :user_id => user).first token = Token.where(:action => action, :user_id => user).first
unless token unless token
@ -42,7 +51,7 @@ class Token < ActiveRecord::Base
# Delete all expired tokens # Delete all expired tokens
def self.destroy_expired def self.destroy_expired
Token.delete_all ["action NOT IN (?) AND created_on < ?", ['feeds', 'api'], Time.now - @@validity_time] Token.delete_all ["action NOT IN (?) AND created_on < ?", ['feeds', 'api', 'autologin'], Time.now - @@validity_time]
end end
# Returns the active user who owns the key for the given action # Returns the active user who owns the key for the given action
@ -80,6 +89,10 @@ class Token < ActiveRecord::Base
Redmine::Utils.random_hex(20) Redmine::Utils.random_hex(20)
end end
def self.delete_user_all_tokens(user)
Token.delete_all(user_id: user.id)
end
private private
# Removes obsolete tokens (same user and action) # Removes obsolete tokens (same user and action)

View File

@ -74,3 +74,6 @@
<%= javascript_include_tag 'avatars' %> <%= javascript_include_tag 'avatars' %>
<% end %> <% end %>
</div> </div>

View File

@ -1,4 +1,4 @@
var imgSpan = $('#avatar_image'); var imgSpan = $('#avatar_image');
imgSpan.attr({"src":'<%= @urlfile.to_s << "?" << Time.now.to_s%>'}); imgSpan.attr({"src":'<%= "#{@urlfile.to_s}?#{Time.now.to_i}" %>'});

View File

@ -1 +1 @@
Rails.application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 20.minutes, :key => '_trustie_session', :domain => :all Rails.application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 90.minutes, :key => '_trustie_session', :domain => :all

View File

@ -227,6 +227,8 @@ RedmineApp::Application.routes.draw do
match '/projects/search', :via => [:get, :post] match '/projects/search', :via => [:get, :post]
match '/users/search', :via => [:get, :post] match '/users/search', :via => [:get, :post]
#end #end
match 'account/heartbeat', to: 'account#heartbeat', :via => :get
match 'login', :to => 'account#login', :as => 'signin', :via => [:get, :post] match 'login', :to => 'account#login', :as => 'signin', :via => [:get, :post]
match 'logout', :to => 'account#logout', :as => 'signout', :via => [:get, :post] match 'logout', :to => 'account#logout', :as => 'signout', :via => [:get, :post]
match 'account/register', :via => [:get, :post], :as => 'register' match 'account/register', :via => [:get, :post], :as => 'register'

View File

@ -55,7 +55,7 @@ module RailsKindeditor
}" }"
else else
"KindEditor.ready(function(K){ "KindEditor.ready(function(K){
#{editor_id}K.create('##{dom_id}', #{get_options(options).to_json}); #{editor_id}K.create('##{dom_id}', #{get_options(options).to_json}).loadPlugin('paste');
});" });"
end end
end end

11
lib/tasks/avatar.rake Normal file
View File

@ -0,0 +1,11 @@
desc "compress and backup avatar"
task :compress_avatar => :environment do
path = File.join(Rails.root, "public/images/avatars/User")
Dir.foreach(path) do |f|
if f.to_s =~ /^\d+$/
puts f
image = Trustie::Utils::Image.new(File.join(path,f), true)
image.compress(300)
end
end
end

View File

@ -1 +1,2 @@
require 'trustie/utils' require 'trustie/utils'
require 'trustie/utils/image'

View File

@ -0,0 +1,35 @@
#coding=utf-8
module Trustie
module Utils
class Image
def initialize(file, bak)
@file = file
@bak = bak
end
def compress(size=300)
backup if @bak
begin
f = Magick::ImageList.new(@file)
if f.format != 'GIF'
width = size
if f[0].columns > width
proportion = (width/f[0].columns.to_f)
height = (f[0].rows*proportion)
f.resize_to_fill!(width,height.to_i)
f.write(@file)
end
end
rescue Exception => e
logger.error "[Error] compress : ===> #{e}"
end
end
def backup
FileUtils.cp @file, "#{@file}.bak"
end
end
end
end

View File

@ -0,0 +1,37 @@
KindEditor.plugin('paste', function(K) {
var editor = this,
name = 'paste';
var contentWindow = document.getElementsByTagName('iframe')[0].contentWindow;
contentWindow.document.getElementsByTagName('body')[0].onpaste = function(event) {
// use event.originalEvent.clipboard for newer chrome versions
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
console.log(JSON.stringify(items)); // will give you the mime types
// find pasted image among pasted items
var blob = null;
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") === 0) {
blob = items[i].getAsFile();
}
}
// load image if there is a pasted image
if (blob !== null) {
var reader = new FileReader();
reader.onload = function(event) {
console.log(event.target.result); // data url!
var data = new FormData();
data.append("imgFile", blob, "imageFilename.png");
$.ajax({
url: '/kindeditor/upload?dir=image',
contentType: false,
type: 'POST',
data: data,
processData: false,
success: function(data) {
editor.exec('insertimage', JSON.parse(data).url);
}
});
};
reader.readAsDataURL(blob);
}
}
});

View File

@ -571,10 +571,16 @@ function warnLeavingUnsaved(message) {
}; };
} }
function setupHeartBeat(){
var time = 60*1000*30; // 30 mins
setInterval(function(){$.getJSON('/account/heartbeat');},time);
}
function setupAjaxIndicator() { function setupAjaxIndicator() {
$('#ajax-indicator').bind('ajaxSend', function(event, xhr, settings) { $('#ajax-indicator').bind('ajaxSend', function(event, xhr, settings) {
if(settings && settings.url && settings.url.endsWith('account/heartbeat')){
return;
}
if ($('.ajax-loading').length === 0 && settings.contentType != 'application/octet-stream') { if ($('.ajax-loading').length === 0 && settings.contentType != 'application/octet-stream') {
$('#ajax-indicator').show(); $('#ajax-indicator').show();
} }
@ -622,6 +628,7 @@ function transpotUrl (scope) {
} }
$(document).ready(setupAjaxIndicator); $(document).ready(setupAjaxIndicator);
$(document).ready(setupHeartBeat);
$(document).ready(hideOnLoad); $(document).ready(hideOnLoad);
$(document).ready(addFormObserversForDoubleSubmit); $(document).ready(addFormObserversForDoubleSubmit);

View File

@ -190,6 +190,9 @@ function dragOutHandler(e) {
} }
function setupFileDrop() { function setupFileDrop() {
$('#avatar_image').on('click', function(){
console.log("click");
});
if (window.File && window.FileList && window.ProgressEvent && window.FormData) { if (window.File && window.FileList && window.ProgressEvent && window.FormData) {
$.event.fixHooks.drop = { props: [ 'dataTransfer' ] }; $.event.fixHooks.drop = { props: [ 'dataTransfer' ] };

292
public/javascripts/paste.js Normal file
View File

@ -0,0 +1,292 @@
// Generated by CoffeeScript 1.9.0
/*
paste.js is an interface to read data ( text / image ) from clipboard in different browsers. It also contains several hacks.
https://github.com/layerssss/paste.js
*/
(function() {
var $, Paste, createHiddenEditable, dataURLtoBlob;
$ = window.jQuery;
$.paste = function(pasteContainer) {
var pm;
if (typeof console !== "undefined" && console !== null) {
console.log("DEPRECATED: This method is deprecated. Please use $.fn.pastableNonInputable() instead.");
}
pm = Paste.mountNonInputable(pasteContainer);
return pm._container;
};
$.fn.pastableNonInputable = function() {
var el, _i, _len;
for (_i = 0, _len = this.length; _i < _len; _i++) {
el = this[_i];
Paste.mountNonInputable(el);
}
return this;
};
$.fn.pastableTextarea = function() {
var el, _i, _len;
for (_i = 0, _len = this.length; _i < _len; _i++) {
el = this[_i];
Paste.mountTextarea(el);
}
return this;
};
$.fn.pastableContenteditable = function() {
var el, _i, _len;
for (_i = 0, _len = this.length; _i < _len; _i++) {
el = this[_i];
Paste.mountContenteditable(el);
}
return this;
};
dataURLtoBlob = function(dataURL, sliceSize) {
var b64Data, byteArray, byteArrays, byteCharacters, byteNumbers, contentType, i, m, offset, slice, _ref;
if (sliceSize == null) {
sliceSize = 512;
}
if (!(m = dataURL.match(/^data\:([^\;]+)\;base64\,(.+)$/))) {
return null;
}
_ref = m, m = _ref[0], contentType = _ref[1], b64Data = _ref[2];
byteCharacters = atob(b64Data);
byteArrays = [];
offset = 0;
while (offset < byteCharacters.length) {
slice = byteCharacters.slice(offset, offset + sliceSize);
byteNumbers = new Array(slice.length);
i = 0;
while (i < slice.length) {
byteNumbers[i] = slice.charCodeAt(i);
i++;
}
byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
offset += sliceSize;
}
return new Blob(byteArrays, {
type: contentType
});
};
createHiddenEditable = function() {
return $(document.createElement('div')).attr('contenteditable', true).css({
width: 1,
height: 1,
position: 'fixed',
left: -100,
overflow: 'hidden'
});
};
Paste = (function() {
Paste.prototype._target = null;
Paste.prototype._container = null;
Paste.mountNonInputable = function(nonInputable) {
var paste;
paste = new Paste(createHiddenEditable().appendTo(nonInputable), nonInputable);
$(nonInputable).on('click', (function(_this) {
return function() {
return paste._container.focus();
};
})(this));
paste._container.on('focus', (function(_this) {
return function() {
return $(nonInputable).addClass('pastable-focus');
};
})(this));
return paste._container.on('blur', (function(_this) {
return function() {
return $(nonInputable).removeClass('pastable-focus');
};
})(this));
};
Paste.mountTextarea = function(textarea) {
var ctlDown, paste;
if (-1 !== navigator.userAgent.toLowerCase().indexOf('chrome')) {
return this.mountContenteditable(textarea);
}
paste = new Paste(createHiddenEditable().insertBefore(textarea), textarea);
ctlDown = false;
$(textarea).on('keyup', function(ev) {
var _ref;
if ((_ref = ev.keyCode) === 17 || _ref === 224) {
return ctlDown = false;
}
});
$(textarea).on('keydown', function(ev) {
var _ref;
if ((_ref = ev.keyCode) === 17 || _ref === 224) {
ctlDown = true;
}
if (ctlDown && ev.keyCode === 86) {
return paste._container.focus();
}
});
$(paste._target).on('pasteImage', (function(_this) {
return function() {
return $(textarea).focus();
};
})(this));
$(paste._target).on('pasteText', (function(_this) {
return function() {
return $(textarea).focus();
};
})(this));
$(textarea).on('focus', (function(_this) {
return function() {
return $(textarea).addClass('pastable-focus');
};
})(this));
return $(textarea).on('blur', (function(_this) {
return function() {
return $(textarea).removeClass('pastable-focus');
};
})(this));
};
Paste.mountContenteditable = function(contenteditable) {
var paste;
paste = new Paste(contenteditable, contenteditable);
$(contenteditable).on('focus', (function(_this) {
return function() {
return $(contenteditable).addClass('pastable-focus');
};
})(this));
return $(contenteditable).on('blur', (function(_this) {
return function() {
return $(contenteditable).removeClass('pastable-focus');
};
})(this));
};
function Paste(_at__container, _at__target) {
this._container = _at__container;
this._target = _at__target;
this._container = $(this._container);
this._target = $(this._target).addClass('pastable');
this._container.on('paste', (function(_this) {
return function(ev) {
var clipboardData, file, item, reader, text, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _results;
if (((_ref = ev.originalEvent) != null ? _ref.clipboardData : void 0) != null) {
clipboardData = ev.originalEvent.clipboardData;
if (clipboardData.items) {
_ref1 = clipboardData.items;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
item = _ref1[_i];
if (item.type.match(/^image\//)) {
reader = new FileReader();
reader.onload = function(event) {
return _this._handleImage(event.target.result);
};
reader.readAsDataURL(item.getAsFile());
}
if (item.type === 'text/plain') {
item.getAsString(function(string) {
return _this._target.trigger('pasteText', {
text: string
});
});
}
}
} else {
if (-1 !== Array.prototype.indexOf.call(clipboardData.types, 'text/plain')) {
text = clipboardData.getData('Text');
_this._target.trigger('pasteText', {
text: text
});
}
_this._checkImagesInContainer(function(src) {
return _this._handleImage(src);
});
}
}
if (clipboardData = window.clipboardData) {
if ((_ref2 = (text = clipboardData.getData('Text'))) != null ? _ref2.length : void 0) {
return _this._target.trigger('pasteText', {
text: text
});
} else {
_ref3 = clipboardData.files;
_results = [];
for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
file = _ref3[_j];
_this._handleImage(URL.createObjectURL(file));
_results.push(_this._checkImagesInContainer(function() {}));
}
return _results;
}
}
};
})(this));
}
Paste.prototype._handleImage = function(src) {
var loader;
loader = new Image();
loader.onload = (function(_this) {
return function() {
var blob, canvas, ctx, dataURL;
canvas = document.createElement('canvas');
canvas.width = loader.width;
canvas.height = loader.height;
ctx = canvas.getContext('2d');
ctx.drawImage(loader, 0, 0, canvas.width, canvas.height);
dataURL = null;
try {
dataURL = canvas.toDataURL('image/png');
blob = dataURLtoBlob(dataURL);
} catch (_error) {}
if (dataURL) {
return _this._target.trigger('pasteImage', {
blob: blob,
dataURL: dataURL,
width: loader.width,
height: loader.height
});
}
};
})(this);
return loader.src = src;
};
Paste.prototype._checkImagesInContainer = function(cb) {
var img, timespan, _i, _len, _ref;
timespan = Math.floor(1000 * Math.random());
_ref = this._container.find('img');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
img = _ref[_i];
img["_paste_marked_" + timespan] = true;
}
return setTimeout((function(_this) {
return function() {
var _j, _len1, _ref1, _results;
_ref1 = _this._container.find('img');
_results = [];
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
img = _ref1[_j];
if (!img["_paste_marked_" + timespan]) {
cb(img.src);
}
_results.push($(img).remove());
}
return _results;
};
})(this), 1);
};
return Paste;
})();
}).call(this);