forked from Katello/katello
/
gpg_keys_controller.rb
188 lines (150 loc) · 5.89 KB
/
gpg_keys_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
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
#
# Copyright 2011 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
class GpgKeysController < ApplicationController
include AutoCompleteSearch
before_filter :require_user
before_filter :find_gpg_key, :only => [:show, :edit, :update, :destroy, :products_repos]
before_filter :authorize
before_filter :panel_options, :only => [:index, :items]
before_filter :search_filter, :only => [:auto_complete_search]
respond_to :html, :js
def section_id
'contents'
end
def rules
read_test = lambda{@gpg_key.readable?}
manage_test = lambda{@gpg_key.manageable?}
create_test = lambda{current_organization && GpgKey.createable?(current_organization)}
index_test = lambda{current_organization && GpgKey.any_readable?(current_organization)}
{
:index => index_test,
:items => index_test,
:show => read_test,
:products_repos => read_test,
:auto_complete_search => index_test,
:new => create_test,
:create => create_test,
:edit => read_test,
:update => manage_test,
:destroy => manage_test
}
end
def items
render_panel_direct(GpgKey, @panel_options, params[:search], params[:offset], [:name_sort, :asc],
:filter=>{:organization_id=>[current_organization.id]})
end
def show
render :partial=>"common/list_update", :locals=>{:item=>@gpg_key, :accessor=>"id", :columns=>['name']}
end
def new
render :partial => "new", :layout => "tupane_layout"
end
def edit
render :partial => "edit", :layout => "tupane_layout", :locals => {:editable => @gpg_key.manageable?,
:name => controller_display_name }
end
def products_repos
products = @gpg_key.products
repos_hash = {}
@gpg_key.repositories.each do |repo|
repos_hash[repo.environment_product.product.name] ||= []
repos_hash[repo.environment_product.product.name] << repo
end
products_repos = repos_hash.sort_by{|product, repo| product}
render :partial => "products_repos", :layout => "tupane_layout",
:locals => {:products => products, :products_repos => products_repos}
end
def create
gpg_key_params = params[:gpg_key]
if params[:gpg_key].has_key?("content_upload") and not params[:gpg_key].has_key?("content")
gpg_key_params['content'] = params[:gpg_key][:content_upload].read
gpg_key_params.delete('content_upload')
end
@gpg_key = GpgKey.create!( gpg_key_params.merge({ :organization => current_organization }) )
notice _("GPG Key '#{@gpg_key['name']}' was created.")
if search_validate(GpgKey, @gpg_key.id, params[:search])
render :partial=>"common/list_item", :locals=>{:item=>@gpg_key, :accessor=>"id", :columns=>['name'], :name=>controller_display_name}
else
notice _("'#{@gpg_key["name"]}' did not meet the current search criteria and is not being shown."), { :level => 'message', :synchronous_request => false }
render :json => { :no_match => true }
end
rescue Exception => error
Rails.logger.error error.to_s
return_error = notice(error, {:level => :error})
render :json => return_error.to_json, :status => :bad_request
end
def update
gpg_key_params = params[:gpg_key]
if params[:gpg_key].has_key?("content_upload") and not params[:gpg_key].has_key?("content")
gpg_key_params['content'] = params[:gpg_key][:content_upload].read
gpg_key_params.delete('content_upload')
end
@gpg_key.update_attributes!(gpg_key_params)
notice _("GPG Key '#{@gpg_key["name"]}' was updated.")
if not search_validate(GpgKey, @gpg_key.id, params[:search])
notice _("'#{@gpg_key["name"]}' no longer matches the current search criteria."), { :level => :message, :synchronous_request => true }
end
render :text => escape_html(gpg_key_params.values.first)
rescue Exception => error
notice error, {:level => :error}
respond_to do |format|
format.js { render :partial => "layouts/notification", :status => :bad_request, :content_type => 'text/html' and return}
end
end
def destroy
begin
@gpg_key.destroy
if @gpg_key.destroyed?
notice _("GPG Key '#{@gpg_key[:name]}' was deleted.")
render :partial => "common/list_remove", :locals => {:id=>params[:id], :name=>controller_display_name}
else
raise
end
rescue Exception => e
notice e, {:level => :error}
end
end
protected
def find_gpg_key
begin
@gpg_key = GpgKey.find(params[:id])
rescue Exception => error
notice error.to_s, {:level => :error}
# flash_to_headers is an after_filter executed on the application controller;
# however, a render from within a before_filter will halt the filter chain.
# as a result, we are explicitly executing it here.
flash_to_headers
render :text => error, :status => :bad_request
end
end
def panel_options
@panel_options = {
:title => _('GPG Keys'),
:col => ['name'],
:titles => [_('Name')],
:create => _('GPG Key'),
:name => controller_display_name,
:ajax_load => true,
:ajax_scroll => items_gpg_keys_path(),
:initial_action=> :products_repos,
:enable_create => GpgKey.createable?(current_organization),
:search_class=>GpgKey
}
end
private
def controller_display_name
return 'gpg_key'
end
def search_filter
@filter = {:organization_id => current_organization}
end
end