Passed
Push — 2.x ( 285083...cd4fe9 )
by Jordi
06:30
created

senaite.core.browser.user.passwordpanel   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 44
dl 0
loc 75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A PasswordPanelAdapter.__init__() 0 2 1
A PasswordPanelAdapter.get_dummy() 0 4 1
A PasswordPanel.validate_password() 0 22 4
1
# -*- coding: utf-8 -*-
2
3
from bika.lims import api
4
from bika.lims import senaiteMessageFactory as _
5
from plone.app.users.browser.passwordpanel import PasswordPanel as Base
6
from plone.app.users.utils import notifyWidgetActionExecutionError
7
from plone.autoform import directives
8
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
9
from zope import schema
10
from zope.interface import Interface
11
12
13
class IPasswordSchema(Interface):
14
    """User Password Schema
15
    """
16
    directives.widget("new_password", klass="form-control form-control-sm")
17
    new_password = schema.Password(
18
        title=_(u"label_new_password", default=u"New password"),
19
        description=_(
20
            u"help_new_password",
21
            default=u"Enter your new password."),
22
    )
23
    directives.widget("new_password_ctl", klass="form-control form-control-sm")
24
    new_password_ctl = schema.Password(
25
        title=_(u"label_confirm_password", default=u"Confirm password"),
26
        description=_(
27
            u"help_confirm_password",
28
            default=u"Re-enter the password. "
29
            u"Make sure the passwords are identical."),
30
    )
31
32
33
class PasswordPanelAdapter(object):
34
    """Data manager for Password
35
    """
36
37
    def __init__(self, context):
38
        self.context = context
39
40
    def get_dummy(self):
41
        """We don't actually need to 'get' anything ...
42
        """
43
        return ""
44
45
    new_password = property(get_dummy)
46
    new_password_ctl = property(get_dummy)
47
48
49
class PasswordPanel(Base):
50
    template = ViewPageTemplateFile("templates/account-panel.pt")
51
    schema = IPasswordSchema
52
53
    def validate_password(self, action, data):
54
        """Validate new password
55
56
        NOTE: We do not check the current password and allow to set a
57
              new password directly
58
        """
59
        registration = api.get_tool("portal_registration")
60
61
        # check if passwords are same and valid according to the PAS plugin
62
        new_password = data.get("new_password")
63
        new_password_ctl = data.get("new_password_ctl")
64
65
        if new_password and new_password_ctl:
66
            err_str = registration.testPasswordValidity(new_password,
67
                                                        new_password_ctl)
68
69
            if err_str:
70
                # add error to new_password widget
71
                notifyWidgetActionExecutionError(action,
72
                                                 "new_password", err_str)
73
                notifyWidgetActionExecutionError(action,
74
                                                 "new_password_ctl", err_str)
75