Commit 033da3c0 authored by Jérome Perrin's avatar Jérome Perrin

authentication_policy: return messages directly in Login_analyzePassword

parent cc578417
"""
Returns if password is valid or not.
If not valid return a negative code to indicate failure.
Returns the list of messages in case a password does not comply with the policy
"""
from Products.Formulator.Errors import ValidationError
from Products.ERP5Type.Message import translateString
from DateTime import DateTime
import re
MARKER = ['', None]
message_dict = { 0: 'Unknown error',
-1: 'Too short.',
-2: 'Not complex enough.',
-3: 'You have changed your password too recently.',
-4: 'You have already used this password.',
-5: 'You can not use any parts of your first and last name in password.'}
def addError(error_code):
result_code_list.append(translateString(message_dict[error_code]))
portal = context.getPortalObject()
request = context.REQUEST
......@@ -22,7 +30,7 @@ if password is None:
# not long enough
if min_password_length is not None:
if len(password) < min_password_length:
result_code_list.append(-1)
addError(-1)
# password contain X out of following Y regular expression groups ?
regular_expression_list = portal.portal_preferences.getPreferredRegularExpressionGroupList()
......@@ -36,7 +44,7 @@ if regular_expression_list:
#context.log('%s %s %s %s' %(password, group_counter, min_regular_expression_group_number, regular_expression_list))
if group_counter < min_regular_expression_group_number:
# not enough groups match
result_code_list.append(-2)
addError(-2)
if not is_temp_object:
# not changed in last period ?
......@@ -57,13 +65,13 @@ if not is_temp_object:
min_password_lifetime_duration is not None and \
(last_password_modification_date + min_password_lifetime_duration*one_hour) > now:
# too early to change password
result_code_list.append(-3)
addError(-3)
# not already used before ?
preferred_number_of_last_password_to_check = portal.portal_preferences.getPreferredNumberOfLastPasswordToCheck()
if preferred_number_of_last_password_to_check not in [None, 0]:
if context.isPasswordAlreadyUsed(password):
result_code_list.append(-4)
addError(-4)
# not contain the full name of the user in password or any parts of it (i.e. last and / or first name)
if portal.portal_preferences.isPrefferedForceUsernameCheckInPassword():
......@@ -85,6 +93,6 @@ if portal.portal_preferences.isPrefferedForceUsernameCheckInPassword():
if (first_name not in MARKER and first_name in lower_password) or \
(last_name not in MARKER and last_name in lower_password):
# user's name must not be contained in password
result_code_list.append(-5)
addError(-5)
return result_code_list
......@@ -6,19 +6,11 @@ from Products.Formulator.Errors import ValidationError
portal = context.getPortalObject()
message_dict = { 0: 'Unknown error',
-1: 'Too short.',
-2: 'Not complex enough.',
-3: 'You have changed your password too recently.',
-4: 'You have already used this password.',
-5: 'You can not use any parts of your first and last name in password.'}
def doValidation(login, password):
# raise so Formulator shows proper message
result_code_list = login.analyzePassword(password)
if result_code_list!=[]:
translateString = context.Base_translateString
message = ' '.join([translateString(message_dict[x]) for x in result_code_list])
result_message_list = login.analyzePassword(password)
if result_message_list:
message = u' '.join([str(x) for x in result_message_list])
raise ValidationError('external_validator_failed', context, error_text=message)
return 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment