Skip to content

Commit af25b48

Browse files
authored
Merge pull request #451 from wearepal/admin-panel
basic admin panel
2 parents a7c6ae9 + 2dca527 commit af25b48

File tree

8 files changed

+113
-1
lines changed

8 files changed

+113
-1
lines changed

app/assets/stylesheets/admin.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the admin controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: https://sass-lang.com/

app/controllers/admin_controller.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class AdminController < ApplicationController
2+
3+
def index
4+
if current_user.admin?
5+
@users = User.all
6+
@teams = Team.all
7+
authorize!
8+
else
9+
redirect_to root_path
10+
end
11+
end
12+
end

app/controllers/teams_controller.rb

+14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ def update
4040
redirect_to root_url, alert: 'Team not found'
4141
end
4242

43+
def toggle_permission
44+
admin = current_user.admin?
45+
if admin
46+
authorize!
47+
else
48+
redirect_to root_path
49+
end
50+
team = Team.find(params[:id])
51+
permission = Permission.find_or_create_by(name: params[:permission_key])
52+
team_permission = TeamPermission.find_or_create_by(team: team, permission: permission)
53+
team_permission.update(enabled: !team_permission.enabled)
54+
redirect_to admin_index_path
55+
end
56+
4357
def select_team
4458
authorize!
4559
@teams = current_user.teams

app/helpers/admin_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module AdminHelper
2+
end

app/models/team.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,24 @@ class Team < ApplicationRecord
1919

2020
validates :name, presence: true
2121

22+
def permission(name)
23+
@p = Permission.find_by(name: name)
24+
tp = TeamPermission.find_by(team: self, permission: @p)
25+
tp ? tp.enabled : false
26+
end
27+
28+
def update_permission(name)
29+
@p = Permission.find_or_create_by(name: name)
30+
tp = TeamPermission.find_or_create_by(team: self, permission: @p)
31+
tp.update(enabled: tp.enabled ? false : true)
32+
end
33+
2234
private
2335

2436
def assign_permissions
2537
Permission.all.each do |permission|
2638
TeamPermission.create(team: self, permission: permission, enabled: false)
2739
end
2840
end
29-
41+
3042
end

app/views/admin/index.html.erb

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<div class="card">
2+
<table class="table table-striped">
3+
<thead>
4+
<tr>
5+
<th>Name</th>
6+
<th>Email</th>
7+
<th>Admin</th>
8+
</tr>
9+
</thead>
10+
<tbody>
11+
<% User.order(:name).each do |user| %>
12+
<tr>
13+
<td><%= user.name %></td>
14+
<td><%= user.email %></td>
15+
<td><%= user.admin? ? "✅" : "❌" %></td>
16+
</tr>
17+
<% end %>
18+
</tbody>
19+
</table>
20+
<table class="table table-striped">
21+
<thead>
22+
<tr>
23+
<th>Name</th>
24+
<th>Kew RGB 25cm</th>
25+
<th>Kew Samples</th>
26+
<th>DEFRA Hedgerows</th>
27+
<th>NATMAP</th>
28+
</tr>
29+
</thead>
30+
<tbody>
31+
<% @teams.each do |team| %>
32+
<tr>
33+
<td><%= team.name %></td>
34+
<td>
35+
<%= button_to team.permission('kew_rgb25cm') ? "✅" : "❌",
36+
toggle_permission_team_path(team, permission_key: 'kew_rgb25cm'),
37+
method: :patch,
38+
class: "btn btn-sm btn-light" %>
39+
</td>
40+
<td>
41+
<%= button_to team.permission('kew_samples') ? "✅" : "❌",
42+
toggle_permission_team_path(team, permission_key: 'kew_samples'),
43+
method: :patch,
44+
class: "btn btn-sm btn-light" %>
45+
</td>
46+
<td>
47+
<%= button_to team.permission('defra_hedgerows') ? "✅" : "❌",
48+
toggle_permission_team_path(team, permission_key: 'defra_hedgerows'),
49+
method: :patch,
50+
class: "btn btn-sm btn-light" %>
51+
</td>
52+
<td>
53+
<%= button_to team.permission('natmap_soil') ? "✅" : "❌",
54+
toggle_permission_team_path(team, permission_key: 'natmap_soil'),
55+
method: :patch,
56+
class: "btn btn-sm btn-light" %>
57+
</td>
58+
</tr>
59+
<% end %>
60+
</tbody>
61+
</table>
62+
</div>

app/views/layouts/application.html.erb

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
<div class="dropdown-divider"></div>
4545
<%= link_to "Create team...", new_team_path, class: "dropdown-item" %>
4646
<div class="dropdown-divider"></div>
47+
<% if current_user.admin? %>
48+
<%= link_to "🔒 Admin panel", admin_index_path, class: "dropdown-item text-warning" %>
49+
<div class="dropdown-divider"></div>
50+
<% end %>
4751
<a class="dropdown-item" href="https://forms.gle/1zJnyaCbFExwPzJW8">Leave feedback</a>
4852
<%= link_to "Sign Out", session_path, method: :delete, class: "dropdown-item" %>
4953
</div>

config/routes.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Rails.application.routes.draw do
2+
get 'admin/index'
23
resources :teams, shallow: true do
4+
patch :toggle_permission, on: :member
35
resources :map_tile_layers, only: [:index]
46
resources :memberships
57
resources :overlays, only: [:index]
@@ -55,6 +57,7 @@
5557

5658
# New route for the new app
5759
root to: "teams#select_team"
60+
5861

5962
# legacy content from the old app content
6063
get "legacy", to: "maps#show"

0 commit comments

Comments
 (0)