Skip to content

Commit

Permalink
moves userHasNoPermissions repeated code into a function in helpers, …
Browse files Browse the repository at this point in the history
…adds a few test cases, updated references to new function
  • Loading branch information
dhurley14 committed Apr 3, 2020
1 parent c6858d9 commit 6507f72
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { StepScheduleRule } from '../components/step_schedule_rule';
import { StepRuleActions } from '../components/step_rule_actions';
import { DetectionEngineHeaderPage } from '../../components/detection_engine_header_page';
import * as RuleI18n from '../translations';
import { redirectToDetections, getActionMessageParams } from '../helpers';
import { redirectToDetections, getActionMessageParams, userHasNoPermissions } from '../helpers';
import {
AboutStepRule,
DefineStepRule,
Expand Down Expand Up @@ -116,7 +116,6 @@ const CreateRulePageComponent: React.FC = () => {
getActionMessageParams((stepsData.current['define-rule'].data as DefineStepRule).ruleType),
[stepsData.current['define-rule'].data]
);
const userHasNoPermissions = canUserCRUD != null ? !canUserCRUD : false;

const setStepData = useCallback(
(step: RuleStep, data: unknown, isValid: boolean) => {
Expand Down Expand Up @@ -272,7 +271,7 @@ const CreateRulePageComponent: React.FC = () => {

if (redirectToDetections(isSignalIndexExists, isAuthenticated, hasEncryptionKey)) {
return <Redirect to={`/${DETECTION_ENGINE_PAGE_NAME}`} />;
} else if (userHasNoPermissions) {
} else if (userHasNoPermissions(canUserCRUD)) {
return <Redirect to={`/${DETECTION_ENGINE_PAGE_NAME}/rules`} />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import * as detectionI18n from '../../translations';
import { ReadOnlyCallOut } from '../components/read_only_callout';
import { RuleSwitch } from '../components/rule_switch';
import { StepPanel } from '../components/step_panel';
import { getStepsData, redirectToDetections } from '../helpers';
import { getStepsData, redirectToDetections, userHasNoPermissions } from '../helpers';
import * as ruleI18n from '../translations';
import * as i18n from './translations';
import { GlobalTime } from '../../../../containers/global_time';
Expand Down Expand Up @@ -114,7 +114,6 @@ const RuleDetailsPageComponent: FC<PropsFromRedux> = ({
scheduleRuleData: null,
};
const [lastSignals] = useSignalInfo({ ruleId });
const userHasNoPermissions = canUserCRUD != null ? !canUserCRUD : false;

const title = isLoading === true || rule === null ? <EuiLoadingSpinner size="m" /> : rule.name;
const subTitle = useMemo(
Expand Down Expand Up @@ -225,7 +224,7 @@ const RuleDetailsPageComponent: FC<PropsFromRedux> = ({
return (
<>
{hasIndexWrite != null && !hasIndexWrite && <NoWriteSignalsCallOut />}
{userHasNoPermissions && <ReadOnlyCallOut />}
{userHasNoPermissions(canUserCRUD) && <ReadOnlyCallOut />}
<WithSource sourceId="default" indexToAdd={indexToAdd}>
{({ indicesExist, indexPattern }) => {
return indicesExistOrDataTemporarilyUnavailable(indicesExist) ? (
Expand Down Expand Up @@ -262,7 +261,7 @@ const RuleDetailsPageComponent: FC<PropsFromRedux> = ({
<EuiFlexItem grow={false}>
<RuleSwitch
id={rule?.id ?? '-1'}
isDisabled={userHasNoPermissions}
isDisabled={userHasNoPermissions(canUserCRUD)}
enabled={rule?.enabled ?? false}
optionLabel={i18n.ACTIVATE_RULE}
onChange={handleOnChangeEnabledRule}
Expand All @@ -275,15 +274,15 @@ const RuleDetailsPageComponent: FC<PropsFromRedux> = ({
<EuiButton
href={getEditRuleUrl(ruleId ?? '')}
iconType="controlsHorizontal"
isDisabled={userHasNoPermissions ?? true}
isDisabled={userHasNoPermissions(canUserCRUD) ?? true}
>
{ruleI18n.EDIT_RULE_SETTINGS}
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<RuleActionsOverflow
rule={rule}
userHasNoPermissions={userHasNoPermissions}
userHasNoPermissions={userHasNoPermissions(canUserCRUD)}
/>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ import { StepDefineRule } from '../components/step_define_rule';
import { StepScheduleRule } from '../components/step_schedule_rule';
import { StepRuleActions } from '../components/step_rule_actions';
import { formatRule } from '../create/helpers';
import { getStepsData, redirectToDetections, getActionMessageParams } from '../helpers';
import {
getStepsData,
redirectToDetections,
getActionMessageParams,
userHasNoPermissions,
} from '../helpers';
import * as ruleI18n from '../translations';
import {
RuleStep,
Expand Down Expand Up @@ -73,8 +78,6 @@ const EditRulePageComponent: FC = () => {
const { detailName: ruleId } = useParams();
const [loading, rule] = useRule(ruleId);

const userHasNoPermissions = canUserCRUD != null ? !canUserCRUD : false;

const [initForm, setInitForm] = useState(false);
const [myAboutRuleForm, setMyAboutRuleForm] = useState<AboutStepRuleForm>({
data: null,
Expand Down Expand Up @@ -344,7 +347,7 @@ const EditRulePageComponent: FC = () => {

if (redirectToDetections(isSignalIndexExists, isAuthenticated, hasEncryptionKey)) {
return <Redirect to={`/${DETECTION_ENGINE_PAGE_NAME}`} />;
} else if (userHasNoPermissions) {
} else if (userHasNoPermissions(canUserCRUD)) {
return <Redirect to={`/${DETECTION_ENGINE_PAGE_NAME}/rules/id/${ruleId}`} />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getHumanizedDuration,
getModifiedAboutDetailsData,
determineDetailsValue,
userHasNoPermissions,
} from './helpers';
import { mockRuleWithEverything, mockRule } from './all/__mocks__/mock';
import { esFilters } from '../../../../../../../../src/plugins/data/public';
Expand Down Expand Up @@ -337,4 +338,27 @@ describe('rule helpers', () => {
expect(result).toEqual(aboutRuleDetailsData);
});
});

describe('userHasNoPermissions', () => {
test("returns false when user's CRUD operations are null", () => {
const result: boolean = userHasNoPermissions(null);
const userHasNoPermissionsExpectedResult = false;

expect(result).toEqual(userHasNoPermissionsExpectedResult);
});

test('returns false when user cannot CRUD', () => {
const result: boolean = userHasNoPermissions(false);
const userHasNoPermissionsExpectedResult = true;

expect(result).toEqual(userHasNoPermissionsExpectedResult);
});

test('returns false when user can CRUD', () => {
const result: boolean = userHasNoPermissions(true);
const userHasNoPermissionsExpectedResult = false;

expect(result).toEqual(userHasNoPermissionsExpectedResult);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,7 @@ export const getActionMessageParams = memoizeOne((ruleType: RuleType | undefined
...actionMessageRuleParams.map(param => `context.rule.${param}`),
];
});

// typed as null not undefined as the initial state for this value is null.
export const userHasNoPermissions = (canUserCRUD: boolean | null): boolean =>
canUserCRUD != null ? !canUserCRUD : false;
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { AllRules } from './all';
import { ImportDataModal } from '../../../components/import_data_modal';
import { ReadOnlyCallOut } from './components/read_only_callout';
import { UpdatePrePackagedRulesCallOut } from './components/pre_packaged_rules/update_callout';
import { getPrePackagedRuleStatus, redirectToDetections } from './helpers';
import { getPrePackagedRuleStatus, redirectToDetections, userHasNoPermissions } from './helpers';
import * as i18n from './translations';

type Func = (refreshPrePackagedRule?: boolean) => void;
Expand Down Expand Up @@ -61,8 +61,6 @@ const RulesPageComponent: React.FC = () => {
rulesNotUpdated
);

const userHasNoPermissions = canUserCRUD != null ? !canUserCRUD : false;

const handleRefreshRules = useCallback(async () => {
if (refreshRulesData.current != null) {
refreshRulesData.current(true);
Expand Down Expand Up @@ -92,7 +90,7 @@ const RulesPageComponent: React.FC = () => {

return (
<>
{userHasNoPermissions && <ReadOnlyCallOut />}
{userHasNoPermissions(canUserCRUD) && <ReadOnlyCallOut />}
<ImportDataModal
checkBoxLabel={i18n.OVERWRITE_WITH_SAME_NAME}
closeModal={() => setShowImportModal(false)}
Expand Down Expand Up @@ -122,7 +120,7 @@ const RulesPageComponent: React.FC = () => {
<EuiButton
iconType="indexOpen"
isLoading={loadingCreatePrePackagedRules}
isDisabled={userHasNoPermissions || loading}
isDisabled={userHasNoPermissions(canUserCRUD) || loading}
onClick={handleCreatePrePackagedRules}
>
{i18n.LOAD_PREPACKAGED_RULES}
Expand All @@ -135,7 +133,7 @@ const RulesPageComponent: React.FC = () => {
data-test-subj="reloadPrebuiltRulesBtn"
iconType="plusInCircle"
isLoading={loadingCreatePrePackagedRules}
isDisabled={userHasNoPermissions || loading}
isDisabled={userHasNoPermissions(canUserCRUD) || loading}
onClick={handleCreatePrePackagedRules}
>
{i18n.RELOAD_MISSING_PREPACKAGED_RULES(rulesNotInstalled ?? 0)}
Expand All @@ -145,7 +143,7 @@ const RulesPageComponent: React.FC = () => {
<EuiFlexItem grow={false}>
<EuiButton
iconType="importAction"
isDisabled={userHasNoPermissions || loading}
isDisabled={userHasNoPermissions(canUserCRUD) || loading}
onClick={() => {
setShowImportModal(true);
}}
Expand All @@ -159,7 +157,7 @@ const RulesPageComponent: React.FC = () => {
fill
href={getCreateRuleUrl()}
iconType="plusInCircle"
isDisabled={userHasNoPermissions || loading}
isDisabled={userHasNoPermissions(canUserCRUD) || loading}
>
{i18n.ADD_NEW_RULE}
</EuiButton>
Expand All @@ -177,7 +175,7 @@ const RulesPageComponent: React.FC = () => {
createPrePackagedRules={createPrePackagedRules}
loading={loading || prePackagedRuleLoading}
loadingCreatePrePackagedRules={loadingCreatePrePackagedRules}
hasNoPermissions={userHasNoPermissions}
hasNoPermissions={userHasNoPermissions(canUserCRUD)}
refetchPrePackagedRulesStatus={handleRefetchPrePackagedRulesStatus}
rulesCustomInstalled={rulesCustomInstalled}
rulesInstalled={rulesInstalled}
Expand Down

0 comments on commit 6507f72

Please sign in to comment.