Changeset 8346 for main/waeup.kofa


Ignore:
Timestamp:
4 May 2012, 20:44:48 (12 years ago)
Author:
Henrik Bettermann
Message:

Implement a ChangePasswordRequestPage? for all portal users (more tests will follow).

Location:
main/waeup.kofa/trunk/src/waeup/kofa
Files:
1 added
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/test_browser.py

    r8311 r8346  
    869869    def test_change_password_request(self):
    870870        self.browser.open('http://localhost/app/sendpw')
    871         self.browser.getControl(name="form.reg_number").value = '1234'
     871        self.browser.getControl(name="form.identifier").value = '1234'
    872872        self.browser.getControl(name="form.email").value = 'aa@aa.ng'
    873         self.browser.getControl("Get new login credentials").click()
     873        self.browser.getControl("Get login credentials").click()
    874874        self.assertTrue('No record found' in self.browser.contents)
    875875        self.applicant.email = 'aa@aa.ng'
     
    877877        notify(grok.ObjectModifiedEvent(self.applicant))
    878878        self.browser.open('http://localhost/app/sendpw')
    879         self.browser.getControl(name="form.reg_number").value = '1234'
     879        self.browser.getControl(name="form.identifier").value = '1234'
    880880        self.browser.getControl(name="form.email").value = 'aa@aa.ng'
    881         self.browser.getControl("Get new login credentials").click()
     881        self.browser.getControl("Get login credentials").click()
    882882        self.assertTrue(
    883883            'An email with your user name and password has been sent'
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/pages.py

    r8176 r8346  
    4545    IUniversity, IFacultiesContainer, IFaculty, IFacultyAdd,
    4646    IDepartment, IDepartmentAdd, ICourse, ICourseAdd, ICertificate,
    47     ICertificateAdd, ICertificateCourse, ICertificateCourseAdd)
     47    ICertificateAdd, ICertificateCourse, ICertificateCourseAdd,
     48    ICaptchaManager)
    4849from waeup.kofa.browser.layout import jsaction, action, UtilityView
    4950from waeup.kofa.browser.resources import warning, datepicker, tabs, datatable
     
    5455    ILocalRolesAssignable, DuplicationError, IConfigurationContainer,
    5556    ISessionConfiguration, ISessionConfigurationAdd,
    56     IPasswordValidator, IContactForm, IKofaUtils, ICSVExporter)
     57    IPasswordValidator, IContactForm, IKofaUtils, ICSVExporter,
     58    IChangePassword)
    5759from waeup.kofa.permissions import get_users_with_local_roles, get_all_roles
    5860from waeup.kofa.students.catalog import search as searchstudents
     
    19571959        return
    19581960
     1961class ChangePasswordRequestPage(KofaForm):
     1962    """Captcha'd page for all kind of users to request a password change.
     1963    """
     1964    grok.context(IUniversity)
     1965    grok.name('sendpw')
     1966    grok.require('waeup.Anonymous')
     1967    grok.template('sendpassword')
     1968    label = _('Send me a new password')
     1969    form_fields = grok.AutoFields(IChangePassword)
     1970
     1971    def update(self):
     1972        # Handle captcha
     1973        self.captcha = getUtility(ICaptchaManager).getCaptcha()
     1974        self.captcha_result = self.captcha.verify(self.request)
     1975        self.captcha_code = self.captcha.display(self.captcha_result.error_code)
     1976        return
     1977
     1978    def _searchUser(self, identifier, email):
     1979        # Search student
     1980        cat = queryUtility(ICatalog, name='students_catalog')
     1981        results = cat.searchResults(
     1982            #reg_number=(identifier, identifier),
     1983            email=(email,email))
     1984        for result in results:
     1985            if result.student_id == identifier or \
     1986                result.reg_number == identifier or \
     1987                result.matric_number == identifier:
     1988                return result
     1989        # Search applicant
     1990        cat = queryUtility(ICatalog, name='applicants_catalog')
     1991        if cat is not None:
     1992            results = cat.searchResults(
     1993                #reg_number=(identifier, identifier),
     1994                email=(email,email))
     1995            for result in results:
     1996                if result.applicant_id == identifier or \
     1997                    result.reg_number == identifier:
     1998                    return result
     1999        # Search portal user
     2000        user = grok.getSite()['users'].get(identifier, None)
     2001        if user is not None and user.email == email:
     2002            return user
     2003        return None
     2004
     2005    @action(_('Get login credentials'), style='primary')
     2006    def request(self, **data):
     2007        if not self.captcha_result.is_valid:
     2008            # Captcha will display error messages automatically.
     2009            # No need to flash something.
     2010            return
     2011        # Search student or applicant
     2012        identifier = data['identifier']
     2013        email = data['email']
     2014        user = self._searchUser(identifier, email)
     2015        if user is None:
     2016            self.flash(_('No record found.'))
     2017            return
     2018        # Change password
     2019        kofa_utils = getUtility(IKofaUtils)
     2020        pwd = kofa_utils.genPassword()
     2021        IUserAccount(user).setPassword(pwd)
     2022        # Send email with new redentials
     2023        msg = _('You have successfully changed your password for the')
     2024        login_url = self.url(grok.getSite(), 'login')
     2025        success = kofa_utils.sendCredentials(
     2026            IUserAccount(user),pwd,login_url,msg)
     2027        if success:
     2028            self.flash(_('An email with your user name and password ' +
     2029                'has been sent to ${a}.', mapping = {'a':email}))
     2030        else:
     2031            self.flash(_('An smtp server error occurred.'))
     2032        return
  • main/waeup.kofa/trunk/src/waeup/kofa/interfaces.py

    r8307 r8346  
    10431043        required = True,
    10441044        )
     1045
     1046class IChangePassword(IKofaObject):
     1047    """Interface needed for change pasword page.
     1048
     1049    """
     1050    identifier = schema.TextLine(
     1051        title = _(u'Unique Identifier'),
     1052        description = _(u'User Name, Student or Applicant Id, Matriculation or Registration Number'),
     1053        required = True,
     1054        readonly = False,
     1055        )
     1056
     1057    email = schema.ASCIILine(
     1058        title = _(u'Email Address'),
     1059        required = True,
     1060        constraint=validate_email,
     1061        )
  • main/waeup.kofa/trunk/src/waeup/kofa/students/browser.py

    r8325 r8346  
    3737    KofaPage, KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage,
    3838    ContactAdminForm, KofaForm, NullValidator)
    39 from waeup.kofa.browser.interfaces import ICaptchaManager
    4039from waeup.kofa.browser.breadcrumbs import Breadcrumb
    4140from waeup.kofa.browser.resources import datepicker, datatable, tabs, warning
     
    5554    IStudentAccommodation, IStudentStudyLevel,
    5655    ICourseTicket, ICourseTicketAdd, IStudentPaymentsContainer,
    57     IStudentOnlinePayment, IBedTicket, IStudentsUtils, IStudentChangePassword
     56    IStudentOnlinePayment, IBedTicket, IStudentsUtils
    5857    )
    5958from waeup.kofa.students.catalog import search
     
    19071906        return
    19081907
    1909 class ChangePasswordRequestPage(KofaForm):
    1910     """Captcha'd page for students to request a password change.
    1911     """
    1912     grok.context(IUniversity)
    1913     grok.name('sendpw')
    1914     grok.require('waeup.Anonymous')
    1915     grok.template('sendpassword')
    1916     label = _('Send me a new password')
    1917     form_fields = grok.AutoFields(IStudentChangePassword)
    1918 
    1919     def update(self):
    1920         # Handle captcha
    1921         self.captcha = getUtility(ICaptchaManager).getCaptcha()
    1922         self.captcha_result = self.captcha.verify(self.request)
    1923         self.captcha_code = self.captcha.display(self.captcha_result.error_code)
    1924         return
    1925 
    1926     @action(_('Get new login credentials'), style='primary')
    1927     def request(self, **data):
    1928         if not self.captcha_result.is_valid:
    1929             # Captcha will display error messages automatically.
    1930             # No need to flash something.
    1931             return
    1932         # Search student or applicant
    1933         reg_number = data['reg_number']
    1934         email = data['email']
    1935         cat = queryUtility(ICatalog, name='students_catalog')
    1936         results = cat.searchResults(
    1937             reg_number=(reg_number, reg_number),
    1938             email=(email,email))
    1939         if len(results) == 0:
    1940             # Try also the applicants_catalog if no student record was found.
    1941             cat = queryUtility(ICatalog, name='applicants_catalog')
    1942             if cat is None:
    1943                 self.flash(_('Application package not installed.'))
    1944                 return
    1945             results = cat.searchResults(
    1946                 reg_number=(reg_number, reg_number),
    1947                 email=(email,email))
    1948         if len(results) == 0:
    1949             self.flash(_('No record found.'))
    1950             return
    1951         student = list(results)[0]
    1952         # Change password
    1953         kofa_utils = getUtility(IKofaUtils)
    1954         pwd = kofa_utils.genPassword()
    1955         IUserAccount(student).setPassword(pwd)
    1956         # Send email with new redentials
    1957         msg = _('You have successfully changed your password for the')
    1958         login_url = self.url(grok.getSite(), 'login')
    1959         success = kofa_utils.sendCredentials(
    1960             IUserAccount(student),pwd,login_url,msg)
    1961         if success:
    1962             self.flash(_('An email with your user name and password ' +
    1963                 'has been sent to ${a}.', mapping = {'a':email}))
    1964         else:
    1965             self.flash(_('An smtp server error occurred.'))
    1966         return
    19671908
    19681909class SetPasswordPage(KofaPage):
  • main/waeup.kofa/trunk/src/waeup/kofa/students/interfaces.py

    r8268 r8346  
    510510IStudentOnlinePayment['p_level'].order = IStudentOnlinePayment[
    511511    'p_session'].order
    512 
    513 
    514 class IStudentChangePassword(IKofaObject):
    515     """Interface needed for change pasword page.
    516 
    517     """
    518     reg_number = schema.TextLine(
    519         title = _(u'Registration Number'),
    520         required = True,
    521         readonly = False,
    522         )
    523 
    524     email = schema.ASCIILine(
    525         title = _(u'Email Address'),
    526         required = True,
    527         constraint=validate_email,
    528         )
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_browser.py

    r8322 r8346  
    16951695    def test_change_password_request(self):
    16961696        self.browser.open('http://localhost/app/sendpw')
    1697         self.browser.getControl(name="form.reg_number").value = '123'
     1697        self.browser.getControl(name="form.identifier").value = '123'
    16981698        self.browser.getControl(name="form.email").value = 'aa@aa.ng'
    1699         self.browser.getControl("Get new login credentials").click()
     1699        self.browser.getControl("Get login credentials").click()
    17001700        self.assertTrue('An email with' in self.browser.contents)
    17011701
Note: See TracChangeset for help on using the changeset viewer.