diff --git a/Gemfile b/Gemfile index 1cfde4d..b2342b0 100644 --- a/Gemfile +++ b/Gemfile @@ -53,6 +53,8 @@ gem "heroicon" # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] # gem "image_processing", "~> 1.2" +gem 'pundit', "~> 2.1.1" + group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem gem "debug", platforms: %i[ mri mingw x64_mingw ] diff --git a/Gemfile.lock b/Gemfile.lock index fd3a3a7..4330bed 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -142,6 +142,8 @@ GEM public_suffix (4.0.6) puma (5.5.2) nio4r (~> 2.0) + pundit (2.1.1) + activesupport (>= 3.0.0) racc (1.6.0) rack (2.2.3) rack-test (1.1.0) @@ -251,6 +253,7 @@ DEPENDENCIES importmap-rails jbuilder puma (~> 5.0) + pundit (~> 2.1.1) rails (~> 7.0.0) redis (~> 4.0) selenium-webdriver diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 361611f..f6286a4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,4 @@ class ApplicationController < ActionController::Base include Authentication + include Pundit end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 663842e..ef192f8 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,7 +3,8 @@ class UsersController < ApplicationController # GET /users or /users.json def index - @users = User.all + @users = policy_scope(User) + # @users = User.all end # GET /users/1 or /users/1.json diff --git a/app/policies/application_policy.rb b/app/policies/application_policy.rb new file mode 100644 index 0000000..d989e9b --- /dev/null +++ b/app/policies/application_policy.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +class ApplicationPolicy + attr_reader :user, :record + + def initialize(user, record) + @user = user + @record = record + end + + def index? + false + end + + def show? + false + end + + def create? + false + end + + def new? + create? + end + + def update? + false + end + + def edit? + update? + end + + def destroy? + false + end + + class Scope + def initialize(user, scope) + @user = user + @scope = scope + end + + def resolve + scope.all + end + + private + + attr_reader :user, :scope + end +end diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb new file mode 100644 index 0000000..556dd1a --- /dev/null +++ b/app/policies/user_policy.rb @@ -0,0 +1,11 @@ +class UserPolicy < ApplicationPolicy + class Scope < Scope + def resolve + if user.admin? + scope.all + else + scope.where(id: user.id) + end + end + end +end \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index f67a425..85666b3 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -76,7 +76,8 @@
- <%= controller_name %> + Controller: <%= controller_name %> + User: <%= current_user.email %>