Class: DiscourseSubscriptionClient::SuppliersController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/discourse_subscription_client/suppliers_controller.rb

Instance Method Summary collapse

Methods inherited from AdminController

#ensure_can_manage_subscriptions, #failed_json, #render_json_dump, #render_serialized, #serialize_data, #success_json

Instance Method Details

#authorizeObject



19
20
21
22
23
24
25
26
# File 'app/controllers/discourse_subscription_client/suppliers_controller.rb', line 19

def authorize
  session[:final_landing_path] = params[:final_landing_path]

  request_id = DiscourseSubscriptionClient::Authorization.request_id(@supplier.id)
  cookies[:user_api_request_id] = request_id
  redirect_to DiscourseSubscriptionClient::Authorization.url(current_user, @supplier, request_id).to_s,
              allow_other_host: true
end

#authorize_callbackObject

Raises:

  • (Discourse::InvalidParameters)


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
# File 'app/controllers/discourse_subscription_client/suppliers_controller.rb', line 28

def authorize_callback
  payload = params[:payload]
  request_id = cookies[:user_api_request_id]
  supplier_id = request_id.split("-").first

  data = DiscourseSubscriptionClient::Authorization.process_response(request_id, payload)
  raise Discourse::InvalidParameters, :payload unless data

  supplier = SubscriptionClientSupplier.find(supplier_id)
  raise Discourse::InvalidParameters, :supplier_id unless supplier

  supplier.update(
    api_key: data[:key],
    user_id: data[:user_id],
    authorized_at: DateTime.now.iso8601(3)
  )

  DiscourseSubscriptionClient::Subscriptions.update

  if !session[:final_landing_path].blank?
    redirect_to session[:final_landing_path]
  else
    redirect_to "/admin/plugins"
  end
end

#destroyObject



54
55
56
57
58
59
60
# File 'app/controllers/discourse_subscription_client/suppliers_controller.rb', line 54

def destroy
  if @supplier.destroy_authorization
    render json: success_json.merge(supplier_id: @supplier.id)
  else
    render json: failed_json
  end
end

#indexObject



9
10
11
12
13
14
15
16
17
# File 'app/controllers/discourse_subscription_client/suppliers_controller.rb', line 9

def index
  suppliers = SubscriptionClientSupplier.all
  if params[:resource]
    suppliers = suppliers
                .joins(:resources)
                .where(resources: { name: params[:resource] })
  end
  render_serialized(suppliers, SupplierSerializer, root: "suppliers")
end