Add a rake task to reset all email and private tokens

This commit is contained in:
James Lopez 2017-03-21 13:36:46 +00:00 committed by Sean McGivern
parent 4ebc62391d
commit b356ce7e12
3 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,4 @@
---
title: New rake task to reset all email and private tokens
merge_request:
author:

38
lib/tasks/tokens.rake Normal file
View File

@ -0,0 +1,38 @@
require_relative '../../app/models/concerns/token_authenticatable.rb'
namespace :tokens do
desc "Reset all GitLab user auth tokens"
task reset_all_auth: :environment do
reset_all_users_token(:reset_authentication_token!)
end
desc "Reset all GitLab email tokens"
task reset_all_email: :environment do
reset_all_users_token(:reset_incoming_email_token!)
end
def reset_all_users_token(reset_token_method)
TmpUser.find_in_batches do |batch|
puts "Processing batch starting with user ID: #{batch.first.id}"
STDOUT.flush
batch.each(&reset_token_method)
end
end
end
class TmpUser < ActiveRecord::Base
include TokenAuthenticatable
self.table_name = 'users'
def reset_authentication_token!
write_new_token(:authentication_token)
save!(validate: false)
end
def reset_incoming_email_token!
write_new_token(:incoming_email_token)
save!(validate: false)
end
end

21
spec/tasks/tokens_spec.rb Normal file
View File

@ -0,0 +1,21 @@
require 'rake_helper'
describe 'tokens rake tasks' do
let!(:user) { create(:user) }
before do
Rake.application.rake_require 'tasks/tokens'
end
describe 'reset_all task' do
it 'invokes create_hooks task' do
expect { run_rake_task('tokens:reset_all_auth') }.to change { user.reload.authentication_token }
end
end
describe 'reset_all_email task' do
it 'invokes create_hooks task' do
expect { run_rake_task('tokens:reset_all_email') }.to change { user.reload.incoming_email_token }
end
end
end