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

Am auth with pundit #26

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8db973c
added authorizations for photos and private vs public users
amandaag39 Mar 7, 2024
3e3c729
added authorizations for photo commenting
amandaag39 Mar 7, 2024
568f06b
installed pundit gem and set up policies file with photo policy
amandaag39 Mar 8, 2024
2d28167
added first pundit method to photos controller
amandaag39 Mar 8, 2024
5926908
added UserPolicy class and show method, also updated user show logic …
amandaag39 Mar 11, 2024
0b53420
added ApplicationPolicy parent class and inherited UserPolicy and Pho…
amandaag39 Mar 11, 2024
b0e364c
added FollowRequest pundit authorization logic
amandaag39 Mar 11, 2024
5f1c9ef
added bones for likepolicy
amandaag39 Mar 11, 2024
f913439
edited follow_requests index.html.erb so user can only see requests p…
amandaag39 Mar 12, 2024
7364672
added logic to restrict feed and discover to current_user only
amandaag39 Mar 12, 2024
9d74371
added policy to hide self follow request button
amandaag39 Mar 12, 2024
cf3be7b
added after_action
amandaag39 Mar 12, 2024
f81c830
added liked? method to userpolicy
amandaag39 Mar 12, 2024
49fb52f
added logic to hide private user photos
amandaag39 Mar 12, 2024
8b6038a
removed hide_photo logic
amandaag39 Mar 12, 2024
ad38422
got further, need to omit private users current_user is following
amandaag39 Mar 12, 2024
83e16e3
refined logic for hiding private user content
amandaag39 Mar 13, 2024
9329bf3
got rid of unecessary methods, fixed comment_policy#show? logic, fixe…
amandaag39 Mar 13, 2024
32be59b
adjusted delete and update policy and view policy for comments
amandaag39 Mar 13, 2024
46ce408
cleaned up logic, only allow deletion of other comments on photos
amandaag39 Mar 13, 2024
a7738e2
modified follow_request_policy#destroy? and #update? to allow both us…
amandaag39 Mar 13, 2024
5bfe159
edited logic for see_follow_request_button?
amandaag39 Mar 13, 2024
37dbd9d
cleaned up redundnacy in view_private_profile_content?
amandaag39 Mar 13, 2024
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ gem "htmlbeautifier"
gem "http"
gem "sqlite3", "~> 1.4"
gem "table_print"
gem "pundit"

group :development do
gem "annotate"
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ GEM
net-smtp (0.3.3)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.5-arm64-darwin)
racc (~> 1.4)
nokogiri (1.15.5-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.15.5-x86_64-linux)
Expand All @@ -235,6 +237,8 @@ GEM
public_suffix (5.0.4)
puma (5.6.7)
nio4r (~> 2.0)
pundit (2.3.1)
activesupport (>= 3.0.0)
racc (1.6.2)
rack (2.2.8)
rack-protection (3.0.6)
Expand Down Expand Up @@ -345,6 +349,7 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.6.8-arm64-darwin)
sqlite3 (1.6.8-x86_64-darwin)
sqlite3 (1.6.8-x86_64-linux)
stimulus-rails (1.2.2)
Expand Down Expand Up @@ -402,6 +407,7 @@ GEM
zeitwerk (2.6.12)

PLATFORMS
arm64-darwin-22
x86_64-darwin-22
x86_64-linux

Expand All @@ -427,6 +433,7 @@ DEPENDENCIES
pg (~> 1.1)
pry-rails
puma (~> 5.0)
pundit
rails (~> 7.0.4, >= 7.0.4.3)
rails-erd
rails_db
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
class ApplicationController < ActionController::Base
include Pundit

before_action :authenticate_user!

before_action :configure_permitted_parameters, if: :devise_controller?

rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

after_action :verify_authorized, unless: :devise_controller?
after_action :verify_policy_scoped, only: :index, unless: :devise_controller?

private

def user_not_authorized
flash[:alert] = "You are not authorized to perform this action"

redirect_back fallback_location: root_url
end

protected

def configure_permitted_parameters
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def index

# GET /comments/1 or /comments/1.json
def show
authorize @comment
end

# GET /comments/new
Expand All @@ -17,12 +18,14 @@ def new

# GET /comments/1/edit
def edit
authorize @comment
end

# POST /comments or /comments.json
def create
@comment = Comment.new(comment_params)
@comment.author = current_user
authorize @comment

respond_to do |format|
if @comment.save
Expand All @@ -37,6 +40,7 @@ def create

# PATCH/PUT /comments/1 or /comments/1.json
def update
authorize @comment
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to root_url, notice: "Comment was successfully updated." }
Expand All @@ -50,6 +54,7 @@ def update

# DELETE /comments/1 or /comments/1.json
def destroy
authorize @comment
@comment.destroy
respond_to do |format|
format.html { redirect_back fallback_location: root_url, notice: "Comment was successfully destroyed." }
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/follow_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ class FollowRequestsController < ApplicationController

# GET /follow_requests or /follow_requests.json
def index
@follow_requests = FollowRequest.all
authorize (@follow_request || FollowRequest)
@follow_requests = policy_scope(FollowRequest)
end

# GET /follow_requests/1 or /follow_requests/1.json
def show
authorize @follow_request
end

# GET /follow_requests/new
Expand All @@ -17,12 +19,14 @@ def new

# GET /follow_requests/1/edit
def edit
authorize @follow_request
end

# POST /follow_requests or /follow_requests.json
def create
@follow_request = FollowRequest.new(follow_request_params)
@follow_request.sender = current_user
authorize @follow_request

respond_to do |format|
if @follow_request.save
Expand All @@ -37,6 +41,7 @@ def create

# PATCH/PUT /follow_requests/1 or /follow_requests/1.json
def update
authorize @follow_request
respond_to do |format|
if @follow_request.update(follow_request_params)
format.html { redirect_back fallback_location: root_url, notice: "Follow request was successfully updated." }
Expand All @@ -50,6 +55,7 @@ def update

# DELETE /follow_requests/1 or /follow_requests/1.json
def destroy
authorize @follow_request
@follow_request.destroy
respond_to do |format|
format.html { redirect_back fallback_location: root_url, notice: "Follow request was successfully destroyed." }
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class PhotosController < ApplicationController
before_action :set_photo, only: %i[ show edit update destroy ]
before_action :authorize_photo, except: %i[index new]

def authorize_photo
authorize @photo
end
# GET /photos or /photos.json
def index
@photos = Photo.all
Expand All @@ -13,6 +17,7 @@ def show
# GET /photos/new
def new
@photo = Photo.new
authorize @photo
end

# GET /photos/1/edit
Expand All @@ -23,6 +28,7 @@ def edit
def create
@photo = Photo.new(photo_params)
@photo.owner = current_user
authorize @photo

respond_to do |format|
if @photo.save
Expand Down Expand Up @@ -50,7 +56,10 @@ def update

# DELETE /photos/1 or /photos/1.json
def destroy
@photo = Photo.find(params[:id])
authorize @photo
@photo.destroy

respond_to do |format|
format.html { redirect_back fallback_location: root_url, notice: "Photo was successfully destroyed." }
format.json { head :no_content }
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show liked feed followers following discover ]
before_action { authorize @user }

def see_follow_request_button
end

def view_private_profile_content
end

private

Expand All @@ -10,4 +17,4 @@ def set_user
@user = current_user
end
end
end
end
1 change: 1 addition & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class Comment < ApplicationRecord
belongs_to :author, class_name: "User", counter_cache: true
belongs_to :photo, counter_cache: true
has_one :owner, through: :photo

validates :body, presence: true
end
53 changes: 53 additions & 0 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
@@ -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
raise NotImplementedError, "You must define #resolve in #{self.class}"
end

private

attr_reader :user, :scope
end
end
27 changes: 27 additions & 0 deletions app/policies/comment_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# app/policies/comment_policy.rb

class CommentPolicy < ApplicationPolicy
attr_reader :user, :comment

def initialize(user, comment)
@user = user
@comment = comment
end

def show?
user == comment.author || PhotoPolicy.new(user, comment.photo).show?
end

def create?
true
end

def update?
user == comment.author
end

def destroy?
user == comment.author || user == comment.photo.owner
end

end
37 changes: 37 additions & 0 deletions app/policies/follow_request_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class FollowRequestPolicy < ApplicationPolicy
attr_reader :user, :follow_request

def initialize(user, follow_request)
@user = user
@follow_request = follow_request
end

def create?
true
end

def destroy?
user == follow_request.sender || user == follow_request.recipient
end

def update?
user == follow_request.recipient || user == follow_request.sender
end

# Scope class
class Scope
attr_reader :user, :scope

def initialize(user, scope)
@user = user
@scope = scope
end

# This method defines which follow requests the user is allowed to view
def resolve
scope.where(sender_id: user.id).or(scope.where(recipient_id: user.id))
end

end

end
11 changes: 11 additions & 0 deletions app/policies/like_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# app/policies/like_policy.rb

class LikePolicy < ApplicationPolicy
attr_reader :user, :like

def initialize(user, like)
@user = user
@like = like
end

end
29 changes: 29 additions & 0 deletions app/policies/photo_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# app/policies/photo_policy.rb

class PhotoPolicy < ApplicationPolicy
attr_reader :user, :photo

def initialize(user, photo)
@user = user
@photo = photo
end

def show?
user == photo.owner ||
!photo.owner.private? ||
photo.owner.followers.include?(user)
end

def update?
user == photo.owner

end

def create?
!user.nil?
end

def destroy?
user == photo.owner
end
end
Loading