Use Url util to construct urls everywhere (#12394)

Fixes #12345
This commit is contained in:
Rijk van Zanten
2022-03-25 13:21:59 -04:00
committed by GitHub
parent ff4fc3b8a9
commit caf698ed2f
4 changed files with 10 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ import { userName } from '../utils/user-name';
import { uniq } from 'lodash';
import env from '../env';
import validateUUID from 'uuid-validate';
import { Url } from '../utils/url';
export class ActivityService extends ItemsService {
notificationsService: NotificationsService;
@@ -83,7 +84,9 @@ ${userName(sender)} has mentioned you in a comment:
${comment}
<a href="${env.PUBLIC_URL}/admin/content/${data.collection}/${data.item}">Click here to view.</a>
<a href="${new Url(env.PUBLIC_URL)
.addPath('admin', 'content', data.collection, data.item)
.toString()}">Click here to view.</a>
`;
await this.notificationsService.createOne({

View File

@@ -19,6 +19,7 @@ import { UsersService } from './users';
import { MailService } from './mail';
import { userName } from '../utils/user-name';
import { md } from '../utils/md';
import { Url } from '../utils/url';
export class SharesService extends ItemsService {
authorizationService: AuthorizationService;
@@ -137,7 +138,7 @@ Hello!
${userName(userInfo)} has invited you to view an item in ${share.collection}.
[Open](${env.PUBLIC_URL}/admin/shared/${payload.share})
[Open](${new Url(env.PUBLIC_URL).addPath('admin', 'shared', payload.share).toString()})
`;
for (const email of payload.emails) {

View File

@@ -324,7 +324,9 @@ export class UsersService extends ItemsService {
const payload = { email, scope: 'password-reset', hash: getSimpleHash('' + user.password) };
const token = jwt.sign(payload, env.SECRET as string, { expiresIn: '1d', issuer: 'directus' });
const acceptURL = url ? `${url}?token=${token}` : `${env.PUBLIC_URL}/admin/reset-password?token=${token}`;
const acceptURL = url
? new Url(url).setQuery('token', token).toString()
: new Url(env.PUBLIC_URL).addPath('admin', 'reset-password').setQuery('token', token);
const subjectLine = subject ? subject : 'Password Reset Request';
await mailService.send({

View File

@@ -38,7 +38,7 @@ export class Url {
return this.protocol === null && this.host === null;
}
public addPath(...paths: string[]): Url {
public addPath(...paths: (string | number)[]): Url {
const pathToAdd = paths.flatMap((p) => p.split('/')).filter((p) => p !== '');
for (const pathSegment of pathToAdd) {