Index: main/waeup.kofa/trunk/CHANGES.txt
===================================================================
--- main/waeup.kofa/trunk/CHANGES.txt	(revision 18165)
+++ main/waeup.kofa/trunk/CHANGES.txt	(revision 18166)
@@ -4,4 +4,6 @@
 1.8.2.dev0 (unreleased)
 =======================
+
+* Implement student password expiration feature. 
 
 * Reject login attempts from invalid sources.
Index: main/waeup.kofa/trunk/src/waeup/kofa/browser/interfaces.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/browser/interfaces.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/browser/interfaces.py	(revision 18166)
@@ -189,4 +189,22 @@
         )
 
+class IPWExpired(IKofaObject):
+    """Interface needed for change pasword page after password has expired.
+
+    """
+    identifier = schema.TextLine(
+        title = _(u'Student Id'),
+        description = _(
+            u'Alternatively you can use your Matriculation or Registration Number.'),
+        required = True,
+        readonly = False,
+        )
+
+    email = schema.ASCIILine(
+        title = _(u'Email Address'),
+        required = True,
+        constraint=validate_email,
+        )
+
 class IStudentNavigationBase(IKofaObject):
     """Objects that provide student navigation should
Index: main/waeup.kofa/trunk/src/waeup/kofa/browser/pages.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/browser/pages.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/browser/pages.py	(revision 18166)
@@ -52,5 +52,5 @@
     IDepartment, ICourse, ICertificate,
     ICertificateCourse,
-    ICaptchaManager, IChangePassword)
+    ICaptchaManager, IChangePassword, IPWExpired)
 from waeup.kofa.browser.layout import jsaction, action, UtilityView
 from waeup.kofa.interfaces import MessageFactory as _
@@ -466,5 +466,5 @@
             # Display appropriate flash message if credentials are correct
             # but student has been deactivated or a temporary or parents 
-            # password has been set.
+            # password has been set or password has expired.
             login = self.request.form['form.login']
             if len(login) == 8 and login in grok.getSite()['students']:
@@ -487,4 +487,8 @@
                               'because your parents have logged in.'),
                             type='warning')
+                        return
+                    # Finally we check if password has expired.
+                    if student.password_expired:
+                        self.redirect(self.application_url() + '/pwexpired')
                         return
                     # Now we know that the student is suspended.
@@ -2956,2 +2960,11 @@
             '%s - %s - %s' % (ob_class, data['identifier'], data['email']))
         return
+
+
+class PasswordExpiredPage(ChangePasswordRequestPage):
+
+    grok.name('pwexpired')
+    grok.template('pwexpired')
+    form_fields = grok.AutoFields(IPWExpired)
+
+    label = _('Your password has expired!')
Index: main/waeup.kofa/trunk/src/waeup/kofa/interfaces.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/interfaces.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/interfaces.py	(revision 18166)
@@ -771,4 +771,14 @@
         )
 
+    days_pw = schema.Int(
+        title = _(u'Student password expiration period'),
+        description = _(u'Number of days after a student password becomes '
+                         'invalid. Enter 0 if passwords will never expire.'),
+        default = 0,
+        readonly = False,
+        required = False,
+        )
+
+
     captcha = schema.Choice(
         title = _(u'Captcha used for public registration pages'),
Index: main/waeup.kofa/trunk/src/waeup/kofa/mandates/tests.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/mandates/tests.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/mandates/tests.py	(revision 18166)
@@ -133,4 +133,7 @@
         self.assertEqual(redirect_path, '/login?login=K1000000&password=mypwd1')
         self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
+        # Password update time has been set
+        now = datetime.utcnow()
+        self.assertEqual((now - student.password_updated).seconds, 0)
         # All mandates have been removed.
         self.assertEqual(len(self.app['mandates'].keys()), 0)
Index: main/waeup.kofa/trunk/src/waeup/kofa/students/authentication.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/students/authentication.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/students/authentication.py	(revision 18166)
@@ -20,5 +20,5 @@
 """
 import grok
-import time
+from datetime import datetime
 from zope.component import getUtility
 from zope.password.interfaces import IPasswordManager
@@ -109,4 +109,5 @@
         passwordmanager = getUtility(IPasswordManager, 'SSHA')
         self.context.password = passwordmanager.encodePassword(password)
+        self.context.password_updated = datetime.utcnow()
 
     def checkPassword(self, password):
@@ -114,6 +115,6 @@
         students, the temporary password was set by officers or
         the parents_password was set by parents.
-        We additionally check if student account has been suspended
-        or if the portal is blocked.
+        We additionally check if student account has been suspended,
+        if the portal is blocked or the password has expired.
         """
         try:
@@ -137,4 +138,6 @@
         if self.context.suspended == True:
             return False
+        if self.context.password_expired:
+            return False
         return passwordmanager.checkPassword(self.context.password, password)
 
Index: main/waeup.kofa/trunk/src/waeup/kofa/students/browser.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/students/browser.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/students/browser.py	(revision 18166)
@@ -481,4 +481,6 @@
     form_fields = grok.AutoFields(IStudentBase).omit(
         'password', 'suspended', 'suspended_comment', 'flash_notice')
+    form_fields[
+        'password_updated'].custom_widget = FriendlyDatetimeDisplayWidget('le')
     pnav = 4
 
@@ -656,5 +658,5 @@
     grok.require('waeup.manageStudent')
     form_fields = grok.AutoFields(IStudentBase).omit(
-        'student_id', 'adm_code', 'suspended')
+        'student_id', 'adm_code', 'suspended', 'password_updated')
     grok.template('basemanagepage')
     label = _('Manage base data')
Index: main/waeup.kofa/trunk/src/waeup/kofa/students/export.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/students/export.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/students/export.py	(revision 18166)
@@ -354,5 +354,5 @@
     grok.name('students')
 
-    fields = tuple(sorted(iface_names(IStudent))) + (
+    fields = tuple(sorted(iface_names(IStudent, omit=['password_updated']))) + (
         'password', 'state', 'history', 'certcode', 'is_postgrad',
         'current_level', 'current_session', 'entry_session')
Index: main/waeup.kofa/trunk/src/waeup/kofa/students/interfaces.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/students/interfaces.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/students/interfaces.py	(revision 18166)
@@ -238,4 +238,5 @@
     temp_password = Attribute('Dictionary with user name, timestamp and encrypted password')
     parents_password = Attribute('Dictionary with student_id, timestamp and encrypted password')
+    password_expired = Attribute('Can be used to force students to request a new password')
 
     suspended = schema.Bool(
@@ -322,4 +323,10 @@
         required = False,
         constraint=validate_email,
+        )
+
+    password_updated = schema.Datetime(
+        title = _(u'Password updated'),
+        required = False,
+        readonly = False,
         )
 
Index: main/waeup.kofa/trunk/src/waeup/kofa/students/student.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/students/student.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/students/student.py	(revision 18166)
@@ -143,4 +143,27 @@
                     self.student_id, granted=True))
         return None
+
+    @property
+    def password_expired(self):
+        """Can be used to force students to request a new password.
+        """
+        try:
+            days_pw = grok.getSite()['configuration'].days_pw
+        except TypeError: # in unit tests
+            return False
+        if not days_pw:
+            return False
+        now = datetime.utcnow()
+        if self.password_updated is None:
+            return True
+        try:
+            days_ago = getattr(now - self.password_updated, 'days')
+        except TypeError:
+            # This happens after reimport of personal_updated because
+            # Python  can't subtract offset-naive and offset-aware datetimes
+            return True
+        if days_ago > days_pw:
+            return True
+        return False
 
     def writeLogMessage(self, view, message):
Index: main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_browser.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_browser.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_browser.py	(revision 18166)
@@ -2761,4 +2761,35 @@
         return
 
+    def test_student_password_expired(self):
+        # Password update time has been set
+        now = datetime.utcnow()
+        self.assertEqual((now - self.student.password_updated).days, 0)
+        # Set expiration days
+        config = grok.getSite()['configuration']
+        config.days_pw = 1
+        # Student can login
+        self.student.personal_updated = datetime.utcnow()
+        self.browser.open(self.login_path)
+        self.browser.getControl(name="form.login").value = self.student_id
+        self.browser.getControl(name="form.password").value = 'spwd'
+        self.browser.getControl("Login").click()
+        self.assertEqual(self.browser.url, self.student_path)
+        self.assertTrue('You logged in' in self.browser.contents)
+        self.browser.getLink("Logout").click()
+        # Student is requested to change password when old password expires
+        delta = timedelta(days=3)
+        self.student.password_updated = datetime.utcnow() - delta
+        self.assertEqual((now - self.student.password_updated).days, 2)
+        self.browser.open(self.login_path)
+        self.browser.getControl(name="form.login").value = self.student_id
+        self.browser.getControl(name="form.password").value = 'spwd'
+        self.browser.getControl("Login").click()
+        self.assertEqual(self.browser.url, 'http://localhost/app/pwexpired')
+        self.assertTrue(
+            'Your password has expired' in self.browser.contents)
+        self.assertRaises(
+            Unauthorized, self.browser.open, self.student_path)
+        return
+
     def test_forbidden_name(self):
         self.student.lastname = u'<TAG>Tester</TAG>'
Index: main/waeup.kofa/trunk/src/waeup/kofa/tests/test_configuration.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/tests/test_configuration.py	(revision 18165)
+++ main/waeup.kofa/trunk/src/waeup/kofa/tests/test_configuration.py	(revision 18166)
@@ -125,10 +125,10 @@
         result = open(self.outfile, 'rb').read()
         self.assertTrue(
-            'acronym,carry_over,current_academic_session,email_admin,'
+            'acronym,carry_over,current_academic_session,days_pw,email_admin,'
             'email_subject,export_disabled_message,frontpage,'
             'maintmode_enabled_by,name,name_admin,next_matric_integer,'
             'next_matric_integer_2,next_matric_integer_3,'
             'next_matric_integer_4,smtp_mailer,curr_stud_id\r\n'
-            'WAeUP.Kofa,0,,contact@waeup.org,Kofa Contact,,'
+            'WAeUP.Kofa,0,,0,contact@waeup.org,Kofa Contact,,'
             '"<h1>Welcome to WAeUP.Kofa\n<br>' in result
             )
