Delete possible orphan icon when updating a twofaccount

This commit is contained in:
Bubka 2020-01-27 13:56:19 +01:00
parent 52ac639940
commit 59fe66710a

View File

@ -92,8 +92,23 @@ public function update(Request $request, $id)
'service' => 'required',
]);
// Here we catch a possible missing model exception in order to
// delete orphan submited icon
try {
$twofaccount = TwoFAccount::FindOrFail($id);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
if( $request->icon ) {
Storage::delete('public/icons/' . $request->icon);
}
throw $e;
}
if( $twofaccount->type === 'hotp' ) {
// HOTP can be desynchronized from the verification
@ -138,11 +153,7 @@ public function destroy($id)
$twofaccount = TwoFAccount::FindOrFail($id);
// delete icon
$storedIcon = 'public/icons/' . $twofaccount->icon;
if( Storage::exists($storedIcon) ) {
Storage::delete($storedIcon);
}
Storage::delete('public/icons/' . $twofaccount->icon);
// delete account
$twofaccount->delete();