Ignore:
Timestamp:
3 Sep 2025, 07:38:18 (15 months ago)
Author:
Henrik Bettermann
Message:

Implement student password expiration feature.

Location:
main/waeup.kofa/trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/CHANGES.txt

    r18163 r18166  
    441.8.2.dev0 (unreleased)
    55=======================
     6
     7* Implement student password expiration feature.
    68
    79* Reject login attempts from invalid sources.
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/interfaces.py

    r13089 r18166  
    189189        )
    190190
     191class IPWExpired(IKofaObject):
     192    """Interface needed for change pasword page after password has expired.
     193
     194    """
     195    identifier = schema.TextLine(
     196        title = _(u'Student Id'),
     197        description = _(
     198            u'Alternatively you can use your Matriculation or Registration Number.'),
     199        required = True,
     200        readonly = False,
     201        )
     202
     203    email = schema.ASCIILine(
     204        title = _(u'Email Address'),
     205        required = True,
     206        constraint=validate_email,
     207        )
     208
    191209class IStudentNavigationBase(IKofaObject):
    192210    """Objects that provide student navigation should
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/pages.py

    r18163 r18166  
    5252    IDepartment, ICourse, ICertificate,
    5353    ICertificateCourse,
    54     ICaptchaManager, IChangePassword)
     54    ICaptchaManager, IChangePassword, IPWExpired)
    5555from waeup.kofa.browser.layout import jsaction, action, UtilityView
    5656from waeup.kofa.interfaces import MessageFactory as _
    … …  
    466466            # Display appropriate flash message if credentials are correct
    467467            # but student has been deactivated or a temporary or parents
    468             # password has been set.
     468            # password has been set or password has expired.
    469469            login = self.request.form['form.login']
    470470            if len(login) == 8 and login in grok.getSite()['students']:
    … …  
    487487                              'because your parents have logged in.'),
    488488                            type='warning')
     489                        return
     490                    # Finally we check if password has expired.
     491                    if student.password_expired:
     492                        self.redirect(self.application_url() + '/pwexpired')
    489493                        return
    490494                    # Now we know that the student is suspended.
    … …  
    29562960            '%s - %s - %s' % (ob_class, data['identifier'], data['email']))
    29572961        return
     2962
     2963
     2964class PasswordExpiredPage(ChangePasswordRequestPage):
     2965
     2966    grok.name('pwexpired')
     2967    grok.template('pwexpired')
     2968    form_fields = grok.AutoFields(IPWExpired)
     2969
     2970    label = _('Your password has expired!')
  • main/waeup.kofa/trunk/src/waeup/kofa/interfaces.py

    r17787 r18166  
    771771        )
    772772
     773    days_pw = schema.Int(
     774        title = _(u'Student password expiration period'),
     775        description = _(u'Number of days after a student password becomes '
     776                         'invalid. Enter 0 if passwords will never expire.'),
     777        default = 0,
     778        readonly = False,
     779        required = False,
     780        )
     781
     782
    773783    captcha = schema.Choice(
    774784        title = _(u'Captcha used for public registration pages'),
  • main/waeup.kofa/trunk/src/waeup/kofa/mandates/tests.py

    r16550 r18166  
    133133        self.assertEqual(redirect_path, '/login?login=K1000000&password=mypwd1')
    134134        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
     135        # Password update time has been set
     136        now = datetime.utcnow()
     137        self.assertEqual((now - student.password_updated).seconds, 0)
    135138        # All mandates have been removed.
    136139        self.assertEqual(len(self.app['mandates'].keys()), 0)
  • main/waeup.kofa/trunk/src/waeup/kofa/students/authentication.py

    r15606 r18166  
    2020"""
    2121import grok
    22 import time
     22from datetime import datetime
    2323from zope.component import getUtility
    2424from zope.password.interfaces import IPasswordManager
    … …  
    109109        passwordmanager = getUtility(IPasswordManager, 'SSHA')
    110110        self.context.password = passwordmanager.encodePassword(password)
     111        self.context.password_updated = datetime.utcnow()
    111112
    112113    def checkPassword(self, password):
    … …  
    114115        students, the temporary password was set by officers or
    115116        the parents_password was set by parents.
    116         We additionally check if student account has been suspended
    117         or if the portal is blocked.
     117        We additionally check if student account has been suspended,
     118        if the portal is blocked or the password has expired.
    118119        """
    119120        try:
    … …  
    137138        if self.context.suspended == True:
    138139            return False
     140        if self.context.password_expired:
     141            return False
    139142        return passwordmanager.checkPassword(self.context.password, password)
    140143
  • main/waeup.kofa/trunk/src/waeup/kofa/students/browser.py

    r18153 r18166  
    481481    form_fields = grok.AutoFields(IStudentBase).omit(
    482482        'password', 'suspended', 'suspended_comment', 'flash_notice')
     483    form_fields[
     484        'password_updated'].custom_widget = FriendlyDatetimeDisplayWidget('le')
    483485    pnav = 4
    484486
    … …  
    656658    grok.require('waeup.manageStudent')
    657659    form_fields = grok.AutoFields(IStudentBase).omit(
    658         'student_id', 'adm_code', 'suspended')
     660        'student_id', 'adm_code', 'suspended', 'password_updated')
    659661    grok.template('basemanagepage')
    660662    label = _('Manage base data')
  • main/waeup.kofa/trunk/src/waeup/kofa/students/export.py

    r17641 r18166  
    354354    grok.name('students')
    355355
    356     fields = tuple(sorted(iface_names(IStudent))) + (
     356    fields = tuple(sorted(iface_names(IStudent, omit=['password_updated']))) + (
    357357        'password', 'state', 'history', 'certcode', 'is_postgrad',
    358358        'current_level', 'current_session', 'entry_session')
  • main/waeup.kofa/trunk/src/waeup/kofa/students/interfaces.py

    r17245 r18166  
    238238    temp_password = Attribute('Dictionary with user name, timestamp and encrypted password')
    239239    parents_password = Attribute('Dictionary with student_id, timestamp and encrypted password')
     240    password_expired = Attribute('Can be used to force students to request a new password')
    240241
    241242    suspended = schema.Bool(
    … …  
    322323        required = False,
    323324        constraint=validate_email,
     325        )
     326
     327    password_updated = schema.Datetime(
     328        title = _(u'Password updated'),
     329        required = False,
     330        readonly = False,
    324331        )
    325332
  • main/waeup.kofa/trunk/src/waeup/kofa/students/student.py

    r17769 r18166  
    143143                    self.student_id, granted=True))
    144144        return None
     145
     146    @property
     147    def password_expired(self):
     148        """Can be used to force students to request a new password.
     149        """
     150        try:
     151            days_pw = grok.getSite()['configuration'].days_pw
     152        except TypeError: # in unit tests
     153            return False
     154        if not days_pw:
     155            return False
     156        now = datetime.utcnow()
     157        if self.password_updated is None:
     158            return True
     159        try:
     160            days_ago = getattr(now - self.password_updated, 'days')
     161        except TypeError:
     162            # This happens after reimport of personal_updated because
     163            # Python  can't subtract offset-naive and offset-aware datetimes
     164            return True
     165        if days_ago > days_pw:
     166            return True
     167        return False
    145168
    146169    def writeLogMessage(self, view, message):
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_browser.py

    r18001 r18166  
    27612761        return
    27622762
     2763    def test_student_password_expired(self):
     2764        # Password update time has been set
     2765        now = datetime.utcnow()
     2766        self.assertEqual((now - self.student.password_updated).days, 0)
     2767        # Set expiration days
     2768        config = grok.getSite()['configuration']
     2769        config.days_pw = 1
     2770        # Student can login
     2771        self.student.personal_updated = datetime.utcnow()
     2772        self.browser.open(self.login_path)
     2773        self.browser.getControl(name="form.login").value = self.student_id
     2774        self.browser.getControl(name="form.password").value = 'spwd'
     2775        self.browser.getControl("Login").click()
     2776        self.assertEqual(self.browser.url, self.student_path)
     2777        self.assertTrue('You logged in' in self.browser.contents)
     2778        self.browser.getLink("Logout").click()
     2779        # Student is requested to change password when old password expires
     2780        delta = timedelta(days=3)
     2781        self.student.password_updated = datetime.utcnow() - delta
     2782        self.assertEqual((now - self.student.password_updated).days, 2)
     2783        self.browser.open(self.login_path)
     2784        self.browser.getControl(name="form.login").value = self.student_id
     2785        self.browser.getControl(name="form.password").value = 'spwd'
     2786        self.browser.getControl("Login").click()
     2787        self.assertEqual(self.browser.url, 'http://localhost/app/pwexpired')
     2788        self.assertTrue(
     2789            'Your password has expired' in self.browser.contents)
     2790        self.assertRaises(
     2791            Unauthorized, self.browser.open, self.student_path)
     2792        return
     2793
    27632794    def test_forbidden_name(self):
    27642795        self.student.lastname = u'<TAG>Tester</TAG>'
  • main/waeup.kofa/trunk/src/waeup/kofa/tests/test_configuration.py

    r17787 r18166  
    125125        result = open(self.outfile, 'rb').read()
    126126        self.assertTrue(
    127             'acronym,carry_over,current_academic_session,email_admin,'
     127            'acronym,carry_over,current_academic_session,days_pw,email_admin,'
    128128            'email_subject,export_disabled_message,frontpage,'
    129129            'maintmode_enabled_by,name,name_admin,next_matric_integer,'
    130130            'next_matric_integer_2,next_matric_integer_3,'
    131131            'next_matric_integer_4,smtp_mailer,curr_stud_id\r\n'
    132             'WAeUP.Kofa,0,,contact@waeup.org,Kofa Contact,,'
     132            'WAeUP.Kofa,0,,0,contact@waeup.org,Kofa Contact,,'
    133133            '"<h1>Welcome to WAeUP.Kofa\n<br>' in result
    134134            )
Note: See TracChangeset for help on using the changeset viewer.