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/src/waeup/kofa/students
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • 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>'
Note: See TracChangeset for help on using the changeset viewer.