Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabled serialization for JSON type columns #306

Merged
merged 2 commits into from
Jul 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ script:
before_script:
- mysql -e 'create database devise_token_auth_test'
- psql -c 'create database devise_token_auth_test' -U postgres

addons:
postgresql: "9.3"
23 changes: 18 additions & 5 deletions app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def self.tokens_match?(token_hash, token)
self.devise_modules.delete(:omniauthable)
end

serialize :tokens, JSON
unless tokens_has_json_column_type?
serialize :tokens, JSON
end

validates :email, presence: true, email: true, if: Proc.new { |u| u.provider == 'email' }
validates_presence_of :uid, if: Proc.new { |u| u.provider != 'email' }
Expand Down Expand Up @@ -82,6 +84,15 @@ def send_reset_password_instructions(opts=nil)
end
end

module ClassMethods
protected


def tokens_has_json_column_type?
table_exists? && self.columns_hash['tokens'] && self.columns_hash['tokens'].type.in?([:json, :jsonb])
end
end


def valid_token?(token, client_id='default')
client_id ||= 'default'
Expand Down Expand Up @@ -240,10 +251,12 @@ def sync_uid
end

def destroy_expired_tokens
self.tokens.delete_if{|cid,v|
expiry = v[:expiry] || v["expiry"]
DateTime.strptime(expiry.to_s, '%s') < Time.now
}
if self.tokens
self.tokens.delete_if do |cid, v|
expiry = v[:expiry] || v["expiry"]
DateTime.strptime(expiry.to_s, '%s') < Time.now
end
end
end

end
1 change: 1 addition & 0 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ class Application < Rails::Application
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.autoload_paths << Rails.root.join('lib')
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
Expand Down Expand Up @@ -42,7 +44,11 @@ def change
t.string :uid, :null => false, :default => ""

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateMangs < ActiveRecord::Migration
def change
create_table(:mangs) do |t|
Expand Down Expand Up @@ -42,7 +44,11 @@ def change
t.string :uid, :null => false, :default => ""

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateEvilUsers < ActiveRecord::Migration
def change
create_table(:evil_users) do |t|
Expand Down Expand Up @@ -40,7 +42,11 @@ def change
t.string :uid, :null => false, :default => ""

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

## etc.
t.string :favorite_color
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateOnlyEmailUsers < ActiveRecord::Migration
def change
create_table(:only_email_users) do |t|
Expand Down Expand Up @@ -40,7 +42,11 @@ def change
t.string :email

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateUnregisterableUsers < ActiveRecord::Migration
def change
create_table(:unregisterable_users) do |t|
Expand Down Expand Up @@ -40,7 +42,11 @@ def change
t.string :email

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateNiceUsers < ActiveRecord::Migration
def change
create_table(:nice_users) do |t|
Expand Down Expand Up @@ -40,7 +42,11 @@ def change
t.string :email

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateUnconfirmableUsers < ActiveRecord::Migration
def change
create_table(:unconfirmable_users) do |t|
Expand Down Expand Up @@ -40,7 +42,11 @@ def change
t.string :email

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
29 changes: 29 additions & 0 deletions test/dummy/lib/migration_database_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module MigrationDatabaseHelper
def json_supported_database?
(postgres? && postgres_correct_version?) || (mysql? && mysql_correct_version?)
end

def postgres?
database_name == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
end

def postgres_correct_version?
database_version > '9.3'
end

def mysql?
database_name == 'ActiveRecord::ConnectionAdapters::MysqlAdapter'
end

def mysql_correct_version?
database_version > '5.7.7'
end

def database_name
ActiveRecord::Base.connection.class.name
end

def database_version
ActiveRecord::Base.connection.select_value('SELECT VERSION()')
end
end
13 changes: 13 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ class UserTest < ActiveSupport::TestCase
end
end

describe 'nil tokens are handled properly' do
before do
@resource = users(:confirmed_email_user)
@resource.skip_confirmation!
@resource.save!
end

test 'tokens can be set to nil' do
@resource.tokens = nil
assert @resource.save
end
end

describe "#generate_url" do
test 'URI fragment should appear at the end of URL' do
params = {client_id: 123}
Expand Down