mirror of https://github.com/docusealco/docuseal
parent
63e1246a11
commit
7912c9a1ee
@ -0,0 +1,38 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
class SubmissionsController < ApiBaseController
|
||||
def create
|
||||
template = current_account.templates.find(params[:template_id])
|
||||
|
||||
submissions =
|
||||
if params[:emails].present?
|
||||
Submissions.create_from_emails(template:,
|
||||
user: current_user,
|
||||
send_email: params[:send_email] != 'false',
|
||||
emails: params[:emails])
|
||||
else
|
||||
Submissions.create_from_submitters(template:,
|
||||
user: current_user,
|
||||
send_email: params[:send_email] != 'false',
|
||||
submissions_attrs: submissions_params[:submission])
|
||||
end
|
||||
|
||||
submitters = submissions.flat_map(&:submitters)
|
||||
|
||||
if params[:send_email] != 'false'
|
||||
submitters.each do |submitter|
|
||||
SubmitterMailer.invitation_email(submitter, message: params[:message]).deliver_later!
|
||||
end
|
||||
end
|
||||
|
||||
render json: submitters
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def submissions_params
|
||||
params.permit(submission: [{ submitters: [%i[uuid name email]] }])
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AddUuidToUsers < ActiveRecord::Migration[7.0]
|
||||
class MigrationUser < ApplicationRecord
|
||||
self.table_name = 'users'
|
||||
end
|
||||
|
||||
def up
|
||||
add_column :users, :uuid, :text
|
||||
add_index :users, :uuid, unique: true
|
||||
|
||||
MigrationUser.all.each do |user|
|
||||
user.update_columns(uuid: SecureRandom.uuid)
|
||||
end
|
||||
|
||||
change_column_null :users, :uuid, false
|
||||
end
|
||||
|
||||
def down
|
||||
drop_column :users, :uuid
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ApiPathConsiderJsonMiddleware
|
||||
def initialize(app)
|
||||
@app = app
|
||||
end
|
||||
|
||||
def call(env)
|
||||
if env['PATH_INFO'].starts_with?('/api') &&
|
||||
!env['PATH_INFO'].ends_with?('/documents') &&
|
||||
!env['PATH_INFO'].ends_with?('/attachments')
|
||||
env['CONTENT_TYPE'] = 'application/json'
|
||||
end
|
||||
|
||||
@app.call(env)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AuthWithTokenStrategy < Devise::Strategies::Base
|
||||
def valid?
|
||||
request.headers['X-Auth-Token'].present?
|
||||
end
|
||||
|
||||
def authenticate!
|
||||
payload = JsonWebToken.decode(request.headers['X-Auth-Token'])
|
||||
|
||||
user = User.find_by(uuid: payload['uuid'])
|
||||
|
||||
if user
|
||||
success!(user)
|
||||
else
|
||||
fail!('Invalid token')
|
||||
end
|
||||
rescue JWT::VerificationError
|
||||
fail!('Invalid token')
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module JsonWebToken
|
||||
module_function
|
||||
|
||||
def encode(payload)
|
||||
JWT.encode(payload, Rails.application.secrets.secret_key_base)
|
||||
end
|
||||
|
||||
def decode(token)
|
||||
JWT.decode(token, Rails.application.secrets.secret_key_base)[0]
|
||||
end
|
||||
end
|
||||
Loading…
Reference in new issue