2016-05-24 15:06:59 +03:00
|
|
|
import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
|
2019-02-22 06:17:33 +03:00
|
|
|
import {pluralize} from 'ember-inflector';
|
2017-07-22 02:11:24 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
export default AuthenticatedRoute.extend({
|
2018-06-04 11:46:49 +03:00
|
|
|
beforeModel(transition) {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
// if the transition is not new->edit, reset the post on the controller
|
|
|
|
// so that the editor view is cleared before showing the loading state
|
|
|
|
if (transition.urlMethod !== 'replace') {
|
|
|
|
let editor = this.controllerFor('editor');
|
|
|
|
editor.set('post', null);
|
|
|
|
editor.reset();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-03-07 20:31:03 +03:00
|
|
|
model(params, transition) {
|
2019-03-11 19:26:36 +03:00
|
|
|
// eslint-disable-next-line camelcase
|
2019-03-07 20:31:03 +03:00
|
|
|
let {type: modelName, post_id} = params;
|
|
|
|
|
|
|
|
if (!['post', 'page'].includes(modelName)) {
|
|
|
|
let path = transition.intent.url.replace(/^\//, '');
|
|
|
|
return this.replaceWith('error404', {path, status: 404});
|
|
|
|
}
|
|
|
|
|
2016-11-14 13:53:15 +03:00
|
|
|
let query = {
|
2019-09-18 17:11:35 +03:00
|
|
|
id: post_id
|
2014-07-30 20:44:49 +04:00
|
|
|
};
|
2014-06-09 04:18:39 +04:00
|
|
|
|
2019-03-07 20:31:03 +03:00
|
|
|
return this.store.query(modelName, query)
|
2018-01-17 16:27:37 +03:00
|
|
|
.then(records => records.get('firstObject'));
|
2014-11-21 21:07:30 +03:00
|
|
|
},
|
2014-07-31 12:29:05 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// the API will return a post even if the logged in user doesn't have
|
|
|
|
// permission to edit it (all posts are public) so we need to do our
|
|
|
|
// own permissions check and redirect if necessary
|
2015-10-28 14:36:45 +03:00
|
|
|
afterModel(post) {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return this.get('session.user').then((user) => {
|
2019-06-18 13:47:21 +03:00
|
|
|
let returnRoute = pluralize(post.constructor.modelName);
|
2019-02-22 06:17:33 +03:00
|
|
|
|
2018-02-07 12:42:46 +03:00
|
|
|
if (user.get('isAuthorOrContributor') && !post.isAuthoredByUser(user)) {
|
2019-02-22 06:17:33 +03:00
|
|
|
return this.replaceWith(returnRoute);
|
2018-02-07 12:42:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the post is not a draft and user is contributor, redirect to index
|
|
|
|
if (user.get('isContributor') && !post.get('isDraft')) {
|
2019-02-22 06:17:33 +03:00
|
|
|
return this.replaceWith(returnRoute);
|
2014-11-21 21:07:30 +03:00
|
|
|
}
|
2014-06-05 21:23:28 +04:00
|
|
|
});
|
2014-07-31 23:15:55 +04:00
|
|
|
},
|
|
|
|
|
2019-02-22 06:17:33 +03:00
|
|
|
serialize(model) {
|
|
|
|
return {
|
|
|
|
type: model.constructor.modelName,
|
|
|
|
post_id: model.id
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// there's no specific controller for this route, instead all editor
|
|
|
|
// handling is done on the editor route/controler
|
|
|
|
setupController(controller, post) {
|
|
|
|
let editor = this.controllerFor('editor');
|
|
|
|
editor.setPost(post);
|
2014-03-02 18:30:35 +04:00
|
|
|
}
|
2014-03-04 00:18:10 +04:00
|
|
|
});
|