annotate .svn/pristine/b9/b90f665732cea00de6a43cb9e0e055b111dd8028.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 module Redmine
Chris@909 19 module Ciphering
Chris@909 20 def self.included(base)
Chris@909 21 base.extend ClassMethods
Chris@909 22 end
Chris@909 23
Chris@909 24 class << self
Chris@909 25 def encrypt_text(text)
Chris@909 26 if cipher_key.blank? || text.blank?
Chris@909 27 text
Chris@909 28 else
Chris@909 29 c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
Chris@909 30 iv = c.random_iv
Chris@909 31 c.encrypt
Chris@909 32 c.key = cipher_key
Chris@909 33 c.iv = iv
Chris@909 34 e = c.update(text.to_s)
Chris@909 35 e << c.final
Chris@909 36 "aes-256-cbc:" + [e, iv].map {|v| Base64.encode64(v).strip}.join('--')
Chris@909 37 end
Chris@909 38 end
Chris@909 39
Chris@909 40 def decrypt_text(text)
Chris@909 41 if text && match = text.match(/\Aaes-256-cbc:(.+)\Z/)
Chris@909 42 if cipher_key.blank?
Chris@909 43 logger.error "Attempt to decrypt a ciphered text with no cipher key configured in config/configuration.yml" if logger
Chris@909 44 return text
Chris@909 45 end
Chris@909 46 text = match[1]
Chris@909 47 c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
Chris@909 48 e, iv = text.split("--").map {|s| Base64.decode64(s)}
Chris@909 49 c.decrypt
Chris@909 50 c.key = cipher_key
Chris@909 51 c.iv = iv
Chris@909 52 d = c.update(e)
Chris@909 53 d << c.final
Chris@909 54 else
Chris@909 55 text
Chris@909 56 end
Chris@909 57 end
Chris@909 58
Chris@909 59 def cipher_key
Chris@909 60 key = Redmine::Configuration['database_cipher_key'].to_s
Chris@909 61 key.blank? ? nil : Digest::SHA256.hexdigest(key)
Chris@909 62 end
Chris@909 63
Chris@909 64 def logger
Chris@909 65 Rails.logger
Chris@909 66 end
Chris@909 67 end
Chris@909 68
Chris@909 69 module ClassMethods
Chris@909 70 def encrypt_all(attribute)
Chris@909 71 transaction do
Chris@909 72 all.each do |object|
Chris@909 73 clear = object.send(attribute)
Chris@909 74 object.send "#{attribute}=", clear
Chris@909 75 raise(ActiveRecord::Rollback) unless object.save(false)
Chris@909 76 end
Chris@909 77 end ? true : false
Chris@909 78 end
Chris@909 79
Chris@909 80 def decrypt_all(attribute)
Chris@909 81 transaction do
Chris@909 82 all.each do |object|
Chris@909 83 clear = object.send(attribute)
Chris@909 84 object.write_attribute attribute, clear
Chris@909 85 raise(ActiveRecord::Rollback) unless object.save(false)
Chris@909 86 end
Chris@909 87 end
Chris@909 88 end ? true : false
Chris@909 89 end
Chris@909 90
Chris@909 91 private
Chris@909 92
Chris@909 93 # Returns the value of the given ciphered attribute
Chris@909 94 def read_ciphered_attribute(attribute)
Chris@909 95 Redmine::Ciphering.decrypt_text(read_attribute(attribute))
Chris@909 96 end
Chris@909 97
Chris@909 98 # Sets the value of the given ciphered attribute
Chris@909 99 def write_ciphered_attribute(attribute, value)
Chris@909 100 write_attribute(attribute, Redmine::Ciphering.encrypt_text(value))
Chris@909 101 end
Chris@909 102 end
Chris@909 103 end