Created
October 23, 2013 10:40
-
-
Save dalpo/7116348 to your computer and use it in GitHub Desktop.
Sample Masquerades Controller
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 | |
| helper_method :masquerading?, :current_unit_measure | |
| protected | |
| def masquerading? | |
| session[:masked_id].present? | |
| 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
| .page-header | |
| %h1 | |
| %i.icon-eye-open | |
| Who would you like to spy? | |
| %blockquote Be careful... | |
| %h3 Select a user: | |
| %table.table.table-striped | |
| %thead | |
| %th # | |
| %th First name | |
| %th Last name | |
| %th Email | |
| %th Actions | |
| %tbody | |
| - @users.each do |user| | |
| - if current_user.id != user.id | |
| %tr | |
| %td= user.id | |
| %td= user.first_name | |
| %td= user.last_name | |
| %td= mail_to user.email | |
| %td | |
| = link_to new_masquerade_path(masquerade_user_id: user.id), class: 'btn btn-danger' do | |
| %i.icon-eye-open | |
| Masquerade! | |
| %thead | |
| %th # | |
| %th First name | |
| %th Last name | |
| %th Email | |
| %th Actions |
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 MasqueradesController < ApplicationController | |
| before_filter :authenticate_user! | |
| before_filter :authorize_super_admin! | |
| def index | |
| @users = User.all | |
| respond_to do |format| | |
| format.html | |
| end | |
| end | |
| def new | |
| user = User.find(params[:masquerade_user_id]) | |
| session[:masked_id] = current_user.id | |
| sign_in(user) | |
| redirect_to root_path, notice: "Now masquerading as #{user.full_name}" | |
| end | |
| def escape | |
| if (user = User.find(session[:masked_id])) && session[:masked_id].present? | |
| sign_in(user) | |
| end | |
| session[:masked_id] = nil | |
| redirect_to root_path, notice: "Stopped masquerading" | |
| end | |
| protected | |
| def authorize_super_admin! | |
| redirect_to root_path, notice: "Unatorized" unless current_user.super_admin? || masquerading? | |
| 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
| SampleApp::Application.routes.draw do | |
| ### | |
| # Masquerade!!! | |
| resources :masquerades, only: [:index, :new] do | |
| collection do | |
| get :escape | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment