Hide public communty.
[ecs.git] / app / controllers / configs_controller.rb
blob8acf048e9ac286c8c552d7c6d75f3aa50855314e
1 class ConfigsController < ApplicationController
3   before_action :authentication
4   before_action :add_cookie_header # only for anonymous participants
6   def initialize
7     super
8   end
10   def index
11     config_render
12   end
14   def create
15     unless Mime::Type.lookup(request.headers["CONTENT_TYPE"]) =~ "application/json"
16       raise Ecs::InvalidMimetypeException, "Please provide \"Content-Type:\" header. Data format has to be in JSON (application/json)"
17     end
19     config= ActiveSupport::JSON.decode request.raw_post
21     if config["participant_events"] == true
22       @participant.events_= true
23     else
24       @participant.events_= false
25     end unless config["participant_events"].nil?
27     if config["selfrouting"] == true
28       @participant.community_selfrouting= true
29     else
30       @participant.community_selfrouting= false
31     end unless config["selfrouting"].nil?
33     @participant.save!
34     config_render
35   #rescue ActiveSupport::OkJson::Error
36   rescue Ecs::InvalidMimetypeException
37     raise
38   rescue ActiveRecord::RecordInvalid
39     raise Ecs::InvalidMessageException, "Data could not be saved (ConfigsController#create)."
40   rescue StandardError
41     raise Ecs::InvalidMessageException, "You have provided invalid JSON data (ConfigsController#create)."
42   end
44 private
46   def config_render
47     config = nil
48     config_txt = ""
49     res_ev={};Ressource.all.each {|r| res_ev["/#{r.namespace}/#{r.ressource}"] = r.events}
50     config= \
51       { 
52         :participant_events => @participant.events?,
53         :resource_events => res_ev,
54         :selfrouting => @participant.community_selfrouting
55       }
56     respond_to do |format|
57       format.json { render :json  => JSON.pretty_generate(config) + "\r\n" }
58       format.xml  { render :xml   => config }
59     end
60   end
62 end