Class: Katello::ApplicationController

Inherits:
ApplicationController
  • Object
show all
Includes:
HomeHelper, Menu
Defined in:
app/controllers/katello/application_controller.rb

Instance Method Summary collapse

Instance Method Details

#authorizedObject

Override Foreman authorized method to support permissions on meta controllers that handle multiple routes



85
86
87
88
89
90
91
# File 'app/controllers/katello/application_controller.rb', line 85

def authorized
  if self.respond_to? :permission_controller
    User.current.allowed_to?(params.slice(:action, :id).merge(:controller => permission_controller))
  else
    super
  end
end

#current_organizationObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/katello/application_controller.rb', line 177

def current_organization
  if !session[:current_organization_id]
    @current_org = Organization.current
    return @current_org
  else
    begin
      if @current_org.nil? && current_user
        o = Organization.find(session[:current_organization_id])
        if current_user.allowed_organizations.include?(o)
          @current_org = o
        else
          fail ActiveRecord::RecordNotFound, _("Permission Denied. User '%{user}' does not have permissions to access organization '%{org}'.") % {:user => User.current., :org => o.name}
        end
      end
      return @current_org
    rescue ActiveRecord::RecordNotFound => error
      log_exception error
      session.delete(:current_organization_id)
      org_not_found_error
    end
  end
end

#current_organization=(org) ⇒ Object



200
201
202
# File 'app/controllers/katello/application_controller.rb', line 200

def current_organization=(org)
  session[:current_organization_id] = org.try(:id)
end

#default_labelObject

‘default_label’ is an action that is used to allow the UI to retrieve a label that is generated by the server based upon a name provided. This action is used by several controllers, but not all. For those controllers that do use it, the controller will determine if access to it is allowed based on the permissions required by that controller.



102
103
104
105
106
107
108
# File 'app/controllers/katello/application_controller.rb', line 102

def default_label
  if params[:name]
    render :text => Util::Model.labelize(params[:name])
  else
    render :nothing => true
  end
end

#default_label_assigned(new_object) ⇒ Object

Generate a message that may be sent to the user (e.g. via a notice) to inform them that a label was automatically assigned to the object.



143
144
145
146
147
148
149
150
# File 'app/controllers/katello/application_controller.rb', line 143

def default_label_assigned(new_object)
  object_type = new_object.class.name.downcase
  # if you modify this string you have to modify it in n_gettext_for_generate_label as well
  msg = "A label was not provided during #{object_type} creation; therefore, a label of '%{label}' was " \
    "automatically assigned. If you would like a different label, please delete the " \
    "#{object_type} and recreate it with the desired label."
  return _(msg) % {:label => new_object.label}
end

#escape_html(input) ⇒ Object



204
205
206
# File 'app/controllers/katello/application_controller.rb', line 204

def escape_html(input)
  CGI.escapeHTML(input)
end

#flash_to_headersObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/katello/application_controller.rb', line 162

def flash_to_headers
  return if @_response.nil? || @_response.response_code == 302
  return if flash.blank?
  [:error, :warning, :success, :message].each do |type|
    unless flash[type].nil? || flash[type].blank?
      @enc = CGI.escape(flash[type].gsub("\n", "<br \\>"))
      response.headers['X-Message'] = @enc.gsub("%2B", "&#43;")
      response.headers['X-Message-Type'] = type.to_s
      response.headers['X-Message-Request-Type'] = requested_action
      flash.delete(type)  # clear the flash
      return
    end
  end
end

#format_time(date, options = {}) ⇒ Object

formats the date time if the dat is not nil



210
211
212
213
# File 'app/controllers/katello/application_controller.rb', line 210

def format_time(date, options = {})
  return I18n.l(date, options) if date
  ""
end

#generate_label(object_name, object_type) ⇒ Object

Generate a label using the name provided, returning the label and a string with text that may be sent to the user (e.g. via a notice).



130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/katello/application_controller.rb', line 130

def generate_label(object_name, object_type)
  # user didn't provide a label, so generate one using the name
  label = Util::Model.labelize(object_name)
  # if you modify this string you have to modify it in n_gettext_for_generate_label as well
  label_assigned_text = "A label was not provided during #{object_type} creation; therefore, a label of '%{label}' was " \
    "automatically assigned. If you would like a different label, please delete the " \
    "#{object_type} and recreate it with the desired label."
  label_assigned_text = _(label_assigned_text) % {:label => label}
  return label, label_assigned_text
end

#label_overridden(new_object, requested_label) ⇒ Object

Generate a message that may be sent to the user (e.g. via a notice) to inform them that the label that they provided has been overriden to ensure uniqueness.



154
155
156
157
158
159
160
# File 'app/controllers/katello/application_controller.rb', line 154

def label_overridden(new_object, requested_label)
  object_type = new_object.class.name.downcase
  _("The label requested is already used by another %s; therefore, a unique label was " \
    "assigned.  If you would like a different label, please delete the %s and recreate " \
    "it with a unique label.  Requested label: %s, Assigned label: %s") %
    [object_type, object_type, requested_label, new_object.label]
end

#n_gettext_for_generate_labelObject

this is intentionally dead code, it just mark some strings for gettext so generate_label and default_label_assigned can use them



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/katello/application_controller.rb', line 112

def n_gettext_for_generate_label
  # the same string for repository, product, organization, environment (see BZ 886718)
  N_("A label was not provided during repository creation; therefore, a label of '%{label}' was " \
    "automatically assigned. If you would like a different label, please delete the " \
    "repository and recreate it with the desired label.")
  N_("A label was not provided during product creation; therefore, a label of '%{label}' was " \
    "automatically assigned. If you would like a different label, please delete the " \
    "product and recreate it with the desired label.")
  N_("A label was not provided during organization creation; therefore, a label of '%{label}' was " \
    "automatically assigned. If you would like a different label, please delete the " \
    "organization and recreate it with the desired label.")
  N_("A label was not provided during environment creation; therefore, a label of '%{label}' was " \
    "automatically assigned. If you would like a different label, please delete the " \
    "environment and recreate it with the desired label.")
end

#no_env_available_msgObject



228
229
230
# File 'app/controllers/katello/application_controller.rb', line 228

def no_env_available_msg
  _("No environments are currently available in this organization.  Please either add some to the organization or select an organization that has an environment to set user default.")
end

#parse_calendar_date(date_str, time_str = "") ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'app/controllers/katello/application_controller.rb', line 217

def parse_calendar_date(date_str, time_str = "")
  return nil if date_str.blank?

  datetime_str = [date_str,
                  time_str.blank? ? '12:00 am' : time_str,
                  DateTime.now.zone].join ' '

  DateTime.strptime(datetime_str, '%m/%d/%Y %I:%M %P %:z') rescue false
end

#permission_deniedObject



245
246
247
# File 'app/controllers/katello/application_controller.rb', line 245

def permission_denied
  render :template => "katello/common/403"
end

#render_correct_navObject



232
233
234
235
236
237
238
239
240
241
242
243
# File 'app/controllers/katello/application_controller.rb', line 232

def render_correct_nav
  if self.respond_to?(:menu_definition) && self.menu_definition[params[:action]] == :admin_menu
    session[:menu_back] = true
    #the menu definition exists, return true
    render_admin_menu
  else
    #the menu definition does not exist, return false
    session[:menu_back] = false
    session[:notifications] = false
    #render_menu(1)
  end
end

#section_idObject



93
94
95
# File 'app/controllers/katello/application_controller.rb', line 93

def section_id
  'generic'
end