Allow reporting of anonymous notes
[rails.git] / app / controllers / issues_controller.rb
1 class IssuesController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :require_user
6   before_action :set_issues
7   before_action :check_permission
8   before_action :find_issue, :only => [:show, :resolve, :reopen, :ignore]
9
10   def index
11     @title = t ".title"
12
13     if current_user.moderator?
14       @issue_types = @moderator_issues
15       @users = User.joins(:roles).where(:user_roles => { :role => "moderator" })
16     else
17       @issue_types = @admin_issues
18       @users = User.joins(:roles).where(:user_roles => { :role => "administrator" })
19     end
20
21     @issues = Issue.where(:assigned_role => current_user.roles.map(&:role))
22
23     # If search
24     if params[:search_by_user] && params[:search_by_user].present?
25       @find_user = User.find_by(:display_name => params[:search_by_user])
26       if @find_user
27         @issues = @issues.where(:reported_user_id => @find_user.id)
28       else
29         notice = t("issues.index.user_not_found")
30       end
31     end
32
33     if params[:status] && params[:status][0].present?
34       @issues = @issues.where(:status => params[:status][0])
35     end
36
37     if params[:issue_type] && params[:issue_type][0].present?
38       @issues = @issues.where(:reportable_type => params[:issue_type][0])
39     end
40
41     if params[:last_updated_by] && params[:last_updated_by][0].present?
42       last_updated_by = params[:last_updated_by][0].to_s == "nil" ? nil : params[:last_updated_by][0].to_i
43       @issues = @issues.where(:updated_by => last_updated_by)
44     end
45
46     redirect_to issues_path, :notice => notice if notice
47   end
48
49   def show
50     @read_reports = @issue.read_reports
51     @unread_reports = @issue.unread_reports
52     @comments = @issue.comments
53     @related_issues = @issue.reported_user.issues.where(:assigned_role => current_user.roles.map(&:role)) if @issue.reported_user
54     @new_comment = IssueComment.new(:issue => @issue)
55   end
56
57   # Status Transistions
58   def resolve
59     if @issue.resolve
60       @issue.save!
61       redirect_to @issue, :notice => t("issues.resolved")
62     else
63       render :show
64     end
65   end
66
67   def ignore
68     if @issue.ignore
69       @issue.updated_by = current_user.id
70       @issue.save!
71       redirect_to @issue, :notice => t("issues.ignored")
72     else
73       render :show
74     end
75   end
76
77   def reopen
78     if @issue.reopen
79       @issue.updated_by = current_user.id
80       @issue.save!
81       redirect_to @issue, :notice => t("issues.reopened")
82     else
83       render :show
84     end
85   end
86
87   private
88
89   def set_issues
90     @admin_issues = %w[DiaryEntry DiaryComment User]
91     @moderator_issues = %w[Changeset Note]
92   end
93
94   def check_if_updated
95     if @issue.reportable && (@issue.ignored? || @issue.resolved?) && @issue.reportable.has_attribute?(:updated_by) && @issue.reportable.updated_at > @last_report.updated_at
96       true
97     else
98       false
99     end
100   end
101
102   def find_issue
103     @issue = Issue.find(params[:id])
104   end
105
106   def check_permission
107     unless current_user.administrator? || current_user.moderator?
108       flash[:error] = t("application.require_admin.not_an_admin")
109       redirect_to root_path
110     end
111   end
112
113   def issue_params
114     params[:issue].permit(:reportable_id, :reportable_type)
115   end
116
117   def report_params
118     params[:report].permit(:details)
119   end
120
121   def issue_comment_params
122     params.require(:issue_comment).permit(:body)
123   end
124
125   def sort_column
126     Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
127   end
128
129   def sort_direction
130     %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
131   end
132 end