Commit f85181ef4ecd21499f44462699818113010cc2d5

Authored by Daniel
Committed by Eduardo Silva Araújo
1 parent a38b2ae2
Exists in colab

[Colab] Initial implementation of OmniAuth-able model

- Add provider and uid fields to user with proper validation
- Manual routes for OmniAuth have to be used because the route generator
  does not play well with the dynamic scope we used for the locale
Gemfile
... ... @@ -67,6 +67,9 @@ gem 'http_accept_language'
67 67 # Routes for JS files
68 68 gem 'js-routes', '~> 1.1.0'
69 69  
  70 +# External logins with OmniAuth
  71 +gem 'omniauth'
  72 +
70 73 group :test do
71 74 # Easier test writing
72 75 gem "shoulda-matchers", '~> 2.8.0'
... ...
Gemfile.lock
... ... @@ -134,6 +134,7 @@ GEM
134 134 globalid (0.3.6)
135 135 activesupport (>= 4.1.0)
136 136 google-analytics-rails (0.0.6)
  137 + hashie (3.4.1)
137 138 http-cookie (1.0.2)
138 139 domain_name (~> 0.5)
139 140 http_accept_language (2.0.5)
... ... @@ -206,6 +207,9 @@ GEM
206 207 nokogiri (1.6.6.2)
207 208 mini_portile (~> 0.6.0)
208 209 ntlm-http (0.1.1)
  210 + omniauth (1.2.2)
  211 + hashie (>= 1.2, < 4)
  212 + rack (~> 1.0)
209 213 orm_adapter (0.5.0)
210 214 pg (0.18.3)
211 215 poltergeist (1.7.0)
... ... @@ -364,6 +368,7 @@ DEPENDENCIES
364 368 konacha
365 369 less-rails (~> 2.7.0)
366 370 mocha
  371 + omniauth
367 372 pg (~> 0.18.1)
368 373 poltergeist (~> 1.7.0)
369 374 rails (= 4.2.4)
... ...
app/models/user.rb
... ... @@ -2,11 +2,14 @@ class User &lt; ActiveRecord::Base
2 2 # Include default devise modules. Others available are:
3 3 # :token_authenticatable, :confirmable,
4 4 # :lockable, :timeoutable and :omniauthable
5   - devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  5 + devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable,
  6 + :omniauthable, omniauth_providers: []
6 7  
7 8 validates :name, presence: true
8   - validates :email, presence: true
9   - validates :email, uniqueness: true
  9 + validates :email, presence: true, uniqueness: true
  10 + validates :provider, presence: true, unless: Proc.new { |user| user.uid.nil? || user.uid.blank? }
  11 + validates :uid, presence: true, uniqueness: { scope: :provider },
  12 + unless: Proc.new { |user| user.provider.nil? || user.provider.blank? }
10 13  
11 14 has_many :project_attributes, class_name: 'ProjectAttributes', dependent: :destroy
12 15 has_many :reading_group_attributes, class_name: 'ReadingGroupAttributes', dependent: :destroy
... ... @@ -17,4 +20,10 @@ class User &lt; ActiveRecord::Base
17 20 def projects
18 21 project_attributes.map { |project_attr| project_attr.project }
19 22 end
  23 +
  24 + protected
  25 +
  26 + def password_required?
  27 + provider.nil?
  28 + end
20 29 end
... ...
config/routes.rb
1 1 Rails.application.routes.draw do
2 2 scope "(:locale)", locale: /en|pt/ do
3   - devise_for :users
  3 + # We need to manually define OmniAuth routes since the automatic generation does not support the dynamic scope
  4 + devise_for :users, skip: :omniauth_callbacks
4 5 get 'users/:user_id/projects' => 'users#projects', as: :user_projects
5 6  
6 7 resources :projects do
... ... @@ -48,6 +49,9 @@ Rails.application.routes.draw do
48 49 root "home#index"
49 50 end
50 51  
  52 +
  53 + # See comment above for devise_for
  54 + devise_for :users, only: :omniauth_callbacks
51 55 # The priority is based upon order of creation: first created -> highest priority.
52 56 # See how all your routes lay out with "rake routes".
53 57  
... ...
db/migrate/20150423204406_add_uid_and_provider_to_user.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +class AddUidAndProviderToUser < ActiveRecord::Migration
  2 + def change
  3 + add_column :users, :uid, :string
  4 + add_column :users, :provider, :string
  5 + add_index :users, [:uid, :provider], unique: true
  6 + end
  7 +end
... ...
db/schema.rb
... ... @@ -62,9 +62,12 @@ ActiveRecord::Schema.define(version: 20160418192431) do
62 62 t.datetime "last_sign_in_at"
63 63 t.string "current_sign_in_ip", limit: 255
64 64 t.string "last_sign_in_ip", limit: 255
  65 + t.string "uid"
  66 + t.string "provider"
65 67 end
66 68  
67 69 add_index "users", ["email"], name: "index_users_on_email", unique: true
68 70 add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  71 + add_index "users", ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, using: :btree
69 72  
70 73 end
... ...