mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-29 15:12:58 +03:00
c646e78fff
no issue Having `session.user` return a promise made dealing with it in components difficult because you always had to remember it returned a promise rather than a model and had to handle the async behaviour. It also meant that you couldn't use any current user properties directly inside getters which made refactors to Glimmer/Octane idioms harder to reason about. `session.user` was a cached computed property so it really made no sense for it to be a promise - it was loaded on first access and then always returned instantly but with a fulfilled promise rather than the underlying model. Refactoring to a synchronous property that is loaded as part of the authentication flows (we load the current user to check that we're logged in - we may as well make use of that!) means one less thing to be aware of/remember and provides a nicer migration process to Glimmer components. As part of the refactor, the auth flows and pre-load of required data across other services was also simplified to make it easier to find and follow. - refactored app setup and `session.user` - added `session.populateUser()` that fetches a user model from the current user endpoint and sets it on `session.user` - removed knowledge of app setup from the `cookie` authenticator and moved it into = `session.postAuthPreparation()`, this means we have the same post-authentication setup no matter which authenticator is used so we have more consistent behaviour in tests which don't use the `cookie` authenticator - switched `session` service to native class syntax to get the expected `super()` behaviour - updated `handleAuthentication()` so it populate's `session.user` and performs post-auth setup before transitioning (handles sign-in after app load) - updated `application` route to remove duplicated knowledge of app preload behaviour that now lives in `session.postAuthPreparation()` (handles already-authed app load) - removed out-of-date attempt at pre-loading data from setup controller as that's now handled automatically via `session.handleAuthentication` - updated app code to not treat `session.user` as a promise - predominant usage was router `beforeModel` hooks that transitioned users without valid permissions, this sets us up for an easier removal of the `current-user-settings` mixin in the future
152 lines
4.4 KiB
JavaScript
152 lines
4.4 KiB
JavaScript
import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
|
|
import {assign} from '@ember/polyfills';
|
|
import {isBlank} from '@ember/utils';
|
|
import {inject as service} from '@ember/service';
|
|
|
|
export default AuthenticatedRoute.extend({
|
|
infinity: service(),
|
|
router: service(),
|
|
|
|
queryParams: {
|
|
type: {refreshModel: true},
|
|
visibility: {refreshModel: true},
|
|
access: {refreshModel: true},
|
|
author: {refreshModel: true},
|
|
tag: {refreshModel: true},
|
|
order: {refreshModel: true}
|
|
},
|
|
|
|
modelName: 'post',
|
|
|
|
perPage: 30,
|
|
|
|
init() {
|
|
this._super(...arguments);
|
|
|
|
// if we're already on this route and we're transiting _to_ this route
|
|
// then the filters are being changed and we shouldn't create a new
|
|
// browser history entry
|
|
// see https://github.com/TryGhost/Ghost/issues/11057
|
|
this.router.on('routeWillChange', (transition) => {
|
|
if (transition.to && (this.routeName === 'posts' || this.routeName === 'pages')) {
|
|
let toThisRoute = transition.to.find(route => route.name === this.routeName);
|
|
if (transition.from && transition.from.name === this.routeName && toThisRoute) {
|
|
transition.method('replace');
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
model(params) {
|
|
const user = this.session.user;
|
|
let queryParams = {};
|
|
let filterParams = {tag: params.tag, visibility: params.visibility};
|
|
let paginationParams = {
|
|
perPageParam: 'limit',
|
|
totalPagesParam: 'meta.pagination.pages'
|
|
};
|
|
|
|
assign(filterParams, this._getTypeFilters(params.type));
|
|
|
|
if (params.type === 'featured') {
|
|
filterParams.featured = true;
|
|
}
|
|
|
|
if (user.isAuthor) {
|
|
// authors can only view their own posts
|
|
filterParams.authors = user.slug;
|
|
} else if (user.isContributor) {
|
|
// Contributors can only view their own draft posts
|
|
filterParams.authors = user.slug;
|
|
filterParams.status = 'draft';
|
|
} else if (params.author) {
|
|
filterParams.authors = params.author;
|
|
}
|
|
|
|
let filter = this._filterString(filterParams);
|
|
if (!isBlank(filter)) {
|
|
queryParams.filter = filter;
|
|
}
|
|
|
|
if (!isBlank(params.order)) {
|
|
queryParams.order = params.order;
|
|
}
|
|
|
|
let perPage = this.perPage;
|
|
let paginationSettings = assign({perPage, startingPage: 1}, paginationParams, queryParams);
|
|
|
|
return this.infinity.model(this.modelName, paginationSettings);
|
|
},
|
|
|
|
// trigger a background load of all tags, authors, and snipps for use in filter dropdowns and card menu
|
|
setupController(controller) {
|
|
this._super(...arguments);
|
|
|
|
if (!controller._hasLoadedTags) {
|
|
this.store.query('tag', {limit: 'all'}).then(() => {
|
|
controller._hasLoadedTags = true;
|
|
});
|
|
}
|
|
|
|
if (!this.session.user.isAuthorOrContributor && !controller._hasLoadedAuthors) {
|
|
this.store.query('user', {limit: 'all'}).then(() => {
|
|
controller._hasLoadedAuthors = true;
|
|
});
|
|
}
|
|
|
|
if (!controller._hasLoadedSnippets) {
|
|
this.store.query('snippet', {limit: 'all'}).then(() => {
|
|
controller._hasLoadedSnippets = true;
|
|
});
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
queryParamsDidChange() {
|
|
// scroll back to the top
|
|
let contentList = document.querySelector('.content-list');
|
|
if (contentList) {
|
|
contentList.scrollTop = 0;
|
|
}
|
|
|
|
this._super(...arguments);
|
|
}
|
|
},
|
|
|
|
buildRouteInfoMetadata() {
|
|
return {
|
|
titleToken: 'Posts'
|
|
};
|
|
},
|
|
|
|
_getTypeFilters(type) {
|
|
let status = '[draft,scheduled,published]';
|
|
|
|
switch (type) {
|
|
case 'draft':
|
|
status = 'draft';
|
|
break;
|
|
case 'published':
|
|
status = 'published';
|
|
break;
|
|
case 'scheduled':
|
|
status = 'scheduled';
|
|
break;
|
|
}
|
|
|
|
return {
|
|
status
|
|
};
|
|
},
|
|
|
|
_filterString(filter) {
|
|
return Object.keys(filter).map((key) => {
|
|
let value = filter[key];
|
|
|
|
if (!isBlank(value)) {
|
|
return `${key}:${filter[key]}`;
|
|
}
|
|
}).compact().join('+');
|
|
}
|
|
});
|