diff --git a/backend/app/controllers/admin/application_controller.rb b/backend/app/controllers/admin/application_controller.rb index 534540e..bb345ac 100644 --- a/backend/app/controllers/admin/application_controller.rb +++ b/backend/app/controllers/admin/application_controller.rb @@ -8,17 +8,28 @@ # you're free to overwrite the RESTful controller actions. module Admin class ApplicationController < Administrate::ApplicationController + before_action :authenticate_user! before_action :authenticate_admin before_action :set_paper_trail_whodunnit + rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized + + include Pundit::Authorization + after_action :verify_authorized + def authenticate_admin - # TODO: Add authentication logic here. + authorize :admin_dashboard, :full_access? + + # authorized = AdminDashboardPolicy.new(current_user, nil).send(:admin_dashboard?) + # raise Pundit::NotAuthorizedError unless authorized end - # Override this value to specify the number of elements to display at a time - # on index pages. Defaults to 20. - # def records_per_page - # params[:per_page] || 20 - # end + private + + def user_not_authorized(exception) + policy_name = exception.policy.class.to_s.underscore + flash.now[:error] = t "#{policy_name}.#{exception.query}", scope: "pundit", default: :default + render file: Rails.public_path.join("401.html").to_s, layout: false, status: :unauthorized + end end end diff --git a/backend/app/policies/admin_dashboard_policy.rb b/backend/app/policies/admin_dashboard_policy.rb new file mode 100644 index 0000000..203b21a --- /dev/null +++ b/backend/app/policies/admin_dashboard_policy.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class AdminDashboardPolicy < ApplicationPolicy + def initialize(user, _record) + @user = user + end + + def full_access? + user.admin? + end +end diff --git a/backend/public/401.html b/backend/public/401.html new file mode 100644 index 0000000..b6a29c3 --- /dev/null +++ b/backend/public/401.html @@ -0,0 +1,66 @@ + + + + The page you were looking for doesn't exist (401) + + + + + + +
+
+

You have no permission.

+
+

Check if you have the correct roles.

+
+ + diff --git a/backend/public/403.html b/backend/public/403.html new file mode 100644 index 0000000..e2b35f9 --- /dev/null +++ b/backend/public/403.html @@ -0,0 +1,66 @@ + + + + Forbidden + + + + + +
+
+

The page you were looking for doesn't exist.

+

You may have mistyped the address or the page may have moved.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/backend/public/404.html b/backend/public/404.html index 2be3af2..9c574ce 100644 --- a/backend/public/404.html +++ b/backend/public/404.html @@ -58,10 +58,9 @@
-

The page you were looking for doesn't exist.

-

You may have mistyped the address or the page may have moved.

+

Unauthorized

+

You need to sign in.

-

If you are the application owner check the logs for more information.