Skip to content

Commit

Permalink
Refactor user delete permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashesh3 committed Dec 3, 2023
1 parent b49247c commit 6ab927b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
12 changes: 9 additions & 3 deletions src/Components/Facility/FacilityUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import CountBlock from "../../CAREUI/display/Count";
import CareIcon from "../../CAREUI/icons/CareIcon";
import { RESULTS_PER_PAGE_LIMIT } from "../../Common/constants";
import * as Notification from "../../Utils/Notifications.js";
import { classNames, isUserOnline, relativeTime } from "../../Utils/utils";
import {
classNames,
isUserOnline,
relativeTime,
showUserDelete,
} from "../../Utils/utils";
import Pagination from "../Common/Pagination";
import UserDetails from "../Common/UserDetails";
import ButtonV2 from "../Common/components/ButtonV2";
Expand All @@ -16,6 +21,7 @@ import useAuthUser from "../../Common/hooks/useAuthUser";
import request from "../../Utils/request/request";
import routes from "../../Redux/api";
import useQuery from "../../Utils/request/useQuery";
import { UserModel } from "../Users/models";

const Loading = lazy(() => import("../Common/Loading"));

Expand Down Expand Up @@ -256,7 +262,7 @@ export default function FacilityUsers(props: any) {
facilityUserData &&
facilityUserData.results &&
facilityUserData.results.length &&
(userList = facilityUserData.results.map((user: any) => {
(userList = facilityUserData.results.map((user: UserModel) => {
return (
<div
key={`usr_${user.id}`}
Expand Down Expand Up @@ -299,7 +305,7 @@ export default function FacilityUsers(props: any) {
aria-label="Online"
></i>
) : null}
{authUser.user_type === "StateAdmin" && (
{showUserDelete(authUser, user) && (
<button
type="button"
className="focus:ring-blue m-3 w-20 self-end rounded-md border border-red-500 bg-white px-3 py-2 text-center text-sm font-medium leading-4 text-red-700 transition duration-150 ease-in-out hover:text-red-500 hover:shadow focus:border-red-300 focus:outline-none active:bg-gray-50 active:text-red-800"
Expand Down
20 changes: 7 additions & 13 deletions src/Components/Users/ManageUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ import UnlinkFacilityDialog from "./UnlinkFacilityDialog";
import UserDeleteDialog from "./UserDeleteDialog";
import UserDetails from "../Common/UserDetails";
import UserFilter from "./UserFilter";
import { classNames, isUserOnline, relativeTime } from "../../Utils/utils";
import {
classNames,
isUserOnline,
relativeTime,
showUserDelete,
} from "../../Utils/utils";
import { navigate } from "raviger";
import useFilters from "../../Common/hooks/useFilters";
import useWindowDimensions from "../../Common/hooks/useWindowDimensions";
Expand Down Expand Up @@ -201,17 +206,6 @@ export default function ManageUsers() {
});
};

const showDelete = (user: any) => {
if (user.is_superuser) return true;

if (
USER_TYPES.indexOf(authUser.user_type) >= USER_TYPES.indexOf("StateAdmin")
)
return user.state_object?.id === authUser.state;

return false;
};

let userList: any[] = [];

users &&
Expand Down Expand Up @@ -275,7 +269,7 @@ export default function ManageUsers() {
aria-label="Online"
></i>
) : null}
{showDelete(user) && (
{showUserDelete(authUser, user) && (
<ButtonV2
variant="danger"
ghost
Expand Down
16 changes: 16 additions & 0 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "../Common/constants";
import phoneCodesJson from "../Common/static/countryPhoneAndFlags.json";
import dayjs from "./dayjs";
import { UserModel } from "../Components/Users/models";

interface ApacheParams {
age: number;
Expand Down Expand Up @@ -454,3 +455,18 @@ export const scrollTo = (id: string | boolean) => {
const element = document.querySelector(`#${id}`);
element?.scrollIntoView({ behavior: "smooth", block: "center" });
};

export const showUserDelete = (authUser: UserModel, targetUser: UserModel) => {
if (
authUser.user_type === "StateAdmin" &&
targetUser.state_object?.id === authUser.state
)
return true;

if (
authUser.user_type === "DistrictAdmin" &&
targetUser.district_object?.id === authUser.district
)
return true;
return false;
};

0 comments on commit 6ab927b

Please sign in to comment.