diff --git a/Gemfile b/Gemfile index 340c199..118c24d 100644 --- a/Gemfile +++ b/Gemfile @@ -67,6 +67,9 @@ gem 'http_accept_language' # Routes for JS files gem 'js-routes', '~> 1.1.0' +# External logins with OmniAuth +gem 'omniauth' + group :test do # Easier test writing gem "shoulda-matchers", '~> 2.8.0' diff --git a/Gemfile.lock b/Gemfile.lock index 6839378..0368fed 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -134,6 +134,7 @@ GEM globalid (0.3.6) activesupport (>= 4.1.0) google-analytics-rails (0.0.6) + hashie (3.4.1) http-cookie (1.0.2) domain_name (~> 0.5) http_accept_language (2.0.5) @@ -206,6 +207,9 @@ GEM nokogiri (1.6.6.2) mini_portile (~> 0.6.0) ntlm-http (0.1.1) + omniauth (1.2.2) + hashie (>= 1.2, < 4) + rack (~> 1.0) orm_adapter (0.5.0) pg (0.18.3) poltergeist (1.7.0) @@ -364,6 +368,7 @@ DEPENDENCIES konacha less-rails (~> 2.7.0) mocha + omniauth pg (~> 0.18.1) poltergeist (~> 1.7.0) rails (= 4.2.4) diff --git a/app/models/user.rb b/app/models/user.rb index 082e5ce..468fd52 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,11 +2,14 @@ class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable - devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, + :omniauthable, omniauth_providers: [] validates :name, presence: true - validates :email, presence: true - validates :email, uniqueness: true + validates :email, presence: true, uniqueness: true + validates :provider, presence: true, unless: Proc.new { |user| user.uid.nil? || user.uid.blank? } + validates :uid, presence: true, uniqueness: { scope: :provider }, + unless: Proc.new { |user| user.provider.nil? || user.provider.blank? } has_many :project_attributes, class_name: 'ProjectAttributes', dependent: :destroy has_many :reading_group_attributes, class_name: 'ReadingGroupAttributes', dependent: :destroy @@ -17,4 +20,10 @@ class User < ActiveRecord::Base def projects project_attributes.map { |project_attr| project_attr.project } end + + protected + + def password_required? + provider.nil? + end end diff --git a/config/routes.rb b/config/routes.rb index b4f3b12..65cbc34 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ Rails.application.routes.draw do scope "(:locale)", locale: /en|pt/ do - devise_for :users + # We need to manually define OmniAuth routes since the automatic generation does not support the dynamic scope + devise_for :users, skip: :omniauth_callbacks get 'users/:user_id/projects' => 'users#projects', as: :user_projects resources :projects do @@ -48,6 +49,9 @@ Rails.application.routes.draw do root "home#index" end + + # See comment above for devise_for + devise_for :users, only: :omniauth_callbacks # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20150423204406_add_uid_and_provider_to_user.rb b/db/migrate/20150423204406_add_uid_and_provider_to_user.rb new file mode 100644 index 0000000..7a7164f --- /dev/null +++ b/db/migrate/20150423204406_add_uid_and_provider_to_user.rb @@ -0,0 +1,7 @@ +class AddUidAndProviderToUser < ActiveRecord::Migration + def change + add_column :users, :uid, :string + add_column :users, :provider, :string + add_index :users, [:uid, :provider], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 365f524..a0eb96a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -62,9 +62,12 @@ ActiveRecord::Schema.define(version: 20160418192431) do t.datetime "last_sign_in_at" t.string "current_sign_in_ip", limit: 255 t.string "last_sign_in_ip", limit: 255 + t.string "uid" + t.string "provider" end add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + add_index "users", ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, using: :btree end -- libgit2 0.21.2