Skip to content

Commit f1d4874

Browse files
author
Edimo Silva
committed
Adding pundit
1 parent 8ee2ce0 commit f1d4874

File tree

8 files changed

+36
-10
lines changed

8 files changed

+36
-10
lines changed

docker-compose.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ services:
3131
RUBYOPT: "-W:no-deprecated -W:no-experimental"
3232
BUNDLE_PATH: "/home/edimossilva/.gems"
3333
HISTFILE: /home/edimossilva/.bashhistory
34-
RAILS_MASTER_KEY: "a667717bf0a47475b0582547379c816d"
34+
RAILS_MASTER_KEY: "a667717bf0a47475b0582547379c816d"
3535
# "${RAILS_MASTER_KEY}"
3636
tty: true
3737
command: bash
3838
ports:
39-
- "3000:3000"
40-
39+
- "4000:3000"
40+
4141
vue:
4242
build:
4343
context: "."
@@ -53,6 +53,6 @@ services:
5353
depends_on:
5454
- rails
5555
# docker-compose build
56-
# docker-compose up
57-
# docker exec -it ror-basic-blog_rails_1 bash
56+
# docker-compose up -d
57+
# docker exec -it rails bash
5858
# railss

volume/blog-backend/Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,5 @@ gem 'bcrypt', '~> 3.1.7'
5555
gem 'rubocop'
5656
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
5757
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
58+
59+
gem "pundit"

volume/blog-backend/Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ GEM
129129
pry (~> 0.9)
130130
slop (~> 3.0)
131131
puma (3.12.4)
132+
pundit (2.1.0)
133+
activesupport (>= 3.0.0)
132134
rack (2.2.2)
133135
rack-cors (1.1.1)
134136
rack (>= 2.0.0)
@@ -236,6 +238,7 @@ DEPENDENCIES
236238
pry-nav
237239
pry-remote
238240
puma (~> 3.11)
241+
pundit
239242
rack-cors
240243
rails (~> 6.0.0)
241244
rspec-rails (~> 4.0.0.beta)

volume/blog-backend/app/controllers/application_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
class ApplicationController < ActionController::API
22
include Auth::JsonWebTokenHelper
3+
include Pundit
4+
35
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found
46
rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity
7+
rescue_from Pundit::NotAuthorizedError, with: :render_unauthorized
58

69
before_action :authorize_request
710

volume/blog-backend/app/controllers/blogs_controller.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ def index
3333
def show
3434
blog = Blog.find_by!(id: search_params[:id])
3535

36-
if BlogAccessLevel.can_show?(@current_user, blog)
37-
render_ok(BlogSerializer.new(blog))
38-
else
39-
render_unauthorized
40-
end
36+
authorize blog
37+
38+
render_ok(BlogSerializer.new(blog))
4139
end
4240

4341
private

volume/blog-backend/app/helpers/auth/json_web_token_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,9 @@ def user_by_token(token)
4747
decoded = decode_token(token)
4848
User.find(decoded[:user_id])
4949
end
50+
51+
def current_user
52+
@current_user
53+
end
5054
end
5155
end

volume/blog-backend/app/models/blog.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ class Blog < ApplicationRecord
77
delegate :registred?, to: :user, prefix: true
88

99
scope :only_public, -> { where('is_private = false') }
10+
11+
def public?
12+
!is_private
13+
end
1014
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class BlogPolicy
2+
attr_reader :user, :blog
3+
4+
def initialize(user, blog)
5+
@user = user
6+
@blog = blog
7+
end
8+
9+
def show?
10+
user&.has_access_level? || blog.public?
11+
end
12+
end

0 commit comments

Comments
 (0)