rollback-2.6.3.patch

Frederico Camara, 2015-12-17 20:10

Download (10.5 KB)

View differences:

redmine/app/controllers/journals_controller.rb 2015-05-22 13:35:53.069405551 -0300
16 16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 17

  
18 18
class JournalsController < ApplicationController
19
  before_filter :find_journal, :only => [:edit, :diff]
19
  before_filter :find_journal, :only => [:edit, :diff, :rollback]
20 20
  before_filter :find_issue, :only => [:new]
21 21
  before_filter :find_optional_project, :only => [:index]
22
  before_filter :authorize, :only => [:new, :edit, :diff]
22
  before_filter :authorize, :only => [:new, :edit, :diff, :rollback]
23 23
  accept_rss_auth :index
24 24
  menu_item :issues
25 25

  
......
94 94
    end
95 95
  end
96 96

  
97
  def rollback
98
    (render_403; return false) unless @journal.can_rollback?(User.current)
99

  
100
    if @journal.rollback
101
      flash[:notice] = l(:notice_successful_update)
102
    else
103
      # can't seem to bring in the helper method 'error_messages_for'
104
      # and injecting it into show.rhtml doesn't seem to work, since 
105
      # the @issue loses the errors on redirect (due to issue reload)
106
      flash[:error] = "<ul>" + @journal.errors.full_messages.map {|msg| "<li>" + ERB::Util.html_escape(msg) + "</li>"}.join + "</ul>"
107
    end
108
    respond_to do |format|
109
      format.html { redirect_to :controller => 'issues', :action => 'show', :id => @journal.journalized_id }
110
      format.api  { render_validation_errors(@issue) }
111
    end
112
  end
113

  
97 114
  private
98 115

  
99 116
  def find_journal
redmine/app/models/issue.rb 2015-05-21 11:53:25.612773462 -0300
678 679
    end
679 680
  end
680 681

  
682
  # rollback the changes in a journal, the journal is destroyed on success
683
  def rollback(journal)
684
    # only allow rollback of journal details for the last journal
685
    (errors.add :base, l(:notice_locking_conflict); return) if !journal.last_valid_journal?(journals)
686

  
687
    # avoid the creation of journals during rollback (see 'attachment removed')
688
    @rolling_back = true
689

  
690
    # roll back each change detailed by the journal
691
    journal.details.each do |d|
692
      case d.property
693
        # issue attribute change
694
        when 'attr'; send "#{d.prop_key}=", d.old_value
695
        # rollback custom field change
696
        when 'cf'; custom_field_values.each {|v| v.value = d.old_value if v.custom_field_id == d.prop_key.to_i}
697
        # remove any added attachments (we can't recover removed attachments)
698
        when 'attachment'; attachments.each {|v| attachments.delete(v) if v.id == d.prop_key.to_i}
699
      end
700
    end
701

  
702
    # allow the creation of journals again
703
    remove_instance_variable(:@rolling_back)
704

  
705
    # destroy the journal once we save the issue changes
706
    if save(:validate => false)
707
      journal.rolled_back = true
708
      journal.save
709
    end
710
  end
711

  
681 712
  # Return true if the issue is closed, otherwise false
682 713
  def closed?
683 714
    self.status.is_closed?
redmine/app/models/journal.rb 2015-05-21 11:53:25.619773701 -0300
100 100
    usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
101 101
  end
102 102

  
103
  def last_valid_journal?(journals)
104
    self == journals.reject{|j| j.rolled_back}.sort_by(&:id).last
105
  end
106

  
107
  def can_rollback?(usr = nil)
108
    user ||= User.current
109
    editable_by?(user) && (details.empty? || user.allowed_to?(:rollback_issue_notes, project))
110
  end
111
  
112
  def show?
113
    !self.rolled_back || User.current.pref.hide_rolled_back_issue_notes == '0'
114
  end
115

  
116
  # rollback the changes in a journal, the journal is destroyed on success
117
  def rollback
118
    # we could have details to rollback, so let the issue take care of this more complicated task
119
    journalized.rollback(self) || 
120
    # on failure, collect the error messages from the issue on failure
121
      (journalized.errors.full_messages.each {|msg| errors.add :base, msg}; false)
122
  end
123

  
103 124
  def project
104 125
    journalized.respond_to?(:project) ? journalized.project : nil
105 126
  end
redmine/app/models/user_preference.rb 2015-05-21 11:53:25.620773743 -0300
57 57
  def warn_on_leaving_unsaved; self[:warn_on_leaving_unsaved] || '1'; end
58 58
  def warn_on_leaving_unsaved=(value); self[:warn_on_leaving_unsaved]=value; end
59 59

  
60
  def hide_rolled_back_issue_notes; self[:hide_rolled_back_issue_notes] || '0'; end
61
  def hide_rolled_back_issue_notes=(value); self[:hide_rolled_back_issue_notes]=value; end
62

  
60 63
  def no_self_notified; (self[:no_self_notified] == true || self[:no_self_notified] == '1'); end
61 64
  def no_self_notified=(value); self[:no_self_notified]=value; end
62 65
end
redmine/app/views/issues/_history.html.erb 2015-05-21 11:53:25.620773743 -0300
1 1
<% reply_links = authorize_for('issues', 'edit') -%>
2 2
<% for journal in journals %>
3
  <% if journal.show? %>
4

  
5
  <% if journal.rolled_back? %>
6
  <strike>
7
  <% end %>
8

  
3 9
  <div id="change-<%= journal.id %>" class="<%= journal.css_classes %>">
4 10
    <div id="note-<%= journal.indice %>">
5 11
    <h4><%= link_to "##{journal.indice}", {:anchor => "note-#{journal.indice}"}, :class => "journal-link" %>
12
    <% if journal.last_valid_journal?(journals) && journal.can_rollback? %>
13
       <div class="journal-link"><%= link_to(image_tag('cancel.png'), {:controller => 'journals', :action => 'rollback', :id => journal}, :confirm => l(:text_are_you_sure), :method => :post, :title => l(:button_rollback)) %>&nbsp;</div>
14
    <% end %>
6 15
    <%= avatar(journal.user, :size => "24") %>
7 16
    <%= authoring journal.created_on, journal.user, :label => :label_updated_time_by %>
8 17
    <%= content_tag('span', l(:field_is_private), :class => 'private') if journal.private_notes? %></h4>
......
18 27
    </div>
19 28
  </div>
20 29
  <%= call_hook(:view_issues_history_journal_bottom, { :journal => journal }) %>
30

  
31
  <% if journal.rolled_back? %>
32
  </strike>
33
  <% end %>
34

  
35
<% end %>
21 36
<% end %>
22 37

  
23 38
<% heads_for_wiki_formatter if User.current.allowed_to?(:edit_issue_notes, issue.project) || User.current.allowed_to?(:edit_own_issue_notes, issue.project) %>
redmine/app/views/users/_preferences.html.erb 2015-05-21 11:53:25.621773784 -0300
3 3
<p><%= pref_fields.time_zone_select :time_zone, nil, :include_blank => true %></p>
4 4
<p><%= pref_fields.select :comments_sorting, [[l(:label_chronological_order), 'asc'], [l(:label_reverse_chronological_order), 'desc']] %></p>
5 5
<p><%= pref_fields.check_box :warn_on_leaving_unsaved %></p>
6
<p><%= pref_fields.check_box :hide_rolled_back_issue_notes %></p>
6 7
<% end %>
redmine/config/locales/en.yml 2015-05-21 11:53:25.621773784 -0300
337 337
  field_inherit_members: Inherit members
338 338
  field_generate_password: Generate password
339 339
  field_must_change_passwd: Must change password at next logon
340
  field_hide_rolled_back_issue_notes: "Hide rolled-back issue notes"
340 341

  
341 342
  setting_app_title: Application title
342 343
  setting_app_subtitle: Application subtitle
......
474 475
  permission_export_wiki_pages: Export wiki pages
475 476
  permission_manage_subtasks: Manage subtasks
476 477
  permission_manage_related_issues: Manage related issues
478
  permission_rollback_issue_notes: Rollback issue notes
477 479

  
478 480
  project_module_issue_tracking: Issue tracking
479 481
  project_module_time_tracking: Time tracking
......
971 974
  button_delete_my_account: Delete my account
972 975
  button_close: Close
973 976
  button_reopen: Reopen
977
  button_rollback: Rollback
974 978

  
975 979
  status_active: active
976 980
  status_registered: registered
redmine/config/routes.rb 2015-05-21 11:53:25.622773824 -0300
51 51

  
52 52
  match '/journals/diff/:id', :to => 'journals#diff', :id => /\d+/, :via => :get
53 53
  match '/journals/edit/:id', :to => 'journals#edit', :id => /\d+/, :via => [:get, :post]
54
  match '/journals/rollback/:id', :to => 'journals#rollback', :id => /\d+/, :via => :post
54 55

  
55 56
  get '/projects/:project_id/issues/gantt', :to => 'gantts#show', :as => 'project_gantt'
56 57
  get '/issues/gantt', :to => 'gantts#show'
redmine/db/migrate/20140417000000_add_journals_rolled_back.rb 2014-04-17 10:33:21.238634641 -0600
1
class AddJournalsRolledBack < ActiveRecord::Migration
2
  def up
3
    add_column :journals, :rolled_back, :boolean, :default => 0
4
  end
5

  
6
  def down
7
    remove_column :journals, :rolled_back
8
  end
9
end
redmine/lib/redmine.rb 2015-05-21 11:53:25.623773864 -0300
112 112
    map.permission :add_issue_notes, {:issues => [:edit, :update, :update_form], :journals => [:new], :attachments => :upload}
113 113
    map.permission :edit_issue_notes, {:journals => :edit}, :require => :loggedin
114 114
    map.permission :edit_own_issue_notes, {:journals => :edit}, :require => :loggedin
115
    map.permission :rollback_issue_notes, {:journals => [:rollback]}
115 116
    map.permission :view_private_notes, {}, :read => true, :require => :member
116 117
    map.permission :set_notes_private, {}, :require => :member
117 118
    map.permission :move_issues, {:issues => [:bulk_edit, :bulk_update]}, :require => :loggedin