Skip to content

Commit

Permalink
Api users & permissions
Browse files Browse the repository at this point in the history
Users and permissions for Api Auth
  • Loading branch information
pol-ak committed Nov 21, 2024
1 parent 3c99c8e commit a5577f8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/models/api_permission.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ApiPermission < ApplicationRecord
belongs_to :api_user

validates :endpoint_name, presence: true
end
7 changes: 7 additions & 0 deletions app/models/api_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ApiUser < ApplicationRecord
has_secure_password

has_many :api_permissions, dependent: :destroy

validates :username, presence: true, uniqueness: true
end
19 changes: 19 additions & 0 deletions db/migrate/20241120202216_create_api_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class CreateApiUsers < ActiveRecord::Migration[6.1]
def change
create_table :api_users do |t|
t.string :username, null: false, unique: true
t.string :password_digest, null: false

t.timestamps
end

add_index :api_users, :username, unique: true

ApiUser.find_or_create_by(id: 1) do |api_user|
api_user.username = 'madeline'
api_user.password = 'k2j3j10pp'
api_user.password_confirmation = 'k2j3j10pp'
end

end
end
18 changes: 18 additions & 0 deletions db/migrate/20241120234518_create_api_permissions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class CreateApiPermissions < ActiveRecord::Migration[6.1]
def change
create_table :api_permissions do |t|
t.references :api_user, null: false, foreign_key: true
t.string :endpoint_name, null: false

t.timestamps
end

add_index :api_permissions, [:api_user_id, :endpoint_name], unique: true

ApiPermission.find_or_create_by(id: 1) do |permission|
permission.api_user_id = 1
permission.endpoint_name = 'addresses'
end

end
end
20 changes: 19 additions & 1 deletion 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.define(version: 2024_05_20_210320) do
ActiveRecord::Schema.define(version: 2024_11_20_234518) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -137,6 +137,23 @@
t.datetime "updated_at", precision: 6, null: false
end

create_table "api_permissions", force: :cascade do |t|
t.bigint "api_user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.string "endpoint_name", null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["api_user_id", "endpoint_name"], name: "index_api_permissions_on_api_user_id_and_endpoint_name", unique: true
t.index ["api_user_id"], name: "index_api_permissions_on_api_user_id"
end

create_table "api_users", force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false
t.string "password_digest", null: false
t.datetime "updated_at", precision: 6, null: false
t.string "username", null: false
t.index ["username"], name: "index_api_users_on_username", unique: true
end

create_table "countries", id: :serial, force: :cascade do |t|
t.datetime "created_at", null: false
t.integer "default_currency_id", null: false
Expand Down Expand Up @@ -566,6 +583,7 @@
add_foreign_key "accounting_transactions", "projects"
add_foreign_key "addresses", "countries"
add_foreign_key "addresses", "geo_states", column: "state_id"
add_foreign_key "api_permissions", "api_users"
add_foreign_key "countries", "currencies", column: "default_currency_id"
add_foreign_key "data_exports", "divisions"
add_foreign_key "divisions", "accounting_accounts", column: "interest_income_account_id"
Expand Down

0 comments on commit a5577f8

Please sign in to comment.