AGirlThatCodes
AGirlThatCodes

Reputation: 585

Devise Invitable - Restricting Access to Only invited users

I might be searching the wrong key words, but I can't find a way to only restrict users that are invited to be allowed to create an account on my rails site (or maybe I'm using devise-invitable wrong). I assume, there should be a method I call in the before filter or flip the switch on the initializer/devise.rb

I tried in my users_controller.rb and had no luck using Ruby Doc as reference

before_filter: invited?

I have read the initializers/devise.rb and the readme and didn't have any luck.

Upvotes: 0

Views: 1050

Answers (2)

AGirlThatCodes
AGirlThatCodes

Reputation: 585

I figured out that I didnt think of the obvious. Restrict users via devise with the routes, not devise-invitable.

Used Solution #2 as a reference (see below)

Lets suppose that you don't want to allow to sign up but you want to allow to change password for registered users. Just paste this code in routes.rb:

devise_for :users, :skip => [:registrations]                                          
    as :user do
      get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'    
      put 'users/:id' => 'devise/registrations#update', :as => 'user_registration'            
    end

And then you can make such link in your view:

= link_to "Change your password", edit_user_registration_path

Notice: you will need to update default devise views accordingly, i.e. in app/views/devise/registrations/edit.html.erb change registration_path(resource_name) to user_registration_path(resource) (If using shared views for multiple models, you can use send("#{resource_name}_registration_path", resource))

Notice: If you are using rails 4.0+ you should be using patch instead of put for updates. You should change the method in the form_tag residing in app/views/devise/registrations/edit.html.erb and the routes.rb file.

Upvotes: 1

East2d
East2d

Reputation: 2096

I think that you should make custom filter for this purpose.

before_action :authenticate_user!

before_filter :restrict_only_invited_users

def restrict_only_invited_users
  redirect_to :root if current_user.invitation_accepted_at.blank?
end

Upvotes: 1

Related Questions