Skip to content

Commit

Permalink
Fix Tenants access (#5314)
Browse files Browse the repository at this point in the history
* Fix Tenants access

* Add :with_super_admin traits in user factory
  • Loading branch information
scouillard authored Jul 7, 2023
1 parent ed1d7e6 commit eb07901
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/admin/tenants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module V1
module Admin
class TenantsController < ApiController
before_action do
# TODO: - ahmad: Add role check
ensure_super_admin
end

# GET /api/v1/admin/tenants
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/concerns/authorizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def ensure_authorized(permission_names, user_id: nil, friendly_id: nil, record_i
).call
end

def ensure_super_admin
return render_error status: :forbidden unless current_user.super_admin?
end

private

# Ensures that requests to the API are explicit enough.
Expand Down
7 changes: 7 additions & 0 deletions app/javascript/components/admin/tenants/Tenants.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import React, { useState } from 'react';
import Card from 'react-bootstrap/Card';
import { Navigate } from 'react-router-dom';
import {
Button,
Col, Container, Row, Stack, Tab,
Expand All @@ -29,10 +30,16 @@ import NoSearchResults from '../../shared_components/search/NoSearchResults';
import TenantsTable from './TenantsTable';
import Modal from '../../shared_components/modals/Modal';
import CreateTenantForm from './forms/CreateTenantForm';
import { useAuth } from '../../../contexts/auth/AuthProvider';

export default function Tenants() {
const { t } = useTranslation();
const [page, setPage] = useState();
const currentUser = useAuth();

if (!currentUser.isSuperAdmin) {
return <Navigate to="/" />;
}

const [searchInput, setSearchInput] = useState();
const { data: tenants, isLoading } = useTenants({ search: searchInput, page });
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/tenants_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
require 'rails_helper'

RSpec.describe Api::V1::Admin::TenantsController, type: :controller do
let(:user) { create(:user) }
let(:user) { create(:user, :with_super_admin) }
let(:valid_tenant_params) do
{
name: 'new_provider',
Expand Down
8 changes: 8 additions & 0 deletions spec/factories/user_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
language { %w[en fr es ar].sample }
verified { true }

trait :with_super_admin do
after(:create) do |user|
user.provider = 'bn'
user.role = create(:role, :with_super_admin)
user.save
end
end

trait :with_manage_users_permission do
after(:create) do |user|
create(:role_permission, role: user.role, permission: create(:permission, name: 'ManageUsers'), value: 'true')
Expand Down

0 comments on commit eb07901

Please sign in to comment.