-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from zunairkhan811/authorization
Authorization For Blog App
- Loading branch information
Showing
18 changed files
with
193 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,4 @@ | ||
class ApplicationController < ActionController::Base | ||
before_action :authenticate_user! | ||
# protect_from_forgery with: :exception | ||
# helper_method :current_user | ||
|
||
# def current_user | ||
# @current_user ||= User.first | ||
# end | ||
include CanCan::ControllerAdditions | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,59 @@ | ||
class CommentsController < ApplicationController | ||
before_action :set_user | ||
before_action :set_post | ||
def index | ||
@comments = @post.comments | ||
end | ||
|
||
def new | ||
@comment = Comment.new | ||
end | ||
|
||
def create | ||
@comment = Comment.new(user_id: current_user.id, post_id: params[:post_id], text: params[:text]) | ||
@comment = @post.comments.build(comment_params) | ||
@comment.user = current_user | ||
if @comment.save | ||
redirect_to user_post_path(:user_id, :post_id), notice: 'Your comment has been successfully created.' | ||
redirect_to user_post_path(@post.author, @post), notice: '🎊 Bravo, you have created your comment!' | ||
else | ||
redirect_to user_post_path(:user_id, :post_id), alert: 'Error creating comment.' | ||
flash[:alert] = 'Apologies try again!' | ||
redirect_to user_post_path(@post.author, @post) | ||
end | ||
end | ||
|
||
# def create | ||
# # puts "Params: #{params.inspect}" | ||
# @comment = Comment.new(comment_params) | ||
# @comment.user_id = params[:user_id] | ||
# @comment.post_id = params[:post_id] | ||
# puts "Comment before save: #{@comment.inspect}" | ||
# if @comment.save | ||
# redirect_to user_posts_path(current_user), notice: 'Comment created successfully.' | ||
# else | ||
# render :new, notice: 'Error occured in creating a comment' | ||
# end | ||
# end | ||
|
||
def show; end | ||
|
||
def destroy | ||
@comment = Comment.find(params[:id]) | ||
authorize! :destroy, @comment | ||
return unless @comment.destroy | ||
|
||
redirect_to user_posts_path(current_user), notice: 'Comment has been deleted successfully' | ||
end | ||
|
||
private | ||
|
||
def set_user | ||
@user = User.find(params[:user_id]) | ||
end | ||
|
||
def set_post | ||
@post = Post.find(params[:post_id]) | ||
end | ||
|
||
def comment_params | ||
params.require(:comment).permit(:text, :user_id, :post_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class Ability | ||
include CanCan::Ability | ||
|
||
def initialize(user) | ||
# @user = current_user || User.new | ||
|
||
if user.admin? | ||
can :manage, :all | ||
else | ||
can :read, :all | ||
can %i[create destroy], Post, author_id: user.id | ||
can %i[create destroy], Comment, user_id: user.id | ||
can :create, Like | ||
end | ||
# Define abilities for the user here. For example: | ||
# | ||
# return unless user.present? | ||
# can :read, :all | ||
# return unless user.admin? | ||
# can :manage, :all | ||
# | ||
# The first argument to `can` is the action you are giving the user | ||
# permission to do. | ||
# If you pass :manage it will apply to every action. Other common actions | ||
# here are :read, :create, :update and :destroy. | ||
# | ||
# The second argument is the resource the user can perform the action on. | ||
# If you pass :all it will apply to every resource. Otherwise pass a Ruby | ||
# class of the resource. | ||
# | ||
# The third argument is an optional hash of conditions to further filter the | ||
# objects. | ||
# For example, here the user can only update published articles. | ||
# | ||
# can :update, Article, published: true | ||
# | ||
# See the wiki for details: | ||
# https://github.com/CanCanCommunity/cancancan/blob/develop/docs/define_check_abilities.md | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div class="col-lg-12 mb-5"> | ||
<%= render 'shared/show_post', post: @post, user: @user, like: @like %> | ||
<%= render 'shared/full_comment', post: @post %> | ||
<%= render 'shared/comment_form', comment: @comment %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,5 @@ | ||
<div class="col-lg-12 mb-5"> | ||
<div class="card"> | ||
<div class="card-body"> | ||
<div class="row"> | ||
<h3 class="font-weight-bold col-sm-7 mb-0"><%= @post.title %> By <%= @user.name %></h3> | ||
<span class="card-text my-1 text-end col-sm-5">Comments: <%= @post.comments_counter %>, Likes: <%= @post.likes_counter %></span> | ||
</div> | ||
<p class="card-text mt-5"><%= @post.text %></p> | ||
<%= render 'shared/like_form', like: @like, post: @post %> | ||
</div> | ||
</div> | ||
<%= render 'shared/show_post'%> | ||
<!-- <% Rails.logger.debug "Current User Role: #{@post.id}" %> --> | ||
<%= render 'shared/full_comment', post: @post %> | ||
<%= render 'shared/comment_form', comment: @comment %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<div class="card"> | ||
<div class="card-body"> | ||
<div class="row"> | ||
<h3 class="font-weight-bold col-lg-10 mb-0"><%= @post.title %> By <%= @post.author.name %></h3> | ||
<% if can? :destroy, @post %> | ||
<span class="text-end col-lg-2"> | ||
<%= link_to 'Delete Post', user_post_path(current_user.id, @post.id), method: :delete, class: "btn btn-danger", confirm: "Are you sure you want to delete this post?" %> | ||
</span> | ||
<% end %> | ||
</div> | ||
<div class= "row"> | ||
<span class="card-text my-1 text-end col-lg-12">Comments: <%= @post.comments_counter %>, Likes: <%= @post.likes_counter %></span> | ||
</div> | ||
|
||
<p class="card-text mt-5"><%= @post.text %></p> | ||
<%= render 'shared/like_form', like: @like, post: @post %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddRoleToUsers < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :users, :role, :integer | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.