Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor user delete permissions to allow StateAdmin and DistrictAdmin #6781

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -15,7 +15,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 @@ -159,17 +164,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[] = [];

userListData?.results &&
Expand Down Expand Up @@ -233,7 +227,7 @@ export default function ManageUsers() {
aria-label="Online"
></i>
) : null}
{showDelete(user) && (
{showUserDelete(authUser, user) && (
<ButtonV2
variant="danger"
ghost
Expand Down
25 changes: 25 additions & 0 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import {
AREACODES,
IN_LANDLINE_AREA_CODES,
LocalStorageKeys,
USER_TYPES,
} 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 @@ -460,6 +462,29 @@ export const scrollTo = (id: string | boolean) => {
element?.scrollIntoView({ behavior: "smooth", block: "center" });
};

export const showUserDelete = (authUser: UserModel, targetUser: UserModel) => {
// Auth user should be higher in hierarchy than target user
if (
USER_TYPES.indexOf(authUser.user_type) <=
USER_TYPES.indexOf(targetUser.user_type)
)
return false;

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;
};

export const invalidateFiltersCache = () => {
for (const key in localStorage) {
if (key.startsWith("filters--")) {
Expand Down
Loading