From 2645db3237159aaebb31f87760e691850d27844d Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Tue, 27 Sep 2016 10:21:04 +0900 Subject: [PATCH 01/11] Add username to users in a migration template file. --- lib/generators/sorcery/templates/migration/core.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/generators/sorcery/templates/migration/core.rb b/lib/generators/sorcery/templates/migration/core.rb index af5f721c..789cad7e 100644 --- a/lib/generators/sorcery/templates/migration/core.rb +++ b/lib/generators/sorcery/templates/migration/core.rb @@ -2,6 +2,7 @@ class SorceryCore < <%= migration_class_name %> def change create_table :<%= model_class_name.tableize %> do |t| t.string :email, :null => false + t.string :username t.string :crypted_password t.string :salt From 409ce455c42ca7197157335489b5ab28eceb90bf Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Tue, 27 Sep 2016 10:21:45 +0900 Subject: [PATCH 02/11] Remove not null constraint from email in users. --- lib/generators/sorcery/templates/migration/core.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/sorcery/templates/migration/core.rb b/lib/generators/sorcery/templates/migration/core.rb index 789cad7e..0c701d5c 100644 --- a/lib/generators/sorcery/templates/migration/core.rb +++ b/lib/generators/sorcery/templates/migration/core.rb @@ -1,7 +1,7 @@ class SorceryCore < <%= migration_class_name %> def change create_table :<%= model_class_name.tableize %> do |t| - t.string :email, :null => false + t.string :email t.string :username t.string :crypted_password t.string :salt From 596c66e9256eda500651ecadd78dfd8c5d992818 Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Fri, 14 Oct 2016 16:31:11 +0900 Subject: [PATCH 03/11] Add unique index to username. --- lib/generators/sorcery/templates/migration/core.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/generators/sorcery/templates/migration/core.rb b/lib/generators/sorcery/templates/migration/core.rb index 0c701d5c..6de1c4dd 100644 --- a/lib/generators/sorcery/templates/migration/core.rb +++ b/lib/generators/sorcery/templates/migration/core.rb @@ -10,5 +10,7 @@ def change end add_index :<%= model_class_name.tableize %>, :email, unique: true + add_index :<%= model_class_name.tableize %>, :username, unique: true + end end From 842a630e3733d727ab10ef1bfc4677fa11c3cf92 Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Sat, 28 Jan 2017 22:55:35 +0900 Subject: [PATCH 04/11] Revert "Remove not null constraint from email in users." This reverts commit b28de94165ba868a0bafb45451aa1a020c8b5a13. --- lib/generators/sorcery/templates/migration/core.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/sorcery/templates/migration/core.rb b/lib/generators/sorcery/templates/migration/core.rb index 6de1c4dd..6ab8e929 100644 --- a/lib/generators/sorcery/templates/migration/core.rb +++ b/lib/generators/sorcery/templates/migration/core.rb @@ -1,7 +1,7 @@ class SorceryCore < <%= migration_class_name %> def change create_table :<%= model_class_name.tableize %> do |t| - t.string :email + t.string :email, :null => false t.string :username t.string :crypted_password t.string :salt From 80fde0e4de896261c6c63d4f2acedf4d75c3fba1 Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Fri, 27 Jan 2017 23:29:02 +0900 Subject: [PATCH 05/11] Set the same constrants as lib/generators/sorcery/templates/migration/core.rb --- .../db/migrate/core/20101224223620_create_users.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/rails_app/db/migrate/core/20101224223620_create_users.rb b/spec/rails_app/db/migrate/core/20101224223620_create_users.rb index a542db86..400b7ecb 100644 --- a/spec/rails_app/db/migrate/core/20101224223620_create_users.rb +++ b/spec/rails_app/db/migrate/core/20101224223620_create_users.rb @@ -1,13 +1,15 @@ class CreateUsers < ActiveRecord::CompatibleLegacyMigration.migration_class def self.up create_table :users do |t| - t.string :username, null: false - t.string :email, default: nil + t.string :username + t.string :email, null: false t.string :crypted_password t.string :salt t.timestamps null: false end + add_index :users, :email, unique: true + add_index :users, :username, unique: true end def self.down From 1d76bc028172b88fc63accaed246dc9135b77071 Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Fri, 27 Jan 2017 23:31:05 +0900 Subject: [PATCH 06/11] Add empty string to email to #create_new_external_user. --- lib/sorcery/test_helpers/internal.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sorcery/test_helpers/internal.rb b/lib/sorcery/test_helpers/internal.rb index 6f603c27..eb98712d 100644 --- a/lib/sorcery/test_helpers/internal.rb +++ b/lib/sorcery/test_helpers/internal.rb @@ -35,7 +35,7 @@ def create_new_user(attributes_hash = nil) end def create_new_external_user(provider, attributes_hash = nil) - user_attributes_hash = attributes_hash || { username: 'gizmo' } + user_attributes_hash = attributes_hash || { username: 'gizmo', email: '' } @user = User.new(user_attributes_hash) @user.sorcery_adapter.save(raise_on_failure: true) @user.authentications.create!(provider: provider, uid: 123) From 1e42b7bffb9184a8de413c65404843ab7573cd3d Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Fri, 27 Jan 2017 23:36:07 +0900 Subject: [PATCH 07/11] Add a feature setting empty string to email. This modification is for an external service that doesn't offer email like twitter. --- lib/sorcery/model/submodules/external.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/sorcery/model/submodules/external.rb b/lib/sorcery/model/submodules/external.rb index 3a84c23b..790973d2 100644 --- a/lib/sorcery/model/submodules/external.rb +++ b/lib/sorcery/model/submodules/external.rb @@ -59,6 +59,11 @@ def create_from_provider(provider, uid, attrs) user.send(:"#{k}=", v) end + # For external services that don't offer an email like twitter. + unless attrs.has_key?(:email) && user.attributes.has_key?('email') + user.send(:'email=', '') + end + if block_given? return false unless yield user end From 8ff1ad700d4256f75b43f9c11da60d605236d658 Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Fri, 27 Jan 2017 23:39:01 +0900 Subject: [PATCH 08/11] Add tests of #create_from_provider. --- spec/shared_examples/user_shared_examples.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/shared_examples/user_shared_examples.rb b/spec/shared_examples/user_shared_examples.rb index c563cc95..68f00550 100644 --- a/spec/shared_examples/user_shared_examples.rb +++ b/spec/shared_examples/user_shared_examples.rb @@ -560,6 +560,26 @@ def self.matches?(crypted, *tokens) expect(User.first.username).to eq 'Noam Ben Ari' end + context 'with email' do + it 'sets passed email' do + expect do + User.create_from_provider('facebook', '123', username: 'Noam Ben Ari', email: 'bla@bla.com') { true } + end.to change { User.count }.by(1) + + expect(User.first.email).to eq 'bla@bla.com' + end + end + + context 'without email' do + it 'sets an empty string to email' do + expect do + User.create_from_provider('facebook', '123', username: 'Noam Ben Ari') { true } + end.to change { User.count }.by(1) + + expect(User.first.email).to eq '' + end + end + context 'with block' do it 'create user when block return true' do expect do From 5e5dbf7f30141dc7661a992c11b6174e43de31f7 Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Sat, 28 Jan 2017 00:06:53 +0900 Subject: [PATCH 09/11] Modify twitter user_info_mapping from email to username. --- lib/generators/sorcery/templates/initializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/sorcery/templates/initializer.rb b/lib/generators/sorcery/templates/initializer.rb index 36df69de..59a542ce 100644 --- a/lib/generators/sorcery/templates/initializer.rb +++ b/lib/generators/sorcery/templates/initializer.rb @@ -107,7 +107,7 @@ # config.twitter.key = "" # config.twitter.secret = "" # config.twitter.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=twitter" - # config.twitter.user_info_mapping = {:email => "screen_name"} + # config.twitter.user_info_mapping = {:username => "screen_name"} # # config.facebook.key = "" # config.facebook.secret = "" From 2fb9646e58280333e966a7a2e1882f24d2741eb2 Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Fri, 27 Jan 2017 23:40:31 +0900 Subject: [PATCH 10/11] Remove #custom_create_new_external_user, which is never used. --- lib/sorcery/test_helpers/internal.rb | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/sorcery/test_helpers/internal.rb b/lib/sorcery/test_helpers/internal.rb index eb98712d..6d557dcd 100644 --- a/lib/sorcery/test_helpers/internal.rb +++ b/lib/sorcery/test_helpers/internal.rb @@ -42,16 +42,6 @@ def create_new_external_user(provider, attributes_hash = nil) @user end - def custom_create_new_external_user(provider, authentication_class, attributes_hash = nil) - authentication_association = authentication_class.name.underscore.pluralize - - user_attributes_hash = attributes_hash || { username: 'gizmo' } - @user = User.new(user_attributes_hash) - @user.sorcery_adapter.save(raise_on_failure: true) - @user.send(authentication_association).create!(provider: provider, uid: 123) - @user - end - def sorcery_model_property_set(property, *values) User.class_eval do sorcery_config.send(:"#{property}=", *values) From 6f7d55305192062f08ee748b875d13cd4175b364 Mon Sep 17 00:00:00 2001 From: Yusuke Ebihara Date: Wed, 13 Dec 2017 11:02:06 +0900 Subject: [PATCH 11/11] Change the email value, '', into "#{SecureRandom.uuid}@example.com" - `"#{provider}-#{uid}@example.com"` breaks backward compatibility because in wiki it says `create_from_provider(provider)`: it doesn't pass uid. - `@example.com` makes it easy to check which email is valid or not --- lib/sorcery/model/submodules/external.rb | 2 +- spec/shared_examples/user_shared_examples.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sorcery/model/submodules/external.rb b/lib/sorcery/model/submodules/external.rb index 790973d2..842ec873 100644 --- a/lib/sorcery/model/submodules/external.rb +++ b/lib/sorcery/model/submodules/external.rb @@ -61,7 +61,7 @@ def create_from_provider(provider, uid, attrs) # For external services that don't offer an email like twitter. unless attrs.has_key?(:email) && user.attributes.has_key?('email') - user.send(:'email=', '') + user.send(:'email=', "#{SecureRandom.uuid}@example.com") end if block_given? diff --git a/spec/shared_examples/user_shared_examples.rb b/spec/shared_examples/user_shared_examples.rb index 68f00550..1dae3646 100644 --- a/spec/shared_examples/user_shared_examples.rb +++ b/spec/shared_examples/user_shared_examples.rb @@ -576,7 +576,7 @@ def self.matches?(crypted, *tokens) User.create_from_provider('facebook', '123', username: 'Noam Ben Ari') { true } end.to change { User.count }.by(1) - expect(User.first.email).to eq '' + expect(User.first.email).not_to be_empty end end