Ejemplo n.º 1
0
    def clean(self):
        cleaned_data = self.cleaned_data
        email = cleaned_data.get('email')
        email_confirm = cleaned_data.get('email_confirm')
        first_name = cleaned_data.get('first_name')
        last_name = cleaned_data.get('last_name')
        password = cleaned_data.get('choose_password')
        confirm_password = cleaned_data.get('confirm_password')

        #make sure email and email confirm match
        if email != email_confirm:
            msg = u'Confirmation Email does not match Email.  Please try again.'
            self._errors["email"] = self.error_class("")
            self._errors["email_confirm"] = self.error_class([msg])
            raise forms.ValidationError(msg)

            del cleaned_data["email"]
            del cleaned_data["email_confirm"]

        #require a harvard email address HAVING A HARVARD EMAIL ADDRESS IS NOT NECESSARY
        #if email and not email.lower().endswith('harvard.edu'):
        #    msg = u'Email must end with "harvard.edu".'
        #    self._errors["email"] = self.error_class([msg])
        #    self._errors["email_confirm"] = self.error_class("")
        #    raise forms.ValidationError(msg)
        #    
        #    del cleaned_data["email"]
        #    del cleaned_data["email_confirm"]            

        #check password matches
        if password != confirm_password:
            msg = u'Your passwords don\'t match.  Please retype your password.'
            self._errors["choose_password"] = self.error_class([msg])
            raise forms.ValidationError(msg)
            
            del password
            del confirm_password

        #check that password is complex
        min_password_length = 8
        special_char_set = set(c for c in '~!@#$%^&*()_+')
        number_char_set = set(c for c in '1234567890')

        if ((len(password) < min_password_length) or #too short
            (password == password.lower()) or #all lowercase
            (password == password.upper()) or #all uppercase
            (not any(passchar in special_char_set for passchar in password)) or #no special chars
            (not any(passchar in number_char_set for passchar in password)) #no numbers
            ): 
            msg = u'Passwords must be at least %s characters in length, contain UPPERCASE letters, lowercase letters, at least one special ch@racter and at least 1 number.' % str(min_password_length)
            self._errors["choose_password"] = self.error_class([msg])
            #raise forms.ValidationError(msg)
            
            del password
            del confirm_password

        #check if user is already in AD
        ldap = LdapConnection()
        #search by email
        email_search = ldap.search_by_email(email)
        #search by first and last name
        name_search = ldap.search_by_firstname_lastname(first_name, last_name)

        if email_search:
            msg = ""
            for name in email_search:
                msg += '{0} {1} ({2}) already has an RC account.<br />  If you have forgotten your password and need it to be reset, please <a href="mailto:[email protected]?subject=\'Password Reset Request for {0} {1}\'">send an email to RCHelp</a>.<br />'.format(name[1]['givenName'][0], name[1]['sn'][0], name[1]['mail'][0])
            msg = mark_safe(msg)
            raise forms.ValidationError(msg)

        if name_search:
            msg = ""
            for name in name_search:
                msg += '{0} {1} ({2}) already has an RC account.<br />  If you have forgotten your password and need it to be reset, please <a href="mailto:[email protected]?subject=\'Password Reset Request for {0}\'">send an email to RCHelp</a>.<br />'.format(name[1]['givenName'][0], name[1]['sn'][0], name[1]['mail'][0])
            msg = mark_safe(msg)
            raise forms.ValidationError(msg)

        ldap.unbind()

        return cleaned_data
Ejemplo n.º 2
0
    def clean(self):
        cleaned_data = self.cleaned_data
        name = cleaned_data.get('name')

        # if the user hasn't selected a lab group from the drop-down list, make sure they have provided all the other fields
        if not name:
            email = cleaned_data.get('email')
            first_name = cleaned_data.get('first_name')
            last_name = cleaned_data.get('last_name')
            email = cleaned_data.get('email')
            phone = cleaned_data.get('phone')
            mailing_address = cleaned_data.get('mailing_address')

            if not email:
                msg = u'Please provide the Faculty Sponsor\'s email address.'
                self._errors["email"] = self.error_class([msg])
                raise forms.ValidationError(msg)

            #require a harvard email address, 
            #might remove later if ldap search is authoritative
            if not email.lower().endswith('harvard.edu'):
                msg = u'Email must end with "harvard.edu".'
                self._errors["email"] = self.error_class([msg])
                raise forms.ValidationError(msg)
                del cleaned_data["email"]

            if not first_name:
                msg = u'Please provide the Faculty Sponsor\'s first name.'
                self._errors["first_name"] = self.error_class([msg])
                raise forms.ValidationError(msg)

            if not last_name:
                msg = u'Please provide the Faculty Sponsor\'s last name.'
                self._errors["last_name"] = self.error_class([msg])
                raise forms.ValidationError(msg)

            if not phone:
                msg = u'Please provide the Faculty Sponsor\'s phone number.'
                self._errors["phone"] = self.error_class([msg])
                raise forms.ValidationError(msg)

            #check if PI is already in the Lab Group list
            pi_search = PIUser.objects.filter(first_name__iexact=first_name, last_name__iexact=last_name)
            if pi_search.count():
                pi = pi_search[0]
                if pi.labgroup_set.all().count():
                    lab_group = pi.labgroup_set.all()[0]
                    msg = u'The Faculty Sponsor you have added is already in the drop-down menu under "%s".' % (lab_group)
                    self._errors["in_list"] = self.error_class([msg])
                    raise forms.ValidationError(msg)
                else:
                    msg = u'The Faculty Sponsor you have entered is already in the system, but they are not associated with a lab group.<br />'
                    msg += u'Please contact <a href="mailto:[email protected]?subject=\'Missing lab for PI %s %s\'">RCHelp</a> for assistance.' % (pi.first_name, pi.last_name)
                    msg = mark_safe(msg)
                    self._errors["in_list"] = self.error_class([msg])
                    raise forms.ValidationError(msg)
            
            #check if PI is not in AD
            ad_result = []
            ldap = LdapConnection()
            #search by email
            email_search = ldap.search_by_email(email)
            #search by first and last name
            name_search = ldap.search_by_firstname_lastname(first_name, last_name)
            ldap.unbind()

            #usernames for piusers must be unique, so check to see if they already have an account
            if email_search:
                msg = ""
                for name in email_search:
                    msg += '{0} {1} ({2}) already has an RC account.<br />  Please <a href="mailto:[email protected]?subject=\'Missing lab group for  {0} {1}\'">send an email to RCHelp</a>.<br />'.format(name[1]['givenName'][0], name[1]['sn'][0], name[1]['mail'][0])
                msg = mark_safe(msg)
                raise forms.ValidationError(msg)
            
            if name_search:
                msg = ""
                for name in name_search:
                    msg += '{0} {1} ({2}) already has an RC account.<br />  Please <a href="mailto:[email protected]?subject=\'Missing lab group for  {0} {1}\'">send an email to RCHelp</a>.<br />'.format(name[1]['givenName'][0], name[1]['sn'][0], name[1]['mail'][0])
                msg = mark_safe(msg)
                raise forms.ValidationError(msg)

        else:
            #if an item from the drop-down menu has been selected, use the pi info from the lab group
            cleaned_data['username'] = name.pi.username
            cleaned_data['password'] = name.pi.password
            cleaned_data['first_name'] = name.pi.first_name
            cleaned_data['last_name'] = name.pi.last_name
            cleaned_data['email'] = name.pi.email
            cleaned_data['phone'] = name.pi.phone
            cleaned_data['mailing_address'] = name.pi.mailing_address

        return cleaned_data