From 9cb27da9471efb2ad7b643680f4f39581bffddc0 Mon Sep 17 00:00:00 2001 From: Carlos Nihelton Date: Mon, 9 May 2022 15:40:38 -0300 Subject: [PATCH] Makes the TUI validate username with the endpoint. --- subiquity/client/controllers/identity.py | 10 +++++++++- subiquity/ui/views/identity.py | 18 +++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/subiquity/client/controllers/identity.py b/subiquity/client/controllers/identity.py index b0ccda7b..f6bb493b 100644 --- a/subiquity/client/controllers/identity.py +++ b/subiquity/client/controllers/identity.py @@ -16,7 +16,7 @@ import logging from subiquity.client.controller import SubiquityTuiController -from subiquity.common.types import IdentityData +from subiquity.common.types import IdentityData, UsernameValidation from subiquity.ui.views import IdentityView log = logging.getLogger('subiquity.client.controllers.identity') @@ -25,6 +25,7 @@ log = logging.getLogger('subiquity.client.controllers.identity') class IdentityController(SubiquityTuiController): endpoint_name = 'identity' + username_validation = UsernameValidation.OK async def make_ui(self): data = await self.endpoint.GET() @@ -48,3 +49,10 @@ class IdentityController(SubiquityTuiController): "IdentityController.done next_screen user_spec=%s", identity_data) self.app.next_screen(self.endpoint.POST(identity_data)) + + async def _validate_username(self, username): + self.username_validation = \ + await self.endpoint.validate_username.GET(username) + + def validate_username(self, _, value): + self.app.aio_loop.create_task(self._validate_username(value)) diff --git a/subiquity/ui/views/identity.py b/subiquity/ui/views/identity.py index d7faccc4..f6c62c6a 100644 --- a/subiquity/ui/views/identity.py +++ b/subiquity/ui/views/identity.py @@ -35,7 +35,7 @@ from subiquitycore.utils import crypt_password from subiquitycore.view import BaseView from subiquity.common.resources import resource_path -from subiquity.common.types import IdentityData +from subiquity.common.types import IdentityData, UsernameValidation log = logging.getLogger("subiquity.ui.views.identity") @@ -83,9 +83,11 @@ PasswordField = simple_field(PasswordEditor) class IdentityForm(Form): - def __init__(self, reserved_usernames, initial): - self.reserved_usernames = reserved_usernames + def __init__(self, controller, initial): + self.controller = controller super().__init__(initial=initial) + connect_signal(self.username.widget, 'change', + controller.validate_username) realname = RealnameField(_("Your name:")) hostname = UsernameField( @@ -116,6 +118,7 @@ class IdentityForm(Form): def validate_username(self): username = self.username.value + state = self.controller.username_validation if len(username) < 1: return _("Username missing") @@ -128,11 +131,16 @@ class IdentityForm(Form): return _( "Username must match USERNAME_REGEX: " + USERNAME_REGEX) - if username in self.reserved_usernames: + if state == UsernameValidation.SYSTEM_RESERVED: return _( 'The username "{username}" is reserved for use by the system.' ).format(username=username) + if state == UsernameValidation.ALREADY_IN_USE: + return _( + 'The username "{username}" is already in use.' + ).format(username=username) + def validate_password(self): if len(self.password.value) < 1: return _("Password must be set") @@ -182,7 +190,7 @@ class IdentityView(BaseView): 'hostname': identity_data.hostname, } - self.form = IdentityForm(reserved_usernames, initial) + self.form = IdentityForm(controller, initial) connect_signal(self.form, 'submit', self.done) setup_password_validation(self.form, _("passwords"))