Browse Source

Add masquerading

Chris Oliver 7 years ago
parent
commit
10cf0a4337

+ 7 - 0
app/assets/stylesheets/application.scss

@@ -2,6 +2,8 @@
 // $light-orange: #ff8c00;
 // $navbar-default-color: $light-orange;
 
+@import "font-awesome-sprockets";
+@import "font-awesome";
 @import "bootstrap";
 @import "sticky-footer";
 @import "announcements";
@@ -13,3 +15,8 @@
     margin-right: 0;
   }
 }
+
+// Masquerade alert shouldn't have a bottom margin
+body > .alert {
+  margin-bottom: 0;
+}

+ 27 - 0
app/controllers/admin/application_controller.rb

@@ -0,0 +1,27 @@
+# All Administrate controllers inherit from this `Admin::ApplicationController`,
+# making it the ideal place to put authentication logic or other
+# before_actions.
+#
+# If you want to add pagination or other controller-level concerns,
+# you're free to overwrite the RESTful controller actions.
+module Admin
+  class ApplicationController < Administrate::ApplicationController
+    before_action :authenticate_admin
+    before_action :default_params
+
+    def authenticate_admin
+      redirect_to '/', alert: 'Not authorized.' unless user_signed_in? && current_user.admin?
+    end
+
+    def default_params
+      params[:order] ||= "created_at"
+      params[:direction] ||= "desc"
+    end
+
+    # Override this value to specify the number of elements to display at a time
+    # on index pages. Defaults to 20.
+    # def records_per_page
+    #   params[:per_page] || 20
+    # end
+  end
+end

+ 1 - 1
app/helpers/announcements_helper.rb

@@ -4,7 +4,7 @@ module AnnouncementsHelper
     return if last_announcement.nil?
 
     # Highlight announcements for anyone not logged in, cuz tempting
-    if user.nil? || user.last_read_announcements_at.nil? || user.last_read_announcements_at < last_announcement.published_at
+    if user.nil? || user.announcements_last_read_at.nil? || user.announcements_last_read_at < last_announcement.published_at
       "unread-announcements"
     end
   end

+ 10 - 0
app/models/user.rb

@@ -0,0 +1,10 @@
+class User < ApplicationRecord
+  # Include default devise modules. Others available are:
+  # :confirmable, :lockable, :timeoutable and :omniauthable
+  devise :masqueradable, :database_authenticatable, :registerable,
+         :recoverable, :rememberable, :trackable, :validatable
+
+  def name
+    "#{first_name} #{last_name}"
+  end
+end

+ 51 - 0
app/views/admin/users/show.html.erb

@@ -0,0 +1,51 @@
+<%#
+# Show
+
+This view is the template for the show page.
+It renders the attributes of a resource,
+as well as a link to its edit page.
+
+## Local variables:
+
+- `page`:
+  An instance of [Administrate::Page::Show][1].
+  Contains methods for accessing the resource to be displayed on the page,
+  as well as helpers for describing how each attribute of the resource
+  should be displayed.
+
+[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Show
+%>
+
+<% content_for(:title) { "#{t("administrate.actions.show")} #{page.page_title}" } %>
+
+<header class="main-content__header" role="banner">
+  <h1 class="main-content__page-title">
+    <%= content_for(:title) %>
+  </h1>
+
+  <div>
+    <%= link_to "Login As User", masquerade_path(page.resource), class: "button" %>
+
+    <%= link_to(
+      "#{t("administrate.actions.edit")} #{page.page_title}",
+      [:edit, namespace, page.resource],
+      class: "button",
+    ) if valid_action? :edit %>
+  </div>
+</header>
+
+<section class="main-content__body">
+  <dl>
+    <% page.attributes.each do |attribute| %>
+      <dt class="attribute-label" id="<%= attribute.name %>">
+      <%= t(
+        "helpers.label.#{resource_name}.#{attribute.name}",
+        default: attribute.name.titleize,
+      ) %>
+      </dt>
+
+      <dd class="attribute-data attribute-data--<%=attribute.html_class%>"
+          ><%= render_field attribute %></dd>
+    <% end %>
+  </dl>
+</section>

+ 21 - 4
app/views/shared/_navbar.html.erb

@@ -1,3 +1,10 @@
+<% if user_masquerade? %>
+  <div class="alert alert-warning text-center">
+    You're logged in as <b><%= current_user.name %> (<%= current_user.email %>)</b>
+    <%= link_to back_masquerade_path(current_user) do %><%= icon("times") %> Logout <% end %>
+  </div>
+<% end %>
+
 <nav class="navbar navbar-toggleable-md navbar-light bg-faded">
   <div class="container">
     <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
@@ -12,11 +19,21 @@
       <ul class="navbar-nav">
         <li class="nav-item"><%= link_to "What's New", announcements_path, class: "nav-link #{unread_announcements(current_user)}" %></li>
         <% if user_signed_in? %>
-          <li class="nav-item">
-            <a class="nav-link" href="javascript:void(0)" data-uv-lightbox="classic_widget" data-uv-mode="full" data-uv-primary-color="#3aa2e3" data-uv-link-color="#56b68b" data-uv-default-mode="support" data-uv-forum-id="259979">Support</a>
+
+          <li class="nav-item dropdown">
+            <%= link_to root_path, id: "navbar-dropdown", class: "nav-link dropdown-toggle", data: { toggle: "dropdown" }, aria: { haspopup: true, expanded: false } do %>
+              <%= image_tag gravatar_image_url(current_user.email, size: 40), height: 20, width: 20, class: "rounded" %>
+            <% end %>
+            <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbar-dropdown">
+              <% if current_user.admin? %>
+                <%= link_to "Admin Area", admin_root_path, class: "dropdown-item" %>
+              <% end %>
+              <%= link_to "Settings", edit_user_registration_path, class: "dropdown-item" %>
+              <div class="dropdown-divider"></div>
+              <%= link_to "Logout", destroy_user_session_path, method: :delete, class: "dropdown-item" %>
+            </div>
           </li>
-          <li class="nav-item"><%= link_to "Account", edit_user_registration_path, class: "nav-link" %></li>
-          <li class="nav-item"><%= link_to "Logout", destroy_user_session_path, method: :delete, class: "nav-link" %></li>
+
         <% else %>
           <li class="nav-item"><%= link_to "Sign Up", new_user_registration_path, class: "nav-link" %></li>
           <li class="nav-item"><%= link_to "Login", new_user_session_path, class: "nav-link" %></li>

+ 4 - 0
config/initializers/gravatar.rb

@@ -0,0 +1,4 @@
+GravatarImageTag.configure do |config|
+  config.default_image = "mm"
+  config.secure        = true
+end

+ 7 - 1
template.rb

@@ -6,6 +6,9 @@ def add_gems
   gem 'administrate', '~> 0.8.1'
   gem 'devise', '~> 4.3.0'
   gem 'devise-bootstrapped', github: 'excid3/devise-bootstrapped', branch: 'bootstrap4'
+  gem 'devise_masquerade', '~> 0.6.0'
+  gem 'font-awesome-sass', '~> 4.7'
+  gem 'gravatar_image_tag', github: 'mdeering/gravatar_image_tag'
   gem 'jquery-rails', '~> 4.3.1'
   gem 'bootstrap', '~> 4.0.0.alpha6'
   gem 'rails-assets-tether', '>= 1.3.3', source: 'https://rails-assets.org'
@@ -38,6 +41,9 @@ def add_users
     migration = Dir.glob("db/migrate/*").max_by{ |f| File.mtime(f) }
     gsub_file migration, /:admin/, ":admin, default: false"
   end
+
+  # Add Devise masqueradable to users
+  #inject_into_file("app/models/user.rb", "masqueradable, :", :after => "devise :")
 end
 
 def add_bootstrap
@@ -54,6 +60,7 @@ end
 
 def copy_templates
   directory "app", force: true
+  directory "config", force: true
 
   route "get '/terms', to: 'home#terms'"
   route "get '/privacy', to: 'home#privacy'"
@@ -112,7 +119,6 @@ after_bundle do
 
   copy_templates
 
-
   git :init
   git add: "."
   git commit: %Q{ -m 'Initial commit' }