Class: SupplejackApi::ApplicationController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/supplejack_api/application_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_admin!Object



45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/supplejack_api/application_controller.rb', line 45

def authenticate_admin!
  if RecordSchema.roles[current_user.role.to_sym].try(:admin)
    return true
  else
    render request.format.to_sym => {
      errors: 'You need Administrator privileges to perform this request'
    }, status: :forbidden
    return false
  end
end

#authenticate_user!Object



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
# File 'app/controllers/supplejack_api/application_controller.rb', line 15

def authenticate_user!
  error_message = nil

  if params[:api_key].blank?
    error_message = I18n.t('users.blank_token')
  elsif current_user
    if current_user.over_limit?
      error_message = I18n.t('users.reached_limit')
    else
      current_user.update_tracked_fields(request)
      current_user.update_daily_activity(request)
      current_user.check_daily_requests
      current_user.save(validate: false)
    end
  else
    error_message = I18n.t('users.invalid_token')
  end

  format = :json
  format = request.format.to_sym if [:xml, :json, :rss].include?(request.format.try(:to_sym))

  if error_message
    render format => { errors: error_message }, status: :forbidden
  end
end

#current_userObject



41
42
43
# File 'app/controllers/supplejack_api/application_controller.rb', line 41

def current_user
  @current_user ||= User.find_by_api_key(params[:api_key])
end

#find_user_setObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/supplejack_api/application_controller.rb', line 56

def find_user_set
  user_set_id = params[:user_set_id] || params[:id]

  @user_set = if RecordSchema.roles[current_user.role.to_sym].try(:admin)
                UserSet.custom_find(user_set_id)
              else
                current_user.user_sets.custom_find(user_set_id)
              end

  unless @user_set
    render json: { errors: "Set with id: #{params[:id]} was not found." }, status: :not_found
  end
end