cirandas.net

ref: master

app/controllers/admin/trusted_sites_controller.rb


 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
class TrustedSitesController < AdminController
  protect 'manage_environment_trusted_sites', :environment

  def index
    @sites = environment.trusted_sites_for_iframe
  end

  def new
    @site = ""
  end

  def create
    if add_trusted_site(params[:site])
      session[:notice] = _('New trusted site added.')
      redirect_to :action => 'index'
    else
      session[:notice] = _('Failed to add trusted site.')
      render :action => 'new'
    end
  end

  def edit
    if is_trusted_site? params[:site]
      @site = params[:site]
    else
      session[:notice] = _('Trusted site was not found')
      redirect_to :action => 'index'
    end
  end

  def update
    site = params[:site]
    orig_site = params[:orig_site]
    if rename_trusted_site(orig_site, site)
      redirect_to :action => 'edit', :site => @site
    else
      session[:notice] = _('Failed to edit trusted site.')
      render :action => 'edit'
    end
  end

  def destroy
    if delete_trusted_site(params[:site])
      session[:notice] = _('Trusted site removed')
    else
      session[:notice] = _('Trusted site could not be removed')
    end
    redirect_to :action => 'index'
  end

  protected
  def add_trusted_site (site)
    trusted_sites = environment.trusted_sites_for_iframe
    trusted_sites << site
    environment.trusted_sites_for_iframe = trusted_sites
    environment.save
  end

  def rename_trusted_site(orig_site, site)
    trusted_sites = environment.trusted_sites_for_iframe
    i = trusted_sites.index orig_site
    if i.nil?
      return false
    else
      trusted_sites[i] = site
      environment.trusted_sites_for_iframe = trusted_sites
      environment.save
    end
  end


  def delete_trusted_site (site)
    trusted_sites = environment.trusted_sites_for_iframe
    trusted_sites.delete site
    environment.trusted_sites_for_iframe = trusted_sites
    environment.save
  end

  def is_trusted_site? (site)
    environment.trusted_sites_for_iframe.include? site
  end
end