Class: Caboose::SitesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/sites_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_json, #admin_json_single, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject

POST /admin/sites



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
# File 'app/controllers/caboose/sites_controller.rb', line 105

def admin_add
  return if !user_is_allowed('sites', 'add')
  render :json => { :error => "You are not allowed to manage sites." } and return if !@site.is_master
          
  resp = StdClass.new      
  site = Site.new
  site.name = params[:name].strip
  
  if site.name.length == 0
    resp.error = "Please enter a valid domain."      
  else        
    site.save
    StoreConfig.create(:site_id => site.id)
    SmtpConfig.create( :site_id => site.id)
    resp.redirect = "/admin/sites/#{site.id}"
  end
  
  # Create an admin user for the account
  if !User.where(:username => 'admin', :site_id => site.id).exists?        
    admin_user = User.create(:username => 'admin', :email => '[email protected]', :site_id => site.id, :password => Digest::SHA1.hexdigest(Caboose::salt + 'caboose'))
    admin_role = Role.where(:name => 'Admin').first
    if admin_role
      RoleMembership.create(:user_id => admin_user.id, :role_id => admin_role.id)        
    end
  end
  
  render :json => resp
end

#admin_add_memberObject

POST /admin/sites/:id/members



190
191
192
193
194
195
196
197
198
199
# File 'app/controllers/caboose/sites_controller.rb', line 190

def admin_add_member
  return if !user_is_allowed('sites', 'edit')
  render :json => { :error => "You are not allowed to manage sites." } and return if !@site.is_master
  
  sm = SiteMembership.where(:site_id => params[:id], :user_id => params[:user_id]).first
  sm = SiteMembership.create(:site_id => params[:id], :user_id => params[:user_id]) if sm.nil?
  sm.role = params[:role]
  sm.save      
  render :json => true
end

#admin_default_layout_optionsObject

GET /admin/sites/:id/default-layout-options



220
221
222
223
224
225
226
227
228
# File 'app/controllers/caboose/sites_controller.rb', line 220

def admin_default_layout_options
  return if !user_is_allowed('sites', 'view')
  cat_ids = Caboose::BlockTypeCategory.layouts.collect{ |cat| cat.id }
  block_types = Caboose::BlockType.includes(:block_type_site_memberships).where("block_type_category_id in (?) and block_type_site_memberships.site_id = ?", cat_ids, params[:id]).reorder(:description).all
  options = block_types.collect do |bt|
    { 'value' => bt.id, 'text' => bt.description } 
  end      
  render :json => options
end

#admin_deleteObject

DELETE /admin/sites/:id



176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/controllers/caboose/sites_controller.rb', line 176

def admin_delete
  return if !user_is_allowed('sites', 'delete')
  render :json => { :error => "You are not allowed to manage sites." } and return if !@site.is_master
  
  site = Site.find(params[:id])
  site.destroy
  
  resp = StdClass.new({
    'redirect' => '/admin/sites'
  })
  render :json => resp
end

#admin_delete_formObject

GET /admin/sites/:id/delete



95
96
97
98
99
100
101
102
# File 'app/controllers/caboose/sites_controller.rb', line 95

def admin_delete_form
  return if !user_is_allowed('sites', 'edit')
  if !@site.is_master
    @error = "You are not allowed to manage sites."
    render :file => 'caboose/extras/error' and return
  end
  @site = Site.find(params[:id])      
end

#admin_editObject

GET /admin/sites/:id



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/caboose/sites_controller.rb', line 43

def admin_edit
  return if !user_is_allowed('sites', 'edit')
  if !@site.is_master
    @error = "You are not allowed to manage sites."
    render :file => 'caboose/extras/error' and return
  end
  
  @site = Site.find(params[:id])      
  
  # Create an admin user for the account
  if !User.where(:username => 'admin', :site_id => @site.id).exists?
    admin_user = User.create(:username => 'admin', :email => '[email protected]', :site_id => @site.id, :password => Digest::SHA1.hexdigest(Caboose::salt + 'caboose'))        
    admin_role = Role.where(:name => 'Admin').first        
    if admin_user && admin_role                      
      RoleMembership.create(:user_id => admin_user.id, :role_id => admin_role.id)          
    end
  end
  
end

#admin_edit_block_typesObject

GET /admin/sites/:id/block-types



64
65
66
67
68
69
70
71
72
# File 'app/controllers/caboose/sites_controller.rb', line 64

def admin_edit_block_types
  return if !user_is_allowed('sites', 'edit')
  if !@site.is_master
    @error = "You are not allowed to manage sites."
    render :file => 'caboose/extras/error' and return
  end
  
  @site = Site.find(params[:id])      
end

#admin_edit_cssObject

GET /admin/sites/:id/css



75
76
77
78
79
80
81
82
# File 'app/controllers/caboose/sites_controller.rb', line 75

def admin_edit_css
  return if !user_is_allowed('sites', 'edit')
  if !@site.is_master
    @error = "You are not allowed to manage sites."
    render :file => 'caboose/extras/error' and return
  end      
  @site = Site.find(params[:id])      
end

#admin_edit_jsObject

GET /admin/sites/:id/js



85
86
87
88
89
90
91
92
# File 'app/controllers/caboose/sites_controller.rb', line 85

def admin_edit_js
  return if !user_is_allowed('sites', 'edit')
  if !@site.is_master
    @error = "You are not allowed to manage sites."
    render :file => 'caboose/extras/error' and return
  end      
  @site = Site.find(params[:id])      
end

#admin_indexObject

GET /admin/sites



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/caboose/sites_controller.rb', line 12

def admin_index
  return if !user_is_allowed('sites', 'view')
  if !@site.is_master
    @error = "You are not allowed to manage sites."
    render :file => 'caboose/extras/error' and return
  end
  
  @pager = PageBarGenerator.new(params, {
		  'name_like' => '',    		  
		},{
		  'model'          => 'Caboose::Site',
	    'sort'			     => 'name',
		  'desc'			     => false,
		  'base_url'		   => '/admin/sites',
		  'use_url_params' => false
	})
	@sites = @pager.items
end

#admin_newObject

GET /admin/sites/new



32
33
34
35
36
37
38
39
40
# File 'app/controllers/caboose/sites_controller.rb', line 32

def admin_new
  return if !user_is_allowed('sites', 'add')
  if !@site.is_master
    @error = "You are not allowed to manage sites."
    render :file => 'caboose/extras/error' and return
  end
  
  @site = Site.new
end

#admin_remove_memberObject

DELETE /admin/sites/:id/members/:user_id



202
203
204
205
206
207
208
# File 'app/controllers/caboose/sites_controller.rb', line 202

def admin_remove_member
  return if !user_is_allowed('sites', 'edit')
  render :json => { :error => "You are not allowed to manage sites." } and return if !@site.is_master
  
  SiteMembership.where(:site_id => params[:id], :user_id => params[:user_id]).destroy_all        
  render :json => true
end

#admin_updateObject

PUT /admin/sites/:id



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/controllers/caboose/sites_controller.rb', line 135

def admin_update
  return if !user_is_allowed('sites', 'edit')
  render :json => { :error => "You are not allowed to manage sites." } and return if !@site.is_master

  resp = StdClass.new     
  site = Site.find(params[:id])

  save = true
  params.each do |name,value|
    case name
      when 'name'                     then site.name                    = value
      when 'description'              then site.description             = value
      when 'under_construction_html'  then site.under_construction_html = value
      when 'use_store'                then site.use_store               = value
      when 'use_retargeting'          then site.use_retargeting         = value
      when 'custom_css'               then site.custom_css              = value            
      when 'custom_js'                then site.custom_js               = value
      when 'default_layout_id'        then site.default_layout_id       = value
	  end
	end
	
	resp.success = save && site.save
	render :json => resp
end

#admin_update_logoObject

POST /admin/sites/:id/logo



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/caboose/sites_controller.rb', line 161

def 
  return if !user_is_allowed('sites', 'edit')
  render :json => { :error => "You are not allowed to manage sites." } and return if !@site.is_master
  
  site = Site.find(params[:id])       
  site. = params[:logo]
  site.save
  
  resp = StdClass.new
  resp.success = true
  resp.attributes = { :image => { :value => site..url(:thumb) }}
  render :json => resp
end

#before_actionObject



7
8
9
# File 'app/controllers/caboose/sites_controller.rb', line 7

def before_action
  @page = Page.page_with_uri(request.host_with_port, '/admin')
end

#optionsObject

GET /admin/sites/options



211
212
213
214
215
216
217
# File 'app/controllers/caboose/sites_controller.rb', line 211

def options
  return if !user_is_allowed('sites', 'view')
  render :json => { :error => "You are not allowed to manage sites." } and return if !@site.is_master
  
  options = Site.reorder('name').all.collect { |s| { 'value' => s.id, 'text' => s.name }}
  render :json => options
end