Skip to content

Scaffold Staff Interface #2

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions frontend/src/styles/_admin.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.sa-admin-navbar {
position: fixed;
top: 50px;
left: 0px;
width: 100%;
z-index: 1;
background: $sa-gray3;

a {
color: $sa-dark-blue;
font-weight: 500;
text-transform: uppercase;
padding: 5px;
}

.first-row, .second-row {
padding: 0px 20px;
}

.first-row a {
font-size: 1.1em;
}

.second-row a {
font-size: 0.85em;
}

.second-row {
background: $sa-gray4;
}

a.pt-button::after {
display: none !important;
}
}
6 changes: 6 additions & 0 deletions frontend/src/styles/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ $pt-font-size: 14px !default;
$sa-dark-blue: #101e35 !default;
$sa-darker-blue: darken($sa-dark-blue, 5%);

$sa-gray5: #F2F4F7;
$sa-gray4: darken($sa-gray5, 5%);
$sa-gray3: darken($sa-gray5, 10%);
$sa-gray2: darken($sa-gray5, 15%);
$sa-gray1: darken($sa-gray5, 20%);

// Layout Variables
$sa-header-bg-color: black;

Expand Down
2 changes: 2 additions & 0 deletions frontend/src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

@import 'layout';
@import 'session';

@import 'admin';
7 changes: 7 additions & 0 deletions lib/cadet_web/controllers/admin_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule CadetWeb.AdminController do
use CadetWeb, :controller

def index(conn, _) do
render(conn, "index.html")
end
end
25 changes: 25 additions & 0 deletions lib/cadet_web/plug/ensure_roles.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule CadetWeb.Plug.EnsureRoles do
@moduledoc """
Ensures that :current_user's role is inside a list provided as option.
If the user is not inside the list, HTTP 403 response will be sent.
"""

import Plug.Conn

def init(opts), do: opts

def call(conn, %{roles: roles}) do
if conn.assigns[:current_user].role in roles do
conn
else
body =
roles
|> Enum.map(&to_string/1)
|> Enum.join("/")
conn
|> put_resp_content_type("text/html")
|> send_resp(:forbidden, "Not #{body}")
|> halt()
end
end
end
11 changes: 11 additions & 0 deletions lib/cadet_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ defmodule CadetWeb.Router do
plug(Guardian.Plug.EnsureAuthenticated)
end

pipeline :ensure_admin_staff do
plug(CadetWeb.Plug.EnsureRoles, %{roles: [:admin, :staff]})
end

# Public Pages
scope "/", CadetWeb do
pipe_through([:browser, :auth])
Expand All @@ -33,6 +37,13 @@ defmodule CadetWeb.Router do
get("/", PageController, :index)
end

# Admin Pages
scope "/admin", CadetWeb do
pipe_through([:browser, :auth, :ensure_auth, :ensure_admin_staff])

get("/", AdminController, :index)
end

# Other scopes may use custom stacks.
# scope "/api", CadetWeb do
# pipe_through :api
Expand Down
24 changes: 24 additions & 0 deletions lib/cadet_web/templates/admin/index.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="sa-admin-navbar">
<div class="first-row">
<div class="container">
<div class="pt-button-group pt-minimal">
<%= link "Announcements", to: "#", class: "pt-button" %>
<%= link "Assessments", to: "#", class: "pt-button" %>
<%= link "My Students", to: "#", class: "pt-button" %>
<%= link "Gradings", to: "#", class: "pt-button" %>
<%= link "Path Submissions", to: "#", class: "pt-button" %>
</div>
</div>
</div>
<div class="second-row">
<div class="container">
<div class="pt-button-group pt-minimal">
<%= link "Achievements", to: "#", class: "pt-button" %>
<%= link "Materials", to: "#", class: "pt-button" %>
<%= link "Discussion Groups", to: "#", class: "pt-button" %>
<%= link "Students", to: "#", class: "pt-button" %>
<%= link "Libraries", to: "#", class: "pt-button" %>
</div>
</div>
</div>
</div>
6 changes: 5 additions & 1 deletion lib/cadet_web/templates/layout/app.navbar.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<button class="pt-button pt-minimal pt-icon-globe">
Game
</button>
<button class="pt-button pt-minimal pt-icon-dashboard pt-active">
<button class="pt-button pt-minimal pt-icon-dashboard">
Dashboard
</button>
<% end %>
Expand All @@ -20,6 +20,10 @@
Playground
</button>

<%= if is_roles?(@conn, [:admin, :staff]) do %>
<%= button "Admin", to: admin_path(@conn, :index), method: "get", class: "pt-button pt-minimal pt-icon-settings" %>
<% end %>

<%= if logged_in?(@conn) do %>
<%= button "Logout", to: session_path(@conn, :logout), method: "get", class: "pt-button pt-minimal pt-intent-danger" %>
<% else %>
Expand Down
3 changes: 3 additions & 0 deletions lib/cadet_web/views/admin_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule CadetWeb.AdminView do
use CadetWeb, :view
end
8 changes: 8 additions & 0 deletions lib/cadet_web/views/view_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ defmodule CadetWeb.ViewHelpers do
def logged_in?(conn) do
conn.assigns[:current_user] != nil
end

def is_roles?(conn, roles) do
if logged_in?(conn) do
conn.assigns[:current_user].role in roles
else
false
end
end
end
26 changes: 26 additions & 0 deletions test/cadet_web/controllers/admin_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule CadetWeb.AdminControllerTest do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please implement the test. I just fixed Coveralls

use CadetWeb.ConnCase

describe "Unauthenticated User" do
test "GET /admin", %{conn: conn} do
conn = get(conn, "/admin")
assert html_response(conn, 401) =~ "unauthenticated"
end
end

@tag authenticate: :student
describe "Authenticated Student" do
test "GET /admin", %{conn: conn} do
conn = get(conn, "/admin")
assert html_response(conn, 403) =~ "Not admin"
end
end

@tag authenticate: :admin
describe "Authenticated Admin" do
test "GET /admin", %{conn: conn} do
conn = get(conn, "/admin")
assert html_response(conn, 200) =~ "Admin"
end
end
end
32 changes: 32 additions & 0 deletions test/cadet_web/plug/ensure_roles_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule CadetWeb.Plug.EnsureRolesTest do
use CadetWeb.ConnCase

alias CadetWeb.Plug.AssignCurrentUser
alias CadetWeb.Plug.EnsureRoles

test "init" do
EnsureRoles.init(%{})
# nothing to test
end

@tag authenticate: :student
test "logged in as student", %{conn: conn} do
conn = AssignCurrentUser.call(conn, %{})
conn = EnsureRoles.call(conn, %{roles: [:admin, :staff]})
assert html_response(conn, 403) =~ "Not admin/staff"
end

@tag authenticate: :staff
test "logged in as staff", %{conn: conn} do
conn = AssignCurrentUser.call(conn, %{})
conn = EnsureRoles.call(conn, %{roles: [:admin, :staff]})
refute conn.status # conn.status is not set yet
end

@tag authenticate: :admin
test "logged in as admin", %{conn: conn} do
conn = AssignCurrentUser.call(conn, %{})
conn = EnsureRoles.call(conn, %{roles: [:admin, :staff]})
refute conn.status # conn.status is not set yet
end
end