Changeset 9001 for main/waeup.kofa


Ignore:
Timestamp:
13 Jul 2012, 16:17:44 (12 years ago)
Author:
Henrik Bettermann
Message:

Update local roles by import. Test if ignore and deltion marker are doing what they are supposed to do.

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

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/batching.py

    r8995 r9001  
    184184
    185185        # Update password
     186        # XXX: Tale DELETION_MARKER into consideration
    186187        if row.has_key('password'):
    187188            passwd = row.get('password', IGNORE_MARKER)
  • main/waeup.kofa/trunk/src/waeup/kofa/university/batching.py

    r8996 r9001  
    3030from zope.catalog.interfaces import ICatalog
    3131from zope.event import notify
    32 from zope.securitypolicy.interfaces import IPrincipalRoleManager
     32from zope.securitypolicy.interfaces import (
     33    IPrincipalRoleManager, IPrincipalRoleMap)
    3334from waeup.kofa.authentication import LocalRoleSetEvent
    34 from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER, FatalCSVError
     35from waeup.kofa.interfaces import (
     36    IBatchProcessor, IGNORE_MARKER, DELETION_MARKER, FatalCSVError)
    3537from waeup.kofa.university.interfaces import (
    3638    IFacultiesContainer, IFaculty, ICourse, IDepartment, ICertificate,
     
    9597        items_changed = ''
    9698
    97         if row.has_key('local_roles'):
    98             local_roles = eval(row['local_roles'])
    99             for rolemap in local_roles:
    100                 user = rolemap['user_name']
    101                 local_role = rolemap['local_role']
    102                 role_manager = IPrincipalRoleManager(obj)
    103                 role_manager.assignRoleToPrincipal(local_role, user)
    104                 notify(LocalRoleSetEvent(obj, local_role, user, granted=True))
    105                 items_changed += (
    106                     '%s=%s, ' % ('local_roles', '%s|%s' % (user,local_role)))
     99        if row.has_key('local_roles') and row['local_roles'] not in (
     100            None, IGNORE_MARKER):
     101            role_manager = IPrincipalRoleManager(obj)
     102            role_map = IPrincipalRoleMap(obj)
     103            # Remove all existing local roles.
     104            for local_role, user_name, setting in role_map.getPrincipalsAndRoles():
     105                role_manager.unsetRoleForPrincipal(local_role, user_name)
     106                notify(LocalRoleSetEvent(
     107                        obj, local_role, user_name, granted=False))
     108            # Add new local roles.
     109            if row['local_roles'] != DELETION_MARKER:
     110                local_roles = eval(row['local_roles'])
     111                for rolemap in local_roles:
     112                    user = rolemap['user_name']
     113                    local_role = rolemap['local_role']
     114                    role_manager.assignRoleToPrincipal(local_role, user)
     115                    notify(LocalRoleSetEvent(obj, local_role, user, granted=True))
     116                    items_changed += (
     117                        '%s=%s, ' % ('local_roles', '%s|%s' % (user,local_role)))
    107118            row.pop('local_roles')
    108119
     
    122133            FacultyProcessor, self).checkConversion(row, mode=mode)
    123134        if row.has_key('local_roles'):
     135            if row['local_roles'] in (None, DELETION_MARKER, IGNORE_MARKER):
     136                return errs, inv_errs, conv_dict
    124137            try:
    125138                local_roles = eval(row['local_roles'])
  • main/waeup.kofa/trunk/src/waeup/kofa/university/tests/test_batching.py

    r9000 r9001  
    2424from zope.component.hooks import setSite, clearSite
    2525from zope.component import createObject
    26 from zope.securitypolicy.interfaces import IPrincipalRoleMap
     26from zope.securitypolicy.interfaces import (
     27    IPrincipalRoleMap, IPrincipalRoleManager)
    2728from zope.interface.verify import verifyClass, verifyObject
    2829from waeup.kofa.interfaces import IBatchProcessor
     
    4243
    4344FACULTY_HEADER_FIELDS = FACULTY_SAMPLE_DATA.split(
     45    '\n')[0].split(',')
     46
     47FACULTY_SAMPLE_DATA_UPDATE = open(
     48    os.path.join(os.path.dirname(__file__), 'sample_faculty_data_update.csv'),
     49    'rb').read()
     50
     51FACULTY_HEADER_FIELDS_UPDATE = FACULTY_SAMPLE_DATA_UPDATE.split(
    4452    '\n')[0].split(',')
    4553
     
    108116        self.csv_file_faculty = os.path.join(self.workdir, 'sample_faculty_data.csv')
    109117        open(self.csv_file_faculty, 'wb').write(FACULTY_SAMPLE_DATA)
     118        self.csv_file_faculty_update = os.path.join(self.workdir, 'sample_faculty_data_update.csv')
     119        open(self.csv_file_faculty_update, 'wb').write(FACULTY_SAMPLE_DATA_UPDATE)
    110120        return
    111121
     
    214224        self.assertEqual(user_name, 'bob')
    215225        self.assertEqual(local_role, 'waeup.local.DepartmentManager')
     226        shutil.rmtree(os.path.dirname(fin_file))
     227        return
     228
     229    def test_import_update(self):
     230        self.app['faculties']['FAC2'] = Faculty(code='FAC2')
     231        self.app['faculties']['FAC3'] = Faculty(code='FAC3')
     232        self.app['faculties']['FAC4'] = Faculty(code='FAC4')
     233
     234        role_manager1 = IPrincipalRoleManager(self.app['faculties']['FAC1'])
     235        role_manager1.assignRoleToPrincipal('alfonsrole', 'alfons')
     236        role_map1 = IPrincipalRoleMap(self.app['faculties']['FAC1'])
     237        self.assertEqual(len(role_map1.getPrincipalsAndRoles()), 1)
     238
     239        role_manager2 = IPrincipalRoleManager(self.app['faculties']['FAC2'])
     240        role_manager2.assignRoleToPrincipal('alfonsrole', 'alfons')
     241        role_map2 = IPrincipalRoleMap(self.app['faculties']['FAC2'])
     242        self.assertEqual(len(role_map2.getPrincipalsAndRoles()), 1)
     243
     244        role_manager3 = IPrincipalRoleManager(self.app['faculties']['FAC3'])
     245        role_manager3.assignRoleToPrincipal('alfonsrole', 'alfons')
     246        role_map3 = IPrincipalRoleMap(self.app['faculties']['FAC3'])
     247        self.assertEqual(len(role_map3.getPrincipalsAndRoles()), 1)
     248
     249        role_manager4 = IPrincipalRoleManager(self.app['faculties']['FAC4'])
     250        role_manager4.assignRoleToPrincipal('alfonsrole', 'alfons')
     251        role_map4 = IPrincipalRoleMap(self.app['faculties']['FAC4'])
     252        self.assertEqual(len(role_map4.getPrincipalsAndRoles()), 1)
     253
     254        local_role, user_name, setting = role_map2.getPrincipalsAndRoles()[0]
     255        self.assertEqual(user_name, 'alfons')
     256        self.assertEqual(local_role, 'alfonsrole')
     257
     258        num, num_warns, fin_file, fail_file = self.proc.doImport(
     259            self.csv_file_faculty_update, FACULTY_HEADER_FIELDS_UPDATE, 'update')
     260        self.assertEqual(num_warns,0)
     261        # Local roles have been removed in FAC1 due to deletion marker.
     262        self.assertEqual(len(role_map1.getPrincipalsAndRoles()), 0)
     263        # Old local roles have been removed and new roles have been added in FAC2.
     264        self.assertEqual(len(role_map2.getPrincipalsAndRoles()), 1)
     265        local_role, user_name, setting = role_map2.getPrincipalsAndRoles()[0]
     266        self.assertEqual(user_name, 'bob')
     267        self.assertEqual(local_role, 'waeup.local.DepartmentManager')
     268        # Local roles are not touched in FAC3 due to ignore marker.
     269        self.assertEqual(len(role_map3.getPrincipalsAndRoles()), 1)
     270        local_role, user_name, setting = role_map3.getPrincipalsAndRoles()[0]
     271        self.assertEqual(user_name, 'alfons')
     272        self.assertEqual(local_role, 'alfonsrole')
     273        # Local roles are not touched in FAC4 due to empty cell.
     274        self.assertEqual(len(role_map4.getPrincipalsAndRoles()), 1)
     275        local_role, user_name, setting = role_map4.getPrincipalsAndRoles()[0]
     276        self.assertEqual(user_name, 'alfons')
     277        self.assertEqual(local_role, 'alfonsrole')
    216278        shutil.rmtree(os.path.dirname(fin_file))
    217279        return
Note: See TracChangeset for help on using the changeset viewer.