|
|
@ -8,6 +8,8 @@ const fs = require('fs-extra') |
|
|
|
const moment = require('moment') |
|
|
|
const graphHelper = require('../../helpers/graph') |
|
|
|
const request = require('request-promise') |
|
|
|
const crypto = require('crypto') |
|
|
|
const nanoid = require('nanoid/non-secure/generate') |
|
|
|
|
|
|
|
/* global WIKI */ |
|
|
|
|
|
|
@ -85,28 +87,106 @@ module.exports = { |
|
|
|
return graphHelper.generateError(err) |
|
|
|
} |
|
|
|
}, |
|
|
|
/** |
|
|
|
* Import Users from a v1 installation |
|
|
|
*/ |
|
|
|
async importUsersFromV1(obj, args, context) { |
|
|
|
try { |
|
|
|
const MongoClient = require('mongodb').MongoClient |
|
|
|
if (args.mongoDbConnString && args.mongoDbConnString.length > 10) { |
|
|
|
// -> Connect to DB
|
|
|
|
|
|
|
|
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' } }) |
|
|
|
|
|
|
|
const curDateISO = new Date().toISOString() |
|
|
|
|
|
|
|
let failed = [] |
|
|
|
let usersCount = 0 |
|
|
|
let groupsCount = 0 |
|
|
|
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
|
|
|
|
|
|
|
|
while (await userCursor.hasNext()) { |
|
|
|
const usr = await userCursor.next() |
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
try { |
|
|
|
await WIKI.models.users.createNewUser({ |
|
|
|
providerKey: usr.provider, |
|
|
|
email: usr.email, |
|
|
|
name: usr.name, |
|
|
|
passwordRaw: usr.password, |
|
|
|
groups: (usrGroup.length > 0) ? usrGroup : assignableGroups, |
|
|
|
mustChangePassword: false, |
|
|
|
sendWelcomeEmail: false |
|
|
|
}) |
|
|
@ -121,6 +201,12 @@ module.exports = { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// -> Reload group permissions
|
|
|
|
|
|
|
|
if (args.groupMode !== `NONE`) { |
|
|
|
await WIKI.auth.reloadGroups() |
|
|
|
} |
|
|
|
|
|
|
|
client.close() |
|
|
|
return { |
|
|
|
responseResult: graphHelper.generateSuccess('Import completed.'), |
|
|
|