Commit 00fb4c8c

mo kareem <email@mokhan.ca>
2011-04-19 02:48:30
add authentications to user and allow for associating twitter account authentication with User.
1 parent 7e922d4
app/controllers/authentications_controller.rb
@@ -0,0 +1,25 @@
+class AuthenticationsController < ApplicationController
+  # GET /authentications
+  # GET /authentications.xml
+  def index
+    @authentications = current_user.authentications if current_user
+  end
+
+  # POST /authentications
+  # POST /authentications.xml
+  def create
+    auth = request.env["omniauth.auth"]
+    current_user.authentications.find_or_create_by_provider_and_uid(auth['provider'], auth['uid'])
+    flash[:notice] = "Authentication successful."
+    redirect_to authentications_url
+  end
+
+  # DELETE /authentications/1
+  # DELETE /authentications/1.xml
+  def destroy
+    @authentication = current_user.authentications.find(params[:id])
+    @authentication.destroy
+    flash[:notice] = "Successfully destroyed authentication."
+    redirect_to authentications_url
+  end
+end
app/helpers/authentications_helper.rb
@@ -0,0 +1,2 @@
+module AuthenticationsHelper
+end
app/models/authentication.rb
@@ -0,0 +1,3 @@
+class Authentication < ActiveRecord::Base
+  belongs_to :user
+end
app/models/user.rb
@@ -1,4 +1,6 @@
 class User < ActiveRecord::Base
+  has_many :authentications
+  
   # Include default devise modules. Others available are:
   # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
   devise :database_authenticatable, :registerable,
app/views/authentications/index.html.erb
@@ -0,0 +1,39 @@
+
+<% if @authentications %>
+  <% unless @authentications.empty? %>
+    <p><strong>You can sign in to this account using:</strong></p>
+    <div class="authentications">
+      <% for authentication in @authentications %>
+        <div class="authentication">
+          <%= image_tag "#{authentication.provider}_32.png", :size => "32x32" %>
+          <div class="provider"><%= authentication.provider.titleize %></div>
+          <div class="uid"><%= authentication.uid %></div>
+          <%= link_to "X", authentication, :confirm => 'Are you sure you want to remove this authentication option?', :method => :delete, :class => "remove" %>
+        </div>
+      <% end %>
+      <div class="clear"></div>
+    </div>
+  <% end %>
+  <p><strong>Add another service to sign in with:</strong></p>
+<% else %>
+  <p><strong>Sign in through one of these services:</strong></p>
+<% end %>
+
+<a href="/auth/twitter" class="auth_provider">
+  <%= image_tag "twitter_64.png", :size => "64x64", :alt => "Twitter" %>
+  Twitter
+</a>
+<a href="/auth/facebook" class="auth_provider">
+  <%= image_tag "facebook_64.png", :size => "64x64", :alt => "Facebook" %>
+  Facebook
+</a>
+<a href="/auth/google_apps" class="auth_provider">
+  <%= image_tag "google_64.png", :size => "64x64", :alt => "Google" %>
+  Google
+</a>
+<a href="/auth/open_id" class="auth_provider">
+  <%= image_tag "openid_64.png", :size => "64x64", :alt => "OpenID" %>
+  OpenID
+</a>
+<div class="clear"></div>
+
app/views/layouts/application.html.erb
@@ -18,8 +18,6 @@
       <%= link_to "sign up", new_user_registration_path %> or <%= link_to "sign in", new_user_session_path %>
     <% end %>
   </div>
-
 <%= yield %>
-
 </body>
 </html>
config/initializers/session_store.rb
@@ -1,6 +1,6 @@
 # Be sure to restart your server when you modify this file.
 
-Cake::Application.config.session_store :cookie_store, :key => '_cake_session'
+Cake::Application.config.session_store :cookie_store, :key => '_cake_eater'
 
 # Use the database for sessions instead of the cookie-based default,
 # which shouldn't be used to store highly confidential information
config/routes.rb
@@ -1,4 +1,6 @@
 Cake::Application.routes.draw do
+  resources :authentications
+
   get "dashboard/index"
 
   get "home/index"
@@ -56,7 +58,7 @@ Cake::Application.routes.draw do
   # just remember to delete public/index.html.
   #root :to => "home#index"
   authenticate :user do
-  root :to => "home#index"
+    root :to => "home#index"
   end
   root :to => "devise:sessions#new"
 
@@ -65,4 +67,6 @@ Cake::Application.routes.draw do
   # This is a legacy wild controller route that's not recommended for RESTful applications.
   # Note: This route will make all actions in every controller accessible via GET requests.
   # match ':controller(/:action(/:id(.:format)))'
+  #
+  match '/auth/:provider/callback' => 'authentications#create'
 end
db/migrate/20110419012853_create_authentications.rb
@@ -0,0 +1,15 @@
+class CreateAuthentications < ActiveRecord::Migration
+  def self.up
+    create_table :authentications do |t|
+      t.integer :user_id
+      t.string :provider
+      t.string :uid
+
+      t.timestamps
+    end
+  end
+
+  def self.down
+    drop_table :authentications
+  end
+end
db/schema.rb
@@ -10,7 +10,15 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20110417070236) do
+ActiveRecord::Schema.define(:version => 20110419012853) do
+
+  create_table "authentications", :force => true do |t|
+    t.integer  "user_id"
+    t.string   "provider"
+    t.string   "uid"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+  end
 
   create_table "users", :force => true do |t|
     t.string   "email",                                 :default => "", :null => false
public/images/facebook_64.png
Binary file
public/images/google_64.png
Binary file
public/images/openid_64.png
Binary file
public/images/twitter_32.png
Binary file
public/images/twitter_64.png
Binary file
public/stylesheets/scaffold.css
@@ -0,0 +1,56 @@
+body { background-color: #fff; color: #333; }
+
+body, p, ol, ul, td {
+  font-family: verdana, arial, helvetica, sans-serif;
+  font-size:   13px;
+  line-height: 18px;
+}
+
+pre {
+  background-color: #eee;
+  padding: 10px;
+  font-size: 11px;
+}
+
+a { color: #000; }
+a:visited { color: #666; }
+a:hover { color: #fff; background-color:#000; }
+
+div.field, div.actions {
+  margin-bottom: 10px;
+}
+
+#notice {
+  color: green;
+}
+
+.field_with_errors {
+  padding: 2px;
+  background-color: red;
+  display: table;
+}
+
+#error_explanation {
+  width: 450px;
+  border: 2px solid red;
+  padding: 7px;
+  padding-bottom: 0;
+  margin-bottom: 20px;
+  background-color: #f0f0f0;
+}
+
+#error_explanation h2 {
+  text-align: left;
+  font-weight: bold;
+  padding: 5px 5px 5px 15px;
+  font-size: 12px;
+  margin: -7px;
+  margin-bottom: 0px;
+  background-color: #c00;
+  color: #fff;
+}
+
+#error_explanation ul li {
+  font-size: 12px;
+  list-style: square;
+}
test/fixtures/authentications.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+  user_id: 1
+  provider: MyString
+  uid: MyString
+
+two:
+  user_id: 1
+  provider: MyString
+  uid: MyString
test/functional/authentications_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class AuthenticationsControllerTest < ActionController::TestCase
+  setup do
+    @authentication = authentications(:one)
+  end
+
+  test "should get index" do
+    get :index
+    assert_response :success
+    assert_not_nil assigns(:authentications)
+  end
+
+  test "should get new" do
+    get :new
+    assert_response :success
+  end
+
+  test "should create authentication" do
+    assert_difference('Authentication.count') do
+      post :create, :authentication => @authentication.attributes
+    end
+
+    assert_redirected_to authentication_path(assigns(:authentication))
+  end
+
+  test "should show authentication" do
+    get :show, :id => @authentication.to_param
+    assert_response :success
+  end
+
+  test "should get edit" do
+    get :edit, :id => @authentication.to_param
+    assert_response :success
+  end
+
+  test "should update authentication" do
+    put :update, :id => @authentication.to_param, :authentication => @authentication.attributes
+    assert_redirected_to authentication_path(assigns(:authentication))
+  end
+
+  test "should destroy authentication" do
+    assert_difference('Authentication.count', -1) do
+      delete :destroy, :id => @authentication.to_param
+    end
+
+    assert_redirected_to authentications_path
+  end
+end
test/unit/helpers/authentications_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class AuthenticationsHelperTest < ActionView::TestCase
+end
test/unit/authentication_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class AuthenticationTest < ActiveSupport::TestCase
+  # Replace this with your real tests.
+  test "the truth" do
+    assert true
+  end
+end