Skip to content

Commit

Permalink
Make advancements on the profile page and optimize a few thinks here …
Browse files Browse the repository at this point in the history
…and there + provide small fixes
  • Loading branch information
TheophileDiot committed Aug 27, 2024
1 parent efe8835 commit 8228e60
Show file tree
Hide file tree
Showing 25 changed files with 2,037 additions and 2,880 deletions.
9 changes: 1 addition & 8 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ CODE_OF_CONDUCT.md
LICENSE.md
README.md
SECURITY.md
tsparticles.bundle.min.js
flatpickr.*
src/ui/static/js/lottie-web.min.js
src/ui/static/js/editor/*
src/ui/static/js/utils/purify/*
src/ui/static/json/particles.json
src/ui/templates/*
src/ui/app/templates/*
src/common/core/*/ui/*
datepicker-foundation.css
examples/
4 changes: 2 additions & 2 deletions src/ui/app/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class AnonymousUser(AnonymousUserMixin):
method = "manual"
admin = False
totp_secret = None
creation_date = datetime.now()
update_date = datetime.now()
creation_date = datetime.now().astimezone()
update_date = datetime.now().astimezone()
list_roles = []
list_permissions = []
list_recovery_codes = []
Expand Down
17 changes: 6 additions & 11 deletions src/ui/app/models/ui_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ def init_ui_tables(self, bunkerweb_version: str) -> Tuple[bool, str]:

if db_version != bunkerweb_version:
self.logger.warning(f"UI tables version ({db_version}) is different from BunkerWeb version ({bunkerweb_version}), migrating them ...")
current_time = datetime.now()
current_time = datetime.now().astimezone()
error = True
while error:
try:
metadata = MetaData()
metadata.reflect(self.sql_engine)
error = False
except BaseException as e:
if (datetime.now() - current_time).total_seconds() > 10:
if (datetime.now().astimezone() - current_time).total_seconds() > 10:
raise e
sleep(1)

Expand Down Expand Up @@ -201,7 +201,7 @@ def create_ui_user(
return f"Role {role} doesn't exist"
session.add(RolesUsers(user_name=username, role_name=role))

current_time = datetime.now()
current_time = datetime.now().astimezone()
session.add(
Users(
username=username,
Expand Down Expand Up @@ -263,7 +263,7 @@ def update_ui_user(
user.password = password.decode("utf-8")
user.totp_secret = totp_secret
user.method = method
user.update_date = datetime.now()
user.update_date = datetime.now().astimezone()

try:
session.commit()
Expand Down Expand Up @@ -354,7 +354,7 @@ def create_ui_role(self, name: str, description: str, permissions: List[str]) ->
if session.query(Roles).with_entities(Roles.name).filter_by(name=name).first():
return f"Role {name} already exists"

session.add(Roles(name=name, description=description, update_datetime=datetime.now()))
session.add(Roles(name=name, description=description, update_datetime=datetime.now().astimezone()))

for permission in permissions:
if not session.query(Permissions).with_entities(Permissions.name).filter_by(name=permission).first():
Expand Down Expand Up @@ -479,15 +479,10 @@ def use_ui_user_recovery_code(self, username: str, hashed_code: str) -> str:

return ""

def get_ui_user_last_session(self, username: str) -> Optional[UserSessions]:
"""Get ui user last session."""
with self._db_session() as session:
return session.query(UserSessions).filter_by(user_name=username).order_by(UserSessions.creation_date.desc()).first()

def get_ui_user_sessions(self, username: str) -> List[UserSessions]:
"""Get ui user sessions."""
with self._db_session() as session:
return session.query(UserSessions).filter_by(user_name=username).order_by(UserSessions.creation_date.desc()).limit(10).all()
return session.query(UserSessions).filter_by(user_name=username).order_by(UserSessions.creation_date.desc()).all()

def delete_ui_user_old_sessions(self, username: str) -> str:
"""Delete ui user old sessions."""
Expand Down
3 changes: 2 additions & 1 deletion src/ui/app/routes/bans.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ def get_load_data():
ban_end = float(ban["ban_end"])
except ValueError:
continue
ban_end = (datetime.fromtimestamp(ban_end) - datetime.now()).total_seconds()
current_time = datetime.now().astimezone()
ban_end = (datetime.fromtimestamp(ban_end, tz=current_time.tzinfo) - current_time).total_seconds()

if redis_client:
ok = redis_client.set(f"bans_ip_{ban['ip']}", dumps({"reason": reason, "date": time()}))
Expand Down
10 changes: 4 additions & 6 deletions src/ui/app/routes/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ def login_page():
ui_user = DB.get_ui_user(username=request.form["username"])
if ui_user and ui_user.username == request.form["username"] and ui_user.check_password(request.form["password"]):
# log the user in
session["creation_date"] = datetime.now().astimezone()
session["ip"] = request.remote_addr
session["user_agent"] = request.headers.get("User-Agent")
session["totp_validated"] = False
session["flash_messages"] = []

ret = DB.mark_ui_user_login(
ui_user.username,
datetime.now().astimezone(),
request.remote_addr,
request.headers.get("User-Agent"),
)
ret = DB.mark_ui_user_login(ui_user.username, session["creation_date"], session["ip"], session["user_agent"])
if isinstance(ret, str):
LOGGER.error(f"Couldn't mark the user login: {ret}")
else:
Expand Down
54 changes: 45 additions & 9 deletions src/ui/app/routes/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@

profile = Blueprint("profile", __name__)

BROWSERS = {
"Chrome": "bxl-chrome",
"Firefox": "bxl-firefox",
"Safari": "bx-compass",
"Edge": "bxl-edge",
"Opera": "bxl-opera",
"Internet Explorer": "bxl-internet-explorer",
}

OS = {
"Windows": "bxl-windows",
"Mac": "bxl-apple",
"Linux": "bxl-tux",
}

DEVICES = {
"PC": "bx-desktop",
}


@profile.route("/profile", methods=["GET"])
@login_required
Expand All @@ -23,15 +42,32 @@ def profile_page():
last_sessions = []
for db_session in DB.get_ui_user_sessions(current_user.username):
ua_data = parse(db_session.user_agent)
last_sessions.append(
{
"browser": ua_data.get_browser(),
"os": ua_data.get_os(),
"ip": db_session.ip,
"creation_date": db_session.creation_date.strftime("%Y-%m-%d %H:%M:%S %Z"),
"last_activity": db_session.last_activity.strftime("%Y-%m-%d %H:%M:%S %Z"),
}
)
last_session = {
"current": db_session.id == session.get("session_id"),
"browser": ua_data.get_browser(),
"os": ua_data.get_os(),
"device": ua_data.get_device(),
"ip": db_session.ip,
"creation_date": db_session.creation_date.astimezone().strftime("%Y-%m-%d %H:%M:%S %Z"),
"last_activity": db_session.last_activity.astimezone().strftime("%Y-%m-%d %H:%M:%S %Z"),
}

for browser, icon in BROWSERS.items():
if browser in last_session["browser"]:
last_session["browser"] = f"<i class='bx {icon} text-primary'></i>&nbsp;{last_session['browser']}"
break

for os, icon in OS.items():
if os in last_session["os"]:
last_session["os"] = f"<i class='bx {icon} text-primary'></i>&nbsp;{last_session['os']}"
break

last_session["device"] = f"<i class='bx {DEVICES.get(last_session['device'], 'bx-mobile')} text-primary'></i>&nbsp;{last_session['device']}"

if last_session["current"] and last_sessions:
last_sessions.insert(0, last_session)
continue
last_sessions.append(last_session)

return render_template(
"profile.html",
Expand Down
6 changes: 3 additions & 3 deletions src/ui/app/routes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@


def wait_applying():
current_time = datetime.now()
current_time = datetime.now().astimezone()
ready = False
while not ready and (datetime.now() - current_time).seconds < 120:
while not ready and (datetime.now().astimezone() - current_time).seconds < 120:
db_metadata = DB.get_metadata()
if isinstance(db_metadata, str):
LOGGER.error(f"An error occurred when checking for changes in the database : {db_metadata}")
Expand Down Expand Up @@ -112,7 +112,7 @@ def manage_bunkerweb(method: str, *args, operation: str = "reloads", is_draft: b
flash(f["content"])

if "flash_messages" in session:
session["flash_messages"].append((f["content"], f["type"], datetime.now().strftime("%Y-%m-%d %H:%M:%S %Z")))
session["flash_messages"].append((f["content"], f["type"], datetime.now().astimezone().strftime("%Y-%m-%d %H:%M:%S %Z")))

DATA["TO_FLASH"] = []

Expand Down
Binary file added src/ui/app/static/img/avatar_profil_BW.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8228e60

Please sign in to comment.