diff --git a/app/controllers/api/submissions_controller.rb b/app/controllers/api/submissions_controller.rb index 2fc30c4c..ce8334fc 100644 --- a/app/controllers/api/submissions_controller.rb +++ b/app/controllers/api/submissions_controller.rb @@ -11,7 +11,7 @@ module Api user: current_user, source: :api, send_email: params[:send_email] != 'false', - emails: params[:emails]) + emails: params[:emails] || params[:email]) else Submissions.create_from_submitters(template:, user: current_user, diff --git a/app/models/access_token.rb b/app/models/access_token.rb new file mode 100644 index 00000000..5f8be162 --- /dev/null +++ b/app/models/access_token.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: access_tokens +# +# id :bigint not null, primary key +# sha256 :text not null +# token :text not null +# created_at :datetime not null +# updated_at :datetime not null +# user_id :bigint not null +# +# Indexes +# +# index_access_tokens_on_sha256 (sha256) UNIQUE +# index_access_tokens_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (user_id => users.id) +# +class AccessToken < ApplicationRecord + TOKEN_LENGTH = 22 + + belongs_to :user + + before_validation :set_sha256, on: :create + + attribute :token, :string, default: -> { SecureRandom.base58(TOKEN_LENGTH) } + + encrypts :token + + private + + def set_sha256 + self.sha256 = Digest::SHA256.hexdigest(token) + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 8d5a098e..5213173c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -45,6 +45,7 @@ class User < ApplicationRecord EMAIL_REGEXP = /[^@,\s]+@[^@,\s]+/ belongs_to :account + has_one :access_token, dependent: :destroy devise :database_authenticatable, :recoverable, :rememberable, :validatable, :trackable devise :registerable, :omniauthable, omniauth_providers: [:google_oauth2] if Docuseal.multitenant? @@ -54,6 +55,10 @@ class User < ApplicationRecord scope :active, -> { where(deleted_at: nil) } + def access_token + super || build_access_token.tap(&:save!) + end + def active_for_authentication? !deleted_at? end diff --git a/app/views/api_settings/index.html.erb b/app/views/api_settings/index.html.erb index 489f2b1e..459b2eb2 100644 --- a/app/views/api_settings/index.html.erb +++ b/app/views/api_settings/index.html.erb @@ -6,8 +6,8 @@