Skip to content

Commit 6243aab

Browse files
committed
Add host dashboard authorization
1 parent d285fda commit 6243aab

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

app/controllers/better_together/host_dashboard_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module BetterTogether
44
class HostDashboardController < ApplicationController # rubocop:todo Style/Documentation
55
def index # rubocop:todo Metrics/MethodLength
6+
authorize :host_dashboard, :index?
67
root_classes = [
78
Community, NavigationArea, Page, Platform, Person, Role, ResourcePermission, User,
89
Conversation, Message, Category
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
# app/policies/better_together/host_dashboard_policy.rb
4+
5+
module BetterTogether
6+
class HostDashboardPolicy < ApplicationPolicy # rubocop:todo Style/Documentation
7+
def index?
8+
user.present? && user.permitted_to?('manage_platform')
9+
end
10+
end
11+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe BetterTogether::HostDashboardController, type: :controller do
6+
include Devise::Test::ControllerHelpers
7+
include BetterTogether::DeviseSessionHelpers
8+
9+
routes { BetterTogether::Engine.routes }
10+
11+
before do
12+
configure_host_platform
13+
@request.env['devise.mapping'] = Devise.mappings[:user]
14+
end
15+
16+
describe 'GET #index' do
17+
context 'when user can manage platform' do
18+
let(:user) { BetterTogether::User.find_by(email: '[email protected]') }
19+
20+
before { sign_in user }
21+
22+
it 'returns http success' do
23+
get :index, params: { locale: I18n.default_locale }
24+
expect(response).to be_successful
25+
end
26+
end
27+
28+
context 'when user cannot manage platform' do
29+
let(:user) { create(:user, :confirmed) }
30+
31+
before { sign_in user }
32+
33+
it 'raises Pundit::NotAuthorizedError' do
34+
expect do
35+
get :index, params: { locale: I18n.default_locale }
36+
end.to raise_error(Pundit::NotAuthorizedError)
37+
end
38+
end
39+
end
40+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe BetterTogether::HostDashboardPolicy, type: :policy do
6+
subject(:policy) { described_class.new(user, nil) }
7+
8+
context 'when user can manage platform' do
9+
let(:user) { create(:user, :confirmed, :platform_manager) }
10+
11+
it 'permits access' do
12+
expect(policy.index?).to be(true)
13+
end
14+
end
15+
16+
context 'when user cannot manage platform' do
17+
let(:user) { create(:user, :confirmed) }
18+
19+
it 'denies access' do
20+
expect(policy.index?).to be(false)
21+
end
22+
end
23+
24+
context 'when no user is present' do
25+
let(:user) { nil }
26+
27+
it 'denies access' do
28+
expect(policy.index?).to be(false)
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)