-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathusers_controller_management_concern.rb
More file actions
247 lines (203 loc) · 6.64 KB
/
users_controller_management_concern.rb
File metadata and controls
247 lines (203 loc) · 6.64 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# frozen_string_literal: true
module Course::UsersControllerManagementConcern
extend ActiveSupport::Concern
include Course::LessonPlan::PersonalizationConcern
include Signals::EmissionConcern
included do
before_action :authorize_show!, only: [:students, :staff, :requests, :invitations]
before_action :authorize_edit!, only: [:update, :destroy, :upgrade_to_staff, :assign_timeline, :suspend, :unsuspend]
signals :enrol_requests, after: [:students]
end
def update
@course_user.assign_attributes(course_user_params)
update_personalized_timeline_for_user(@course_user) if should_update_personalized_timeline
if @course_user.save
update_user_success
else
update_user_failure
end
end
def destroy
if @course_user.destroy
destroy_user_success
else
destroy_user_failure
end
end
def students
respond_to do |format|
format.json do
@course_users = @course_users.students.includes(user: :primary_email).order_alphabetically
end
end
end
def staff
respond_to do |format|
format.json do
@student_options = @course_users.students.order_alphabetically.pluck(:id, :name, :role)
@course_users = @course_users.staff.includes(user: :primary_email).order_alphabetically
end
end
end
def upgrade_to_staff
upgrade_to_staff_params
if upgrade_students_to_staff
upgrade_to_staff_success
else
upgrade_to_staff_failure
end
end
def assign_timeline
course_user_ids = assign_timeline_params[:ids]
timeline_id = assign_timeline_params[:reference_timeline_id]
timeline = Course::ReferenceTimeline.find(timeline_id)
ActiveRecord::Base.transaction do
updated_course_users = []
@course_users.where(id: course_user_ids).find_each do |course_user|
course_user.reference_timeline = timeline
updated_course_users << course_user
end
raise unless updated_course_users.size == course_user_ids.size
CourseUser.import! updated_course_users, on_duplicate_key_update: [:reference_timeline_id]
head :ok
end
rescue StandardError
head :bad_request
end
def suspend
course_user_ids = suspend_params[:ids]
ActiveRecord::Base.transaction do
to_suspend = current_course.course_users.where(id: course_user_ids).includes(user: :primary_email)
return head :bad_request unless to_suspend.size == course_user_ids.size
to_notify = to_suspend.reject(&:is_suspended?)
to_suspend.update_all(is_suspended: true)
to_notify.each { |cu| Course::Mailer.user_suspended_email(cu).deliver_later }
head :ok
end
rescue StandardError
head :bad_request
end
def unsuspend
course_user_ids = unsuspend_params[:ids]
ActiveRecord::Base.transaction do
to_unsuspend = current_course.course_users.where(id: course_user_ids).includes(user: :primary_email)
return head :bad_request unless to_unsuspend.size == course_user_ids.size
to_notify = to_unsuspend.select(&:is_suspended?)
to_unsuspend.update_all(is_suspended: false)
to_notify.each { |cu| Course::Mailer.user_unsuspended_email(cu).deliver_later }
head :ok
end
rescue StandardError
head :bad_request
end
private
def should_update_personalized_timeline
@course_user.timeline_algorithm_changed? || @course_user.reference_timeline_id_changed?
end
def course_user_params
@course_user_params ||= params.require(:course_user).permit(
:user_id, :name, :timeline_algorithm, :role, :phantom, :reference_timeline_id, :external_id
)
end
def upgrade_to_staff_params
@upgrade_to_staff_params ||= params.require(:course_users).permit(:role, ids: [])
params.require(:user).permit(:id)
end
def assign_timeline_params
params.require(:course_users).permit(:reference_timeline_id, ids: [])
end
def suspend_params
params.require(:course_users).permit(ids: [])
end
def unsuspend_params
params.require(:course_users).permit(ids: [])
end
def load_resource
course_users = current_course.course_users
case params[:action]
when 'invitations', 'assign_timeline'
@course_users ||= course_users
when 'students', 'staff'
@course_users ||= course_users.includes(:user)
when 'upgrade_to_staff'
@course_user ||= course_users.includes(:user).find(upgrade_to_staff_params[:id])
end
end
def upgrade_students_to_staff
role = @upgrade_to_staff_params[:role]
course_users = current_course.course_users
@upgraded_course_users = []
@upgrade_to_staff_params[:ids].each do |id|
course_user = course_users.find(id)
course_user.update(role: role)
@upgraded_course_users << course_user.reload
end
true
end
# Prevents access to this set of pages unless the user is a staff of the course.
def authorize_show!
authorize!(:show_users, current_course)
end
# Prevents access to this set of pages unless the user is a staff of the course.
def authorize_edit!
authorize!(:manage_users, current_course)
end
# Deduces which page the update request originated from.
def update_request_origin
@update_request_origin ||=
if course_user_params.key?(:role)
:staff
else
:students
end
end
# Selects an appropriate redirect path depending on the user being deleted.
def delete_redirect_path
if @course_user.staff?
course_users_staff_path(current_course)
else
course_users_students_path(current_course)
end
end
def upgrade_to_staff_success
respond_to do |format|
format.json do
render partial: 'upgrade_to_staff_results', locals: {
upgraded_course_users: @upgraded_course_users
}, status: :ok
end
end
end
def upgrade_to_staff_failure
respond_to do |format|
format.json { render json: { errors: @course_user.errors.full_messages.to_sentence }, status: :bad_request }
end
end
def update_user_success
respond_to do |format|
format.json do
render '_user_list_data', locals: {
course_user: @course_user,
should_show_timeline: true,
should_show_phantom: true,
groups: nil
}, status: :ok
end
end
end
def update_user_failure
respond_to do |format|
format.json { render json: { errors: @course_user.errors.full_messages.to_sentence }, status: :bad_request }
end
end
def destroy_user_success
respond_to do |format|
format.json { head :ok }
end
end
def destroy_user_failure
respond_to do |format|
format.json { render json: { errors: @course_user.errors.full_messages.to_sentence }, status: :bad_request }
end
end
end