Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ideas for compatibility with rails_admin? #95

Closed
mplewis opened this issue Feb 11, 2021 · 5 comments
Closed

Ideas for compatibility with rails_admin? #95

mplewis opened this issue Feb 11, 2021 · 5 comments

Comments

@mplewis
Copy link

mplewis commented Feb 11, 2021

Hi @mikker, thanks so much for building Passwordless! It does exactly what I need.

I am using this with rails_admin via the manual custom auth approach.

My user class looks like this:

#  id         :bigint           not null, primary key
#  email      :string
#  superuser  :boolean          default(FALSE)
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
class User < ApplicationRecord
  passwordless_with :email
  # ...more domain stuff here...
end

I've sort of hacked something together that enables access if a user is signed in and has superuser: true. It looks like this:

RailsAdmin.config do |config|
  config.authorize_with do |controller|
    class RailsAdmin::MainController
      include Passwordless::ControllerHelpers
    end
    user = controller.authenticate_by_session(User)
    redirect_to main_app.root_path unless user&.superuser
  end
  # ...more config here...
end

However, this doesn't seem ideal. I don't like hacking the main admin controller open every request, but I can't seem to get at the authenticate_by_session method any other way.

Do you have any suggestions for what I could try? Happy to PR something to add support for Passwordless into that repo if I can get it working in an ergonomic way.

@mikker
Copy link
Owner

mikker commented Feb 15, 2021

Hi @mplewis! Thank you for using passwordless!

First off, I see nothing explicitly wrong with your approach, so if it works it works 😊

If you don't want to patch the existing controller, you could do something like

RailsAdmin.config do |config|
  config.authorize_with do |controller|
    class PasswordlessAdminHelper
      extend Passwordless::ControllerHelpers
    end
    user = PasswordlessAdminHelper.authenticate_by_session(User)
    redirect_to main_app.root_path unless user&.superuser
  end
end

I think that could work. You also create your very own, "real" controller and set that as RA's parent controller:

RailsAdmin.config do |config|
  config.parent_controller = '::AdminParentController'

  config.authenticate_with do
    require_admin!
  end
end
# app/controllers/admin_parent_controller.rb
class AdminParentController < ActionController::Base
  private

  def require_admin!
    current_user.superuser || redirect_to root_path
  end
end

@mplewis
Copy link
Author

mplewis commented Feb 15, 2021

Thanks for the suggestions! I took a look and found that the original one – editing RailsAdmin::MainController – seems to work with the least caveats.

Do you want me to add something in the Passwordless docs that includes this example as a way to integrate with Pundit?

@mikker
Copy link
Owner

mikker commented Feb 16, 2021

Great!

Yes, a note would be great. I'm considering whether the project Wiki would be a better place to put it than the Readme? What do you think?

@mplewis
Copy link
Author

mplewis commented Feb 16, 2021

I can't seem to access the wiki for this project. I generally feel like the readme is more accessible because you can Cmd-F for everything you need in one spot. I'm happy to put this info wherever you like – let me know!

@mikker
Copy link
Owner

mikker commented Feb 17, 2021

Let's just do the Readme 👍 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants