From 37c28536835a74fdfc6e1639d534501c826aca1e Mon Sep 17 00:00:00 2001 From: Leonid Medovyy Date: Mon, 1 May 2023 16:38:49 -0700 Subject: [PATCH] added access control to the homepage --- app/controllers/concerns/restricted_access.rb | 15 +++++++++++++++ app/controllers/page_controller.rb | 4 ++-- app/controllers/unauthorized_controller.rb | 5 +++++ app/views/unauthorized/index.html.slim | 5 +++++ config/routes.rb | 1 + tailwind.config.js | 1 + 6 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 app/controllers/concerns/restricted_access.rb create mode 100644 app/controllers/unauthorized_controller.rb create mode 100644 app/views/unauthorized/index.html.slim diff --git a/app/controllers/concerns/restricted_access.rb b/app/controllers/concerns/restricted_access.rb new file mode 100644 index 0000000..f52ad51 --- /dev/null +++ b/app/controllers/concerns/restricted_access.rb @@ -0,0 +1,15 @@ +module RestrictedAccess + extend ActiveSupport::Concern + + included do + before_action :authenticate_user! + before_action :check_access + end + + private + + def check_access + redirect_to unauthorized_index_path unless current_user.has_access? + end + +end \ No newline at end of file diff --git a/app/controllers/page_controller.rb b/app/controllers/page_controller.rb index b672171..0e81631 100644 --- a/app/controllers/page_controller.rb +++ b/app/controllers/page_controller.rb @@ -1,6 +1,6 @@ class PageController < ApplicationController - before_action :authenticate_user! - + include RestrictedAccess def index end + end diff --git a/app/controllers/unauthorized_controller.rb b/app/controllers/unauthorized_controller.rb new file mode 100644 index 0000000..a882589 --- /dev/null +++ b/app/controllers/unauthorized_controller.rb @@ -0,0 +1,5 @@ +class UnauthorizedController < ApplicationController + def index + redirect_to root_path if current_user.has_access? + end +end diff --git a/app/views/unauthorized/index.html.slim b/app/views/unauthorized/index.html.slim new file mode 100644 index 0000000..3f53684 --- /dev/null +++ b/app/views/unauthorized/index.html.slim @@ -0,0 +1,5 @@ +=r ux.container + =r ux.row + =r ux.column size: 16, class: 'pt-20' + =r ux.h1 text: 'Unauthorized', class: 'center aligned' + =r ux.text text: 'You are not authorized to access this page.', class: 'text-center' diff --git a/config/routes.rb b/config/routes.rb index 4fc9c4a..6928faf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ apipie root "page#index" + resources :unauthorized, only: %i[index] namespace :api, defaults: { format: 'json' } do namespace :v1 do diff --git a/tailwind.config.js b/tailwind.config.js index deb8a8f..823b334 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -7,6 +7,7 @@ module.exports = { './app/frontend/**/*.js', './app/views/**/*', './app/views/devise/**/*', + './app/views/unauthorized/**/*', './app/components/**/*', ], theme: {