This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| <div style="margin: 0 10px;"> | |
| <%= link_to( | |
| 'Log in as user', | |
| authenticate_as_user_admin_user_path(page.resource), | |
| method: :post, | |
| class: "button", | |
| )%> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Admin | |
| class ApplicationController < Administrate::ApplicationController | |
| before_action :authenticate_admin | |
| helper_method :current_user | |
| def authenticate_admin | |
| if current_user.nil? || current_user.admin? == false | |
| flash[:alert] = 'You do not have authorization to access this page.' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ApplicationController < ActionController::Base | |
| protect_from_forgery with: :exception | |
| helper_method :current_user | |
| helper_method :current_admin_user | |
| def current_user | |
| @current_user ||= User.find_by(id: session[:user_id]) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Admin | |
| class UsersController < Admin::ApplicationController | |
| def login_as_user | |
| @user = User.find(params[:id]) | |
| session[:user_id] = @user.id | |
| redirect_to root_path | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Auth0Controller < ApplicationController | |
| ... | |
| def failure | |
| end | |
| def logout | |
| if admin_logged_as_another_user? | |
| accessed_user = session[:user_id] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Auth0Controller < ApplicationController | |
| ... | |
| def callback | |
| user = find_or_create_user | |
| session[:user_id] = user.id | |
| if user.admin? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CreateUsers < ActiveRecord::Migration[5.2] | |
| def change | |
| create_table :users do |t| | |
| t.string :email, null: false, index: true, unique: true | |
| t.boolean :admin, index: true, default: false | |
| t.timestamps | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.application.routes.draw do | |
| namespace :admin do | |
| resources :users do | |
| post :login_as_user, on: :member | |
| end | |
| root to: 'users#index' | |
| end | |
| get 'auth/auth0', to: 'auth0#authentication', as: :authentication |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.application.config.middleware.use OmniAuth::Builder do | |
| provider( | |
| :auth0, | |
| ENV['AUTH0_CLIENT_ID'], | |
| ENV['AUTH0_CLIENT_SECRET'], | |
| ENV['AUTH0_DOMAIN'], | |
| callback_path: '/auth/auth0/callback', | |
| authorize_params: { | |
| scope: 'openid email' | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| Knock::Authenticable.module_eval do | |
| def define_current_entity_getter(entity_class, getter_name) | |
| unless self.respond_to?(getter_name) | |
| memoization_var_name = "@_#{getter_name}" | |
| self.class.send(:define_method, getter_name) do | |
| unless instance_variable_defined?(memoization_var_name) | |
| current = Knock::AuthToken.new(token: token).entity_for(entity_class) |