From d8e0026df5888af8e7bdf4a6ae5c96c2b5e4d787 Mon Sep 17 00:00:00 2001 From: Didier Roche Date: Tue, 13 Jul 2021 14:35:40 +0200 Subject: [PATCH] WSL identity controller and view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This controller and view inherits from the subiquity identity, but remove the "hostname" field. We are using composition over inheritance for identityController as the UI builder is basing on the order of declared elements as class variable and we can’t override that in the metaclass when inheriting. --- system_setup/client/__init__.py | 14 ++++ system_setup/client/controllers/identity.py | 38 ++++++++++ system_setup/ui/__init__.py | 14 ++++ system_setup/ui/views/__init__.py | 20 +++++ system_setup/ui/views/identity.py | 82 +++++++++++++++++++++ 5 files changed, 168 insertions(+) create mode 100644 system_setup/client/__init__.py create mode 100644 system_setup/client/controllers/identity.py create mode 100644 system_setup/ui/__init__.py create mode 100644 system_setup/ui/views/__init__.py create mode 100644 system_setup/ui/views/identity.py diff --git a/system_setup/client/__init__.py b/system_setup/client/__init__.py new file mode 100644 index 00000000..8290406c --- /dev/null +++ b/system_setup/client/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2021 Canonical, Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . diff --git a/system_setup/client/controllers/identity.py b/system_setup/client/controllers/identity.py new file mode 100644 index 00000000..0680792a --- /dev/null +++ b/system_setup/client/controllers/identity.py @@ -0,0 +1,38 @@ +# Copyright 2015-2021 Canonical, Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import logging + +from subiquity.client.controllers import IdentityController +from subiquity.common.types import IdentityData +from system_setup.ui.views import WSLIdentityView + +log = logging.getLogger('system_setup.client.controllers.identity') + + +class WSLIdentityController(IdentityController): + + async def make_ui(self): + data = await self.endpoint.GET() + return WSLIdentityView(self, data) + + def run_answers(self): + if all(elem in self.answers for elem in + ['realname', 'username', 'password']): + identity = IdentityData( + realname=self.answers['realname'], + username=self.answers['username'], + crypted_password=self.answers['password']) + self.done(identity) diff --git a/system_setup/ui/__init__.py b/system_setup/ui/__init__.py new file mode 100644 index 00000000..8290406c --- /dev/null +++ b/system_setup/ui/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2021 Canonical, Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . diff --git a/system_setup/ui/views/__init__.py b/system_setup/ui/views/__init__.py new file mode 100644 index 00000000..45ffae82 --- /dev/null +++ b/system_setup/ui/views/__init__.py @@ -0,0 +1,20 @@ +# Copyright 2021 Canonical, Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from .identity import WSLIdentityView + +__all__ = [ + 'WSLIdentityView', +] \ No newline at end of file diff --git a/system_setup/ui/views/identity.py b/system_setup/ui/views/identity.py new file mode 100644 index 00000000..979b05ec --- /dev/null +++ b/system_setup/ui/views/identity.py @@ -0,0 +1,82 @@ +# Copyright 2021 Canonical, Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from urwid import ( + connect_signal, + ) + + +from subiquity.common.types import IdentityData +from subiquity.ui.views.identity import IdentityForm, IdentityView, setup_password_validation +from subiquitycore.ui.form import Form +from subiquitycore.ui.utils import screen +from subiquitycore.utils import crypt_password +from subiquitycore.view import BaseView + + +class WSLIdentityForm(Form): + + realname = IdentityForm.realname + username = IdentityForm.username + password = IdentityForm.password + confirm_password = IdentityForm.confirm_password + + def __init__(self, initial): + self.identityForm = IdentityForm([], initial) + super().__init__(initial=initial) + + def validate_realname(self): + self.identityForm.validate_realname() + + def validate_username(self): + self.identityForm.validate_username() + + def validate_password(self): + self.identityForm.validate_password() + + def validate_confirm_password(self): + self.identityForm.validate_confirm_password() + + +class WSLIdentityView(BaseView): + title = IdentityView.title + excerpt = _("Enter the username and password you will use to log in.") + + def __init__(self, controller, identity_data): + self.controller = controller + + initial = { + 'realname': identity_data.realname, + 'username': identity_data.username, + } + + self.form = WSLIdentityForm(initial) + + connect_signal(self.form, 'submit', self.done) + setup_password_validation(self.form, _("passwords")) + + super().__init__( + screen( + self.form.as_rows(), + [self.form.done_btn], + excerpt=_(self.excerpt), + focus_buttons=False)) + + def done(self, result): + self.controller.done(IdentityData( + realname=self.form.realname.value, + username=self.form.username.value, + crypted_password=crypt_password(self.form.password.value), + ))