sql mongo updated
This commit is contained in:
parent
fa4260dbea
commit
d8685dd496
|
|
@ -212,6 +212,7 @@ class UserLoginModule(AuthModule):
|
||||||
@classmethod
|
@classmethod
|
||||||
def login_user_with_credentials(cls, data, request):
|
def login_user_with_credentials(cls, data, request):
|
||||||
from api_services.redis.functions import RedisActions
|
from api_services.redis.functions import RedisActions
|
||||||
|
|
||||||
from databases import (
|
from databases import (
|
||||||
Users,
|
Users,
|
||||||
People,
|
People,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
import datetime
|
|
||||||
|
|
||||||
from databases.no_sql_models.validations import (
|
from databases.no_sql_models.validations import (
|
||||||
PasswordHistoryViaUser,
|
PasswordHistoryViaUser,
|
||||||
DomainViaUser,
|
DomainViaUser,
|
||||||
|
|
@ -9,19 +7,6 @@ from databases.no_sql_models.mongo_database import MongoQuery
|
||||||
from api_library.date_time_actions.date_functions import system_arrow
|
from api_library.date_time_actions.date_functions import system_arrow
|
||||||
|
|
||||||
|
|
||||||
def validate_timestamp(doc):
|
|
||||||
"""Validate and fix timestamp fields in MongoDB documents"""
|
|
||||||
if not doc:
|
|
||||||
return doc
|
|
||||||
|
|
||||||
timestamp_fields = ["modified_at", "created_at", "accessed_at", "timestamp"]
|
|
||||||
for field in timestamp_fields:
|
|
||||||
if field in doc and not isinstance(doc[field], (int, float)):
|
|
||||||
# Convert to proper timestamp if it's not already
|
|
||||||
doc[field] = system_arrow.to_timestamp(doc[field])
|
|
||||||
return doc
|
|
||||||
|
|
||||||
|
|
||||||
class MongoQueryIdentity:
|
class MongoQueryIdentity:
|
||||||
"""
|
"""
|
||||||
4ex. mongo_collection_name = str(Company.uu_id()) + "*" + str('UserPasswordHistory')
|
4ex. mongo_collection_name = str(Company.uu_id()) + "*" + str('UserPasswordHistory')
|
||||||
|
|
@ -30,12 +15,9 @@ class MongoQueryIdentity:
|
||||||
def __init__(self, company_uuid, storage_reasoning: str = None):
|
def __init__(self, company_uuid, storage_reasoning: str = None):
|
||||||
self.company_uuid = company_uuid
|
self.company_uuid = company_uuid
|
||||||
self.mongo_collection_base = str(company_uuid)
|
self.mongo_collection_base = str(company_uuid)
|
||||||
if storage_reasoning:
|
self.mongo_collection_name = (
|
||||||
self.mongo_collection_name = (
|
str(company_uuid) + "*" + str(storage_reasoning or "Domain")
|
||||||
str(company_uuid) + "*" + str(storage_reasoning)
|
)
|
||||||
)
|
|
||||||
else:
|
|
||||||
self.mongo_collection_name = str(company_uuid) + "*" + str("Domain")
|
|
||||||
self.mongo_engine = MongoQuery(
|
self.mongo_engine = MongoQuery(
|
||||||
table_name=self.mongo_collection_name, database_name="mongo_database"
|
table_name=self.mongo_collection_name, database_name="mongo_database"
|
||||||
)
|
)
|
||||||
|
|
@ -49,7 +31,6 @@ class MongoQueryIdentity:
|
||||||
)
|
)
|
||||||
|
|
||||||
def create_domain_via_user(self, payload: DomainViaUser):
|
def create_domain_via_user(self, payload: DomainViaUser):
|
||||||
self.use_collection("Domain")
|
|
||||||
return self.mongo_engine.find_or_insert(
|
return self.mongo_engine.find_or_insert(
|
||||||
field="user_uu_id",
|
field="user_uu_id",
|
||||||
payload={
|
payload={
|
||||||
|
|
@ -61,7 +42,6 @@ class MongoQueryIdentity:
|
||||||
)
|
)
|
||||||
|
|
||||||
def update_domain_via_user(self, payload: DomainViaUser):
|
def update_domain_via_user(self, payload: DomainViaUser):
|
||||||
self.use_collection("Domain")
|
|
||||||
return self.mongo_engine.update(
|
return self.mongo_engine.update(
|
||||||
match=payload.user_uu_id,
|
match=payload.user_uu_id,
|
||||||
payload={
|
payload={
|
||||||
|
|
@ -72,57 +52,25 @@ class MongoQueryIdentity:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_domain_via_user(self, user_uu_id):
|
def get_domain_via_user(self, user_uu_id):
|
||||||
self.use_collection("Domain")
|
|
||||||
result = self.mongo_engine.filter_by(payload={"user_uu_id": user_uu_id})
|
result = self.mongo_engine.filter_by(payload={"user_uu_id": user_uu_id})
|
||||||
return [validate_timestamp(doc) for doc in result.data] if result else None
|
return [self.validate_timestamp(doc) for doc in result.data] if result else None
|
||||||
|
|
||||||
def refresh_password_history_via_user(self, payload: PasswordHistoryViaUser):
|
def refresh_password_history_via_user(self, payload: PasswordHistoryViaUser):
|
||||||
self.use_collection("PasswordHistory")
|
|
||||||
password_history_item = self.mongo_engine.get_one(
|
|
||||||
match=payload.user_uu_id, field="user_uu_id"
|
|
||||||
)
|
|
||||||
if not password_history_item:
|
|
||||||
self.mongo_engine.insert(
|
|
||||||
payload={
|
|
||||||
"user_uu_id": str(payload.user_uu_id),
|
|
||||||
"password_history": [],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
password_history_item = self.mongo_engine.get_one(
|
|
||||||
match=payload.user_uu_id, field="user_uu_id"
|
|
||||||
)
|
|
||||||
password_history_list = password_history_item.get("password_history", [])
|
|
||||||
hashed_password = payload.password_add.get("password")
|
|
||||||
for password_in_history in password_history_list:
|
|
||||||
if str(password_in_history.get("password")) == str(hashed_password):
|
|
||||||
raise Exception(
|
|
||||||
dict(
|
|
||||||
status_code=400,
|
|
||||||
detail="Password already used. Please enter a new password that you have not used last 3 times.",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
if len(password_history_list) > 3:
|
|
||||||
password_history_list.pop(0)
|
|
||||||
|
|
||||||
password_history_list.append(payload.password_add)
|
|
||||||
return self.mongo_engine.update(
|
return self.mongo_engine.update(
|
||||||
|
field="user_uu_id",
|
||||||
match=payload.user_uu_id,
|
match=payload.user_uu_id,
|
||||||
payload={
|
payload={
|
||||||
"password_history": password_history_list,
|
"password_history": payload.password_history,
|
||||||
"access_history_detail": payload.access_history_detail,
|
"access_history_detail": payload.access_history_detail,
|
||||||
"modified_at": system_arrow.to_timestamp(system_arrow.now()),
|
"modified_at": system_arrow.to_timestamp(system_arrow.now()),
|
||||||
},
|
},
|
||||||
field="user_uu_id",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_password_history_via_user(self, user_uu_id):
|
def get_password_history_via_user(self, user_uu_id):
|
||||||
self.use_collection("UserPasswordHistory")
|
|
||||||
result = self.mongo_engine.filter_by(payload={"user_uu_id": user_uu_id})
|
result = self.mongo_engine.filter_by(payload={"user_uu_id": user_uu_id})
|
||||||
return [validate_timestamp(doc) for doc in result.data] if result else None
|
return [self.validate_timestamp(doc) for doc in result.data] if result else None
|
||||||
|
|
||||||
def update_access_history_via_user(self, payload: AccessHistoryViaUser):
|
def update_access_history_via_user(self, payload: AccessHistoryViaUser):
|
||||||
self.use_collection("AccessHistory")
|
|
||||||
if already_dict := self.get_access_history_via_user(
|
if already_dict := self.get_access_history_via_user(
|
||||||
user_uu_id=payload.user_uu_id
|
user_uu_id=payload.user_uu_id
|
||||||
):
|
):
|
||||||
|
|
@ -148,6 +96,16 @@ class MongoQueryIdentity:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_access_history_via_user(self, user_uu_id):
|
def get_access_history_via_user(self, user_uu_id):
|
||||||
self.use_collection("AccessHistory")
|
|
||||||
result = self.mongo_engine.filter_by(payload={"user_uu_id": user_uu_id})
|
result = self.mongo_engine.filter_by(payload={"user_uu_id": user_uu_id})
|
||||||
return [validate_timestamp(doc) for doc in result.data] if result else None
|
return [self.validate_timestamp(doc) for doc in result.data] if result else None
|
||||||
|
|
||||||
|
def validate_timestamp(doc):
|
||||||
|
"""Validate and fix timestamp fields in MongoDB documents"""
|
||||||
|
if not doc:
|
||||||
|
return doc
|
||||||
|
|
||||||
|
timestamp_fields = ["modified_at", "created_at", "accessed_at", "timestamp"]
|
||||||
|
for field in timestamp_fields:
|
||||||
|
if field in doc and not isinstance(doc[field], (int, float)):
|
||||||
|
doc[field] = system_arrow.to_timestamp(doc[field])
|
||||||
|
return doc
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,138 @@
|
||||||
|
from api_library.date_time_actions.date_functions import system_arrow
|
||||||
|
from databases.no_sql_models.mongo_database import MongoQuery
|
||||||
|
from databases.no_sql_models.identity import MongoQueryIdentity
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
found_user: Users = Users.check_user_exits(
|
||||||
|
access_key=data.access_key, domain=data.domain
|
||||||
|
)
|
||||||
|
access_token = found_user.generate_access_token()
|
||||||
|
query_engine = MongoQueryIdentity(company_uuid=found_user.related_company)
|
||||||
|
if found_user.check_password(password=data.password):
|
||||||
|
access_object_to_redis = RedisActions.save_access_token_to_redis(
|
||||||
|
request=request,
|
||||||
|
found_user=found_user,
|
||||||
|
domain=data.domain,
|
||||||
|
access_token=access_token,
|
||||||
|
)
|
||||||
|
refresher_token = found_user.generate_refresher_token(
|
||||||
|
domain=data.domain, remember_me=data.remember_me
|
||||||
|
)
|
||||||
|
headers_request = request.headers
|
||||||
|
headers_request = dict(headers_request)
|
||||||
|
headers_request["evyos-user-agent"] = headers_request.get("user-agent")
|
||||||
|
headers_request["evyos-platform"] = headers_request.get("user-agent")
|
||||||
|
headers_request["evyos-ip-ext"] = "94.54.68.158"
|
||||||
|
# found_user.last_agent = headers_request.get("evyos-user-agent", None)
|
||||||
|
# found_user.last_platform = headers_request.get("evyos-platform", None)
|
||||||
|
# found_user.last_remote_addr = headers_request.get("evyos-ip-ext", None)
|
||||||
|
# found_user.last_seen = str(system_arrow.now())
|
||||||
|
if ext_ip := headers_request.get("evyos-ip-ext"):
|
||||||
|
agent = headers_request.get("evyos-user-agent", "")
|
||||||
|
platform = headers_request.get("evyos-platform", "")
|
||||||
|
address = requests.get(f"http://ip-api.com/json/{ext_ip}").json()
|
||||||
|
address_package = {
|
||||||
|
"city": address["city"],
|
||||||
|
"zip": address["zip"],
|
||||||
|
"country": address["country"],
|
||||||
|
"countryCode": address["countryCode"],
|
||||||
|
"region": address["region"],
|
||||||
|
"regionName": address["regionName"],
|
||||||
|
}
|
||||||
|
mongo_db = MongoQueryIdentity(
|
||||||
|
company_uuid=str(found_user.related_company).replace(" ", ""),
|
||||||
|
storage_reasoning="AccessHistory",
|
||||||
|
)
|
||||||
|
filter_query = {
|
||||||
|
"agent": agent,
|
||||||
|
"platform": platform,
|
||||||
|
"address": address_package,
|
||||||
|
"user_id": found_user.id,
|
||||||
|
}
|
||||||
|
already_exits = mongo_db.mongo_engine.filter_by(filter_query) or None
|
||||||
|
no_address_validates = mongo_db.mongo_engine.get_all()[0] == 0
|
||||||
|
record_id = uuid.uuid4().__str__()
|
||||||
|
notice_link = ApiStatic.blacklist_login(record_id=record_id)
|
||||||
|
found_people = People.filter_one(People.id == found_user.person_id).data
|
||||||
|
access_via_user = query_engine.update_access_history_via_user(
|
||||||
|
AccessHistoryViaUser(
|
||||||
|
**{
|
||||||
|
"user_uu_id": found_user.uu_id.__str__(),
|
||||||
|
"access_history": {
|
||||||
|
"record_id": record_id,
|
||||||
|
"agent": agent,
|
||||||
|
"platform": platform,
|
||||||
|
"address": address_package,
|
||||||
|
"ip": ext_ip,
|
||||||
|
"access_token": access_token,
|
||||||
|
"created_at": system_arrow.now().timestamp(),
|
||||||
|
# "is_confirmed": True if no_address_validates else False,
|
||||||
|
# "is_first": True if no_address_validates else False,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if already_exits:
|
||||||
|
update_mongo = mongo_db.mongo_engine.table.update_one(
|
||||||
|
filter=filter_query,
|
||||||
|
update={
|
||||||
|
"$set": {
|
||||||
|
"ip": ext_ip,
|
||||||
|
"access_token": access_token,
|
||||||
|
"created_at": system_arrow.now().timestamp(),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
mongo_db.mongo_engine.insert(
|
||||||
|
payload={
|
||||||
|
"user_id": found_user.id,
|
||||||
|
"record_id": record_id,
|
||||||
|
"agent": agent,
|
||||||
|
"platform": platform,
|
||||||
|
"address": address_package,
|
||||||
|
"ip": ext_ip,
|
||||||
|
"access_token": access_token,
|
||||||
|
"created_at": system_arrow.now().timestamp(),
|
||||||
|
"is_confirmed": True if no_address_validates else False,
|
||||||
|
"is_first": True if no_address_validates else False,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
found_user.remember_me = bool(data.remember_me)
|
||||||
|
found_user.save()
|
||||||
|
return {
|
||||||
|
"access_token": access_token,
|
||||||
|
"refresher_token": refresher_token,
|
||||||
|
"user": found_user,
|
||||||
|
"access_object": access_object_to_redis,
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
query_engine = MongoQueryIdentity(
|
||||||
|
company_uuid="5f5f4d2b-0b3f-4b1f-8f8a-3b3b7f3b7f3b",
|
||||||
|
)
|
||||||
|
query_engine_mongo = MongoQuery(
|
||||||
|
table_name="5f5f4d2b-0b3f-4b1f-8f8a-3b3b7f3b7f3b*Domain",
|
||||||
|
database_name="mongo_database",
|
||||||
|
)
|
||||||
|
|
||||||
|
payload={
|
||||||
|
"user_uu_id": "5f5f4d2b-0b3f-4b1f-8f8a-3b3b7f3b7f3b",
|
||||||
|
"other_domains_list": ["www.gluglu.com"],
|
||||||
|
"main_domain": "www.gluglu.com",
|
||||||
|
"modified_at": system_arrow.to_timestamp(system_arrow.now()),
|
||||||
|
}
|
||||||
|
query_engine.use_collection("PasswordHistory")
|
||||||
|
or_insert =query_engine.mongo_engine.find_or_insert(
|
||||||
|
payload={
|
||||||
|
"user_uu_id": str(payload.get("user_uu_id")),
|
||||||
|
"password_history": [],
|
||||||
|
},
|
||||||
|
field="user_uu_id",
|
||||||
|
)
|
||||||
|
print('or_insert', or_insert.data)
|
||||||
|
quit()
|
||||||
|
get_all = query_engine_mongo.get_all()
|
||||||
|
print('get_all', get_all.data)
|
||||||
Loading…
Reference in New Issue