ElevatedAccess Mixin

This commit is contained in:
Matthew Ryan Dillon 2015-11-03 13:47:58 -07:00
parent 58cbf4027d
commit 76942a10d0
4 changed files with 44 additions and 22 deletions

View file

@ -0,0 +1,22 @@
import Ember from 'ember';
const { Mixin , inject: { service } } = Ember;
export default Mixin.create({
currentUser: service('session-account'),
beforeModel: function(transition) {
this._super(transition);
this.get('currentUser.account').then((user) => {
if (user.get('isReader')) {
this.transitionTo(this.get('fallbackRouteBefore'));
}
});
},
afterModel: function(model) {
if (!model.get('canEdit')) {
this.transitionTo(this.get('fallbackRouteAfter'), model.get('id'));
}
},
});

View file

@ -1,7 +1,9 @@
import Ember from 'ember';
import ajaxError from '../utils/ajax-error';
export default Ember.Mixin.create({
const { Mixin } = Ember;
export default Mixin.create({
actions: {
save: function() {
const model = this.get('model');
@ -20,12 +22,11 @@ export default Ember.Mixin.create({
cancel: function() {
const model = this.get('model');
const fallbackRoute = this.get('fallbackRoute');
model.get('errors').clear();
model.rollbackAttributes();
this.transitionToRoute(fallbackRoute, model);
this.transitionToRoute(this.get('fallbackRoute'), model);
},
},
});

View file

@ -1,23 +1,10 @@
import Ember from 'ember';
import ElevatedAccess from '../../../../mixins/elevated-access';
const { Route, inject: { service } } = Ember;
export default Route.extend({
currentUser: service('session-account'),
beforeModel: function(transition) {
this._super(transition);
this.get('currentUser.account').then((user) => {
if (user.get('isReader')) {
this.transitionTo('protected.species.index');
}
});
},
afterModel: function(model) {
if (!model.get('canEdit')) {
this.transitionTo('species.show', model.get('id'));
}
},
const { Route } = Ember;
export default Route.extend(ElevatedAccess, {
// Required for ElevatedAccess mixin
fallbackRouteBefore: 'protected.species.index',
fallbackRouteAfter: 'protected.species.show',
});

View file

@ -0,0 +1,12 @@
import Ember from 'ember';
import ElevatedAccessMixin from '../../../mixins/elevated-access';
import { module, test } from 'qunit';
module('Unit | Mixin | elevated access');
// Replace this with your real tests.
test('it works', function(assert) {
var ElevatedAccessObject = Ember.Object.extend(ElevatedAccessMixin);
var subject = ElevatedAccessObject.create();
assert.ok(subject);
});