-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_pass_forms_controller.rb
More file actions
91 lines (71 loc) · 2.04 KB
/
stack_pass_forms_controller.rb
File metadata and controls
91 lines (71 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'time'
class StackPassFormsController < ApplicationController
self.support_email = 'privdesk-library@berkeley.edu'
before_action :init_form!
def index
redirect_with_params(action: :new)
end
# TODO: do we still need this?
def forbidden; end
def result; end
# Show an admin the request
def show
require_admin!
@current_user = current_user
@req = StackPassForm.find(params[:id])
@approvals = @req.approval_count
@denials = @req.denial_count
end
# rubocop:disable Metrics/MethodLength
def create
validate_recaptcha!
if @form.save
@form.submit!
render 'result', status: :created
else
flash[:danger] = @form.errors.to_a
redirect_with_params(action: :new)
end
rescue Recaptcha::RecaptchaError
flash[:danger] = t('.recaptcha')
redirect_with_params(action: :new)
end
# Approve || Deny Request
# rubocop:disable Metrics/AbcSize
def update
require_admin!
@form = StackPassForm.find(params[:id])
@form.approvedeny = params[:stack_pass_][:approve_deny]
@form.processed_by = params[:processed_by]
if @form.approvedeny == false
deny_reason = params[:denial_reason]
deny_reason = params[:stack_pass_denial][:denial_reason] if deny_reason.empty?
@form.denial_reason = deny_reason
@form.deny!
else
@form.approve!
end
@form.save!
flash[:success] = 'Request has been successfully processed'
redirect_to(action: :show, id: params[:id])
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
private
def form_params
params.require(:stack_pass_form).permit(:name, :email, :phone, :local_id, :main_stack, :pass_date)
rescue ActionController::ParameterMissing
{}
end
def init_form!
@form = StackPassForm.new
return if form_params.empty?
@form.attributes = form_params
@form.validate
end
def validate_recaptcha!
verify_recaptcha!(model: @form)
end
def require_admin!
@user_is_admin = authenticate_with_role!(Role.stackpass_admin, :framework_admin)
end
end