Add admin flag to users

This commit is contained in:
Eugene Burmakin 2024-07-16 22:26:16 +02:00
parent 646e5e729d
commit b1f7b98c11
8 changed files with 33 additions and 10 deletions

View file

@ -13,8 +13,8 @@ class ApplicationController < ActionController::Base
@unread_notifications ||= Notification.where(user: current_user).unread
end
def authenticate_first_user!
return if current_user == User.first
def authenticate_admin!
return if current_user.admin?
redirect_to root_path, notice: 'You are not authorized to perform this action.', status: :unauthorized
end

View file

@ -2,7 +2,7 @@
class Settings::BackgroundJobsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_first_user!
before_action :authenticate_admin!
def index
@queues = Sidekiq::Queue.all

View file

@ -2,7 +2,7 @@
class Settings::UsersController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_first_user!
before_action :authenticate_admin!
def create
@user = User.new(
@ -12,7 +12,8 @@ class Settings::UsersController < ApplicationController
)
if @user.save
redirect_to settings_url, notice: "User was successfully created, email is #{@user.email}, password is \"password\"."
redirect_to settings_url,
notice: "User was successfully created, email is #{@user.email}, password is \"password\"."
else
redirect_to settings_url, notice: 'User could not be created.', status: :unprocessable_entity
end

View file

@ -5,7 +5,9 @@ require 'sidekiq/web'
Rails.application.routes.draw do
mount Rswag::Api::Engine => '/api-docs'
mount Rswag::Ui::Engine => '/api-docs'
mount Sidekiq::Web => '/sidekiq'
authenticate :user, ->(u) { u.admin? } do
mount Sidekiq::Web => '/sidekiq'
end
resources :settings, only: :index
namespace :settings do

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
class MakeFirstUserAdmin < ActiveRecord::Migration[7.1]
def up
user = User.first
user.update!(admin: true)
end
def down
user = User.first
user.update!(admin: false)
end
end

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddAdminToUsers < ActiveRecord::Migration[7.1]
def change
add_column :users, :admin, :boolean, default: false
end
end

3
db/schema.rb generated
View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_07_12_141303) do
ActiveRecord::Schema[7.1].define(version: 2024_07_13_103051) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -150,6 +150,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_12_141303) do
t.string "api_key", default: "", null: false
t.string "theme", default: "dark", null: false
t.jsonb "settings", default: {"fog_of_war_meters"=>"200", "meters_between_routes"=>"1000", "minutes_between_routes"=>"60"}
t.boolean "admin", default: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

View file

@ -91,11 +91,10 @@ RSpec.describe User, type: :model do
describe '#total_reverse_geocoded' do
subject { user.total_reverse_geocoded }
let(:import) { create(:import, user:) }
let!(:reverse_geocoded_point) do
create(:point, country: 'Country', city: 'City', geodata: { some: 'data' }, import:)
create(:point, country: 'Country', city: 'City', geodata: { some: 'data' }, user:)
end
let!(:not_reverse_geocoded_point) { create(:point, country: 'Country', city: 'City', import:) }
let!(:not_reverse_geocoded_point) { create(:point, country: 'Country', city: 'City', user:) }
it 'returns number of reverse geocoded points' do
expect(subject).to eq(1)