Skip to content
Snippets Groups Projects
Commit b4b0a077 authored by Escalante Martinez, Marisa's avatar Escalante Martinez, Marisa
Browse files

Adds ACSmI discovery and contracting

parent f43d509a
No related branches found
No related tags found
No related merge requests found
Showing
with 411 additions and 0 deletions
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
private
def render_bad_request
render(file: "#{Rails.root}/public/400", status: :bad_request)
end
def render_contracting_set
render(file: "#{Rails.root}/public/contracting_set", status: :ok)
end
def check_auth
unless current_user
flash[:alert] = 'You need to be authorized to access this action'
redirect_to '/'
end
end
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
helper_method :current_user_session, :current_user
end
class ContractsController < ApplicationController
before_action :check_auth
before_action :set_contract, only: [:show, :destroy]
def index
@contracts = @current_user.contracts
end
def show
end
def destroy
if @contract.destroy
flash[:notice] = 'Contract deleted'
else
flash[:alert] = 'Couldn\'t delete the contract'
end
redirect_to contracts_url
end
private
def set_contract
@contract = @current_user.contracts.where(id: params[:id]).first!
end
end
\ No newline at end of file
class Decide::Acsmi::Contracting::Api::V1::ApiController < ActionController::Base
def error_response(message, status = 422)
render json: { json: { errors: message }, status: status }
end
end
\ No newline at end of file
class Decide::Acsmi::Contracting::Api::V1::SessionsController < Decide::Acsmi::Contracting::Api::V1::ApiController
before_action :set_session, only: [:credentials]
def create
begin
@session = Session.start_for(params[:resource_id])
render json: { session_id: @session.id, contracting_url: sessions_url(session_id: @session.id) }
rescue => e
return error_response("Could not start session: #{e.message}")
end
end
def credentials
return error_response('Session contract is not yet set') unless @session.ready?
render json: @session.cred_response
end
private
def set_session
@session = Session.find_by_id(params[:id])
return error_response("Could not find session with ID #{params[:id]}", 404) unless @session
end
end
\ No newline at end of file
class InformationController < ApplicationController
def license
@license_text = File.read("#{Rails.root}/LICENSE.txt").gsub("\n", "<br/>").html_safe
end
def api_info
end
def usage_terms
end
end
\ No newline at end of file
class SessionsController < ApplicationController
before_action :set_session, except: [:start_demo]
before_action :check_auth, only: [:setup_contracting, :check_existing_contract, :prepare_new_contract]
def index
render_bad_request unless @session
end
def start_demo
begin
@session = Session.start_for(params[:resource_id])
redirect_to sessions_url(session_id: @session.id)
rescue => e
flash[:alert] = "Could not start session: #{e.message}"
redirect_to '/'
end
end
def own_creds_or_contract
if params[:have_credentials] == 'true'
@credential_fields = PlatformConnector.fields_for_credentials(@session.resource_id)
render 'request_credentials'
else
if current_user
@session.update(user: current_user)
redirect_to setup_contracting_session_path(@session)
else
@user_session = UserSession.new
@user = User.new
@have_account = true
render 'start_contracting'
end
end
end
def setup_contracting
end
def success
render 'finish'
end
def check_existing_contract
sleep 2
first_contract = Contract.where(user_id: current_user.id, resource_id: @session.resource_id).first
@contract_exists = first_contract.present?
if @contract_exists
@session.update(contract: first_contract)
@session.make_ready!
end
end
def prepare_new_contract
Contract.prepare(@session)
sleep 1
@session.make_ready!
end
def use_own_creds
@session.own_creds(params)
render 'finish'
end
private
def set_session
@session = Session.find_by_id(params[:session_id]) || Session.find_by_id(params[:id])
end
def contract_params
params[:credential_fields]
end
def invalid_credentials
flash[:alert] = "Couldn't find a contract with given credentials"
redirect_to credentials_url
end
end
\ No newline at end of file
class UserSessionsController < ApplicationController
def new
@user_session = UserSession.new
end
def create
if params[:session_id] && params[:have_account]
create_multiform
elsif params[:sign_in]
create_signle
else
redirect_to '/'
flash[:alert] = 'Couldn\'t sign in'
end
end
def destroy
current_user_session.destroy
redirect_to '#'
end
private
def create_multiform
@session = Session.find(params[:session_id])
@have_account = params[:have_account] == 'true'
if @have_account
@user = User.new
@user_session = UserSession.new(user_session_params)
save_object(@user_session)
else
@user_session = UserSession.new
@user = User.new(user_params)
save_object(@user)
end
end
def create_signle
@user_session = UserSession.new(user_session_params)
if @user_session.save
redirect_to contracts_url
else
render :new
end
end
def save_object(object)
if object.save
@session.update(user: current_user)
redirect_to setup_contracting_session_path(@session)
else
render template: 'sessions/start_contracting'
end
end
def user_session_params
params.require(:user_session).permit(:email, :password)
end
def user_params
params.require(:user).permit(:email, :full_name, :password, :password_confirmation, :organization, :address, :accept_terms)
end
end
\ No newline at end of file
class WelcomeController < ApplicationController
def index
end
end
\ No newline at end of file
module ApplicationHelper
def field_input_type(type)
case type
when 'email'
'email'
when 'password'
'password'
else
'text'
end
end
end
class Contract < ActiveRecord::Base
belongs_to :user
has_many :sessions
validates :resource_id, presence: true
validates :resource_name, presence: true
validates :user, presence: true
def self.prepare(session)
session.user.register_plaform_user if session.user.platform_email.blank?
PlatformConnector.register_access(session.resource_id, session.user.platform_id)
session.contract = Contract.create!(user: session.user, resource_id: session.resource_id, resource_name: session.resource_name)
session.save!
end
end
\ No newline at end of file
class CredentialField < ActiveRecord::Base
belongs_to :session
attr_encrypted :value, key: "Zp\xD0y\x990\xD0D\x83\x11\xF0*)\t!\x164=\xC8\x18\x0F\xBD\x11\xDC-\x80\x7F.Q\rC\xDE"
validates :name, presence: true
def self.write(session, name, value)
CredentialField.create!(session: session, name: name.downcase, value: value)
end
end
\ No newline at end of file
class Session < ActiveRecord::Base
self.primary_key = :id
belongs_to :contract
belongs_to :user
has_many :credential_fields
validates :resource_id, presence: true
include AASM
aasm do
state :initiating, initial: true
state :ready
event :make_ready do
transitions from: :initiating, to: :ready
end
end
def self.start_for(resource_id)
Session.create!(id: SecureRandom.uuid, resource_id: resource_id, resource_name: PlatformConnector.resource_name_by_id(resource_id))
end
def own_creds(params)
params[:credential_fields].each_pair do |cf_name, cf_value|
CredentialField.write(self, cf_name, cf_value)
end
self.make_ready!
end
def cred_response
if self.contract
# User - ACSmI - CBP contract
{session_id: self.id, contracting_type: 'platform',
platform_credentials: {email: self.user.platform_email, password: self.user.platform_password}}
else
# User has provided own cloud credentials
{session_id: self.id, contracting_type: 'own_credentials',
resource_credentials: self.credential_fields.map { |cf| {name: cf.name, value: cf.value} }}
end
end
end
\ No newline at end of file
class Setting < ActiveRecord::Base
validates :key, presence: true, uniqueness: true
validates :encrypted_value, presence: true
attr_encrypted :value, key: "\xEF\x83\xD4d\xAAC\x1F\x15\xC9_q\x10\b\x01\x903|\x11=\xE4\x82H\ntX\x96\x98\xD1c\x8D0H"
def self.value(key, new_value = nil)
if new_value
setting = Setting.first_or_initialize(key: key)
setting.value = new_value
setting.save!
else
Setting.where(key: key).first.value
end
end
end
\ No newline at end of file
class User < ActiveRecord::Base
has_many :contracts, dependent: :destroy
has_many :sessions, dependent: :destroy
acts_as_authentic
attr_accessor :accept_terms
validates :email, presence: true, uniqueness: true
validates :full_name, presence: true
validate :usage_terms_accepted, on: :create
attr_encrypted :platform_email, key: "211aae60391b4152ad4690ce4aa89d8c"
attr_encrypted :platform_password, key: "2694536f973b4e5faab7e4f6e8e4d427"
def register_plaform_user
self.platform_email = "acsmi-#{SecureRandom.hex(10)}@cloudbroker.com"
self.platform_password = SecureRandom.hex(12)
first_name = self.full_name.split(' ')[0]
last_name = self.full_name.split(' ')[1] || 'undefined'
self.platform_id = PlatformConnector.register_user(self.platform_email, first_name, last_name, self.platform_password)
self.save!
end
private
def usage_terms_accepted
unless self.accept_terms == '1'
self.errors.add(:base, 'You must accept usage terms in order to sign up')
end
end
end
class UserSession < Authlogic::Session::Base
end
\ No newline at end of file
%div.contracts-content
- if current_user.contracts.any?
%h3 My Contracts
%table.table.table-striped
%thead
%tr
%th Resource ID
%th Resource Name
%th Actions
%tbody
- current_user.contracts.each do |contract|
%tr
%td
= contract.resource_id
%td
= contract.resource_name
%td
= link_to '', contract_url(contract), class: 'glyphicon glyphicon-info-sign'
= link_to '', contract_url(contract), method: :delete, class: 'glyphicon glyphicon-trash'
- else
%h3.no-records You do not have any active contracts at the moment
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment