Added authorization + issues dashboard
[rails.git] / app / controllers / issues_controller.rb
1 class IssuesController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :check_permission, only: [:index, :show, :resolve,:open,:ignore]
6   before_action :find_issue, only: [:show, :resolve, :reopen, :ignore]
7
8   def index
9     @issues = Issue.all
10   end
11
12   def show
13     @read_reports = @issue.read_reports
14     @unread_reports = @issue.unread_reports
15   end
16
17   def new
18     unless create_new_issue_params.blank?
19       @issue = Issue.find_or_initialize_by(create_new_issue_params)
20     end
21   end
22
23   def create
24     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type])
25     if !@issue 
26       @issue = Issue.find_or_initialize_by(issue_params)
27       @admins = UserRole.where(role: "administrator")
28       @admins.each do |user|
29         Notifier.new_issue_notification(User.find(user.user_id)).deliver_now
30       end
31     end
32
33     @report = @issue.reports.build(report_params)
34
35     if @issue.save
36       redirect_to @issue, notice: 'Issue was successfully created.'
37     else
38       render :new
39     end
40   end
41
42   # Status Transistions
43   def resolve
44     if @issue.resolve
45       @issue.save!
46       redirect_to @issue, notice: "Issue status has been set to: 'Resolved'"
47     else
48       render :show
49     end
50   end
51
52   def ignore
53     if @issue.ignore
54       @issue.save!
55       redirect_to @issue, notice: "Issue status has been set to: 'Ignored'"
56     else
57       render :show
58     end
59   end
60
61   def reopen
62     if @issue.reopen
63       @issue.save!
64       redirect_to @issue, notice: "Issue status has been set to: 'Open'"
65     else
66       render :show
67     end
68   end
69
70   private
71
72     def find_issue
73       @issue = Issue.find(params[:id])
74     end
75
76     def check_permission
77       unless @user.administrator?
78         flash[:error] = t("application.require_admin.not_an_admin")
79         redirect_to root_path
80       end
81     end
82
83     def create_new_issue_params
84       params.permit(:reportable_id, :reportable_type, :user_id)
85     end
86
87     def issue_params
88       params[:issue].permit(:reportable_id, :reportable_type,:user_id)
89     end
90
91     def report_params
92       params[:report].permit(:details, :user_id)
93     end
94 end