Skip to content

Commit

Permalink
Implements enable_registration option in strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
slainer68 committed Mar 16, 2012
1 parent 1cbbb2a commit 238f38d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
10 changes: 10 additions & 0 deletions README.markdown
Expand Up @@ -112,6 +112,16 @@ use OmniAuth::Builder do
end
```

## Disabling registration

To disable the registration feature and disallow unauthorized users to register using the strategy, just set `enable_registration` option to `false` (`true` by default) :

```ruby
use OmniAuth::Builder do
provider :identity, :enable_registration => false
end
```

## Customizing Registration Failure

To use your own custom registration form, create a form that POSTs to
Expand Down
9 changes: 5 additions & 4 deletions lib/omniauth/strategies/identity.rb
@@ -1,13 +1,14 @@
module OmniAuth
module Strategies
# The identity strategy allows you to provide simple internal
# The identity strategy allows you to provide simple internal
# user authentication using the same process flow that you
# use for external OmniAuth providers.
class Identity
include OmniAuth::Strategy

option :fields, [:name, :email]
option :on_failed_registration, nil
option :enable_registration, true

def request_phase
OmniAuth::Form.build(
Expand All @@ -16,8 +17,8 @@ def request_phase
) do |f|
f.text_field 'Login', 'auth_key'
f.password_field 'Password', 'password'
f.html "<p align='center'><a href='#{registration_path}'>Create an Identity</a></p>"
end.to_response
f.html "<p align='center'><a href='#{registration_path}'>Create an Identity</a></p>" if options[:enable_registration]
end.to_response
end

def callback_phase
Expand All @@ -26,7 +27,7 @@ def callback_phase
end

def other_phase
if on_registration_path?
if options[:enable_registration] && on_registration_path?
if request.get?
registration_form
elsif request.post?
Expand Down
47 changes: 39 additions & 8 deletions spec/omniauth/strategies/identity_spec.rb
Expand Up @@ -30,9 +30,22 @@ def set_app!(identity_options = {})
end

describe '#request_phase' do
it 'should display a form' do
get '/auth/identity'
last_response.body.should be_include("<form")
context "registration is enabled" do
it 'should display a form with a link' do
get '/auth/identity'
last_response.body.should be_include("<form")
last_response.body.should be_include("<a")
end
end

context "registration is disabled" do
it 'should display a form without a link if registration is disabled' do
set_app!(:enable_registration => false)
get '/auth/identity'

last_response.body.should be_include("<form")
last_response.body.should_not be_include("<a")
end
end
end

Expand Down Expand Up @@ -72,16 +85,34 @@ def set_app!(identity_options = {})
end

describe '#registration_form' do
it 'should trigger from /auth/identity/register by default' do
get '/auth/identity/register'
last_response.body.should be_include("Register Identity")
context 'registration is enabled' do
it 'should trigger from /auth/identity/register by default' do
get '/auth/identity/register'
last_response.body.should be_include("Register Identity")
end
end

context 'registration is disabled' do
it 'should call app' do
set_app!(:enable_registration => false)
get '/auth/identity/register'
last_response.body.should == "HELLO!"
end
end
end

describe '#registration_phase' do
context 'registration is disabled' do
it 'should call app' do
set_app!(:enable_registration => false)
post '/auth/identity/register'
last_response.body.should == "HELLO!"
end
end

context 'with successful creation' do
let(:properties){ {
:name => 'Awesome Dude',
:name => 'Awesome Dude',
:email => 'awesome@example.com',
:password => 'face',
:password_confirmation => 'face'
Expand All @@ -100,7 +131,7 @@ def set_app!(identity_options = {})

context 'with invalid identity' do
let(:properties) { {
:name => 'Awesome Dude',
:name => 'Awesome Dude',
:email => 'awesome@example.com',
:password => 'NOT',
:password_confirmation => 'MATCHING'
Expand Down

0 comments on commit 238f38d

Please sign in to comment.