Skip to content

Commit

Permalink
Merge pull request #41 from KitaitiMakoto/omniauth-developer
Browse files Browse the repository at this point in the history
Omniauth developer strategy
  • Loading branch information
kentaro committed Nov 12, 2012
2 parents 1e42ac5 + 4649b3e commit 36f6527
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 22 deletions.
12 changes: 6 additions & 6 deletions app/contexts/session_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def initialize (args)
def create (auth_params)
is_new_record = user.new_record?

update_name(auth_params['info']['nickname'])
update_image(auth_params['extra']['raw_info']['avatar_url'])
update_access_token(auth_params['credentials'])
update_privilege
update_name(auth_params.info.nickname)
update_image(auth_params.extra.raw_info.try(:avatar_url))
update_access_token(auth_params.credentials)
update_privilege(auth_params.provider)
update_api_token

# We don't bother to save a user who is new for the service and
Expand Down Expand Up @@ -57,8 +57,8 @@ def update_access_token(credentials)
end
end

def update_privilege
if Settings.github.try(:organizations).present?
def update_privilege(provider)
if provider == 'github' && Settings.github.organizations.present?
organizations.each do |org|
if org['login'].in?(Settings.github.organizations || [])
user.member = true
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class SessionsController < ApplicationController
protect_from_forgery with: :exception, except: ['create']
skip_before_filter :require_member, only: [:create, :destroy]

def create
@user = User.where(
provider: auth_params['provider'],
uid: auth_params['uid'],
provider: auth_params.provider,
uid: auth_params.uid,
).first_or_initialize
context = SessionContext.new(user: @user)

Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class User < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
include LogicallyDeletableRole

validates :provider, presence: true, inclusion: { in: %w(github) }
validates :provider, presence: true, inclusion: { in: Settings.providers }
validates :name, presence: true, uniqueness: true, length: { maximum: 40 }
validates :uid, presence: true, format: { with: /\A[0-9]+\Z/ }
validates :image, presence: true, format: { with: /\A\/\/gravatar\.com\/avatar\/[a-z0-9]{32}\Z/ }
Expand Down
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
</li>
<% else -%>
<li><%= link_to t('layout.nav.sign_in'), signin_path %></li>
<% if Rails.env.development? %>
<li><%= link_to t('layout.nav.dev_sign_in'), dev_signin_path %></li>
<% end %>
<% end -%>
</ul>
</div>
Expand Down
7 changes: 5 additions & 2 deletions app/views/root/caveat.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<% unless current_user.member? -%>
(Not a member)
<% end -%>
<%= link_to "Sign out", signout_path, method: :delete %>
<%= link_to t('layout.nav.sign_out'), signout_path, method: :delete %>
<% else -%>
<%= link_to "Sign in", signin_path %>
<%= link_to t('layout.nav.sign_in'), signin_path %>
<% if Rails.env.development? %>
<%= link_to t('layout.nav.dev_sign_in'), dev_signin_path %>
<% end %>
<% end -%>
10 changes: 3 additions & 7 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
Triglav::Application.config.middleware.use OmniAuth::Builder do
provider :github, Settings.github.client_id, Settings.github.client_secret, scope: 'user'
if Rails.env.development?
provider :developer, fields: [:nickname, :uid], uid_field: :uid
end
end

OmniAuth.config.logger = Rails.logger







1 change: 1 addition & 0 deletions config/locales/translation_en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ en:
api: API
my_page: :helpers.links.my_page
sign_in: Sign in
dev_sign_in: Sign in with developer strategy
sign_out: Sign out

notice:
Expand Down
1 change: 1 addition & 0 deletions config/locales/translation_ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ ja:
api: API
my_page: :helpers.links.my_page
sign_in: サインイン
dev_sign_in: developerストラテジーでサインイン
sign_out: サインアウト

notice:
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
get '/signin' => redirect('/auth/github')
delete '/signout', to: 'sessions#destroy'
get '/auth/:provider/callback', to: 'sessions#create'
if Rails.env.development?
get '/dev_signin' => redirect('/auth/developer')
post '/auth/developer/callback', to: 'sessions#create'
end

resources :users, constraints: { id: /[^\/\.]+/ }, only: %w(show update)

Expand Down
3 changes: 3 additions & 0 deletions config/settings/environment.sample.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
providers:
- developer # Don't use this in production environment
- github
github:
organizations:
- triglav-developers
Expand Down
2 changes: 1 addition & 1 deletion spec/contexts/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@

before {
context.stub(:organizations).and_return([{ "login" => "triglav-developers" }])
context.update_privilege
context.update_privilege('github')
}

it { expect(user.member).to be_true }
Expand Down
21 changes: 20 additions & 1 deletion spec/requests/sessions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec_helper"

describe "Signin/Signout" do
shared_examples "describing Signin/Signout" do
subject { page }

describe "GET /" do
Expand Down Expand Up @@ -251,3 +251,22 @@
end
end
end

describe "Signin/Signout" do
it_behaves_like "describing Signin/Signout"

context "development environment" do
subject { page }

before {
Rails.stub(:env) { ActiveSupport::StringInquirer.new('development') }
Rails.application.reload_routes!
}

it {
visit '/'
expect(subject).to have_content 'Sign in with developer strategy'
}
it_behaves_like "describing Signin/Signout"
end
end
4 changes: 2 additions & 2 deletions spec/support/signin.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def auth_params_for (user)
{
Hashie::Mash.new({
"uid" => user.uid,
"provider" => user.provider,
"info" => {
Expand All @@ -13,7 +13,7 @@ def auth_params_for (user)
"credentials" => {
"token" => user.access_token,
},
}
})
end

def sign_in (user)
Expand Down

0 comments on commit 36f6527

Please sign in to comment.