updated login/select

This commit is contained in:
2025-01-27 21:43:36 +03:00
parent b88f910a43
commit c0bd9c1685
18 changed files with 257 additions and 275 deletions

View File

@@ -22,6 +22,7 @@ from .api_events import (
authentication_reset_password_event,
authentication_download_avatar_event,
)
from .function_handlers import AuthenticationFunctions
AuthenticationLoginEventMethods = MethodToEvent(
@@ -39,7 +40,7 @@ AuthenticationLoginEventMethods = MethodToEvent(
def authentication_login_with_domain_and_creds_endpoint(
request: Request, data: EndpointBaseRequestModel,
request: Request, data: EndpointBaseRequestModel
) -> Dict[str, Any]:
event_2_catch = AuthenticationLoginEventMethods.retrieve_event(
event_function_code=f"{authentication_login_super_user_event.key}"
@@ -68,7 +69,9 @@ AuthenticationSelectEventMethods = MethodToEvent(
)
def authentication_select_company_or_occupant_type(data: EndpointBaseRequestModel) -> Dict[str, Any]:
def authentication_select_company_or_occupant_type(
request: Request, data: EndpointBaseRequestModel
) -> Dict[str, Any]:
"""
Select company or occupant type.
"""
@@ -76,8 +79,13 @@ def authentication_select_company_or_occupant_type(data: EndpointBaseRequestMode
function = AuthenticationSelectEventMethods.retrieve_event(
event_function_code=f"{authentication_select_super_user_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable(data=data)
AuthenticationFunctions.context_retriever = context_retriever
data_model = None
if context_retriever.token.is_employee:
data_model = function.REQUEST_VALIDATOR.get('EmployeeSelection', None)(**data.data)
elif context_retriever.token.is_occupant:
data_model = function.REQUEST_VALIDATOR.get('OccupantSelection', None)(**data.data)
return function.endpoint_callable(data=data_model)
AuthenticationSelectEventMethods.endpoint_callable = (
@@ -103,7 +111,7 @@ def authentication_check_token_is_valid():
function = AuthenticationCheckTokenEventMethods.retrieve_event(
event_function_code=f"{authentication_check_token_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
AuthenticationFunctions.context_retriever = context_retriever
return function.endpoint_callable()
@@ -132,7 +140,7 @@ def authentication_refresh_user_info():
function = AuthenticationRefreshEventMethods.retrieve_event(
event_function_code=f"{authentication_refresh_user_info_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
AuthenticationFunctions.context_retriever = context_retriever
return function.endpoint_callable()
@@ -159,7 +167,7 @@ def authentication_change_password_event_callable(data: EndpointBaseRequestModel
function = AuthenticationChangePasswordEventMethods.retrieve_event(
event_function_code=f"{authentication_change_password_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
AuthenticationFunctions.context_retriever = context_retriever
return function.endpoint_callable(data=data)
@@ -184,7 +192,7 @@ def authentication_create_password(data: EndpointBaseRequestModel):
function = AuthenticationCreatePasswordEventMethods.retrieve_event(
event_function_code=f"{authentication_create_password_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
AuthenticationFunctions.context_retriever = context_retriever
return function.endpoint_callable(data=data)
@@ -213,7 +221,7 @@ def authentication_disconnect_user(data: EndpointBaseRequestModel):
function = AuthenticationDisconnectUserEventMethods.retrieve_event(
event_function_code=f"{authentication_disconnect_user_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
AuthenticationFunctions.context_retriever = context_retriever
return function.endpoint_callable(data=data)
@@ -235,7 +243,7 @@ def authentication_logout_user(data: EndpointBaseRequestModel):
function = AuthenticationLogoutEventMethods.retrieve_event(
event_function_code=f"{authentication_logout_user_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
AuthenticationFunctions.context_retriever = context_retriever
return function.endpoint_callable(data=data)
@@ -262,7 +270,7 @@ def authentication_refresher_token(data: EndpointBaseRequestModel):
function = AuthenticationRefreshTokenEventMethods.retrieve_event(
event_function_code=f"{authentication_refresher_token_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
AuthenticationFunctions.context_retriever = context_retriever
return function.endpoint_callable(data=data)
@@ -290,7 +298,7 @@ def authentication_forgot_password(data: EndpointBaseRequestModel):
function = AuthenticationForgotPasswordEventMethods.retrieve_event(
event_function_code=f"{authentication_forgot_password_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
AuthenticationFunctions.context_retriever = context_retriever
return function.endpoint_callable(data=data)
@@ -319,8 +327,8 @@ def authentication_reset_password(data: EndpointBaseRequestModel):
function = AuthenticationResetPasswordEventMethods.retrieve_event(
event_function_code=f"{authentication_reset_password_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable(data=data)
AuthenticationFunctions.context_retriever = context_retriever
return function.endpoint_callable(data=data.data)
AuthenticationResetPasswordEventMethods.endpoint_callable = (
@@ -348,7 +356,7 @@ def authentication_download_avatar():
function = AuthenticationDownloadAvatarEventMethods.retrieve_event(
event_function_code=f"{authentication_download_avatar_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
AuthenticationFunctions.context_retriever = context_retriever
return function.endpoint_callable()

View File

@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Union
from fastapi import Request
from ApiLayers.ApiLibrary.common.line_number import get_line_number_for_error
@@ -30,7 +30,6 @@ class Handlers:
@classmethod # Requires no auth context
def handle_employee_selection(cls, request: Request, data: Any, token_dict: TokenDictType):
Users.set_user_define_properties(token=token_dict)
db = Users.new_session()
if data.company_uu_id not in token_dict.companies_uu_id_list:
raise HTTPExceptionApi(
@@ -40,8 +39,7 @@ class Handlers:
sys_msg="Company not found in token",
)
selected_company: Companies = Companies.filter_one(
Companies.uu_id == data.company_uu_id,
db=db,
Companies.uu_id == data.company_uu_id, db=db
).data
if not selected_company:
raise HTTPExceptionApi(
@@ -51,15 +49,6 @@ class Handlers:
sys_msg="Company not found in token",
)
# Get department IDs for the company
department_ids = [
dept.id
for dept in Departments.filter_all(
Departments.company_id == selected_company.id,
db=db,
).data
]
# Get duties IDs for the company
duties_ids = [
duty.id
@@ -77,8 +66,7 @@ class Handlers:
# Get employee
employee: Employees = Employees.filter_one(
Employees.people_id == token_dict.person_id,
Employees.staff_id.in_(staff_ids),
db=db,
Employees.staff_id.in_(staff_ids), db=db
).data
if not employee:
@@ -91,9 +79,6 @@ class Handlers:
# Get reachable events
reachable_event_codes = Event2Employee.get_event_codes(employee_id=employee.id)
reachable_event_endpoints = Event2Employee.get_event_endpoints(
employee_id=employee.id
)
# Get staff and duties
staff = Staff.filter_one(Staff.id == employee.staff_id, db=db).data
@@ -107,7 +92,6 @@ class Handlers:
bulk_duty_id = Duties.filter_by_one(
company_id=selected_company.id,
duties_id=bulk_id.id,
**Duties.valid_record_dict,
db=db,
).data
@@ -125,7 +109,6 @@ class Handlers:
employee_id=employee.id,
employee_uu_id=employee.uu_id.__str__(),
reachable_event_codes=reachable_event_codes,
reachable_event_endpoints=reachable_event_endpoints,
)
try: # Update Redis
@@ -143,7 +126,6 @@ class Handlers:
@classmethod # Requires no auth context
def handle_occupant_selection(cls, request: Request, data: Any, token_dict: TokenDictType):
"""Handle occupant type selection"""
Users.set_user_define_properties(token=token_dict)
db = BuildLivingSpace.new_session()
# Get selected occupant type
selected_build_living_space: BuildLivingSpace = BuildLivingSpace.filter_one(
@@ -162,9 +144,6 @@ class Handlers:
reachable_event_codes = Event2Occupant.get_event_codes(
build_living_space_id=selected_build_living_space.id
)
reachable_event_endpoints = Event2Occupant.get_event_endpoints(
build_living_space_id=selected_build_living_space.id
)
occupant_type = OccupantTypes.filter_one(
OccupantTypes.id == selected_build_living_space.occupant_type_id,
db=db,
@@ -208,7 +187,6 @@ class Handlers:
responsible_company_id=company_related.id,
responsible_company_uuid=company_related.uu_id.__str__(),
reachable_event_codes=reachable_event_codes,
reachable_event_endpoints=reachable_event_endpoints,
)
try: # Update Redis
@@ -227,25 +205,7 @@ class Handlers:
class AuthenticationFunctions:
"""Class for handling authentication functions"""
@classmethod # Requires auth context
def authentication_select_company_or_occupant_type(cls, data: Any):
"""Handle selection of company or occupant type"""
context_retriever = ContextRetrievers(func=cls.authentication_select_company_or_occupant_type)
if context_retriever.token.is_employee:
if Handlers.handle_employee_selection(
request=context_retriever.request, data=data, token_dict=context_retriever.token
):
return {
"completed": True, "selected": data.company_uu_id, **context_retriever.base,
}
elif context_retriever.token.is_occupant:
if Handlers.handle_occupant_selection(
request=context_retriever.request, data=data, token_dict=context_retriever.token
):
return {
"completed": True, "selected": data.build_living_space_uu_id, **context_retriever.base,
}
return {"completed": False, "selected": None, **context_retriever.base}
context_retriever: Union[ContextRetrievers] = None
@classmethod # Requires no auth context
def authentication_login_with_domain_and_creds(cls, request: Request, data: Any):
@@ -275,6 +235,32 @@ class AuthenticationFunctions:
code="LOGIN_SUCCESS", lang=user_login_module.language
).as_dict(data=user_login_module.as_dict)
@classmethod # Requires auth context
def authentication_select_company_or_occupant_type(cls, data: Any):
"""
Handle selection of company or occupant type
{"data": {"build_living_space_uu_id": ""}} | {"data": {"company_uu_id": ""}}
"""
if cls.context_retriever.token.is_employee:
if Handlers.handle_employee_selection(
request=cls.context_retriever.request, data=data, token_dict=cls.context_retriever.token
):
return EndpointSuccessResponse(
code="LOGIN_SELECT", lang=cls.context_retriever.token.lang
).as_dict(data={
"selected": data.company_uu_id, **cls.context_retriever.base
})
elif cls.context_retriever.token.is_occupant:
if Handlers.handle_occupant_selection(
request=cls.context_retriever.request, data=data, token_dict=cls.context_retriever.token
):
return EndpointSuccessResponse(
code="LOGIN_SELECT", lang=cls.context_retriever.token.lang
).as_dict(data={
"selected": data.build_living_space_uu_id, **cls.context_retriever.base
})
return {"completed": False, "selected": None, **cls.context_retriever.base}
@classmethod # Requires not auth context
def authentication_check_token_is_valid(cls, data: Any):
"""Check if token is valid for user"""

View File

@@ -1,5 +1,9 @@
from pydantic import BaseModel
from ApiLayers.ApiValidations.Request import Login
from ApiLayers.ApiValidations.Request import (
Login,
EmployeeSelection,
OccupantSelection,
)
class LoginSuperUserRequestModel(Login):
@@ -33,12 +37,15 @@ class OccupantSelectionSuperUserRequestModel(BaseModel):
class OccupantSelectionSuperUserResponseModel(BaseModel):
pass
"""
EmployeeSelection,
OccupantSelection,
"""
class AuthenticationRequestModels:
LoginSuperUserRequestModel = LoginSuperUserRequestModel
SelectCompanyOrOccupantTypeSuperUserRequestModel = (
SelectCompanyOrOccupantTypeSuperUserRequestModel
)
SelectCompanyOrOccupantTypeSuperUserRequestModel = {
"EmployeeSelection": EmployeeSelection, "OccupantSelection":OccupantSelection
}
EmployeeSelectionSuperUserRequestModel = EmployeeSelectionSuperUserRequestModel
OccupantSelectionSuperUserRequestModel = OccupantSelectionSuperUserRequestModel