2011-07-26 00:12:55 +08:00
|
|
|
#
|
2017-04-28 03:10:30 +08:00
|
|
|
# Copyright (C) 2011 - present Instructure, Inc.
|
2011-07-26 00:12:55 +08:00
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
class AlertsController < ApplicationController
|
2017-02-09 01:08:10 +08:00
|
|
|
before_action :require_context
|
2011-07-26 00:12:55 +08:00
|
|
|
|
|
|
|
def create
|
2013-09-18 08:08:19 +08:00
|
|
|
if authorized_action(@context, @current_user, :manage_interaction_alerts)
|
2014-09-08 20:48:45 +08:00
|
|
|
convert_recipients
|
2016-10-13 22:04:21 +08:00
|
|
|
@alert = @context.alerts.build(alert_params)
|
2011-07-26 00:12:55 +08:00
|
|
|
if @alert.save
|
|
|
|
headers['Location'] = named_context_url(@context, :context_alert_url, @alert.id)
|
2013-08-23 06:22:14 +08:00
|
|
|
render :json => @alert.as_json(:include => :criteria)
|
2011-07-26 00:12:55 +08:00
|
|
|
else
|
2013-08-23 06:22:14 +08:00
|
|
|
render :json => @alert.errors, :status => :bad_request
|
2011-07-26 00:12:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2013-09-18 08:08:19 +08:00
|
|
|
if authorized_action(@context, @current_user, :manage_interaction_alerts)
|
2014-09-08 20:48:45 +08:00
|
|
|
convert_recipients
|
2011-07-26 00:12:55 +08:00
|
|
|
@alert = @context.alerts.find(params[:id])
|
2016-10-13 22:04:21 +08:00
|
|
|
if @alert.update_attributes(alert_params)
|
2011-07-26 00:12:55 +08:00
|
|
|
headers['Location'] = named_context_url(@context, :context_alert_url, @alert.id)
|
2013-08-23 06:22:14 +08:00
|
|
|
render :json => @alert.as_json(:include => :criteria)
|
2011-07-26 00:12:55 +08:00
|
|
|
else
|
2013-08-23 06:22:14 +08:00
|
|
|
render :json => @alert.errors, :status => :bad_request
|
2011-07-26 00:12:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2013-09-18 08:08:19 +08:00
|
|
|
if authorized_action(@context, @current_user, :manage_interaction_alerts)
|
2011-07-26 00:12:55 +08:00
|
|
|
@alert = @context.alerts.find(params[:id])
|
|
|
|
@alert.destroy
|
2013-08-23 06:22:14 +08:00
|
|
|
render :json => @alert
|
2011-07-26 00:12:55 +08:00
|
|
|
end
|
|
|
|
end
|
2014-09-08 20:48:45 +08:00
|
|
|
|
|
|
|
protected
|
|
|
|
def convert_recipients
|
2017-01-13 04:24:07 +08:00
|
|
|
params[:alert][:recipients] = params[:alert][:recipients].to_a.map do |r|
|
2014-09-08 20:48:45 +08:00
|
|
|
if r.is_a?(String) && r[0] == ':'
|
|
|
|
r[1..-1].to_sym
|
|
|
|
elsif role = (@context.is_a?(Account) ? @context.get_role_by_id(r) : @context.account.get_role_by_id(r))
|
|
|
|
{:role_id => role.id}
|
|
|
|
end
|
|
|
|
end.flatten
|
|
|
|
end
|
2016-10-13 22:04:21 +08:00
|
|
|
|
|
|
|
def alert_params
|
2017-01-13 04:24:07 +08:00
|
|
|
params.require(:alert).
|
2016-10-13 22:04:21 +08:00
|
|
|
permit(:context, :repetition, :criteria => [:criterion_type, :threshold], :recipients => strong_anything)
|
|
|
|
end
|
2011-07-26 00:12:55 +08:00
|
|
|
end
|