From 81803485214aa49c72f31d7b79b72c3d2e013686 Mon Sep 17 00:00:00 2001 From: Melhaya Date: Tue, 2 Jul 2024 13:58:42 +0200 Subject: [PATCH] Adding an API Key per user Having an API key per user will allow users to utilize the AI capabilities within CodeHarbor (i.e. Unittest generator) This is still a WiP --- app/controllers/users/registrations_controller.rb | 2 +- app/models/user.rb | 1 + app/views/users/registrations/edit.html.slim | 8 ++++++++ app/views/users/show.html.slim | 6 ++++++ config/locales/de/views/users.yml | 1 + config/locales/en/views/users.yml | 1 + .../20240702113838_add_personal_api_key_to_users.rb | 7 +++++++ db/schema.rb | 2 +- 8 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20240702113838_add_personal_api_key_to_users.rb diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 23df9f4ac..ed1b6871a 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -58,7 +58,7 @@ def configure_sign_up_params # If you have extra params to permit, append them to the sanitizer. def configure_account_update_params - devise_parameter_sanitizer.permit(:account_update, keys: %i[first_name last_name avatar]) + devise_parameter_sanitizer.permit(:account_update, keys: %i[first_name last_name avatar personal_api_key]) end def after_update_path_for(resource) diff --git a/app/models/user.rb b/app/models/user.rb index 7e615e94d..857a3b0ea 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,6 +17,7 @@ class User < ApplicationRecord validates :email, presence: true, uniqueness: {case_sensitive: false} validates :first_name, :last_name, :status_group, presence: true validates :password_set, inclusion: [true, false] + validates :personal_api_key, allow_blank: true, length: {maximum: 255} has_many :tasks, dependent: :nullify diff --git a/app/views/users/registrations/edit.html.slim b/app/views/users/registrations/edit.html.slim index 3289ee44b..1950fed8d 100644 --- a/app/views/users/registrations/edit.html.slim +++ b/app/views/users/registrations/edit.html.slim @@ -51,6 +51,14 @@ autocomplete: 'new-password', class: 'form-control' + .form-group.field-element + = f.label :personal_api_key, class: 'form-label' + = f.text_field :personal_api_key, + required: false, + placeholder: t('users.show.personal_api_key'), + autocomplete: 'off', + class: 'form-control' + = render 'avatar_form', f: - if resource.password_set? diff --git a/app/views/users/show.html.slim b/app/views/users/show.html.slim index 17f8945c9..3b687f76e 100644 --- a/app/views/users/show.html.slim +++ b/app/views/users/show.html.slim @@ -46,6 +46,12 @@ | : .col.row-value = @user.email + .row + .col-auto.row-label + = t('.personal_api_key') + | : + .col.row-value + = @user.personal_api_key .row.vertical .col.row-label = t('.account_links.created') diff --git a/config/locales/de/views/users.yml b/config/locales/de/views/users.yml index 8b200eed6..5e3b963c1 100644 --- a/config/locales/de/views/users.yml +++ b/config/locales/de/views/users.yml @@ -34,6 +34,7 @@ de: delete_modal: title: Warnung full_name: Vollständiger Name + personal_api_key: Persönlicher API-Schlüssel private_information: Private Informationen public_information: Öffentliche Informationen send_message: Nachricht senden diff --git a/config/locales/en/views/users.yml b/config/locales/en/views/users.yml index fd68d9d68..1234c0ac1 100644 --- a/config/locales/en/views/users.yml +++ b/config/locales/en/views/users.yml @@ -34,6 +34,7 @@ en: delete_modal: title: Warning full_name: Full name + personal_api_key: Personal API Key private_information: Private Information public_information: Public Information send_message: Send Message diff --git a/db/migrate/20240702113838_add_personal_api_key_to_users.rb b/db/migrate/20240702113838_add_personal_api_key_to_users.rb new file mode 100644 index 000000000..1d57e3bf1 --- /dev/null +++ b/db/migrate/20240702113838_add_personal_api_key_to_users.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddPersonalApiKeyToUsers < ActiveRecord::Migration[7.1] + def change + add_column :users, :personal_api_key, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 872c1c906..c6a308c82 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_05_31_160738) do +ActiveRecord::Schema[7.1].define(version: 2024_07_02_113838) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql"