source: WAeUP_SRP/trunk/PatchCPSUserFolderUserFolder.py @ 2025

Last change on this file since 2025 was 1959, checked in by joachim, 17 years ago

faster method to detect isStudent

File size: 6.3 KB
RevLine 
[880]1from AccessControl import ClassSecurityInfo
2from ExtensionClass import Base
3from Acquisition import Implicit
4from Acquisition import aq_base, aq_parent, aq_inner
[1957]5import re
[880]6security = ClassSecurityInfo()
7
8security.declarePublic('getRolesInContext')
9def getRolesInContext(self, object):
[881]10    """Get the list of roles assigned to the user.
11    This includes local roles assigned in the context of
12    the passed in object.
13    Knows about local roles blocking (roles starting with '-').
14    """
[880]15    name = self.getUserName()
16    roles = self.getRoles()
[881]17    # deal with groups
18    groups = self.getComputedGroups()
19    # end groups
[880]20    local = {}
21    stop_loop = 0
[881]22    real_object = object
[880]23    object = aq_inner(object)
24    while 1:
25        # Collect all roles info
26        lrd = {}
27        local_roles = getattr(object, '__ac_local_roles__', None)
28        if local_roles:
29            if callable(local_roles):
30                local_roles = local_roles() or {}
31            for r in local_roles.get(name, ()):
32                if r:
33                    lrd[r] = None
34        local_group_roles = getattr(object, '__ac_local_group_roles__', None)
35        if local_group_roles:
36            if callable(local_group_roles):
37                local_group_roles = local_group_roles() or {}
38            for g in groups:
39                for r in local_group_roles.get(g, ()):
40                    if r:
41                        lrd[r] = None
42        lr = lrd.keys()
43        # Positive role assertions
44        for r in lr:
45            if r[0] != '-':
46                if not local.has_key(r):
47                    local[r] = 1 # acquired role
48        # Negative (blocking) role assertions
49        for r in lr:
50            if r[0] == '-':
51                r = r[1:]
52                if not r:
53                    # role '-' blocks all acquisition
54                    stop_loop = 1
55                    break
56                if not local.has_key(r):
57                    local[r] = 0 # blocked role
58        if stop_loop:
59            break
[881]60        if hasattr(object, 'aq_parent'):
61            object = aq_inner(object.aq_parent)
[880]62            continue
63        if hasattr(object, 'im_self'):
[881]64            object = aq_inner(object.im_self)
[880]65            continue
66        break
67    roles = list(roles)
68    for r, v in local.items():
69        if v: # only if not blocked
70            roles.append(r)
71    ## patch to assign dynamic roles for WAeUP
[1957]72    while 1:       
73        #if self.isStudent():
74        #    break
75        # if re.match(r'\D\d\d\d\d\d\d',name):
76        #    break
77        #if len(name) != 7:
78        #    break
79        #try:
80        #    int(name[1:])
81        #except:
82        #    break
[1959]83        o = ord(name[1])
84        if o >= 48 and o <= 57:
[1563]85            break
[1576]86        groups = self.portal_membership.getAuthenticatedMember().getGroups()
[1578]87        if not ("ClearanceOfficers" in groups or "CourseAdvisers" in groups):
[1576]88            break
[1035]89        if callable(real_object) and hasattr(real_object,'im_self'):
90            real_object = real_object.im_self
[1547]91
[1563]92        if real_object is None:
93            break
[1035]94        if hasattr(real_object,'portal_type') and\
[1576]95                   real_object.portal_type not in ("Student",
96                                                   "StudentClearance",
97                                                   "StudentStudyLevel"):
[880]98            break
[1607]99
100        # don't test if it is not a proxy
101        #if real_object.portal_type == real_object.meta_type:
102        #    break
103       
[1548]104        # can be later simplified by replacing by students_catalog values - Henrik
105        # getattr works always because of acquisition ?! Henrik
[902]106        sc = getattr(real_object,'study_course',None)
107        if sc is None:
108            break
[1471]109        sc_obj = sc.getContent()
[1549]110        cert_id = sc_obj.study_course
111        res_cert = self.portal_catalog(id = cert_id)
112        if len(res_cert) != 1:
[1471]113            break
[1549]114        certificate_brain = res_cert[0]
115        certificate_obj = certificate_brain.getObject()
116        cert_path = certificate_brain.getPath().split('/')
[1471]117        fac_id = cert_path[-4]
118        dep_id = cert_path[-3]
[1547]119        # temporary self-healing function
[1471]120        # deprecated after reindexing the students_catalog
[1076]121        student_id = self.getStudentId()
122        res = self.students_catalog(id=student_id)
[1066]123        if len(res) != 1:
124            break
[1549]125        st_entry = res[0]
126        if st_entry.faculty != fac_id or\
127           st_entry.department != dep_id or\
128           st_entry.course != cert_id:
129               self.students_catalog.modifyRecord(id = student_id,
[1471]130                                                faculty = fac_id,
131                                                department = dep_id,
[1549]132                                                course = cert_id
[1515]133                                                )
[1549]134        if real_object.portal_type == "StudentStudyLevel":
[1612]135            if real_object.meta_type != "StudentStudyLevel":
136                context_obj = getattr(certificate_obj,real_object.getId(),None)
137            else:
138                context_obj = getattr(certificate_obj,real_object.aq_parent.getId(),None)
[1576]139            if context_obj is None:
[1612]140                #from pdb import set_trace;set_trace()
[1576]141                break
[1581]142            allowed = set(('CourseAdviser', 'SectionManager'))
143        elif real_object.portal_type == "Student" and "CourseAdvisers" in groups:
[1607]144            #we need some special processing since CourseAdvisers are only
[1581]145            #specified per StudyLevel
146            allowed = set(('CourseAdviser', 'SectionManager'))
147            for context_obj in certificate_obj.objectValues():
148                dynamic_roles = set(self.getRolesInContext(context_obj))
149                intersect = dynamic_roles & allowed
150                if intersect:
151                    roles.extend(list(intersect))
152            break
[1549]153        else:
154            res = self.portal_catalog(portal_type="Department",id=dep_id)
[1581]155            allowed = set(('ClearanceOfficer', 'SectionManager'))
[1549]156            if len(res) != 1:
157                break
158            context_obj = res[0].getObject()
[1581]159        dynamic_roles = set(self.getRolesInContext(context_obj))
160        intersect = dynamic_roles & allowed
161        if intersect:
162            roles.extend(list(intersect))
[880]163        break
164    return roles
165
[881]166from Products.CPSUserFolder.CPSUserFolder import CPSUser
167CPSUser.getRolesInContext = getRolesInContext
Note: See TracBrowser for help on using the repository browser.