Skip to content
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

add admin flag on users and admin panel #28

Merged
merged 1 commit into from
May 18, 2024
Merged
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
16 changes: 16 additions & 0 deletions rails/app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class AdminController < ApplicationController
before_action :verify_admin

def index
# Add any data you want to display on the admin page
@users = User.all
end

private

def verify_admin
unless current_user&.admin?
redirect_to root_path, alert: "You are not authorized to view this page."
end
end
end
2 changes: 2 additions & 0 deletions rails/app/helpers/admin_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module AdminHelper
end
20 changes: 20 additions & 0 deletions rails/app/views/admin/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h2> Admin </h2>
<h3> Users </h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Admin</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.admin %></td>
</tr>
<% end %>
</tbody>
</table>
5 changes: 4 additions & 1 deletion rails/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
<div class="text-sm lg:flex-grow">
<a href="/collection_types" class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4">Categories</a>
<a href="/collections" class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4">Collections</a>
<a href="/collection_items" class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white">Items</a>
<a href="/collection_items" class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 mr-4 hover:text-white">Items</a>
<% if current_user&.admin? %>
<a href="/admin" class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4">Admin</a>
<% end %>
</div>
</div>
<div>
Expand Down
1 change: 1 addition & 0 deletions rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
post '/collections/:id/bulk_create_items', to: 'collections#process_bulk_create_items', as: 'process_bulk_create_items'
get '/collection_items/:id/collect', to: 'collection_items#collect'
get '/collection_items/:id/uncollect', to: 'collection_items#uncollect'
get '/admin', to: 'admin#index'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
Expand Down
5 changes: 5 additions & 0 deletions rails/db/migrate/20240518205757_add_admin_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAdminToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :admin, :boolean, default: false
end
end
3 changes: 2 additions & 1 deletion rails/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions rails/lib/tasks/user.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace :user do
desc "Set a user as admin"
task :set_admin, [:email] => :environment do |t, args|
# rake user:set_admin\['[email protected]'\]
user = User.find_by(email: args.email)
if user
user.update(admin: true)
puts "User #{args.email} is now an admin."
else
puts "No user found with the email #{args.email}."
end
end
end
7 changes: 7 additions & 0 deletions rails/test/controllers/admin_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class AdminControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
Loading