def applyChanges(self, action): # service = IStatusMessage(self.request) data, errors = self.extractData() if errors: # service.add(self.formErrorsMessage, 'error') self.status = self.formErrorsMessage elif data["password"]: self.context.changePassword(data["password"]) # service.add(_('Password has been changed for ${title}.', # mapping = {'title': self.principal_title})) self.status = _("Password has been changed for ${title}.", mapping={"title": self.principal_title})
def update(self): super(ResetPassword, self).update() request = self.request mailer = getUtility(mail.IMailer) self.from_name = mailer.email_from_name self.from_address = mailer.email_from_address if request.params.has_key("button.send"): login = request.params.get("login", "") principal = getUtility(IAuthentication).getUserByLogin(login) if principal is not None: passcode = getUtility(IPasswordTool).generatePasscode(principal) template = ResetPasswordTemplate(principal, request) template.passcode = passcode template.send() view.addMessage(request, _("Your password has been reset and is being emailed to you.")) raise HTTPFound(location=request.application_url) view.addMessage(request, _(u"System can't restore password for this principal."))
def update(self): request = self.request ptool = self.ptool = getUtility(IPasswordTool) passcode = request.params.get("passcode") principal = self.ptool.getPrincipal(passcode) self.info = IUserInfo(principal) if principal is not None: self.passcode = passcode self.principal = principal else: view.addMessage(request, _("Passcode is invalid."), "warning") raise HTTPFound(location="%s/resetpassword.html" % request.application_url) super(ResetPasswordForm, self).update()
def changePassword(self, action): request = self.request data, errors = self.extractData() if errors: view.addMessage(request, self.formErrorsMessage, "error") else: try: self.ptool.resetPassword(self.passcode, data["password"]) except Exception, exc: view.addMessage(request, str(exc), "warning") return user = getUtility(IAuthentication).getUserByLogin(self.info.login) headers = security.remember(request, user.id) view.addMessage(request, _("You have successfully changed your password.")) raise HTTPFound(headers=headers, location=request.application_url)
def validatePassword(self, password): """ >>> import zope.interface.verify >>> from zope import interface, component >>> zope.interface.verify.verifyClass( ... interfaces.IPasswordChecker, DefaultPasswordChecker) True Default password checker uses IDefaultPasswordChecker utility to get configuration. We use controlpanel configlet for this but in this code we should create it. >>> checker = default.DefaultPasswordChecker() >>> checker.min_length = 5 >>> checker.letters_digits = False >>> checker.letters_mixed_case = False >>> zope.interface.verify.verifyObject(IPasswordChecker, checker) True >>> checker.validate('passw') >>> checker.validate('ps1') Traceback (most recent call last): ... LengthPasswordError: ... >>> configlet.min_length = 6 >>> checker.validate('passw') Traceback (most recent call last): ... LengthPasswordError: ... >>> checker.validate('password') >>> configlet.letters_digits = True >>> checker.validate('password') Traceback (most recent call last): ... LettersDigitsPasswordError >>> checker.validate('66665555') Traceback (most recent call last): ... LettersDigitsPasswordError >>> checker.validate('pass5word') >>> configlet.letters_mixed_case = True >>> checker.validate('pass5word') Traceback (most recent call last): ... LettersCasePasswordError >>> checker.validate('PASS5WORD') Traceback (most recent call last): ... LettersCasePasswordError >>> checker.validate('Pass5word') By default password strength is always 100% >>> checker.passwordStrength('Pass5word') 100.0 """ if len(password) < self.min_length: raise exceptions.LengthPasswordError( _("Password should be at least ${count} characters.", mapping={"count": self.min_length}) ) elif self.letters_digits and (password.isalpha() or password.isdigit()): raise exceptions.LettersDigitsPasswordError() elif self.letters_mixed_case and (password.isupper() or password.islower()): raise exceptions.LettersCasePasswordError()
LettersCasePasswordError >>> checker.validate('Pass5word') By default password strength is always 100% >>> checker.passwordStrength('Pass5word') 100.0 """ if len(password) < self.min_length: raise exceptions.LengthPasswordError( _("Password should be at least ${count} characters.", mapping={"count": self.min_length}) ) elif self.letters_digits and (password.isalpha() or password.isdigit()): raise exceptions.LettersDigitsPasswordError() elif self.letters_mixed_case and (password.isupper() or password.islower()): raise exceptions.LettersCasePasswordError() def passwordStrength(self, password): return 100.0 controlpanel.registerConfiglet( "principals.password", IPasswordTool, PasswordTool, _("Password tool"), _("Password managing/generating configuration."), )