Commit 3b98e9c283d7e9b25657b1f67e7299bbe173b90d
Committed by
Diego Camarinha
1 parent
8600ff27
[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
Showing
6 changed files
with
35 additions
and
4 deletions
Show diff stats
Gemfile
@@ -67,6 +67,9 @@ gem 'http_accept_language' | @@ -67,6 +67,9 @@ gem 'http_accept_language' | ||
67 | # Routes for JS files | 67 | # Routes for JS files |
68 | gem 'js-routes', '~> 1.1.0' | 68 | gem 'js-routes', '~> 1.1.0' |
69 | 69 | ||
70 | +# External logins with OmniAuth | ||
71 | +gem 'omniauth' | ||
72 | + | ||
70 | group :test do | 73 | group :test do |
71 | # Easier test writing | 74 | # Easier test writing |
72 | gem "shoulda-matchers", '~> 2.8.0' | 75 | gem "shoulda-matchers", '~> 2.8.0' |
Gemfile.lock
@@ -134,6 +134,7 @@ GEM | @@ -134,6 +134,7 @@ GEM | ||
134 | globalid (0.3.6) | 134 | globalid (0.3.6) |
135 | activesupport (>= 4.1.0) | 135 | activesupport (>= 4.1.0) |
136 | google-analytics-rails (0.0.6) | 136 | google-analytics-rails (0.0.6) |
137 | + hashie (3.4.1) | ||
137 | http-cookie (1.0.2) | 138 | http-cookie (1.0.2) |
138 | domain_name (~> 0.5) | 139 | domain_name (~> 0.5) |
139 | http_accept_language (2.0.5) | 140 | http_accept_language (2.0.5) |
@@ -202,6 +203,9 @@ GEM | @@ -202,6 +203,9 @@ GEM | ||
202 | nokogiri (1.6.6.2) | 203 | nokogiri (1.6.6.2) |
203 | mini_portile (~> 0.6.0) | 204 | mini_portile (~> 0.6.0) |
204 | ntlm-http (0.1.1) | 205 | ntlm-http (0.1.1) |
206 | + omniauth (1.2.2) | ||
207 | + hashie (>= 1.2, < 4) | ||
208 | + rack (~> 1.0) | ||
205 | orm_adapter (0.5.0) | 209 | orm_adapter (0.5.0) |
206 | pg (0.18.3) | 210 | pg (0.18.3) |
207 | poltergeist (1.7.0) | 211 | poltergeist (1.7.0) |
@@ -360,6 +364,7 @@ DEPENDENCIES | @@ -360,6 +364,7 @@ DEPENDENCIES | ||
360 | konacha | 364 | konacha |
361 | less-rails (~> 2.7.0) | 365 | less-rails (~> 2.7.0) |
362 | mocha | 366 | mocha |
367 | + omniauth | ||
363 | pg (~> 0.18.1) | 368 | pg (~> 0.18.1) |
364 | poltergeist (~> 1.7.0) | 369 | poltergeist (~> 1.7.0) |
365 | rails (= 4.2.4) | 370 | rails (= 4.2.4) |
app/models/user.rb
@@ -2,11 +2,14 @@ class User < ActiveRecord::Base | @@ -2,11 +2,14 @@ class User < ActiveRecord::Base | ||
2 | # Include default devise modules. Others available are: | 2 | # Include default devise modules. Others available are: |
3 | # :token_authenticatable, :confirmable, | 3 | # :token_authenticatable, :confirmable, |
4 | # :lockable, :timeoutable and :omniauthable | 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 | validates :name, presence: true | 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 | has_many :project_attributes, class_name: 'ProjectAttributes', dependent: :destroy | 14 | has_many :project_attributes, class_name: 'ProjectAttributes', dependent: :destroy |
12 | has_many :reading_group_attributes, class_name: 'ReadingGroupAttributes', dependent: :destroy | 15 | has_many :reading_group_attributes, class_name: 'ReadingGroupAttributes', dependent: :destroy |
@@ -17,4 +20,10 @@ class User < ActiveRecord::Base | @@ -17,4 +20,10 @@ class User < ActiveRecord::Base | ||
17 | def projects | 20 | def projects |
18 | project_attributes.map { |project_attr| project_attr.project } | 21 | project_attributes.map { |project_attr| project_attr.project } |
19 | end | 22 | end |
23 | + | ||
24 | + protected | ||
25 | + | ||
26 | + def password_required? | ||
27 | + provider.nil? | ||
28 | + end | ||
20 | end | 29 | end |
config/routes.rb
1 | Rails.application.routes.draw do | 1 | Rails.application.routes.draw do |
2 | scope "(:locale)", locale: /en|pt/ do | 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 | get 'users/:user_id/projects' => 'users#projects', as: :user_projects | 5 | get 'users/:user_id/projects' => 'users#projects', as: :user_projects |
5 | 6 | ||
6 | resources :projects do | 7 | resources :projects do |
@@ -49,6 +50,9 @@ Rails.application.routes.draw do | @@ -49,6 +50,9 @@ Rails.application.routes.draw do | ||
49 | root "home#index" | 50 | root "home#index" |
50 | end | 51 | end |
51 | 52 | ||
53 | + | ||
54 | + # See comment above for devise_for | ||
55 | + devise_for :users, only: :omniauth_callbacks | ||
52 | # The priority is based upon order of creation: first created -> highest priority. | 56 | # The priority is based upon order of creation: first created -> highest priority. |
53 | # See how all your routes lay out with "rake routes". | 57 | # See how all your routes lay out with "rake routes". |
54 | 58 |
db/migrate/20150423204406_add_uid_and_provider_to_user.rb
0 → 100644
db/schema.rb
@@ -64,9 +64,12 @@ ActiveRecord::Schema.define(version: 20151106182639) do | @@ -64,9 +64,12 @@ ActiveRecord::Schema.define(version: 20151106182639) do | ||
64 | t.datetime "last_sign_in_at" | 64 | t.datetime "last_sign_in_at" |
65 | t.string "current_sign_in_ip", limit: 255 | 65 | t.string "current_sign_in_ip", limit: 255 |
66 | t.string "last_sign_in_ip", limit: 255 | 66 | t.string "last_sign_in_ip", limit: 255 |
67 | + t.string "uid" | ||
68 | + t.string "provider" | ||
67 | end | 69 | end |
68 | 70 | ||
69 | add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree | 71 | add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree |
70 | add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree | 72 | add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree |
73 | + add_index "users", ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, using: :btree | ||
71 | 74 | ||
72 | end | 75 | end |