summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Edwards-Jones <jedwardsjones@gitlab.com>2018-06-22 16:40:34 +1000
committerJames Edwards-Jones <jedwardsjones@gitlab.com>2018-07-27 12:45:30 +0100
commitb69533624fa53655fa1ef901dfe805cac356afbf (patch)
tree6027514c1706a300285b0dcb9992cdc3ffef4357
parentcaeb4597a5b24e0eaa96b24901ce9208c2eef4bf (diff)
downloadgitlab-ce-b69533624fa53655fa1ef901dfe805cac356afbf.tar.gz
PersonalAccessTokens can have associated projects
-rw-r--r--app/models/personal_access_token.rb3
-rw-r--r--app/models/token_resource.rb7
-rw-r--r--db/migrate/20180622054732_create_token_resources.rb12
-rw-r--r--db/schema.rb10
-rw-r--r--spec/models/personal_access_token_spec.rb7
-rw-r--r--spec/models/token_resource_spec.rb13
6 files changed, 52 insertions, 0 deletions
diff --git a/app/models/personal_access_token.rb b/app/models/personal_access_token.rb
index 063dc521324..5dbdc03e642 100644
--- a/app/models/personal_access_token.rb
+++ b/app/models/personal_access_token.rb
@@ -9,6 +9,9 @@ class PersonalAccessToken < ActiveRecord::Base
belongs_to :user
+ has_many :token_resources
+ has_many :projects, through: :token_resources
+
before_save :ensure_token
scope :active, -> { where("revoked = false AND (expires_at >= NOW() OR expires_at IS NULL)") }
diff --git a/app/models/token_resource.rb b/app/models/token_resource.rb
new file mode 100644
index 00000000000..6cf4765dfba
--- /dev/null
+++ b/app/models/token_resource.rb
@@ -0,0 +1,7 @@
+class TokenResource < ActiveRecord::Base
+ belongs_to :personal_access_token
+ belongs_to :project
+
+ validates :personal_access_token, presence: true
+ validates :project, presence: true
+end
diff --git a/db/migrate/20180622054732_create_token_resources.rb b/db/migrate/20180622054732_create_token_resources.rb
new file mode 100644
index 00000000000..c0497b9092d
--- /dev/null
+++ b/db/migrate/20180622054732_create_token_resources.rb
@@ -0,0 +1,12 @@
+class CreateTokenResources < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ def change
+ create_table :token_resources do |t|
+ t.belongs_to :personal_access_token, null: false, foreign_key: { on_delete: :cascade }, index: true
+ t.belongs_to :project, null: false, foreign_key: { on_delete: :cascade }, index: true
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 8ae0197d1b4..68c75b5d8ac 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -1977,6 +1977,14 @@ ActiveRecord::Schema.define(version: 20180722103201) do
add_index "todos", ["user_id", "id"], name: "index_todos_on_user_id_and_id_pending", where: "((state)::text = 'pending'::text)", using: :btree
add_index "todos", ["user_id"], name: "index_todos_on_user_id", using: :btree
+ create_table "token_resources", force: :cascade do |t|
+ t.integer "personal_access_token_id", null: false
+ t.integer "project_id", null: false
+ end
+
+ add_index "token_resources", ["personal_access_token_id"], name: "index_token_resources_on_personal_access_token_id", using: :btree
+ add_index "token_resources", ["project_id"], name: "index_token_resources_on_project_id", using: :btree
+
create_table "trending_projects", force: :cascade do |t|
t.integer "project_id", null: false
end
@@ -2348,6 +2356,8 @@ ActiveRecord::Schema.define(version: 20180722103201) do
add_foreign_key "todos", "projects", name: "fk_45054f9c45", on_delete: :cascade
add_foreign_key "todos", "users", column: "author_id", name: "fk_ccf0373936", on_delete: :cascade
add_foreign_key "todos", "users", name: "fk_d94154aa95", on_delete: :cascade
+ add_foreign_key "token_resources", "personal_access_tokens", on_delete: :cascade
+ add_foreign_key "token_resources", "projects", on_delete: :cascade
add_foreign_key "trending_projects", "projects", on_delete: :cascade
add_foreign_key "u2f_registrations", "users"
add_foreign_key "user_callouts", "users", on_delete: :cascade
diff --git a/spec/models/personal_access_token_spec.rb b/spec/models/personal_access_token_spec.rb
index 2bb1c49b740..e452b0e3d7b 100644
--- a/spec/models/personal_access_token_spec.rb
+++ b/spec/models/personal_access_token_spec.rb
@@ -3,6 +3,13 @@ require 'spec_helper'
describe PersonalAccessToken do
subject { described_class }
+ describe 'associations' do
+ subject { described_class.new }
+
+ it { is_expected.to have_many(:token_resources) }
+ it { is_expected.to have_many(:projects) }
+ end
+
describe '.build' do
let(:personal_access_token) { build(:personal_access_token) }
let(:invalid_personal_access_token) { build(:personal_access_token, :invalid) }
diff --git a/spec/models/token_resource_spec.rb b/spec/models/token_resource_spec.rb
new file mode 100644
index 00000000000..b7a1115b043
--- /dev/null
+++ b/spec/models/token_resource_spec.rb
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe TokenResource do
+ describe 'associations' do
+ it { is_expected.to belong_to(:personal_access_token) }
+ it { is_expected.to belong_to(:project) }
+ end
+
+ describe 'validations' do
+ it { is_expected.to validate_presence_of(:personal_access_token) }
+ it { is_expected.to validate_presence_of(:project) }
+ end
+end