login tested updated

This commit is contained in:
2025-01-26 23:00:58 +03:00
parent a7e48d8755
commit bc300f727a
12 changed files with 65 additions and 145 deletions

View File

@@ -1,12 +1,9 @@
from typing import Any, ClassVar, Dict
from sqlalchemy import or_
from typing import Any, Dict
from ApiLayers.ApiLibrary.common.line_number import get_line_number_for_error
from ApiLayers.ErrorHandlers import HTTPExceptionApi
from ApiLayers.ApiValidations.Request.authentication import Login
from ApiLayers.ApiLibrary.token.password_module import PasswordModule
from ApiLayers.ApiServices.Token.token_handler import TokenService
from ApiLayers.Schemas import Users
from ApiLayers.ApiLibrary.common.line_number import get_line_number_for_error
class UserLoginModule:
@@ -15,15 +12,17 @@ class UserLoginModule:
self.request = request
@staticmethod
async def check_user_exists(access_key) -> ClassVar[Users]:
def check_user_exists(access_key: str):
from ApiLayers.Schemas import Users
"""Check if user exists."""
db_session = Users.new_session()
if "@" in access_key:
found_user = Users.filter_one(
found_user: Users = Users.filter_one(
Users.email == access_key.lower(), db=db_session
).data
else:
found_user = Users.filter_one(
found_user: Users = Users.filter_one(
Users.phone_number == access_key.replace(" ", ""), db=db_session
).data
if not found_user:
@@ -35,15 +34,13 @@ class UserLoginModule:
)
return found_user
async def login_user_via_credentials(self, access_data: "Login") -> Dict[str, Any]:
def login_user_via_credentials(self, access_data: "Login") -> Dict[str, Any]:
from ApiLayers.ApiServices.Token.token_handler import TokenService
from ApiLayers.Schemas import Users
"""Login user via credentials."""
# Get the actual data from the BaseRequestModel if needed
if hasattr(access_data, "data"):
access_data = access_data.data
found_user: Users = await self.check_user_exists(
access_key=access_data.access_key
)
found_user: Users = self.check_user_exists(access_key=access_data.access_key)
if len(found_user.hash_password) < 5:
raise HTTPExceptionApi(
error_code="HTTP_400_BAD_REQUEST",
@@ -51,7 +48,6 @@ class UserLoginModule:
loc=get_line_number_for_error(),
sys_msg="Invalid password create a password to user first",
)
if PasswordModule.check_password(
domain=access_data.domain,
id_=found_user.uu_id,

View File

@@ -126,7 +126,7 @@ class TokenService:
request=dict(request.headers),
available_occupants=occupants_selection_dict,
timezone=user.local_timezone or "GMT+0",
lang=user.lang or "tr",
lang="tr",
).model_dump()
if access_token := cls.set_object_to_redis(user, model_value):
return {
@@ -229,6 +229,7 @@ class TokenService:
}
)
person = People.filter_one(People.id == user.person_id, db=db_session).data
model_value = EmployeeTokenObject(
domain=domain,
user_type=UserType.employee.value,
@@ -243,7 +244,7 @@ class TokenService:
duty_uu_id_list=duty_uu_id_list,
duty_id_list=duty_id_list,
timezone=user.local_timezone or "GMT+0",
lang=user.lang or "tr",
lang="tr",
).model_dump()
if access_token := cls.set_object_to_redis(user, model_value):
return {

View File

@@ -20,6 +20,7 @@ from ApiLayers.ApiLibrary.date_time_actions.date_functions import system_arrow
from ApiLayers.ApiLibrary.extensions.select import SelectAction, SelectActionWithEmployee
from ApiLayers.AllConfigs.Token.config import Auth
from ApiLayers.ApiServices.Login.user_login_handler import UserLoginModule
from Services.PostgresDb import CrudCollection
from config import ApiStatic
@@ -64,10 +65,6 @@ class UsersTokens(CrudCollection):
# users = relationship("Users", back_populates="tokens", foreign_keys=[user_id])
class UserLoginModule:
pass
class Users(CrudCollection, UserLoginModule, SelectAction):
"""
Application User frame to connect to API with assigned token-based HTTP connection
@@ -369,6 +366,14 @@ class People(CrudCollection, SelectAction):
tax_no: Mapped[str] = mapped_column(
String, server_default="", comment="Tax number of the person"
)
# Receive at Create person
# language = mapped_column(
# String, comment="Language code of the person"
# )
# currency = mapped_column(
# String, comment="Currency code of the person"
# )
# ENCRYPT DATA
user = relationship(
"Users", back_populates="person", foreign_keys="Users.person_id"