Commit 400beb72c542312e3009dae8a7cf4e3e0b8bf1e1

Authored by Nathan Broadbent
1 parent 4ac8793d
Exists in master and in 1 other branch production

Added 'repo' scope for OAuth, for access to issues. Added ability to link and unlink github account.

app/assets/stylesheets/errbit.css
... ... @@ -225,7 +225,7 @@ a.action { float: right; font-size: 0.9em;}
225 225 }
226 226  
227 227 #action-bar span.github a { background: url(images/icons/github.png) no-repeat 6px 5px; }
228   -
  228 +#action-bar span.unlink_github a { background: url(images/icons/unlink_github.png) no-repeat 6px 5px; }
229 229  
230 230 /* Content */
231 231 #content {
... ...
app/assets/stylesheets/images/icons/unlink_github.png 0 → 100644

2.02 KB

app/controllers/users/omniauth_callbacks_controller.rb
1 1 class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
2 2 def github
3   - @user = User.find_for_github_oauth(request.env["omniauth.auth"])
  3 + github_login = request.env["omniauth.auth"].extra.raw_info.login
  4 + github_token = request.env["omniauth.auth"].credentials.token
  5 + github_user = User.where(:github_login => github_login).first
4 6  
5   - if @user
6   - flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Github"
  7 + # If user is already signed in, link github details to their account
  8 + if current_user
  9 + # ... unless a user is already registered with same github login
  10 + if github_user && github_user != current_user
  11 + flash[:error] = "User already registered with Github login '#{github_login}'"
  12 + redirect_to user_path(current_user)
  13 + else
  14 + # Add github details to current user
  15 + current_user.update_attributes(
  16 + :github_login => github_login,
  17 + :github_oauth_token => github_token
  18 + )
  19 + flash[:success] = "Successfully linked Github account!"
  20 + redirect_to user_path(current_user)
  21 + end
  22 +
  23 + elsif github_user
  24 + # Store OAuth token
  25 + @user.update_attribute :github_oauth_token, request.env["omniauth.auth"].credentials.token
  26 +
  27 + flash[:success] = I18n.t "devise.omniauth_callbacks.success", :kind => "Github"
7 28 sign_in_and_redirect @user, :event => :authentication
8 29 else
9 30 redirect_to new_user_session_path
... ...
app/controllers/users_controller.rb
... ... @@ -2,7 +2,7 @@ class UsersController &lt; ApplicationController
2 2 respond_to :html
3 3  
4 4 before_filter :require_admin!, :except => [:edit, :update]
5   - before_filter :find_user, :only => [:show, :edit, :update, :destroy]
  5 + before_filter :find_user, :only => [:show, :edit, :update, :destroy, :unlink_github]
6 6 before_filter :require_user_edit_priviledges, :only => [:edit, :update]
7 7  
8 8 def index
... ... @@ -59,6 +59,11 @@ class UsersController &lt; ApplicationController
59 59 redirect_to users_path
60 60 end
61 61  
  62 + def unlink_github
  63 + @user.update_attributes :github_login => nil, :github_oauth_token => nil
  64 + redirect_to user_path(@user)
  65 + end
  66 +
62 67 protected
63 68  
64 69 def find_user
... ...
app/models/user.rb
... ... @@ -7,6 +7,7 @@ class User
7 7  
8 8 field :email
9 9 field :github_login
  10 + field :github_oauth_token
10 11 field :name
11 12 field :admin, :type => Boolean, :default => false
12 13 field :per_page, :type => Fixnum, :default => PER_PAGE
... ... @@ -39,12 +40,6 @@ class User
39 40 apps.all.include?(app)
40 41 end
41 42  
42   - def self.find_for_github_oauth(omniauth_env)
43   - data = omniauth_env.extra.raw_info
44   -
45   - User.where(:github_login => data.login).first
46   - end
47   -
48 43 def password_required?
49 44 github_login.present? ? false : super
50 45 end
... ...
app/views/shared/_link_github_account.html.haml 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +- if Errbit::Config.github_authentication && user == current_user
  2 + - if user.github_login && user.github_oauth_token
  3 + %span.unlink_github= link_to "Unlink GitHub account", unlink_github_user_path(user), :method => :delete, :confirm => "Are you sure?"
  4 + - else
  5 + %span.github= link_to "Link GitHub account", user_omniauth_authorize_path(:github)
... ...
app/views/users/edit.html.haml
1 1 - content_for :title, "Edit #{@user.name}"
2   -- content_for :action_bar, link_to('cancel', user_path(@user), :class => 'button')
  2 +- content_for :action_bar do
  3 + = render :partial => 'shared/link_github_account', :locals => {:user => @user}
  4 + = link_to('cancel', user_path(@user), :class => 'button')
3 5  
4 6 = form_for @user, :html => {:autocomplete => "off"} do |f|
5 7 = @user.errors.full_messages.to_sentence
6 8 = render 'fields', :f => f
7   -
8   - %div.buttons= f.submit 'Update User'
9 9 \ No newline at end of file
  10 +
  11 + %div.buttons= f.submit 'Update User'
... ...
app/views/users/show.html.haml
1 1 - content_for :title, @user.name
2 2 - content_for :action_bar do
  3 + = render :partial => 'shared/link_github_account', :locals => {:user => @user}
3 4 %span= link_to('Add a New User', new_user_path, :class => 'add')
4 5 = link_to 'edit', edit_user_path(@user), :class => 'button'
5 6 = link_to 'destroy', user_path(@user), :method => :delete, :confirm => 'Seriously?', :class => 'button'
6 7  
  8 +
  9 +
7 10 %table.single_user
8 11 %tr
9 12 %th Email
... ... @@ -14,12 +17,9 @@
14 17 %td.main= @user.username
15 18 - if Errbit::Config.github_authentication && @user.github_login.present?
16 19 %tr
17   - %th GitHub
  20 + %th GitHub Login
18 21 %td.main= link_to @user.github_login, "https://github.com/#{@user.github_login}"
19 22 %tr
20   - %th Token
21   - %td= @user.authentication_token
22   - %tr
23 23 %th Admin?
24 24 %td= @user.admin? ? 'Y' : 'N'
25 25 %tr
... ...
config/initializers/devise.rb
... ... @@ -119,7 +119,7 @@ Devise.setup do |config|
119 119 # config.sign_out_all_scopes = false
120 120  
121 121 if Errbit::Config.github_authentication || Rails.env.test?
122   - config.omniauth :github, Errbit::Config.github_client_id, Errbit::Config.github_secret
  122 + config.omniauth :github, Errbit::Config.github_client_id, Errbit::Config.github_secret, :scope => 'repo'
123 123 end
124 124  
125 125 # ==> Navigation configuration
... ...
config/routes.rb
... ... @@ -8,7 +8,11 @@ Errbit::Application.routes.draw do
8 8  
9 9 resources :notices, :only => [:show]
10 10 resources :deploys, :only => [:show]
11   - resources :users
  11 + resources :users do
  12 + member do
  13 + delete :unlink_github
  14 + end
  15 + end
12 16 resources :errs, :only => [:index] do
13 17 collection do
14 18 post :destroy_several
... ...
spec/models/user_spec.rb
... ... @@ -31,15 +31,6 @@ describe User do
31 31 end
32 32 end
33 33  
34   - describe '.find_for_github_oauth' do
35   - let(:auth_hash) { Hashie::Mash.new(:provider => 'github', :extra => { :raw_info => { :login => 'nashby' } }) }
36   -
37   - it 'finds user by github login' do
38   - user = Fabricate(:user, :github_login => 'nashby')
39   - User.find_for_github_oauth(auth_hash).should == user
40   - end
41   - end
42   -
43 34 context 'Watchers' do
44 35  
45 36 it 'has many watchers' do
... ...