[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 |
---|
| 5 | |
---|
| 6 | security = ClassSecurityInfo() |
---|
| 7 | |
---|
| 8 | security.declarePublic('getRolesInContext') |
---|
| 9 | def 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 |
---|
| 72 | while 1: |
---|
[1563] | 73 | if self.isStudent(): |
---|
| 74 | break |
---|
[1035] | 75 | if callable(real_object) and hasattr(real_object,'im_self'): |
---|
| 76 | real_object = real_object.im_self |
---|
[1547] | 77 | |
---|
[1563] | 78 | if real_object is None: |
---|
| 79 | break |
---|
[1035] | 80 | if hasattr(real_object,'portal_type') and\ |
---|
[1548] | 81 | real_object.portal_type not in ("Student","StudentClearance","StudentStudyLevel"): |
---|
[880] | 82 | break |
---|
[1547] | 83 | |
---|
[1564] | 84 | #from pdb import set_trace;set_trace() |
---|
[1548] | 85 | # can be later simplified by replacing by students_catalog values - Henrik |
---|
| 86 | # getattr works always because of acquisition ?! Henrik |
---|
[902] | 87 | sc = getattr(real_object,'study_course',None) |
---|
| 88 | if sc is None: |
---|
| 89 | break |
---|
[1471] | 90 | sc_obj = sc.getContent() |
---|
[1549] | 91 | cert_id = sc_obj.study_course |
---|
| 92 | res_cert = self.portal_catalog(id = cert_id) |
---|
| 93 | if len(res_cert) != 1: |
---|
[1471] | 94 | break |
---|
[1549] | 95 | certificate_brain = res_cert[0] |
---|
| 96 | certificate_obj = certificate_brain.getObject() |
---|
| 97 | cert_path = certificate_brain.getPath().split('/') |
---|
[1471] | 98 | fac_id = cert_path[-4] |
---|
| 99 | dep_id = cert_path[-3] |
---|
[1547] | 100 | # temporary self-healing function |
---|
[1471] | 101 | # deprecated after reindexing the students_catalog |
---|
[1076] | 102 | student_id = self.getStudentId() |
---|
| 103 | res = self.students_catalog(id=student_id) |
---|
[1066] | 104 | if len(res) != 1: |
---|
| 105 | break |
---|
[1549] | 106 | st_entry = res[0] |
---|
| 107 | if st_entry.faculty != fac_id or\ |
---|
| 108 | st_entry.department != dep_id or\ |
---|
| 109 | st_entry.course != cert_id: |
---|
| 110 | self.students_catalog.modifyRecord(id = student_id, |
---|
[1471] | 111 | faculty = fac_id, |
---|
| 112 | department = dep_id, |
---|
[1549] | 113 | course = cert_id |
---|
[1515] | 114 | ) |
---|
[1549] | 115 | if real_object.portal_type == "StudentStudyLevel": |
---|
| 116 | context_obj = getattr(certificate_obj,real_object.getId()) |
---|
| 117 | allowed = ('CourseAdviser', 'SectionManager') |
---|
| 118 | else: |
---|
| 119 | res = self.portal_catalog(portal_type="Department",id=dep_id) |
---|
| 120 | allowed = ('ClearanceOfficer', 'SectionManager') |
---|
| 121 | if len(res) != 1: |
---|
| 122 | break |
---|
| 123 | context_obj = res[0].getObject() |
---|
| 124 | dynamic_roles = self.getRolesInContext(context_obj) |
---|
| 125 | for dr in allowed: |
---|
[880] | 126 | if dr in dynamic_roles: |
---|
| 127 | roles.append(dr) |
---|
| 128 | break |
---|
| 129 | return roles |
---|
| 130 | |
---|
[881] | 131 | from Products.CPSUserFolder.CPSUserFolder import CPSUser |
---|
| 132 | CPSUser.getRolesInContext = getRolesInContext |
---|