Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Recognize when users revoke GH access and force re-login.

  • Loading branch information...
commit 21cf10471cd888fda780d284b8fa2359377845e5 1 parent ea61ad8
Ryan Daigle authored
1  app/controllers/application_controller.rb
View
@@ -26,6 +26,7 @@ def force_user_login
def current_user
begin
@current_user ||= session[:user_id] ? User.find(session[:user_id]) : nil
+ @current_user.gh_auth_active? ? @current_user : nil
rescue Exception => e
nil
end
30 app/models/gist_fetcher.rb
View
@@ -1,4 +1,4 @@
-class GistFetcher
+ class GistFetcher
class << self
@@ -15,18 +15,19 @@ def fetch
def fetch_gists(user_id)
user = User.find(user_id)
- gh = gh_client(user)
- log({ns: self, fn: __method__, measure: true}, user) do
- gh.gists.each do |gh_gist|
- Gist.import(gh_gist)
- end
- user.gists.pluck(:gh_id).each do |gh_gist_id|
- QC.enqueue("GistFetcher.fetch_gist_files", user_id, gh_gist_id)
+ gh_client(user) do |gh|
+ log({ns: self, fn: __method__, measure: true}, user) do
+ gh.gists.each do |gh_gist|
+ Gist.import(gh_gist)
+ end
+ user.gists.pluck(:gh_id).each do |gh_gist_id|
+ QC.enqueue("GistFetcher.fetch_gist_files", user_id, gh_gist_id)
+ end
end
+ QC.enqueue("User.refresh_index", user_id)
+ QC.enqueue("User.fetched!", user_id)
end
- QC.enqueue("User.refresh_index", user_id)
- QC.enqueue("User.fetched!", user_id)
end
def fetch_gist_files(user_id, gh_gist_id)
@@ -40,7 +41,14 @@ def fetch_gist_files(user_id, gh_gist_id)
private
def gh_client(user)
- Octokit::Client.new(:login => user.gh_username, :oauth_token => user.gh_oauth_token, :auto_traversal => true)
+ client = Octokit::Client.new(:login => user.gh_username, :oauth_token => user.gh_oauth_token, :auto_traversal => true)
+ begin
+ client.user # throws exception if oauth not cool
+ yield client
+ rescue Octokit::Unauthorized => e
+ log_exception({ns: self, fn: __method__, measure: true}, user, e)
+ user.invalidate_auth!
+ end
end
end
8 app/models/user.rb
View
@@ -1,6 +1,6 @@
class User < ActiveRecord::Base
- attr_accessible :gh_id, :gh_email, :gh_name, :gh_avatar_url, :gh_oauth_token, :gh_url, :gh_username
+ attr_accessible :gh_id, :gh_email, :gh_name, :gh_avatar_url, :gh_oauth_token, :gh_url, :gh_username, :gh_auth_active
has_many :gists, :dependent => :destroy
has_many :files, :through => :gists
@@ -13,7 +13,7 @@ def authenticate(auth)
attributes = {
gh_id: auth.uid, gh_oauth_token: auth.credentials.token, gh_username: auth.info.nickname, gh_name: auth.info.name,
- gh_email: auth.info.email, gh_avatar_url: auth.info.image, gh_url: auth.info.urls.GitHub
+ gh_email: auth.info.email, gh_avatar_url: auth.info.image, gh_url: auth.info.urls.GitHub, gh_auth_active: true
}
if(existing_user = User.where(gh_id: auth.uid).first)
@@ -40,6 +40,10 @@ def fetched!(user_id)
end
end
+ def invalidate_auth!
+ update_attribute(:gh_auth_active, false)
+ end
+
def fetched!
update_attribute(:last_gh_fetch, Time.now)
end
10 db/migrate/20121107020238_add_auth_active.rb
View
@@ -0,0 +1,10 @@
+class AddAuthActive < ActiveRecord::Migration
+
+ def up
+ add_column :users, :gh_auth_active, :boolean, :default => true
+ end
+
+ def down
+ remove_column :users, :gh_auth_active
+ end
+end
7 db/schema.rb
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121005200216) do
+ActiveRecord::Schema.define(:version => 20121107020238) do
create_table "gist_files", :force => true do |t|
t.integer "gist_id"
@@ -63,9 +63,10 @@
t.string "gh_oauth_token"
t.string "gh_avatar_url"
t.string "gh_url"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
t.datetime "last_gh_fetch"
+ t.boolean "gh_auth_active", :default => true
end
add_index "users", ["gh_id"], :name => "index_users_on_gh_id"
Please sign in to comment.
Something went wrong with that request. Please try again.