Co-authored-by: David Rodenkirchen <davidr.develop@gmail.com> Reviewed-on: Vereins-IT/ez-lan-manager#5
107 lines
4.2 KiB
Python
107 lines
4.2 KiB
Python
from rio import Component, TextStyle, Color, TextInput, Button, Text, Rectangle, Column, Row, Spacer, \
|
|
EventHandler
|
|
|
|
from src.ez_lan_manager.services.LocalDataService import LocalDataService, LocalData
|
|
from src.ez_lan_manager.services.UserService import UserService
|
|
from src.ez_lan_manager.types.SessionStorage import SessionStorage
|
|
from src.ez_lan_manager.types.User import User
|
|
|
|
|
|
class LoginBox(Component):
|
|
status_change_cb: EventHandler = None
|
|
TEXT_STYLE = TextStyle(fill=Color.from_hex("02dac5"), font_size=0.9)
|
|
user_name_input_text: str = ""
|
|
password_input_text: str = ""
|
|
user_name_input_is_valid = True
|
|
password_input_is_valid = True
|
|
login_button_is_loading = False
|
|
is_account_locked: bool = False
|
|
|
|
async def _on_login_pressed(self) -> None:
|
|
if await self.session[UserService].is_login_valid(self.user_name_input_text, self.password_input_text):
|
|
user: User = await self.session[UserService].get_user(self.user_name_input_text)
|
|
if not user.is_active:
|
|
self.is_account_locked = True
|
|
return
|
|
self.user_name_input_is_valid = True
|
|
self.password_input_is_valid = True
|
|
self.login_button_is_loading = False
|
|
self.is_account_locked = False
|
|
await self.session[SessionStorage].set_user_id_and_team_member_flag(user.user_id, user.is_team_member)
|
|
token = self.session[LocalDataService].set_session(self.session[SessionStorage])
|
|
self.session[LocalData].stored_session_token = token
|
|
self.session.attach(self.session[LocalData])
|
|
self.status_change_cb()
|
|
else:
|
|
self.user_name_input_is_valid = False
|
|
self.password_input_is_valid = False
|
|
self.login_button_is_loading = False
|
|
self.is_account_locked = False
|
|
self.force_refresh()
|
|
|
|
def build(self) -> Component:
|
|
user_name_input = TextInput(
|
|
text=self.bind().user_name_input_text,
|
|
label="Benutzername",
|
|
accessibility_label="Benutzername",
|
|
min_height=0.5,
|
|
on_confirm=lambda _: self._on_login_pressed(),
|
|
is_valid=self.user_name_input_is_valid
|
|
)
|
|
password_input = TextInput(
|
|
text=self.bind().password_input_text,
|
|
label="Passwort",
|
|
accessibility_label="Passwort",
|
|
is_secret=True,
|
|
on_confirm=lambda _: self._on_login_pressed(),
|
|
is_valid=self.password_input_is_valid
|
|
)
|
|
login_button = Button(
|
|
Text("LOGIN", style=self.TEXT_STYLE, justify="center"),
|
|
shape="rectangle",
|
|
style="minor",
|
|
color="secondary",
|
|
margin_bottom=0.4,
|
|
on_press=self._on_login_pressed
|
|
)
|
|
register_button = Button(
|
|
Text("REG", style=self.TEXT_STYLE, justify="center"),
|
|
shape="rectangle",
|
|
style="minor",
|
|
color="secondary",
|
|
on_press=lambda: self.session.navigate_to("./register")
|
|
)
|
|
forgot_password_button = Button(
|
|
Text("LST PWD", style=self.TEXT_STYLE, justify="center"),
|
|
shape="rectangle",
|
|
style="minor",
|
|
color="secondary",
|
|
on_press=lambda: self.session.navigate_to("./forgot-password")
|
|
)
|
|
|
|
return Rectangle(
|
|
content=Column(
|
|
user_name_input,
|
|
password_input,
|
|
Column(
|
|
Row(
|
|
login_button
|
|
),
|
|
Row(
|
|
register_button,
|
|
Spacer(),
|
|
forgot_password_button,
|
|
proportions=(49, 2, 49)
|
|
),
|
|
margin_bottom=0.5
|
|
),
|
|
Text(text="Dieses Konto\nist gesperrt", style=TextStyle(fill=self.session.theme.danger_color, font_size=0.9 if self.is_account_locked else 0), align_x=0.5),
|
|
spacing=0.4
|
|
),
|
|
fill=Color.TRANSPARENT,
|
|
min_height=8,
|
|
min_width=12,
|
|
align_x=0.5,
|
|
margin_top=0.3,
|
|
margin_bottom=2
|
|
) |