Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Remove tokens on deletion and add migration and foreign key

Oliver Günther4e2b1fc5eace
diff --git a/app/workers/principals/delete_job.rb b/app/workers/principals/delete_job.rb
index 7c4f6d1695d..0a925463033 100644
--- a/app/workers/principals/delete_job.rb
+++ b/app/workers/principals/delete_job.rb
@@ -65,6 +65,7 @@ class Principals::DeleteJob < ApplicationJob
def delete_associated(principal)
delete_notifications(principal)
delete_private_queries(principal)
+ delete_tokens(principal)
end
def delete_notifications(principal)
@@ -76,6 +77,10 @@ class Principals::DeleteJob < ApplicationJob
CostQuery.where(user_id: principal.id, is_public: false).delete_all
end
+ def delete_tokens(principal)
+ ::Token::Base.where(user_id: principal.id).destroy_all
+ end
+
def update_cost_queries(principal)
CostQuery.in_batches.each_record do |query|
serialized = query.serialized
diff --git a/db/migrate/20221017073431_remove_orphaned_tokens.rb b/db/migrate/20221017073431_remove_orphaned_tokens.rb
new file mode 100644
index 00000000000..61c804451ae
--- /dev/null
+++ b/db/migrate/20221017073431_remove_orphaned_tokens.rb
@@ -0,0 +1,10 @@
+class RemoveOrphanedTokens < ActiveRecord::Migration[7.0]
+ def up
+ Token::Base.where.not(user_id: User.select(:id)).delete_all
+ add_foreign_key :tokens, :users
+ end
+
+ def down
+ # Nothing to do
+ end
+end
diff --git a/spec/workers/principals/delete_job_integration_spec.rb b/spec/workers/principals/delete_job_integration_spec.rb
index 69b1731fbb0..a8abfc0946a 100644
--- a/spec/workers/principals/delete_job_integration_spec.rb
+++ b/spec/workers/principals/delete_job_integration_spec.rb
@@ -220,6 +220,22 @@ describe Principals::DeleteJob, type: :model do
it { expect(Query.find_by(id: query.id)).to be_nil }
end
+ shared_examples_for 'token handling' do
+ let!(:backup_token) do
+ create(:backup_token, user: principal)
+ end
+
+ let!(:invitation_token) do
+ create(:invitation_token, user: principal)
+ end
+
+ before do
+ job
+ end
+
+ it { expect(Token::Base.where(user_id: principal.id)).to be_empty }
+ end
+
shared_examples_for 'issue category handling' do
let(:category) do
create(:category,