-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(doctor): use new getGhostUid util
- Loading branch information
Showing
4 changed files
with
64 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,52 @@ | ||
'use strict'; | ||
const os = require('os'); | ||
const execa = require('execa'); | ||
const path = require('path'); | ||
const errors = require('../../../errors'); | ||
const chalk = require('chalk'); | ||
const fs = require('fs'); | ||
const ghostUser = require('../../../utils/use-ghost-user'); | ||
|
||
const taskTitle = 'Checking logged in user and directory owner'; | ||
|
||
function checkGhostUser() { | ||
let ghostuid; | ||
let ghostgid; | ||
|
||
try { | ||
ghostuid = execa.shellSync('id -u ghost').stdout; | ||
ghostgid = execa.shellSync('id -g ghost').stdout; | ||
} catch (e) { | ||
// CASE: the ghost user doesn't exist, hence can't be used | ||
return false | ||
} | ||
|
||
ghostuid = parseInt(ghostuid); | ||
ghostgid = parseInt(ghostgid); | ||
|
||
return { | ||
uid: ghostuid, | ||
gid: ghostgid | ||
}; | ||
} | ||
|
||
function loggedInUserOwner(ctx) { | ||
const uid = process.getuid(); | ||
const gid = process.getgroups(); | ||
const ghostStats = checkGhostUser(); | ||
const dirStats = fs.lstatSync(path.join(process.cwd())); | ||
const contentDirStats = fs.lstatSync(path.join(process.cwd(), 'content')); | ||
|
||
const ghostStats = ghostUser.getGhostUid(); | ||
|
||
// CASE 1: check if ghost user exists and if it's currently used | ||
if (ghostStats && ghostStats.uid && ghostStats.uid === uid) { | ||
// The ghost user might have been set up on the system and also used, | ||
// but only when it owns the content folder, it's an indication that it's also used | ||
// as the linux user and shall not be used as current user. | ||
if (contentDirStats.uid === ghostStats.uid) { | ||
return Promise.reject(new errors.SystemError({ | ||
throw new errors.SystemError({ | ||
message: 'You can\'t use Ghost with the ghost user. Please log in with your own user.', | ||
help: `${chalk.green('https://docs.ghost.org/docs/install#section-create-a-new-user')}`, | ||
task: taskTitle | ||
})); | ||
}); | ||
} | ||
} | ||
|
||
// CASE 2: check if the current user is the owner of the current dir | ||
if (dirStats.uid !== uid) { | ||
if (gid.indexOf(dirStats.gid) < 0) { | ||
return Promise.reject(new errors.SystemError({ | ||
throw new errors.SystemError({ | ||
message: `Your current user is not the owner of the Ghost directory and also not part of the same group. | ||
Please log in with the user that owns the directory or add your user to the same group.`, | ||
help: `${chalk.green('https://docs.ghost.org/docs/install#section-create-a-new-user')}`, | ||
task: taskTitle | ||
})); | ||
}); | ||
} | ||
// Yup current user is not the owner, but in the same group, so just show a warning | ||
ctx.ui.log('The current user is not the owner of the Ghost directory. This might cause problems.'); | ||
ctx.ui.log(`${chalk.yellow('The current user is not the owner of the Ghost directory. This might cause problems.')}`); | ||
} | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
module.exports = { | ||
title: taskTitle, | ||
task: loggedInUserOwner, | ||
enabled: (ctx) => ctx.instance && ctx.instance.process.name !== 'local', | ||
skip: () => os.platform() !== 'linux', | ||
enabled: (ctx) => ctx.system.platform.linux, | ||
category: ['start', 'update'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.