foodsoft/plugins/links/app/controllers/links_controller.rb
Patrick Gansterer 7657b05787 Add links plugin
This can be used to link to external services related to the foodcoop.
With the indirect mode it is possible to implement a secure login to other
services. In that case Foodsoft will send a HTTP GET request and redirect
the user to the returned Location header. This allows the generation of
a one-time login URL.
A typical use-case would be that a workgroup, which is responsible for
the email account, does not need to share the login credentials and can
use a link within the Foodsoft instead.
2020-07-29 11:25:04 +02:00

29 lines
738 B
Ruby

require 'net/http'
class LinksController < ApplicationController
def show
link = Link.find(params[:id])
url = link.url
if link.workgroup && !current_user.role_admin? && !link.workgroup.member?(current_user)
return deny_access
end
if link.indirect
uri = URI.parse url
request = Net::HTTP::Get.new uri
request['Authorization'] = link.authorization if link.authorization
result = Net::HTTP.start uri.host, uri.port, use_ssl: uri.scheme == 'https' do |http|
http.request request
end
url = result.header['Location']
unless url
return redirect_to root_url, alert: t('.indirect_no_location')
end
end
redirect_to url, status: 302
end
end