Skip to content

Commit

Permalink
Permission the fhir-flag module
Browse files Browse the repository at this point in the history
  • Loading branch information
peterMuriuki committed Nov 7, 2024
1 parent c2d2b68 commit f31c3c4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/src/App/fhir-apps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ const FHIRApps = () => {
disableLoginProtection={DISABLE_LOGIN_PROTECTION}
exact
path={`${URL_CLOSE_FLAGS}/:id`}
// permissions={['Flag.update']}
permissions={['Flag.read']}
component={CloseFlag}
/>
<Route
Expand Down
19 changes: 15 additions & 4 deletions packages/fhir-flag/src/components/BaseComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { IBundle } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IBundle';
import { IPractitioner } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IPractitioner';
import { useTranslation } from '../../mls';
import { thatiMinutes } from '../../constants';
import { RbacCheck } from '@opensrp/rbac';

export interface CloseFlagProps {
fhirBaseURL: string;
Expand Down Expand Up @@ -54,7 +55,7 @@ export const CloseFlag = (props: CloseFlagProps) => {
enabled: !!user_id,
staleTime: thatiMinutes,
select: (response) => {
return getResourcesFromBundle<IPractitioner>(response)?.[0];
return getResourcesFromBundle<IPractitioner>(response)[0];
},
}
);
Expand All @@ -80,10 +81,20 @@ export const CloseFlag = (props: CloseFlagProps) => {
return (
<Row>
<Col span={24}>
{flag.data?.subject?.reference?.includes('Location') ? (
<LocationFlag {...commonProps} locationReference={flag.data.subject.reference} />
{flag.data?.subject.reference?.includes('Location') ? (
<RbacCheck
permissions={['Location.read']}
fallback={t('Missing Required permissions to perform this action')}
>
<LocationFlag {...commonProps} locationReference={flag.data.subject.reference} />
</RbacCheck>
) : (
<ProductFlag {...commonProps} inventoryGroupReference={flag.data?.subject.reference} />
<RbacCheck
permissions={['Group.read', 'Location.read', 'List.read']}
fallback={t('Missing Required permissions to perform this action')}
>
<ProductFlag {...commonProps} inventoryGroupReference={flag.data?.subject.reference} />
</RbacCheck>
)}
</Col>
</Row>
Expand Down
20 changes: 18 additions & 2 deletions packages/fhir-flag/src/components/CloseFlagForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { sendErrorNotification, sendSuccessNotification } from '@opensrp/notific
import { useTranslation } from '../../mls';
import { comments, locationName, productName, status } from '../../constants';
import { IFlag } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IFlag';
import { useUserRole } from '@opensrp/rbac';

const { Item: FormItem } = Form;
const { TextArea } = Input;
Expand Down Expand Up @@ -36,6 +37,7 @@ const headerProps = {

const CloseFlagForm = (props: CloseFlagFormProps) => {
const { initialValues, flag, mutationEffect } = props;
const userRole = useUserRole();
const { t } = useTranslation();

const stableInitialValues = useMemo(() => initialValues, [initialValues]);
Expand Down Expand Up @@ -63,6 +65,15 @@ const CloseFlagForm = (props: CloseFlagFormProps) => {
mutate(values);
};

const saveBtnDisabled = userRole.hasPermissions([
'Flag.update',
'Encounter.update',
'Observation.update',
]);
const saveBtnDisabledReason = saveBtnDisabled
? t('Missing Required permissions to perform this action')
: undefined;

return (
<BodyLayout headerProps={headerProps}>
<Row className="user-group">
Expand All @@ -89,8 +100,13 @@ const CloseFlagForm = (props: CloseFlagFormProps) => {
<TextArea rows={4} placeholder={t('How was the flag resolved?')} />
</FormItem>

<FormItem {...tailLayout}>
<Button type="primary" id="submit-button" htmlType="submit">
<FormItem {...tailLayout} help={saveBtnDisabledReason}>
<Button
type="primary"
id="submit-button"
htmlType="submit"
disabled={saveBtnDisabled}
>
{isLoading ? t('Saving') : t('Save')}
</Button>
</FormItem>
Expand Down

0 comments on commit f31c3c4

Please sign in to comment.