Skip to content

Commit

Permalink
Merge pull request #451 from wearepal/admin-panel
Browse files Browse the repository at this point in the history
basic admin panel
  • Loading branch information
paulthatjazz authored Nov 22, 2024
2 parents a7c6ae9 + 2dca527 commit af25b48
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/admin.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the admin controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
12 changes: 12 additions & 0 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AdminController < ApplicationController

def index
if current_user.admin?
@users = User.all
@teams = Team.all
authorize!
else
redirect_to root_path
end
end
end
14 changes: 14 additions & 0 deletions app/controllers/teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ def update
redirect_to root_url, alert: 'Team not found'
end

def toggle_permission
admin = current_user.admin?
if admin
authorize!
else
redirect_to root_path
end
team = Team.find(params[:id])
permission = Permission.find_or_create_by(name: params[:permission_key])
team_permission = TeamPermission.find_or_create_by(team: team, permission: permission)
team_permission.update(enabled: !team_permission.enabled)
redirect_to admin_index_path
end

def select_team
authorize!
@teams = current_user.teams
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/admin_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module AdminHelper
end
14 changes: 13 additions & 1 deletion app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@ class Team < ApplicationRecord

validates :name, presence: true

def permission(name)
@p = Permission.find_by(name: name)
tp = TeamPermission.find_by(team: self, permission: @p)
tp ? tp.enabled : false
end

def update_permission(name)
@p = Permission.find_or_create_by(name: name)
tp = TeamPermission.find_or_create_by(team: self, permission: @p)
tp.update(enabled: tp.enabled ? false : true)
end

private

def assign_permissions
Permission.all.each do |permission|
TeamPermission.create(team: self, permission: permission, enabled: false)
end
end

end
62 changes: 62 additions & 0 deletions app/views/admin/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<div class="card">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Admin</th>
</tr>
</thead>
<tbody>
<% User.order(:name).each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.admin? ? "✅" : "❌" %></td>
</tr>
<% end %>
</tbody>
</table>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Kew RGB 25cm</th>
<th>Kew Samples</th>
<th>DEFRA Hedgerows</th>
<th>NATMAP</th>
</tr>
</thead>
<tbody>
<% @teams.each do |team| %>
<tr>
<td><%= team.name %></td>
<td>
<%= button_to team.permission('kew_rgb25cm') ? "✅" : "❌",
toggle_permission_team_path(team, permission_key: 'kew_rgb25cm'),
method: :patch,
class: "btn btn-sm btn-light" %>
</td>
<td>
<%= button_to team.permission('kew_samples') ? "✅" : "❌",
toggle_permission_team_path(team, permission_key: 'kew_samples'),
method: :patch,
class: "btn btn-sm btn-light" %>
</td>
<td>
<%= button_to team.permission('defra_hedgerows') ? "✅" : "❌",
toggle_permission_team_path(team, permission_key: 'defra_hedgerows'),
method: :patch,
class: "btn btn-sm btn-light" %>
</td>
<td>
<%= button_to team.permission('natmap_soil') ? "✅" : "❌",
toggle_permission_team_path(team, permission_key: 'natmap_soil'),
method: :patch,
class: "btn btn-sm btn-light" %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
4 changes: 4 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<div class="dropdown-divider"></div>
<%= link_to "Create team...", new_team_path, class: "dropdown-item" %>
<div class="dropdown-divider"></div>
<% if current_user.admin? %>
<%= link_to "🔒 Admin panel", admin_index_path, class: "dropdown-item text-warning" %>
<div class="dropdown-divider"></div>
<% end %>
<a class="dropdown-item" href="https://forms.gle/1zJnyaCbFExwPzJW8">Leave feedback</a>
<%= link_to "Sign Out", session_path, method: :delete, class: "dropdown-item" %>
</div>
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Rails.application.routes.draw do
get 'admin/index'
resources :teams, shallow: true do
patch :toggle_permission, on: :member
resources :map_tile_layers, only: [:index]
resources :memberships
resources :overlays, only: [:index]
Expand Down Expand Up @@ -55,6 +57,7 @@

# New route for the new app
root to: "teams#select_team"


# legacy content from the old app content
get "legacy", to: "maps#show"
Expand Down

0 comments on commit af25b48

Please sign in to comment.