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