Ignore:
Timestamp:
22 Aug 2013, 21:21:52 (11 years ago)
Author:
Henrik Bettermann
Message:

Call core_user_get_users first and bring order into tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.cas/trunk/waeup/cas/authenticators.py

    r10525 r10526  
    190190    name = 'kofa_moodle1'
    191191
    192     def _create_user(self, username, userdata, moodle):
    193         try:
    194             # Usernames in Moodle must not contain uppercase
    195             # letters even if extendedusernamechars is set True.
    196             # Usernames in Moodle are case insensitive.
    197             # In other words, Moodle changes usernames from
    198             # uppercase to lowercase when you login.
    199             result = moodle.core_user_create_users([
    200                 {'username':username.lower(),
    201                  'password':'dummy',
    202                  'firstname':userdata['firstname'],
    203                  'lastname':userdata['lastname'],
    204                  'email':userdata['email']}])
    205         except xmlrpclib.Fault:
    206             faultstring = sys.exc_info()[1].faultString
    207             if 'Email address already exists' in faultstring:
    208                 return (False,
    209                     'Another Moodle user is using the same email address.'
    210                     ' Email addresses can\'t be used twice in Moodle.')
    211             # If user exists just pass. Unfortunately the error message
    212             # given by Moodle is not always the same.
    213             if not ('Username already exists' in faultstring or
    214                     'Unknown error' in faultstring):
    215                 return (False, faultstring)
     192    def _update_user(self, username, userdata, moodle):
    216193        try:
    217194            result = moodle.core_user_get_users([
     
    220197            faultstring = sys.exc_info()[1].faultString
    221198            return (False, faultstring)
    222         user_id = result['users'][0]['id']
     199        if result['users']:
     200            user_id = result['users'][0]['id']
     201        else:
     202            try:
     203                # Usernames in Moodle must not contain uppercase
     204                # letters even if extendedusernamechars is set True.
     205                # Usernames in Moodle are case insensitive.
     206                # In other words, Moodle changes the username from
     207                # uppercase to lowercase when you login.
     208                result = moodle.core_user_create_users([
     209                    {'username':username.lower(),
     210                     'password':'dummy',
     211                     'firstname':userdata['firstname'],
     212                     'lastname':userdata['lastname'],
     213                     'email':userdata['email']}])
     214            except xmlrpclib.Fault:
     215                faultstring = sys.exc_info()[1].faultString
     216                if 'Email address already exists' in faultstring:
     217                    return (False,
     218                        'Another Moodle user is using the same email address.'
     219                        ' Email addresses can\'t be used twice in Moodle.')
     220                return (False, faultstring)
     221            user_id = result[0]['id']
    223222        # Due to a lack of Moodle (Moodle requires an LDAP
    224223        # connection) the authentication method can't
     
    255254            if principal['type'] == 'student':
    256255                userdata = proxy.get_student_moodle_data(kofa_username)
    257                 return self._create_user(username, userdata, moodle)
     256                return self._update_user(username, userdata, moodle)
    258257            if principal['type'] == 'applicant':
    259258                userdata = proxy.get_applicant_moodle_data(kofa_username)
    260                 return self._create_user(username, userdata, moodle)
     259                return self._update_user(username, userdata, moodle)
    261260            return (False, 'User not eligible')
    262261        return (False, 'Invalid username or password')
Note: See TracChangeset for help on using the changeset viewer.