2025-07-23 01:45:22 +07:00
|
|
|
"""Module to check if the user is allowed to use the device."""
|
|
|
|
|
2025-04-30 03:42:38 -04:00
|
|
|
import os
|
|
|
|
import glob
|
2025-07-24 21:05:41 +07:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
def sanitize_username(username):
|
|
|
|
"""Sanitize the username."""
|
|
|
|
return re.sub(r"[^a-zA-Z0-9_\-]", "_", username).lower()
|
2025-04-30 03:42:38 -04:00
|
|
|
|
2025-07-22 20:01:22 +07:00
|
|
|
|
2025-04-30 03:42:38 -04:00
|
|
|
def user_allowed_to_use_device(device, username):
|
2025-07-23 01:45:22 +07:00
|
|
|
"""Check if the user is allowed to use the device."""
|
2025-07-24 21:05:41 +07:00
|
|
|
base_path = os.path.join(
|
|
|
|
os.getcwd(), "configs", "CDMs", "users_uploaded", sanitize_username(username)
|
|
|
|
)
|
2025-04-30 03:42:38 -04:00
|
|
|
|
|
|
|
# Get filenames with extensions
|
2025-07-22 20:01:22 +07:00
|
|
|
pr_files = [
|
|
|
|
os.path.basename(f) for f in glob.glob(os.path.join(base_path, "PR", "*.prd"))
|
|
|
|
]
|
|
|
|
wv_files = [
|
|
|
|
os.path.basename(f) for f in glob.glob(os.path.join(base_path, "WV", "*.wvd"))
|
|
|
|
]
|
2025-04-30 03:42:38 -04:00
|
|
|
|
|
|
|
# Combine all filenames
|
|
|
|
all_files = pr_files + wv_files
|
|
|
|
|
|
|
|
# Check if filename matches directly or by adding extensions
|
|
|
|
possible_names = {device, f"{device}.prd", f"{device}.wvd"}
|
|
|
|
|
2025-07-22 20:01:22 +07:00
|
|
|
return any(name in all_files for name in possible_names)
|