Changeset 10517


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

Define private _create_user method which can be reused for applicants.

File:
1 edited

Legend:

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

    r10515 r10517  
    189189    name = 'kofa_moodle1'
    190190
     191    def _create_user(self, username, userdata, moodle):
     192        try:
     193            # Usernames in Moodle must not contain uppercase
     194            # letters even if extendedusernamechars is set True.
     195            result = moodle.core_user_create_users([
     196                {'username':username.lower(),
     197                 'password':'dummy',
     198                 'firstname':userdata['firstname'],
     199                 'lastname':userdata['lastname'],
     200                 'email':userdata['email']}])
     201        except xmlrpclib.Fault:
     202            faultstring = sys.exc_info()[1].faultString
     203            if not 'Username already exists' in faultstring:
     204                return (False, faultstring)
     205        try:
     206            result = moodle.core_user_get_users([
     207                {'key':'username', 'value':username}])
     208        except xmlrpclib.Fault:
     209            faultstring = sys.exc_info()[1].faultString
     210            return (False, faultstring)
     211        user_id = result['users'][0]['id']
     212        # Due to a lack of Moodle (Moodle requires an LDAP
     213        # connection) the authentication method can't
     214        # be set when the user is created. It must be updated
     215        # after creation.
     216        try:
     217            result = moodle.core_user_update_users([
     218                {'id':user_id,'auth':'cas'}])
     219        except xmlrpclib.Fault:
     220            faultstring = sys.exc_info()[1].faultString
     221            return (False, faultstring)
     222        return (True, '')
     223
    191224    def check_credentials(self, username='', password=''):
    192225        """Do the real check.
     
    206239            if principal is not None:
    207240                if principal['type'] == 'student':
    208                     student = proxy.get_student_moodle_data(username)
    209                     try:
    210                         # Usernames in Moodle must not contain uppercase
    211                         # letters even if extendedusernamechars is set True.
    212                         result = moodle.core_user_create_users([
    213                             {'username':username.lower(),
    214                              'password':'dummy',
    215                              'firstname':student['firstname'],
    216                              'lastname':student['lastname'],
    217                              'email':student['email']}])
    218                     except xmlrpclib.Fault:
    219                         faultstring = sys.exc_info()[1].faultString
    220                         if not 'Username already exists' in faultstring:
    221                             return (False, faultstring)
    222                     try:
    223                         result = moodle.core_user_get_users([
    224                             {'key':'username', 'value':username}])
    225                     except xmlrpclib.Fault:
    226                         faultstring = sys.exc_info()[1].faultString
    227                         return (False, faultstring)
    228                     user_id = result['users'][0]['id']
    229                     # Due to a lack of Moodle (Moodle requires an LDAP
    230                     # connection) the authentication method can't
    231                     # be set when the user is created. It must be updated
    232                     # after creation.
    233                     try:
    234                         result = moodle.core_user_update_users([
    235                             {'id':user_id,'auth':'cas'}])
    236                     except xmlrpclib.Fault:
    237                         faultstring = sys.exc_info()[1].faultString
    238                         return (False, faultstring)
    239                 return (True, '')
     241                    userdata = proxy.get_student_moodle_data(username)
     242                    return self._create_user(username, userdata, moodle)
    240243        return (False, 'Invalid username or password.')
Note: See TracChangeset for help on using the changeset viewer.