2018-03-18 04:34:32 +00:00
|
|
|
const _ = require('lodash')
|
2018-05-06 21:33:41 +00:00
|
|
|
const Promise = require('bluebird')
|
|
|
|
const getos = Promise.promisify(require('getos'))
|
2018-03-18 04:34:32 +00:00
|
|
|
const os = require('os')
|
|
|
|
const filesize = require('filesize')
|
2018-05-06 21:33:41 +00:00
|
|
|
const path = require('path')
|
2018-11-11 04:40:55 +00:00
|
|
|
const fs = require('fs-extra')
|
2019-01-13 20:37:45 +00:00
|
|
|
const moment = require('moment')
|
2019-02-23 23:22:25 +00:00
|
|
|
const graphHelper = require('../../helpers/graph')
|
2019-09-23 04:37:18 +00:00
|
|
|
const request = require('request-promise')
|
2019-09-30 00:53:41 +00:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const nanoid = require('nanoid/non-secure/generate')
|
2018-03-18 04:34:32 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
2018-05-06 21:33:41 +00:00
|
|
|
const dbTypes = {
|
2018-09-08 19:49:36 +00:00
|
|
|
mysql: 'MySQL',
|
|
|
|
mariadb: 'MariaDB',
|
2018-05-06 21:33:41 +00:00
|
|
|
postgres: 'PostgreSQL',
|
2018-05-19 20:40:07 +00:00
|
|
|
sqlite: 'SQLite',
|
|
|
|
mssql: 'MS SQL Server'
|
2018-05-06 21:33:41 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 04:34:32 +00:00
|
|
|
module.exports = {
|
|
|
|
Query: {
|
2019-09-22 20:32:39 +00:00
|
|
|
async system () { return {} }
|
2018-03-18 04:34:32 +00:00
|
|
|
},
|
2019-02-23 23:22:25 +00:00
|
|
|
Mutation: {
|
2019-09-22 20:32:39 +00:00
|
|
|
async system () { return {} }
|
2019-02-23 23:22:25 +00:00
|
|
|
},
|
2018-03-18 04:34:32 +00:00
|
|
|
SystemQuery: {
|
2019-09-22 20:32:39 +00:00
|
|
|
flags () {
|
2019-02-23 23:22:25 +00:00
|
|
|
return _.transform(WIKI.config.flags, (result, value, key) => {
|
|
|
|
result.push({ key, value })
|
|
|
|
}, [])
|
|
|
|
},
|
2018-09-04 00:58:54 +00:00
|
|
|
async info() { return {} }
|
|
|
|
},
|
2019-02-23 23:22:25 +00:00
|
|
|
SystemMutation: {
|
2019-09-22 20:32:39 +00:00
|
|
|
async updateFlags (obj, args, context) {
|
2019-02-23 23:22:25 +00:00
|
|
|
WIKI.config.flags = _.transform(args.flags, (result, row) => {
|
|
|
|
_.set(result, row.key, row.value)
|
|
|
|
}, {})
|
|
|
|
await WIKI.configSvc.applyFlags()
|
|
|
|
await WIKI.configSvc.saveToDb(['flags'])
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('System Flags applied successfully')
|
|
|
|
}
|
2019-07-06 21:43:50 +00:00
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
async resetTelemetryClientId (obj, args, context) {
|
2019-07-06 21:43:50 +00:00
|
|
|
try {
|
|
|
|
WIKI.telemetry.generateClientId()
|
|
|
|
await WIKI.configSvc.saveToDb(['telemetry'])
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Telemetry state updated successfully')
|
|
|
|
}
|
2019-08-04 20:31:13 +00:00
|
|
|
} catch (err) {
|
2019-07-06 21:43:50 +00:00
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
async setTelemetry (obj, args, context) {
|
2019-07-06 21:43:50 +00:00
|
|
|
try {
|
|
|
|
_.set(WIKI.config, 'telemetry.isEnabled', args.enabled)
|
|
|
|
WIKI.telemetry.enabled = args.enabled
|
|
|
|
await WIKI.configSvc.saveToDb(['telemetry'])
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Telemetry Client ID has been reset successfully')
|
|
|
|
}
|
2019-08-04 20:31:13 +00:00
|
|
|
} catch (err) {
|
2019-07-06 21:43:50 +00:00
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2019-09-22 20:32:39 +00:00
|
|
|
},
|
|
|
|
async performUpgrade (obj, args, context) {
|
|
|
|
try {
|
2019-09-23 04:37:18 +00:00
|
|
|
if (process.env.UPGRADE_COMPANION) {
|
|
|
|
await request({
|
|
|
|
method: 'POST',
|
|
|
|
uri: 'http://wiki-update-companion/upgrade'
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Upgrade has started.')
|
2019-09-22 20:32:39 +00:00
|
|
|
}
|
2019-09-23 04:37:18 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('You must run the wiki-update-companion container and pass the UPGRADE_COMPANION env var in order to use this feature.')
|
2019-09-22 20:32:39 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2019-09-29 02:19:33 +00:00
|
|
|
},
|
2019-09-30 00:53:41 +00:00
|
|
|
/**
|
|
|
|
* Import Users from a v1 installation
|
|
|
|
*/
|
2019-09-29 02:19:33 +00:00
|
|
|
async importUsersFromV1(obj, args, context) {
|
|
|
|
try {
|
|
|
|
const MongoClient = require('mongodb').MongoClient
|
|
|
|
if (args.mongoDbConnString && args.mongoDbConnString.length > 10) {
|
2019-09-30 00:53:41 +00:00
|
|
|
// -> Connect to DB
|
|
|
|
|
2019-09-29 02:19:33 +00:00
|
|
|
const client = await MongoClient.connect(args.mongoDbConnString, {
|
|
|
|
appname: `Wiki.js ${WIKI.version} Migration Tool`
|
|
|
|
})
|
|
|
|
const dbUsers = client.db().collection('users')
|
|
|
|
const userCursor = dbUsers.find({ email: { '$ne': 'guest' } })
|
|
|
|
|
2019-09-30 00:53:41 +00:00
|
|
|
const curDateISO = new Date().toISOString()
|
|
|
|
|
2019-09-29 02:19:33 +00:00
|
|
|
let failed = []
|
|
|
|
let usersCount = 0
|
|
|
|
let groupsCount = 0
|
2019-09-30 00:53:41 +00:00
|
|
|
let assignableGroups = []
|
|
|
|
let reuseGroups = []
|
|
|
|
|
|
|
|
// -> Create SINGLE group
|
|
|
|
|
|
|
|
if (args.groupMode === `SINGLE`) {
|
|
|
|
const singleGroup = await WIKI.models.groups.query().insert({
|
|
|
|
name: `Import_${curDateISO}`,
|
|
|
|
permissions: JSON.stringify(WIKI.data.groups.defaultPermissions),
|
|
|
|
pageRules: JSON.stringify(WIKI.data.groups.defaultPageRules)
|
|
|
|
})
|
|
|
|
groupsCount++
|
|
|
|
assignableGroups.push(singleGroup.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Iterate all users
|
2019-09-29 02:19:33 +00:00
|
|
|
|
|
|
|
while (await userCursor.hasNext()) {
|
|
|
|
const usr = await userCursor.next()
|
2019-09-30 00:53:41 +00:00
|
|
|
|
|
|
|
let usrGroup = []
|
|
|
|
if (args.groupMode === `MULTI`) {
|
|
|
|
// -> Check if global admin
|
|
|
|
|
|
|
|
if (_.some(usr.rights, ['role', 'admin'])) {
|
|
|
|
usrGroup.push(1)
|
|
|
|
} else {
|
|
|
|
// -> Check if identical group already exists
|
|
|
|
|
|
|
|
const currentRights = _.sortBy(_.map(usr.rights, r => _.pick(r, ['role', 'path', 'exact', 'deny'])), ['role', 'path', 'exact', 'deny'])
|
|
|
|
const ruleSetId = crypto.createHash('sha1').update(JSON.stringify(currentRights)).digest('base64')
|
|
|
|
const existingGroup = _.find(reuseGroups, ['hash', ruleSetId])
|
|
|
|
if (existingGroup) {
|
|
|
|
usrGroup.push(existingGroup.groupId)
|
|
|
|
} else {
|
|
|
|
// -> Build new group
|
|
|
|
|
|
|
|
const pageRules = _.map(usr.rights, r => {
|
|
|
|
let roles = ['read:pages', 'read:assets', 'read:comments', 'write:comments']
|
|
|
|
if (r.role === `write`) {
|
|
|
|
roles = _.concat(roles, ['write:pages', 'manage:pages', 'read:source', 'read:history', 'write:assets', 'manage:assets'])
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
id: nanoid('1234567890abcdef', 10),
|
|
|
|
roles: roles,
|
|
|
|
match: r.exact ? 'EXACT' : 'START',
|
|
|
|
deny: r.deny,
|
|
|
|
path: (r.path.indexOf('/') === 0) ? r.path.substring(1) : r.path,
|
|
|
|
locales: []
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const perms = _.chain(pageRules).reject('deny').map('roles').union().flatten().value()
|
|
|
|
|
|
|
|
// -> Create new group
|
|
|
|
|
|
|
|
const newGroup = await WIKI.models.groups.query().insert({
|
|
|
|
name: `Import_${curDateISO}_${groupsCount + 1}`,
|
|
|
|
permissions: JSON.stringify(perms),
|
|
|
|
pageRules: JSON.stringify(pageRules)
|
|
|
|
})
|
|
|
|
reuseGroups.push({
|
|
|
|
groupId: newGroup.id,
|
|
|
|
hash: ruleSetId
|
|
|
|
})
|
|
|
|
groupsCount++
|
|
|
|
usrGroup.push(newGroup.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Create User
|
|
|
|
|
2019-09-29 02:19:33 +00:00
|
|
|
try {
|
|
|
|
await WIKI.models.users.createNewUser({
|
|
|
|
providerKey: usr.provider,
|
|
|
|
email: usr.email,
|
|
|
|
name: usr.name,
|
|
|
|
passwordRaw: usr.password,
|
2019-09-30 00:53:41 +00:00
|
|
|
groups: (usrGroup.length > 0) ? usrGroup : assignableGroups,
|
2019-09-29 02:19:33 +00:00
|
|
|
mustChangePassword: false,
|
|
|
|
sendWelcomeEmail: false
|
|
|
|
})
|
|
|
|
usersCount++
|
|
|
|
} catch (err) {
|
|
|
|
failed.push({
|
|
|
|
provider: usr.provider,
|
|
|
|
email: usr.email,
|
|
|
|
error: err.message
|
|
|
|
})
|
|
|
|
WIKI.logger.warn(`${usr.email}: ${err}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 00:53:41 +00:00
|
|
|
// -> Reload group permissions
|
|
|
|
|
|
|
|
if (args.groupMode !== `NONE`) {
|
|
|
|
await WIKI.auth.reloadGroups()
|
|
|
|
}
|
|
|
|
|
2019-09-29 02:19:33 +00:00
|
|
|
client.close()
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Import completed.'),
|
|
|
|
usersCount: usersCount,
|
|
|
|
groupsCount: groupsCount,
|
|
|
|
failed: failed
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error('MongoDB Connection String is missing or invalid.')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2020-01-20 02:30:25 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Set HTTPS Redirection State
|
|
|
|
*/
|
|
|
|
async setHTTPSRedirection (obj, args, context) {
|
|
|
|
_.set(WIKI.config, 'server.sslRedir', args.enabled)
|
|
|
|
await WIKI.configSvc.saveToDb(['server'])
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('HTTP Redirection state set successfully.')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Renew SSL Certificate
|
|
|
|
*/
|
|
|
|
async renewHTTPSCertificate (obj, args, context) {
|
|
|
|
try {
|
|
|
|
if (!WIKI.config.ssl.enabled) {
|
|
|
|
throw new WIKI.Error.SystemSSLDisabled()
|
|
|
|
} else if (WIKI.config.ssl.provider !== `letsencrypt`) {
|
|
|
|
throw new WIKI.Error.SystemSSLRenewInvalidProvider()
|
|
|
|
} else if (!WIKI.servers.le) {
|
|
|
|
throw new WIKI.Error.SystemSSLLEUnavailable()
|
|
|
|
} else {
|
|
|
|
await WIKI.servers.le.requestCertificate()
|
|
|
|
await WIKI.servers.restartServer('https')
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('SSL Certificate renewed successfully.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2019-02-23 23:22:25 +00:00
|
|
|
}
|
|
|
|
},
|
2018-09-04 00:58:54 +00:00
|
|
|
SystemInfo: {
|
2019-09-22 20:32:39 +00:00
|
|
|
configFile () {
|
2018-09-04 00:58:54 +00:00
|
|
|
return path.join(process.cwd(), 'config.yml')
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
cpuCores () {
|
2019-07-06 21:43:50 +00:00
|
|
|
return os.cpus().length
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
currentVersion () {
|
2018-09-04 00:58:54 +00:00
|
|
|
return WIKI.version
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
dbType () {
|
2018-09-04 00:58:54 +00:00
|
|
|
return _.get(dbTypes, WIKI.config.db.type, 'Unknown DB')
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
async dbVersion () {
|
2018-09-08 19:49:36 +00:00
|
|
|
let version = 'Unknown Version'
|
|
|
|
switch (WIKI.config.db.type) {
|
|
|
|
case 'mariadb':
|
|
|
|
case 'mysql':
|
|
|
|
const resultMYSQL = await WIKI.models.knex.raw('SELECT VERSION() as version;')
|
|
|
|
version = _.get(resultMYSQL, '[0][0].version', 'Unknown Version')
|
|
|
|
break
|
|
|
|
case 'mssql':
|
|
|
|
const resultMSSQL = await WIKI.models.knex.raw('SELECT @@VERSION as version;')
|
|
|
|
version = _.get(resultMSSQL, '[0].version', 'Unknown Version')
|
|
|
|
break
|
|
|
|
case 'postgres':
|
|
|
|
version = _.get(WIKI.models, 'knex.client.version', 'Unknown Version')
|
|
|
|
break
|
|
|
|
case 'sqlite':
|
|
|
|
version = _.get(WIKI.models, 'knex.client.driver.VERSION', 'Unknown Version')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return version
|
2018-09-04 00:58:54 +00:00
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
dbHost () {
|
2018-09-08 19:49:36 +00:00
|
|
|
if (WIKI.config.db.type === 'sqlite') {
|
|
|
|
return WIKI.config.db.storage
|
|
|
|
} else {
|
|
|
|
return WIKI.config.db.host
|
|
|
|
}
|
2018-09-04 00:58:54 +00:00
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
hostname () {
|
2019-07-06 21:43:50 +00:00
|
|
|
return os.hostname()
|
|
|
|
},
|
2020-01-20 02:30:25 +00:00
|
|
|
httpPort () {
|
|
|
|
return WIKI.servers.servers.http ? _.get(WIKI.servers.servers.http.address(), 'port', 0) : 0
|
|
|
|
},
|
|
|
|
httpRedirection () {
|
|
|
|
return _.get(WIKI.config, 'server.sslRedir', false)
|
|
|
|
},
|
|
|
|
httpsPort () {
|
|
|
|
return WIKI.servers.servers.https ? _.get(WIKI.servers.servers.https.address(), 'port', 0) : 0
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
latestVersion () {
|
2019-01-13 20:37:45 +00:00
|
|
|
return WIKI.system.updates.version
|
2018-09-04 00:58:54 +00:00
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
latestVersionReleaseDate () {
|
2019-01-13 20:37:45 +00:00
|
|
|
return moment.utc(WIKI.system.updates.releaseDate)
|
2018-09-04 00:58:54 +00:00
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
nodeVersion () {
|
2019-07-06 21:43:50 +00:00
|
|
|
return process.version.substr(1)
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
async operatingSystem () {
|
2018-05-06 21:33:41 +00:00
|
|
|
let osLabel = `${os.type()} (${os.platform()}) ${os.release()} ${os.arch()}`
|
|
|
|
if (os.platform() === 'linux') {
|
|
|
|
const osInfo = await getos()
|
|
|
|
osLabel = `${os.type()} - ${osInfo.dist} (${osInfo.codename || os.platform()}) ${osInfo.release || os.release()} ${os.arch()}`
|
|
|
|
}
|
2018-12-30 02:30:51 +00:00
|
|
|
return osLabel
|
|
|
|
},
|
|
|
|
async platform () {
|
2018-11-11 04:40:55 +00:00
|
|
|
const isDockerized = await fs.pathExists('/.dockerenv')
|
|
|
|
if (isDockerized) {
|
2018-12-30 02:30:51 +00:00
|
|
|
return 'docker'
|
2018-11-11 04:40:55 +00:00
|
|
|
}
|
2018-12-30 02:30:51 +00:00
|
|
|
return os.platform()
|
2018-09-04 00:58:54 +00:00
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
ramTotal () {
|
2018-09-04 00:58:54 +00:00
|
|
|
return filesize(os.totalmem())
|
|
|
|
},
|
2020-01-20 02:30:25 +00:00
|
|
|
sslDomain () {
|
|
|
|
return WIKI.config.ssl.enabled && WIKI.config.ssl.provider === `letsencrypt` ? WIKI.config.ssl.domain : null
|
|
|
|
},
|
|
|
|
sslExpirationDate () {
|
|
|
|
return WIKI.config.ssl.enabled && WIKI.config.ssl.provider === `letsencrypt` ? _.get(WIKI.config.letsencrypt, 'payload.expires', null) : null
|
|
|
|
},
|
|
|
|
sslProvider () {
|
|
|
|
return WIKI.config.ssl.enabled ? WIKI.config.ssl.provider : null
|
|
|
|
},
|
|
|
|
sslStatus () {
|
|
|
|
return 'OK'
|
|
|
|
},
|
|
|
|
sslSubscriberEmail () {
|
|
|
|
return WIKI.config.ssl.enabled && WIKI.config.ssl.provider === `letsencrypt` ? WIKI.config.ssl.subscriberEmail : null
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
telemetry () {
|
2019-07-06 21:43:50 +00:00
|
|
|
return WIKI.telemetry.enabled
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
telemetryClientId () {
|
2019-07-06 21:43:50 +00:00
|
|
|
return WIKI.config.telemetry.clientId
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
async upgradeCapable () {
|
2019-09-23 04:37:18 +00:00
|
|
|
return !_.isNil(process.env.UPGRADE_COMPANION)
|
2019-09-22 20:32:39 +00:00
|
|
|
},
|
|
|
|
workingDirectory () {
|
2018-09-04 00:58:54 +00:00
|
|
|
return process.cwd()
|
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
async groupsTotal () {
|
2020-01-26 18:11:33 +00:00
|
|
|
const total = await WIKI.models.groups.query().count('* as total').first()
|
|
|
|
return _.toSafeInteger(total.total)
|
2018-09-04 00:58:54 +00:00
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
async pagesTotal () {
|
2020-01-26 18:11:33 +00:00
|
|
|
const total = await WIKI.models.pages.query().count('* as total').first()
|
|
|
|
return _.toSafeInteger(total.total)
|
2018-09-04 00:58:54 +00:00
|
|
|
},
|
2019-09-22 20:32:39 +00:00
|
|
|
async usersTotal () {
|
2020-01-26 18:11:33 +00:00
|
|
|
const total = await WIKI.models.users.query().count('* as total').first()
|
|
|
|
return _.toSafeInteger(total.total)
|
2020-02-09 05:49:32 +00:00
|
|
|
},
|
|
|
|
async tagsTotal () {
|
|
|
|
const total = await WIKI.models.tags.query().count('* as total').first()
|
|
|
|
return _.toSafeInteger(total.total)
|
2018-03-18 04:34:32 +00:00
|
|
|
}
|
2018-09-04 00:58:54 +00:00
|
|
|
}
|
2018-03-18 04:34:32 +00:00
|
|
|
}
|