@@ -43,17 +43,17 @@ def reconcile_access(commit_changes=False):
4343 netid = strip_domain (row [0 ])
4444 accessee = get_accessee_model (netid )
4545
46- for delegate , rights in get_delegations (row [1 ]). items ( ):
46+ for delegate , right in mailbox_delegations (row [1 ]):
4747 try :
48- record = reconcile_delegation (accessee , delegate , rights )
48+ record = reconcile_delegation (accessee , delegate , right )
4949 clear_record_id (record_ids , record .id )
5050 except NullDelegateException :
5151 logger .info (
5252 f"NULL DELEGATE: mailbox { netid } delegate null "
53- f"with rights : { rights } " )
53+ f"with right : { right } " )
5454 except NoAccessRecordException :
5555 logger .info (f"NO ACCESS RECORD FOR: mailbox { netid } "
56- f"delegate { delegate } rights : { rights } " )
56+ f"delegate { delegate } right : { right } " )
5757 if commit_changes :
5858 new_access_record (accessee , delegate , right_record )
5959 except DeletedAccessRecordException as ex :
@@ -71,9 +71,23 @@ def reconcile_access(commit_changes=False):
7171 f"{ record .datetime_expired } " )
7272
7373 if commit_changes :
74- right = next (iter (rights ))
75- right_record = get_access_right (right )
76- assign_access_right (record , right_record )
74+ # right still match? update
75+ if record .access_right .name != right :
76+ logger .info (
77+ "UPDATE DELETED ACCESS RECORD: "
78+ f"mailbox { netid } "
79+ f"delegate { delegate } "
80+ f"({ record .access_right .name } ) to { right } " )
81+ right_record = get_access_right (right )
82+ assign_access_right (record , right_record )
83+
84+ logger .info (
85+ f"UNDELETED ACCESS RECORD: mailbox { netid } "
86+ f"delegate { delegate } "
87+ f"({ record .access_right .name } )" )
88+ undelete_access_record (record )
89+
90+ clear_record_id (record_ids , record .id )
7791 except EmptyDelegateRightsException as ex :
7892 record = ex .record
7993 logger .info (f"NO RIGHTS FOR DELEGATION: "
@@ -83,41 +97,35 @@ def reconcile_access(commit_changes=False):
8397 except TooManyRightsException as ex :
8498 logger .info (
8599 f"CONFLICT: mailbox { netid } delegate { delegate } "
86- f"rights : { rights } " )
100+ f"right : { right } " )
87101 record = ex .record
88102 if commit_changes :
89103 revoke_record (record )
90- save_conflict_record (accessee , record , delegate , rights )
104+ save_conflict_record (accessee , record , delegate , right )
91105
92106 clear_record_id (record_ids , record .id )
93107 except DelegateRightMismatchException as ex :
94108 record = ex .record
95- right = next (iter (rights ))
96-
97109 logger .info (
98- f"DELEGATION CHANGE: mailbox { netid } delegate { delegate } "
99- f" ({ record .access_right .name } ) to { right } " )
110+ f"DELEGATION CHANGE: mailbox { netid } delegate { delegate } "
111+ f"({ record .access_right .name } ) to { right } " )
100112
101113 if commit_changes :
102114 right_record = get_access_right (right )
103115 assign_access_right (record , right_record )
104116
105117 clear_record_id (record_ids , record .id )
106- except Exception as ex :
107- logger .error (
108- f"UNEXPECTED ERROR: mailbox { netid } delegate { delegate } "
109- f"rights: { rights } error: { ex } " )
110118
111119 # access records for which no delegation was reported
112120 for record in AccessRecord .objects .filter (id__in = record_ids ):
113- if commit_changes :
121+ logger .info (f"UNREPORTED DELEGATION: mailbox { accessee .netid } "
122+ f"delegate { record .accessor .name } "
123+ f"({ record .access_right .name } ) "
124+ f"on { record .datetime_granted } not "
125+ "assigned in Outlook" )
126+ # disable until policy is decided
127+ if False and commit_changes :
114128 assign_delegation (accessee , record )
115- else :
116- logger .info (f"MISSING DELEGATION: mailbox { accessee .netid } "
117- f"delegate { record .accessor .name } "
118- f"({ record .access_right .name } )"
119- f" on { record .datetime_granted } not "
120- "assigned in Outlook" )
121129
122130
123131def clear_record_id (record_ids , record_id ):
@@ -127,7 +135,7 @@ def clear_record_id(record_ids, record_id):
127135 pass
128136
129137
130- def reconcile_delegation (accessee , delegate , rights ):
138+ def reconcile_delegation (accessee , delegate , right ):
131139 if not delegate or delegate .lower () == 'null' :
132140 raise NullDelegateException ()
133141
@@ -137,16 +145,20 @@ def reconcile_delegation(accessee, delegate, rights):
137145 except AccessRecord .DoesNotExist :
138146 raise NoAccessRecordException ()
139147
140- if len (rights ) > 1 :
141- raise TooManyRightsException (record = record )
142-
143148 if record .is_deleted :
144149 raise DeletedAccessRecordException (record = record )
145150
146- if len (rights ) < 1 :
147- raise EmptyDelegateRightsException (record = record )
151+ if isinstance (right , str ):
152+ if not right :
153+ raise EmptyDelegateRightsException (record = record )
154+ elif isinstance (right , list ):
155+ if len (right ) == 0 :
156+ raise EmptyDelegateRightsException (record = record )
157+ elif len (rights ) > 1 :
158+ raise TooManyRightsException (record = record )
159+
160+ right = right [0 ]
148161
149- right = next (iter (rights ))
150162 if record .access_right .name != right :
151163 raise DelegateRightMismatchException (record = record )
152164
@@ -159,10 +171,6 @@ def get_access_right(right):
159171
160172
161173def new_access_record (accessee , delegate , right ):
162- logger .info (
163- f"CREATE RECORD: mailbox { accessee .netid } "
164- f"delegate { delegate } ({ right .name } )" )
165-
166174 logger .info ("FAILSAFE HIT" )
167175 return
168176
@@ -171,11 +179,12 @@ def new_access_record(accessee, delegate, right):
171179 store_access_record (
172180 accessee , accessor , right , is_reconcile = True )
173181
174- logger .info (f"mailbox { accessee .netid } delegation { delegate } "
175- f"({ right } ) record created" )
182+ logger .info (
183+ f"CREATEED RECORD: mailbox { accessee .netid } "
184+ f"delegate { delegate } ({ right .name } )" )
176185 except (UnrecognizedUWNetid , UnrecognizedGroupID ):
177186 logger .error (
178- "Unknown netid or group: {}" .format (delegate ))
187+ "CREATE RECORDE: Unknown netid or group: {}" .format (delegate ))
179188
180189
181190def assign_delegation (accessee , record ):
@@ -206,6 +215,14 @@ def revoke_record(record):
206215 record .revoke ()
207216
208217
218+ def undelete_access_record (record ):
219+ logger .info ("FAILSAFE HIT" )
220+ return
221+
222+ record .is_deleted = False
223+ record .save ()
224+
225+
209226def assign_access_right (record , right ):
210227 logger .info (f"UPDATE CHANGE: mailbox { record .accessee .netid } "
211228 f"delegate { record .accessor .name } "
@@ -236,18 +253,16 @@ def save_conflict_record(accessee, record, delegate, rights):
236253 conflict .save ()
237254
238255
239- def get_delegations (raw ):
240- delegates = {}
241- cooked = json .loads (raw )
242- for right in [cooked ] if isinstance (cooked , dict ) else cooked :
256+ def mailbox_delegations (column ):
257+ rights = json .loads (column )
258+ for right in [rights ] if isinstance (rights , dict ) else rights :
243259 user = right ["User" ]
244260 if user and user .lower () != 'null' :
245- try :
246- delegates [user ].append (right ['AccessRights' ])
247- except KeyError :
248- delegates [user ] = [right ['AccessRights' ]]
249-
250- return delegates
261+ yield user , right ['AccessRights' ]
262+ else :
263+ logger .debug (
264+ f"NULL RIGHT: mailbox { netid } delegate { delegate } "
265+ f" right: { right } " )
251266
252267
253268def access_user (a ):
0 commit comments