Skip to content

Commit

Permalink
Add pwpolicy support to TUI interface
Browse files Browse the repository at this point in the history
This adds a new argument to EditTUIDialog so that the policy details can
be shared between the places that need it. root password and user entry
now support pwpolicy user and pwpolicy root settings.
  • Loading branch information
bcl committed Mar 20, 2015
1 parent eebce29 commit f3a8ca6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
38 changes: 28 additions & 10 deletions pyanaconda/ui/tui/spokes/__init__.py
Expand Up @@ -19,14 +19,15 @@
# Red Hat Author(s): Martin Sivak <msivak@redhat.com>
#
from pyanaconda.ui.tui import simpleline as tui
from pyanaconda.ui.tui.tuiobject import TUIObject
from pyanaconda.ui.tui.tuiobject import TUIObject, YesNoDialog
from pyanaconda.ui.common import Spoke, StandaloneSpoke, NormalSpoke
from pyanaconda.users import validatePassword, cryptPassword
import re
from collections import namedtuple
from pyanaconda.iutil import setdeepattr, getdeepattr
from pyanaconda.i18n import N_, _
from pyanaconda.constants import PASSWORD_CONFIRM_ERROR_TUI, PW_ASCII_CHARS
from pyanaconda.constants import PASSWORD_WEAK, PASSWORD_WEAK_WITH_ERROR

__all__ = ["TUISpoke", "EditTUISpoke", "EditTUIDialog", "EditTUISpokeEntry",
"StandaloneSpoke", "NormalTUISpoke"]
Expand Down Expand Up @@ -102,14 +103,19 @@ class EditTUIDialog(NormalTUISpoke):
title = N_("New value")
PASSWORD = re.compile(".*")

def __init__(self, app, data, storage, payload, instclass):
def __init__(self, app, data, storage, payload, instclass, policy_name=""):
if self.__class__ is EditTUIDialog:
raise TypeError("EditTUIDialog is an abstract class")

NormalTUISpoke.__init__(self, app, data, storage, payload, instclass)
self.value = None

def refresh(self, args=None):
# Configure the password policy, if available. Otherwise use defaults.
self.policy = self.data.pwpolicy.get_policy(policy_name)
if not self.policy:
self.policy = self.data.pwpolicy.handler.PwPolicyData()

self._window = []
self.value = None
return True
Expand All @@ -135,19 +141,31 @@ def prompt(self, entry=None):
self.value = ""
return None

valid, strength, message = validatePassword(pw, user=None)
valid, strength, message = validatePassword(pw, user=None, minlen=self.policy.minlen)

if not valid:
print(message)
return None

if strength < 50:
if strength < self.policy.minquality:
if self.policy.strict:
done_msg = ""
else:
done_msg = _("\nWould you like to use it anyway?")

if message:
error = _("You have provided a weak password: %s\n") % message
error = _(PASSWORD_WEAK_WITH_ERROR) % (message, done_msg)
else:
error = _("You have provided a weak password.")
print(error)
return None
error = _(PASSWORD_WEAK) % done_msg

if not self.policy.strict:
question_window = YesNoDialog(self._app, error)
self._app.switch_screen_modal(question_window)
if not question_window.answer:
return None
else:
print(error)
return None

if any(char not in PW_ASCII_CHARS for char in pw):
print(_("You have provided a password containing non-ASCII characters.\n"
Expand Down Expand Up @@ -218,12 +236,12 @@ class EditTUISpoke(NormalTUISpoke):
edit_fields = [
]

def __init__(self, app, data, storage, payload, instclass):
def __init__(self, app, data, storage, payload, instclass, policy_name=""):
if self.__class__ is EditTUISpoke:
raise TypeError("EditTUISpoke is an abstract class")

NormalTUISpoke.__init__(self, app, data, storage, payload, instclass)
self.dialog = OneShotEditTUIDialog(app, data, storage, payload, instclass)
self.dialog = OneShotEditTUIDialog(app, data, storage, payload, instclass, policy_name=policy_name)

# self.args should hold the object this Spoke is supposed
# to edit
Expand Down
4 changes: 2 additions & 2 deletions pyanaconda/ui/tui/spokes/password.py
Expand Up @@ -33,7 +33,7 @@ class PasswordSpoke(FirstbootSpokeMixIn, EditTUIDialog):
category = UserSettingsCategory

def __init__(self, app, data, storage, payload, instclass):
EditTUIDialog.__init__(self, app, data, storage, payload, instclass)
EditTUIDialog.__init__(self, app, data, storage, payload, instclass, "root")
self._password = None

@property
Expand All @@ -42,7 +42,7 @@ def completed(self):

@property
def showable(self):
return not (self.completed and flags.automatedInstall)
return not (self.completed and flags.automatedInstall and not self.policy.changesok)

@property
def mandatory(self):
Expand Down
5 changes: 3 additions & 2 deletions pyanaconda/ui/tui/spokes/user.py
Expand Up @@ -66,7 +66,7 @@ def should_run(cls, environment, data):

def __init__(self, app, data, storage, payload, instclass):
FirstbootSpokeMixIn.__init__(self)
EditTUISpoke.__init__(self, app, data, storage, payload, instclass)
EditTUISpoke.__init__(self, app, data, storage, payload, instclass, "user")

if self.data.user.userList:
self.args = self.data.user.userList[0]
Expand Down Expand Up @@ -99,7 +99,8 @@ def completed(self):

@property
def showable(self):
return not (self.completed and flags.automatedInstall)
return not (self.completed and flags.automatedInstall
and self.data.user.seen and not self.dialog.policy.changesok)

@property
def mandatory(self):
Expand Down

0 comments on commit f3a8ca6

Please sign in to comment.