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

staff permission data structure change(id, name) #212

Merged
merged 3 commits into from
Jan 31, 2025
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
8 changes: 4 additions & 4 deletions .github/workflows/api-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ jobs:
sudo apt update
sudo apt install libsasl2-dev libldap2-dev libssl-dev --yes
make setup
- name: Lint with pylint
id: pylint
run: |
make pylint
- name: Lint with flake8
id: flake8
run: |
make flake8
- name: Lint with pylint
id: pylint
run: |
make pylint

testing:
needs: setup-job
Expand Down
2 changes: 1 addition & 1 deletion compliance-api/requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ flake8==4.0.1
flake8-blind-except
flake8-debugger
flake8-docstrings
flake8-isort
flake8-isort==6.1.1
flake8-quotes
pep8-naming
autopep8
Expand Down
2 changes: 1 addition & 1 deletion compliance-api/requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Flask-SQLAlchemy
SQLAlchemy-Continuum
flask-restx
flask-marshmallow==1.2.1
flask-jwt-oidc
flask-jwt-oidc==0.7.0
python-dotenv
psycopg2-binary
marshmallow-sqlalchemy==1.0.0
Expand Down
6 changes: 3 additions & 3 deletions compliance-api/src/compliance_api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def decorated(f):
@wraps(f)
def wrapper(*args, **kwargs):
mapped_groups = _map_permission_to_groups(permissions)
if jwt.contains_role(mapped_groups):
if jwt.contains_role(roles=mapped_groups):
return f(*args, **kwargs)

raise PermissionDeniedError(
Expand All @@ -72,13 +72,13 @@ def wrapper(*args, **kwargs):
@classmethod
def has_role(cls, role):
"""Validate the role."""
return jwt.validate_roles(role)
return jwt.validate_roles(required_roles=role)

@classmethod
def has_permission(cls, permissions):
"""Check to see if the user has right permissions."""
mapped_groups = _map_permission_to_groups(permissions)
return jwt.contains_role(mapped_groups)
return jwt.contains_role(roles=mapped_groups)


def _map_permission_to_groups(permissions):
Expand Down
13 changes: 9 additions & 4 deletions compliance-api/src/compliance_api/schemas/staff_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ class Meta(AutoSchemaBase.Meta): # pylint: disable=too-few-public-methods
include_fk = True

position = fields.Nested(KeyValueSchema, dump_only=True)
permission = fields.Str(
metadata={"description": "The permission level of the user in the app"}
)
# permission = fields.Str(
# metadata={"description": "The permission level of the user in the app"}
# )
permission = fields.Raw()
name = fields.Method("get_full_name")

def get_full_name(self, obj): # pylint: disable=no-self-use
Expand All @@ -52,7 +53,11 @@ def nullify_nested(
if data.get("supervisor_id") is None:
data["supervisor"] = None
if data.get("permission") in [p.name for p in PermissionEnum]:
data["permission"] = getattr(PermissionEnum, data.get("permission")).name
# data["permission"] = getattr(PermissionEnum, data.get("permission")).value
data["permission"] = {
"id": getattr(PermissionEnum, data.get("permission")).name,
"name": getattr(PermissionEnum, data.get("permission")).value
}
return data


Expand Down
8 changes: 6 additions & 2 deletions compliance-api/src/compliance_api/services/staff_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _create_staff_user_object(user_data: dict, auth_user: dict):
"deputy_director_id": user_data.get("deputy_director_id"),
"supervisor_id": user_data.get("supervisor_id", None),
"auth_user_guid": auth_user.get("username", None),
"is_active": auth_user.get("is_active")
"is_active": auth_user.get("is_active"),
}


Expand All @@ -129,7 +129,11 @@ def _set_permission_level_in_compliance_user_obj(
"""Set the permission level in compliance user."""
if auth_user and auth_user.get("groups", None):
sorted_groups = sorted(auth_user.get("groups", None), key=_get_level)
if sorted_groups[-1] and sorted_groups[-1]["name"]:
if (
sorted_groups[-1]
and sorted_groups[-1]["name"]
and sorted_groups[-1]["name"] in [p.name for p in PermissionEnum]
):
setattr(compliance_user, "permission", sorted_groups[-1]["name"])
return compliance_user

Expand Down
6 changes: 3 additions & 3 deletions compliance-api/tests/integration/api/test_staff_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_create_staff_user_mandatory(
print(result.json)
assert result.status_code == HTTPStatus.CREATED
assert result.json["auth_user_guid"] == staff_user_data["auth_user_guid"]
assert result.json["permission"] == "VIEWER"
assert result.json["permission"]["id"] == "VIEWER"
assert result.json["position_id"] == staff_user_data["position_id"]
assert result.json["first_name"] == firstname
assert result.json["last_name"] == lastname
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_create_staff_user_all_fields(

assert result.status_code == HTTPStatus.CREATED
assert result.json["auth_user_guid"] == staff_user_data["auth_user_guid"]
assert result.json["permission"] == "USER"
assert result.json["permission"]["id"] == "USER"
assert result.json["position_id"] == staff_user_data["position_id"]
assert result.json["deputy_director_id"] == staff_user_data["deputy_director_id"]
assert result.json["supervisor_id"] == staff_user_data["supervisor_id"]
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_get_users(mock_auth_service, mocker, client, auth_header_super_user):
)
print(filtered_user)
assert filtered_user is not None
assert filtered_user.get("permission", None) == "USER"
assert filtered_user.get("permission", {}).get("id", None) == "USER"
assert result.status_code == HTTPStatus.OK


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import FileProfileProperty from "@/components/App/FileProfileProperty";
import { CaseFile } from "@/models/CaseFile";
import { formatAuthorization } from "@/utils/appUtils";
import dateUtils from "@/utils/dateUtils";
import { EditRounded } from "@mui/icons-material";
import { Box, Button, Stack, Typography } from "@mui/material";
import FileProfileProperty from "@/components/App/FileProfileProperty";
import { CaseFile } from "@/models/CaseFile";
import CaseFileInspectionsTable from "./CaseFileInspectionsTable";
import CaseFileComplaintsTable from "./CaseFileComplaintsTable";
import { formatAuthorization } from "@/utils/appUtils";
import CaseFileInspectionsTable from "./CaseFileInspectionsTable";

interface CaseFileGeneralInformationProps {
caseFileData: CaseFile;
Expand Down Expand Up @@ -47,7 +47,7 @@ const CaseFileGeneralInformation: React.FC<CaseFileGeneralInformationProps> = ({
size="small"
/>
<FileProfileProperty
propertyName="Certificate Holder"
propertyName="Regulated Party"
propertyValue={caseFileData.regulated_party}
size="small"
/>
Expand Down
2 changes: 1 addition & 1 deletion compliance-web/src/components/App/Staff/StaffModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const StaffModal: React.FC<StaffModalProps> = ({ onSubmit, staff }) => {
undefined,
position: staff.position || undefined,
permission:
permissionsList?.find((item) => item.id === staff.permission) ||
permissionsList?.find((item) => item.id === staff.permission?.id) ||
undefined,
deputyDirector:
staffUsersList?.find(
Expand Down
2 changes: 1 addition & 1 deletion compliance-web/src/models/Staff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface StaffUser {
position_id?: number;
deputy_director_id?: number;
supervisor_id?: number;
permission?: string;
permission?: Permission;
position?: Position;
deputy_director?: StaffUser;
supervisor?: StaffUser;
Expand Down
8 changes: 3 additions & 5 deletions compliance-web/src/routes/_authenticated/admin/staff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function Staff() {
);
setPermissionList(
[
...new Set(staffUsersList?.map((staff) => staff.permission ?? "")),
...new Set(staffUsersList?.map((staff) => staff.permission?.name)),
].filter(Boolean)
);
}, [staffUsersList]);
Expand Down Expand Up @@ -187,7 +187,7 @@ export function Staff() {
},
},
{
accessorKey: "permission",
accessorFn: (row) => row.permission?.name,
header: "Permission Level",
filterVariant: "multi-select",
filterSelectOptions: permissionList,
Expand Down Expand Up @@ -223,7 +223,6 @@ export function Staff() {
},
filterVariant: "multi-select",
filterSelectOptions: ["Active", "Inactive"],
filterValue: ["Active"],
Filter: ({ header, column }) => {
return (
<TableFilter
Expand Down Expand Up @@ -251,8 +250,7 @@ export function Staff() {
id: "name",
desc: false,
},
],
columnFilters: [{ id: "Status", value: ["Active"] }],
]
}}
state={{
isLoading: isLoading,
Expand Down