-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_pass_admin_controller.rb
More file actions
81 lines (64 loc) · 2.31 KB
/
stack_pass_admin_controller.rb
File metadata and controls
81 lines (64 loc) · 2.31 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
require 'time'
class StackPassAdminController < AuthenticatedFormController
helper_method :sort_column, :sort_direction
before_action :require_admin!
def admin; end
def stackpasses
@requests = StackPassForm.where('pass_date > ?', start_school_year).order("#{sort_column} #{sort_direction}")
end
def refcards
@requests = ReferenceCardForm.where('pass_date_end > ?', start_calendar_year).order("#{sort_column} #{sort_direction}")
end
def users
@users = Role.stackpass_admin.framework_users.order(:name)
end
def add_user
# First check if the user exists in framework user - or create the user
framework_user = FrameworkUsers.find_or_create_by(lcasid: params['lcasid']) do |new_user|
new_user.name = params['name']
new_user.role = 'Admin'
end
# Now that we have the user, create the assignment:
Assignment.create(framework_users: framework_user, role: Role.stackpass_admin)
# And redirect back to the admin users page:
flash[:success] = "Added #{framework_user.name} as an administrator"
redirect_to forms_stack_pass_admin_users_path
end
def destroy_user
# First grab the user (so we can grab the name!)
admin = FrameworkUsers.find(params[:id])
admin_name = admin.name
# Now lets find the assignment
user_assignment = Assignment.where(framework_users: admin, role: Role.stackpass_admin).first
user_assignment.destroy
flash[:success] = "Removed #{admin_name} from administrator list"
redirect_to forms_stack_pass_admin_users_path
end
private
def init_form!; end
# You shall not pass....unless you're an admin
def require_admin!
@user_is_admin = current_user.any_role?(Role.stackpass_admin, :framework_admin)
redirect_to stack_pass_forms_path unless @user_is_admin
end
def sort_column
# only allow column names as sorting param
StackRequest.column_names.include?(params[:sort]) ? params[:sort] : 'created_at'
end
def sort_direction
# only allow asc||desc for direction param
%w[asc desc].include?(params[:direction]) ? params[:direction] : 'desc'
end
def start_school_year
today = Date.current
mo = today.month
yr = today.year
yr -= 1 if mo < 7
Date.new(yr, 7, 1)
end
def start_calendar_year
today = Date.current
yr = today.year
Date.new(yr, 1, 1)
end
end