Index: /main/waeup.sirp/trunk/src/waeup/sirp/students/studycourse.py
===================================================================
--- /main/waeup.sirp/trunk/src/waeup/sirp/students/studycourse.py	(revision 6814)
+++ /main/waeup.sirp/trunk/src/waeup/sirp/students/studycourse.py	(revision 6815)
@@ -21,4 +21,5 @@
 from grok import index
 from zope.component.interfaces import IFactory
+from zope.securitypolicy.interfaces import IPrincipalRoleManager
 from waeup.sirp.students.interfaces import (
     IStudentStudyCourse, IStudentNavigation, IStudentStudyLevel)
@@ -65,2 +66,79 @@
 
 StudentStudyCourse = attrs_to_fields(StudentStudyCourse)
+
+@grok.subscribe(IStudentStudyCourse, grok.IObjectAddedEvent)
+def handle_studycourse_added(obj, event):
+    """When a studycourse is added, update the local roles.
+    """
+    cert = getattr(obj, 'certificate', None)
+    if cert is None:
+        # cert never set yet, no need to update local roles
+        return
+    update_local_roles(obj)
+    return
+
+@grok.subscribe(IStudentStudyCourse, grok.IObjectModifiedEvent)
+def handle_studycourse_modified(obj, event):
+    """When a studycourse is changed, also update the local roles.
+    """
+    update_local_roles(obj)
+    return
+
+def update_local_roles(studycourse):
+    """Update local roles of given studycourse.
+    """
+    student = getattr(studycourse, '__parent__', None)
+    if student is None:
+        # not part of student, no need to update local roles
+        return
+    replace_local_clearance_officers(student)
+    return
+
+def replace_local_clearance_officers(student):
+    """Set local roles of given student.
+
+    The local roles are determined by looking up desired
+    principals. Then we remove those principals from local roles that
+    are not wanted/outdated and add those wanted.
+    """
+    prm = IPrincipalRoleManager(student)
+    curr_officers = prm.getPrincipalsForRole(
+        'waeup.local.ClearanceOfficer')
+    new_officers = get_valid_clearance_officers(student)
+    #print "STUD %s, CURR: %s, NEW: %s" % (student, curr_officers,
+    #                                      new_officers)
+    for officer in curr_officers:
+        if officer in new_officers:
+            continue
+        prm.unsetRoleForPrincipal('waeup.local.ClearanceOfficer', officer)
+    for officer in new_officers:
+        if officer in curr_officers:
+            continue
+        prm.assignRoleToPrincipal('waeup.local.ClearanceOfficer', officer)
+
+    return
+
+def get_valid_clearance_officers(student):
+    """Lookup desired clearance officers for `student`.
+
+    We look up the connected certificate, then its dept. and faculty
+    to see which local roles are required: students get the clearance
+    officers assigned to their certificate.
+
+    Not sure, though, whether we really have to set the real clearance
+    officer role for the wanted principals on the dept and faculty.
+    """
+    cert = getattr(student, 'certificate', None)
+    if cert is None:
+        return []
+    dept = getattr(getattr(cert, '__parent__', None), '__parent__', None)
+    fac = getattr(dept, '__parent__', None)
+    if dept is None or fac is None:
+        # not a serious cert
+        return []
+    # get officers (principals) responsible for the dept and faculty
+    dept_officers = IPrincipalRoleManager(dept).getPrincipalsForRole(
+        'waeup.local.ClearanceOfficer')
+    fac_officers =  IPrincipalRoleManager(fac).getPrincipalsForRole(
+        'waeup.local.ClearanceOfficer')
+    return dept_officers + fac_officers
