Skip to content

Commit

Permalink
Adding an API Key per user
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Melhaya committed Jul 2, 2024
1 parent 04fc65c commit efe342b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions app/views/users/registrations/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
6 changes: 6 additions & 0 deletions app/views/users/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions config/locales/de/views/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions config/locales/en/views/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions db/migrate/20240702113838_add_personal_api_key_to_users.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -326,7 +326,8 @@
t.datetime "locked_at"
t.string "preferred_locale"
t.boolean "password_set", default: true, null: false
t.integer "status_group", limit: 1, default: 0, null: false, comment: "Used as enum in Rails"
t.integer "status_group", limit: 2, default: 0, null: false, comment: "Used as enum in Rails"
t.string "personal_api_key"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
Expand Down

0 comments on commit efe342b

Please sign in to comment.