From 225817583f3dfce41bb0b6e95d639a446b460ff2 Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Tue, 24 Oct 2017 16:06:58 -0700 Subject: [PATCH] Common code for handling 403s in routes --- ui/app/mixins/with-forbidden-state.js | 18 ++++++++++++++++++ ui/app/utils/notify-forbidden.js | 12 ++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 ui/app/mixins/with-forbidden-state.js create mode 100644 ui/app/utils/notify-forbidden.js diff --git a/ui/app/mixins/with-forbidden-state.js b/ui/app/mixins/with-forbidden-state.js new file mode 100644 index 000000000..4b1397e09 --- /dev/null +++ b/ui/app/mixins/with-forbidden-state.js @@ -0,0 +1,18 @@ +import Ember from 'ember'; + +const { Mixin } = Ember; + +export default Mixin.create({ + setupController(controller) { + if (this.get('isForbidden')) { + this.set('isForbidden', undefined); + controller.set('isForbidden', true); + } + this._super(...arguments); + }, + + resetController(controller) { + controller.set('isForbidden', false); + this._super(...arguments); + }, +}); diff --git a/ui/app/utils/notify-forbidden.js b/ui/app/utils/notify-forbidden.js new file mode 100644 index 000000000..433d1fc40 --- /dev/null +++ b/ui/app/utils/notify-forbidden.js @@ -0,0 +1,12 @@ +// An error handler to provide to a promise catch to set a +// forbidden flag on the route +import codesForError from './codes-for-error'; +export default function notifyForbidden(route) { + return error => { + if (codesForError(error).includes('403')) { + route.set('isForbidden', true); + } else { + throw error; + } + }; +}