简体   繁体   中英

Is it possible to have different paths on the same devise model with constraints?

I'm trying to have different paths for the same devise model/resource with constraints, but the first path is the one being applied in this case "visitor".

constraints(ValidSubdomainFrontend) do
    devise_for :users, :path => "visitor", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
end


constraints(ValidSubdomainAdmin) do
    devise_for :users do
        get 'users', :path => "admin", :to => 'site_backend#index', :as => :user_root # Rails 3
    end
    devise_for :users, :controllers  => { :registrations => 'users' }, :path => "admin", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
end

Is it possible to have different paths for the same resource with Devise on different constraints?

The constraints being used are:

class ValidSubdomainAdmin
  def self.matches?(request)
        request.subdomain.present? && 
        request.env['PATH_INFO'].start_with?('/admin')
  end
end

class ValidSubdomainFrontend
  def self.matches?(request)
        request.subdomain.present? && 
        !request.env['PATH_INFO'].start_with?('/admin')
  end
end

Is this possible with Devise at all or is this a Bug?

Found the solution for this issue!

More on the context, basically needed to have something like /visitor on the front-end where users can login (to the members area) and edit their profile and other members/clients operations. But I would also want /admin where admins/editors/authors can login to the (backend) admin dashboard/area. Both areas use the same Devise model/resource (so an admin can also visit the members area). As a side note, currently I'm using CanCan permissions to prevent members from accessing the admin area.

I just had to replace the admin area constraint for:

constraints(ValidSubdomainAdmin) do
    devise_scope :user do
      #root :to => "devise/registrations#new"
      get "admin/" => "admin#index"
      post 'admin/' => 'devise/registrations#new', :as => :new_admin_registration 
      match 'admin/', :to => 'admin#index'    
      get "admin/edit" => "devise/registrations#edit"
      match 'admin/edit', :to => 'devise/registrations#edit'   
      get "admin/login" => "devise/sessions#new", :as => :new_admin_session
      match 'admin/login', :to => 'devise/sessions#new'
      get "admin/logout" => "devise/sessions#destroy", :as => 'destroy_admin_session'
      match 'admin/logout', :to => 'devise/sessions#destroy' 
      post   "admin/password" => "devise/passwords#create"
      get    "admin/password/new" => "devise/passwords#new", :as => 'new_admin_password'
      get    "admin/password/edit" => "devise/passwords#edit" 
      put    "admin/password" => "devise/passwords#update"   
    end
end

I also updated the views /admin/users (being used by devise on the on the admin area) to use the new admin devise paths.

Many thanks to José Valim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM