-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Pundit gem and ApplicationPolicy * Reorder create? and update? in ApplicationPolicy * Add policy checks for notes * Add policy checks for registration * Add policy checks for sessions
- Loading branch information
Showing
10 changed files
with
134 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationController < ActionController::Base | ||
include Pundit | ||
|
||
delegate :current_user, to: :helpers, allow_nil: true | ||
|
||
rescue_from Pundit::NotAuthorizedError, with: :redirect_user_back | ||
|
||
private | ||
|
||
def redirect_user_back | ||
redirect_back(fallback_location: root_path, alert: 'You are not authorized to perform this action.') | ||
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
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationPolicy | ||
attr_reader :user, :record | ||
|
||
class Scope | ||
attr_reader :user, :scope | ||
|
||
def initialize(user, scope) | ||
@user = user | ||
@scope = scope | ||
end | ||
|
||
def resolve | ||
scope.all | ||
end | ||
end | ||
|
||
def initialize(user, record) | ||
@user = user | ||
@record = record | ||
end | ||
|
||
def index? | ||
false | ||
end | ||
|
||
def show? | ||
false | ||
end | ||
|
||
def new? | ||
create? | ||
end | ||
|
||
def create? | ||
false | ||
end | ||
|
||
def edit? | ||
update? | ||
end | ||
|
||
def update? | ||
false | ||
end | ||
|
||
def destroy? | ||
false | ||
end | ||
|
||
private | ||
|
||
def logged_in? | ||
user.present? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class NotePolicy < ApplicationPolicy | ||
class Scope < Scope | ||
def resolve | ||
scope.not_completed.where(user: user.presence) | ||
end | ||
end | ||
|
||
def show? | ||
record.user == user.presence | ||
end | ||
|
||
def update? | ||
return false if record.completed? | ||
|
||
show? | ||
end | ||
|
||
def complete? | ||
update? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class RegisterPolicy < ApplicationPolicy | ||
def create? | ||
!logged_in? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class SessionPolicy < ApplicationPolicy | ||
def create? | ||
!logged_in? | ||
end | ||
|
||
def destroy? | ||
logged_in? | ||
end | ||
end |