Commit 633fe59363889030c2f85ffa845a870235edb8ac
Committed by
Rafael Manzo
1 parent
e14c9da2
[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
37 additions
and
6 deletions
Show diff stats
Gemfile
... | ... | @@ -63,6 +63,9 @@ gem 'google-analytics-rails', '~> 0.0.6' |
63 | 63 | # Browser language detection |
64 | 64 | gem 'http_accept_language' |
65 | 65 | |
66 | +# External logins with OmniAuth | |
67 | +gem 'omniauth' | |
68 | + | |
66 | 69 | group :test do |
67 | 70 | # Easier test writing |
68 | 71 | gem "shoulda-matchers", '~> 2.8.0' | ... | ... |
Gemfile.lock
... | ... | @@ -138,6 +138,7 @@ GEM |
138 | 138 | globalid (0.3.6) |
139 | 139 | activesupport (>= 4.1.0) |
140 | 140 | google-analytics-rails (0.0.6) |
141 | + hashie (3.4.1) | |
141 | 142 | http-cookie (1.0.2) |
142 | 143 | domain_name (~> 0.5) |
143 | 144 | http_accept_language (2.0.5) |
... | ... | @@ -203,6 +204,9 @@ GEM |
203 | 204 | nokogiri (1.6.6.2) |
204 | 205 | mini_portile (~> 0.6.0) |
205 | 206 | ntlm-http (0.1.1) |
207 | + omniauth (1.2.2) | |
208 | + hashie (>= 1.2, < 4) | |
209 | + rack (~> 1.0) | |
206 | 210 | orm_adapter (0.5.0) |
207 | 211 | pg (0.18.2) |
208 | 212 | poltergeist (1.6.0) |
... | ... | @@ -354,6 +358,7 @@ DEPENDENCIES |
354 | 358 | kalibro_client (~> 1.3.0) |
355 | 359 | konacha |
356 | 360 | mocha |
361 | + omniauth | |
357 | 362 | pg (~> 0.18.1) |
358 | 363 | poltergeist (~> 1.6.0) |
359 | 364 | rails (= 4.2.4) | ... | ... |
app/models/user.rb
... | ... | @@ -2,11 +2,14 @@ class User < 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' |
12 | 15 | has_many :reading_group_attributes, class_name: 'ReadingGroupAttributes' |
... | ... | @@ -17,4 +20,10 @@ class User < 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 |
... | ... | @@ -46,6 +47,9 @@ Rails.application.routes.draw do |
46 | 47 | root "home#index" |
47 | 48 | end |
48 | 49 | |
50 | + | |
51 | + # See comment above for devise_for | |
52 | + devise_for :users, only: :omniauth_callbacks | |
49 | 53 | # The priority is based upon order of creation: first created -> highest priority. |
50 | 54 | # See how all your routes lay out with "rake routes". |
51 | 55 | ... | ... |
db/migrate/20150423204406_add_uid_and_provider_to_user.rb
0 → 100644
db/schema.rb
... | ... | @@ -61,9 +61,12 @@ ActiveRecord::Schema.define(version: 20150616164352) do |
61 | 61 | t.datetime "last_sign_in_at" |
62 | 62 | t.string "current_sign_in_ip", limit: 255 |
63 | 63 | t.string "last_sign_in_ip", limit: 255 |
64 | + t.string "uid" | |
65 | + t.string "provider" | |
64 | 66 | end |
65 | 67 | |
66 | - add_index "users", ["email"], name: "index_users_on_email", unique: true | |
67 | - add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true | |
68 | + add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree | |
69 | + add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree | |
70 | + add_index "users", ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, using: :btree | |
68 | 71 | |
69 | 72 | end | ... | ... |