Skip to content

Commit fe2755c

Browse files
committed
Integrated Pundit to it
1 parent b73379b commit fe2755c

File tree

8 files changed

+82
-11
lines changed

8 files changed

+82
-11
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
55

66
ruby '3.0.3'
77
gem 'bcrypt', '~> 3.1.7'
8-
gem 'cancancan'
98
gem 'good_job'
109
gem 'interactor', '~> 3.0'
1110
gem 'interactor-rails', '~> 2.0'
@@ -15,6 +14,7 @@ gem 'pagy', '~> 5.10'
1514
gem 'pg', '~> 1.1'
1615
gem 'premailer-rails'
1716
gem 'puma', '~> 5.0'
17+
gem 'pundit'
1818
gem 'rack-cors'
1919
gem 'rails', '~> 7.0.2', '>= 7.0.2.3'
2020
gem 'strong_migrations'

Gemfile.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ GEM
7979
bullet (7.0.1)
8080
activesupport (>= 3.0.0)
8181
uniform_notifier (~> 1.11)
82-
cancancan (3.3.0)
8382
coderay (1.1.3)
8483
concurrent-ruby (1.1.9)
8584
crack (0.4.5)
@@ -185,6 +184,8 @@ GEM
185184
public_suffix (4.0.6)
186185
puma (5.6.2)
187186
nio4r (~> 2.0)
187+
pundit (2.2.0)
188+
activesupport (>= 3.0.0)
188189
raabro (1.4.0)
189190
racc (1.6.0)
190191
rack (2.2.3)
@@ -284,7 +285,6 @@ DEPENDENCIES
284285
bcrypt (~> 3.1.7)
285286
bootsnap
286287
bullet (~> 7.0.1)
287-
cancancan
288288
dotenv-rails
289289
factory_bot_rails (~> 6.2.0)
290290
faker (~> 2.20.0)
@@ -300,6 +300,7 @@ DEPENDENCIES
300300
premailer-rails
301301
pry-rails
302302
puma (~> 5.0)
303+
pundit
303304
rack-cors
304305
rails (~> 7.0.2, >= 7.0.2.3)
305306
reek (~> 6.1.0)

app/controllers/api/v1/users_controller.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Api
44
module V1
55
class UsersController < ApplicationController
6-
skip_before_action :authenticate_user
6+
skip_before_action :authenticate_user, only: [:create, :login, :verify]
77
before_action :set_user, only: [:update]
88

99
# serves as signup
@@ -17,7 +17,8 @@ def create
1717
end
1818

1919
def update
20-
result = User::Update.call(params: user_params.except(:email))
20+
authorize @user
21+
# result = User::Update.call(params: user_params.except(:email))
2122
end
2223

2324
def login

app/controllers/application_controller.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# frozen_string_literal: true
22

33
class ApplicationController < ActionController::API
4+
include Pundit::Authorization
5+
46
before_action :authenticate_user
57
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
8+
rescue_from Pundit::NotAuthorizedError, with: :unauthorized_access
69
# rescue_from UnauthorizedError, with: :unauthorized_access
710

811
def authenticate_user
@@ -22,13 +25,17 @@ def authenticate_user
2225
end
2326
end
2427

28+
def current_user
29+
@current_user
30+
end
31+
2532
private
2633

2734
def record_not_found
28-
render json: { errors: 'Record not found' }, status: :not_found
35+
return render json: { errors: I18n.t('generic.record_not_found') }, status: :not_found
2936
end
3037

3138
def unauthorized_access
32-
render json: { message: 'You are not authorized to access this resource!' }, status: :unprocessable_entity
39+
return render json: { message: I18n.t('authorization.error') }, status: :forbidden
3340
end
3441
end

app/policies/application_policy.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
class ApplicationPolicy
4+
attr_reader :user, :record
5+
6+
def initialize(user, record)
7+
@user = user
8+
@record = record
9+
end
10+
11+
def index?
12+
false
13+
end
14+
15+
def show?
16+
false
17+
end
18+
19+
def create?
20+
false
21+
end
22+
23+
def new?
24+
create?
25+
end
26+
27+
def update?
28+
false
29+
end
30+
31+
def edit?
32+
update?
33+
end
34+
35+
def destroy?
36+
false
37+
end
38+
39+
class Scope
40+
def initialize(user, scope)
41+
@user = user
42+
@scope = scope
43+
end
44+
45+
def resolve
46+
raise NotImplementedError, "You must define #resolve in #{self.class}"
47+
end
48+
49+
private
50+
51+
attr_reader :user, :scope
52+
end
53+
end

app/policies/user_policy.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class UserPolicy < ApplicationPolicy
2+
def update?
3+
user.id == record.id
4+
end
5+
end

config/locales/en.yml

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
en:
22
user:
33
errors:
4-
verify: User not found with the given token!
5-
login: Email or password is incorrect!
6-
not_found: User not found with the given ID!
4+
verify: User not found with the given token.
5+
login: Email or password is incorrect.
6+
not_found: User not found with the given ID.
7+
generic:
8+
record_not_found: Record not found.
9+
authorization:
10+
error: Unauthorized access.

config/routes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace :api do
88
namespace :v1 do
99
resources :users, only: %i[create show update] do
10-
post :login
10+
post :login, on: :collection
1111
end
1212
end
1313
end

0 commit comments

Comments
 (0)