-
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.
Merge pull request #3 from GSA-TTS/role-authorization
Role authorization
- Loading branch information
Showing
46 changed files
with
1,180 additions
and
95 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,14 +1 @@ | ||
@forward "uswds-global"; | ||
@forward "uswds-utilities"; | ||
@forward "uswds-typography"; | ||
@forward "usa-layout-grid"; | ||
@forward "usa-header"; | ||
@forward "usa-banner"; | ||
@forward "usa-section"; | ||
@forward "usa-language-selector"; | ||
// add additional packages here as you use them | ||
@forward "usa-table"; | ||
|
||
// or replace these all with | ||
// @forward "uswds"; | ||
// to import the entirety of uswds | ||
@forward "uswds"; |
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,2 +1,16 @@ | ||
class ApplicationController < ActionController::Base | ||
include Pundit::Authorization | ||
after_action :verify_pundit_authorization, unless: :devise_controller? | ||
|
||
def verify_pundit_authorization | ||
if action_name == "index" | ||
verify_policy_scoped | ||
else | ||
verify_authorized | ||
end | ||
end | ||
|
||
def devise_controller? | ||
self.class.ancestors.include? DeviseController | ||
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,77 @@ | ||
class DocumentsController < ApplicationController | ||
before_action :set_document, only: %i[show edit update destroy] | ||
|
||
# GET /documents or /documents.json | ||
def index | ||
@documents = policy_scope(Document) | ||
end | ||
|
||
# GET /documents/1 or /documents/1.json | ||
def show | ||
end | ||
|
||
# GET /documents/new | ||
def new | ||
@document = Document.new | ||
authorize @document | ||
end | ||
|
||
# GET /documents/1/edit | ||
def edit | ||
end | ||
|
||
# POST /documents or /documents.json | ||
def create | ||
@document = Document.new(document_params) | ||
authorize @document | ||
|
||
respond_to do |format| | ||
if @document.save | ||
logger.info "[PRIVILEGED] Document(#{@document.id}) created by #{current_user.id}" | ||
format.html { redirect_to document_url(@document), notice: "Document was successfully created." } | ||
format.json { render :show, status: :created, location: @document } | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @document.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /documents/1 or /documents/1.json | ||
def update | ||
respond_to do |format| | ||
if @document.update(document_params) | ||
logger.info "[PRIVILEGED] Document(#{@document.id}) updated by #{current_user.id}" | ||
format.html { redirect_to document_url(@document), notice: "Document was successfully updated." } | ||
format.json { render :show, status: :ok, location: @document } | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @document.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /documents/1 or /documents/1.json | ||
def destroy | ||
@document.destroy! | ||
logger.info "[PRIVILEGED] Document(#{@document.id}) destroyed by #{current_user.id}" | ||
|
||
respond_to do |format| | ||
format.html { redirect_to documents_url, notice: "Document was successfully destroyed." } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
|
||
# Use callbacks to share common setup or constraints between actions. | ||
def set_document | ||
@document = Document.find(params[:id]) | ||
authorize @document | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def document_params | ||
params.fetch(:document, {}).permit :url, :title, :description | ||
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
class PagesController < ApplicationController | ||
def home | ||
@documents = Document.all | ||
skip_authorization # skip the authorization check because we're using policy_scope here. | ||
@documents = policy_scope(Document) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class User < ApplicationRecord | ||
# Include default devise modules. Others available are: | ||
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable | ||
devise :database_authenticatable, :registerable, | ||
:rememberable, :validatable, :lockable | ||
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,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 NoMethodError, "You must define #resolve in #{self.class}" | ||
end | ||
|
||
private | ||
|
||
attr_reader :user, :scope | ||
end | ||
end |
Oops, something went wrong.