Skip to content

Commit

Permalink
[bug] Fix bug 49946
Browse files Browse the repository at this point in the history
  • Loading branch information
konovalovsergey committed Apr 30, 2021
1 parent 98ae4f7 commit f60c332
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 30 deletions.
40 changes: 28 additions & 12 deletions DocService/sources/DocsCoServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,18 @@ function* getChangesIndex(docId) {
}
return res;
}

const hasChanges = co.wrap(function*(docId) {
let puckerIndex = yield* getChangesIndex(docId);
if (0 === puckerIndex) {
let selectRes = yield taskResult.select(docId);
if (selectRes.length > 0 && selectRes[0].password) {
return sqlBase.DocumentPassword.prototype.hasPasswordChanges(docId, selectRes[0].password);
}
return false;
}
return true;
});
function* setForceSave(docId, forceSave, cmd, success) {
let forceSaveType = forceSave.getType();
if (commonDefines.c_oAscForceSaveTypes.Form !== forceSaveType) {
Expand Down Expand Up @@ -781,6 +793,16 @@ let startForceSave = co.wrap(function*(docId, type, opt_userdata, opt_userId, op
logger.debug('startForceSave end:docId = %s', docId);
return res;
});
let resetForceSaveAfterChanges = co.wrap(function*(docId, newChangesLastTime, puckerIndex, baseUrl) {
//last save
if (newChangesLastTime) {
yield editorData.setForceSave(docId, newChangesLastTime, puckerIndex, baseUrl);
if (cfgForceSaveEnable) {
let expireAt = newChangesLastTime + cfgForceSaveInterval;
yield editorData.addForceSaveTimerNX(docId, expireAt);
}
}
});
function* startRPC(conn, responseKey, data) {
let docId = conn.docId;
logger.debug('startRPC start responseKey:%s , %j:docId = %s', responseKey, data, docId);
Expand Down Expand Up @@ -856,8 +878,8 @@ function* sendStatusDocument(docId, bChangeBase, opt_userAction, opt_userIndex,
var status = c_oAscServerStatus.Editing;
var participants = yield* getOriginalParticipantsId(docId);
if (0 === participants.length) {
var puckerIndex = yield* getChangesIndex(docId);
if (!(puckerIndex > 0) || opt_forceClose) {
let bHasChanges = yield hasChanges(docId);
if (!bHasChanges || opt_forceClose) {
status = c_oAscServerStatus.Closed;
}
}
Expand Down Expand Up @@ -1180,11 +1202,12 @@ exports.hasEditors = hasEditors;
exports.getEditorsCountPromise = co.wrap(getEditorsCount);
exports.getCallback = getCallback;
exports.getIsShutdown = getIsShutdown;
exports.getChangesIndexPromise = co.wrap(getChangesIndex);
exports.hasChanges = hasChanges;
exports.cleanDocumentOnExitPromise = co.wrap(cleanDocumentOnExit);
exports.cleanDocumentOnExitNoChangesPromise = co.wrap(cleanDocumentOnExitNoChanges);
exports.setForceSave = setForceSave;
exports.startForceSave = startForceSave;
exports.resetForceSaveAfterChanges = resetForceSaveAfterChanges;
exports.checkJwt = checkJwt;
exports.getRequestParams = getRequestParams;
exports.checkJwtHeader = checkJwtHeader;
Expand Down Expand Up @@ -1399,8 +1422,7 @@ exports.install = function(server, callbackFunction) {
// Только если редактируем
if (false === isView) {
bHasEditors = yield* hasEditors(docId, hvals);
var puckerIndex = yield* getChangesIndex(docId);
bHasChanges = puckerIndex > 0;
bHasChanges = yield hasChanges(docId);

let needSendStatus = true;
if (conn.encrypted) {
Expand Down Expand Up @@ -2539,13 +2561,7 @@ exports.install = function(server, callbackFunction) {
// Автоматически снимаем lock сами и посылаем индекс для сохранения
yield* unSaveLock(conn, changesIndex, newChangesLastTime);
//last save
if (newChangesLastTime) {
yield editorData.setForceSave(docId, newChangesLastTime, puckerIndex, utils.getBaseUrlByConnection(conn));
if (cfgForceSaveEnable) {
let expireAt = newChangesLastTime + cfgForceSaveInterval;
yield editorData.addForceSaveTimerNX(docId, expireAt);
}
}
yield resetForceSaveAfterChanges(docId, newChangesLastTime, puckerIndex, utils.getBaseUrlByConnection(conn));
} else {
let changesToSend = arrNewDocumentChanges;
if(changesToSend.length > cfgPubSubMaxChanges) {
Expand Down
56 changes: 56 additions & 0 deletions DocService/sources/baseConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,59 @@ UserCallback.prototype.getCallbackByUserIndex = function(docId, callbacksStr, op
return callbackUrl;
};
exports.UserCallback = UserCallback;

function DocumentPassword() {
this.password = undefined;
this.userId = undefined;
this.userIndex = undefined;
}
DocumentPassword.prototype.fromString = function(passwordStr){
var parsed = JSON.parse(passwordStr);
this.fromValues(parsed.password, parsed.userId, parsed.userIndex);
};
DocumentPassword.prototype.fromValues = function(password, userId, userIndex){
if(null !== password){
this.password = password;
}
if(null !== userId){
this.userId = userId;
}
if(null !== userIndex){
this.userIndex = userIndex;
}
};
DocumentPassword.prototype.delimiter = String.fromCharCode(5);
DocumentPassword.prototype.toSQLInsert = function(){
return this.delimiter + JSON.stringify(this);
};
DocumentPassword.prototype.isInitial = function(){
return !this.userId;
};
DocumentPassword.prototype.getDocPassword = function(docId, docPasswordStr) {
let res = {initial: undefined, current: undefined, userId: undefined, userIndex: undefined};
if (docPasswordStr) {
logger.debug("getDocPassword: docId = %s passwords = %s", docId, docPasswordStr);
let passwords = docPasswordStr.split(UserCallback.prototype.delimiter);

for (let i = 1; i < passwords.length; ++i) {
let password = new DocumentPassword();
password.fromString(passwords[i]);
if (password.isInitial()) {
res.initial = password.password;
}
res.current = password.password;
res.userId = password.userId;
res.userIndex = password.userIndex;
}
}
return res;
};
DocumentPassword.prototype.getCurPassword = function(docId, docPasswordStr) {
let docPassword = this.getDocPassword(docId, docPasswordStr);
return docPassword.current;
};
DocumentPassword.prototype.hasPasswordChanges = function(docId, docPasswordStr) {
let docPassword = this.getDocPassword(docId, docPasswordStr);
return docPassword.initial !== docPassword.current;
};
exports.DocumentPassword = DocumentPassword;
40 changes: 29 additions & 11 deletions DocService/sources/canvasservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function* getOutputData(cmd, outputData, key, optConn, optAdditionalOutput, opt_
let row = selectRes[0];
status = row.status;
statusInfo = row.status_info;
password = row.password;
password = sqlBase.DocumentPassword.prototype.getCurPassword(key, row.password);
creationDate = row.created_at && row.created_at.getTime();
}
switch (status) {
Expand Down Expand Up @@ -419,7 +419,7 @@ function* commandReopen(conn, cmd, outputData) {
let selectRes = yield taskResult.select(cmd.getDocId());
if (selectRes.length > 0) {
let row = selectRes[0];
if (row.password) {
if (sqlBase.DocumentPassword.prototype.getCurPassword(cmd.getDocId(), row.password)) {
logger.debug('commandReopen has password: docId = %s', cmd.getDocId());
yield* commandOpenFillOutput(conn, cmd, outputData, false);
docsCoServer.modifyConnectionForPassword(conn, constants.FILE_STATUS_OK === outputData.getStatus());
Expand Down Expand Up @@ -502,8 +502,13 @@ function* commandSfctByCmd(cmd, opt_priority, opt_expiration, opt_queue) {
yield* addRandomKeyTaskCmd(cmd);
var selectRes = yield taskResult.select(cmd.getDocId());
var row = selectRes.length > 0 ? selectRes[0] : null;
if (row && row.password) {
cmd.setSavePassword(row.password);
let docPassword = row && sqlBase.DocumentPassword.prototype.getDocPassword(cmd.getDocId(), row.password);
if (docPassword.current) {
cmd.setSavePassword(docPassword.current);
if (docPassword.userId) {
cmd.setUserId(docPassword.userId);
cmd.setUserIndex(docPassword.userIndex);
}
}
var queueData = getSaveTask(cmd);
queueData.setFromChanges(true);
Expand Down Expand Up @@ -699,7 +704,7 @@ function* commandSetPassword(conn, cmd, outputData) {
if (selectRes.length > 0) {
let row = selectRes[0];
hasPasswordCol = undefined !== row.password;
if (taskResult.FileStatus.Ok === row.status && row.password) {
if (taskResult.FileStatus.Ok === row.status && sqlBase.DocumentPassword.prototype.getCurPassword(cmd.getDocId(), row.password)) {
hasDocumentPassword = true;
}
}
Expand All @@ -712,13 +717,20 @@ function* commandSetPassword(conn, cmd, outputData) {
var task = new taskResult.TaskResultData();
task.key = cmd.getDocId();
task.password = cmd.getPassword() || "";
if (conn.user) {
task.innerUserId = conn.user.idOriginal;
task.innerUserIndex = utils.getIndexFromUserId(conn.user.id, conn.user.idOriginal);
}

var upsertRes = yield taskResult.updateIf(task, updateMask);
if (upsertRes.affectedRows > 0) {
outputData.setStatus('ok');
if (!conn.isEnterCorrectPassword) {
docsCoServer.modifyConnectionForPassword(conn, true);
}
let newChangesLastDate = new Date();
newChangesLastDate.setMilliseconds(0);//remove milliseconds avoid issues with MySQL datetime rounding
yield docsCoServer.resetForceSaveAfterChanges(cmd.getDocId(), newChangesLastDate.getTime(), 0x7FFFFFFF, utils.getBaseUrlByConnection(conn));
} else {
logger.debug('commandSetPassword sql update error: docId = %s', cmd.getDocId());
outputData.setStatus('err');
Expand Down Expand Up @@ -815,7 +827,7 @@ function* commandSfcCallback(cmd, isSfcm, isEncrypted) {
} else {
isError = true;
}
if (getRes) {
if (getRes && userLastChangeId) {
logger.debug('Callback commandSfcCallback: docId = %s callback = %s', docId, getRes.server.href);
var outputSfc = new commonDefines.OutputSfcData();
outputSfc.setKey(docId);
Expand Down Expand Up @@ -954,7 +966,7 @@ function* commandSfcCallback(cmd, isSfcm, isEncrypted) {
}
}
} else {
logger.warn('Empty Callback commandSfcCallback: docId = %s', docId);
logger.warn('Empty Callback or userLastChangeId=%s commandSfcCallback: docId = %s', userLastChangeId, docId);
storeForgotten = true;
}
if (undefined !== updateIfTask && !isSfcm) {
Expand Down Expand Up @@ -1172,8 +1184,9 @@ exports.downloadAs = function(req, res) {
}
var selectRes = yield taskResult.select(docId);
var row = selectRes.length > 0 ? selectRes[0] : null;
if (row && row.password && !cmd.getWithoutPassword()) {
cmd.setSavePassword(row.password);
let password = row && sqlBase.DocumentPassword.prototype.getCurPassword(cmd.getDocId(), row.password);
if (password && !cmd.getWithoutPassword()) {
cmd.setSavePassword(password);
}
cmd.setData(req.body);
var outputData = new OutputData(cmd.getCommand());
Expand Down Expand Up @@ -1289,8 +1302,13 @@ exports.saveFromChanges = function(docId, statusInfo, optFormat, opt_userId, opt
cmd.setStatusInfoIn(statusInfo);
cmd.setUserActionId(opt_userId);
cmd.setUserActionIndex(opt_userIndex);
if (row.password) {
cmd.setSavePassword(row.password);
let docPassword = row && sqlBase.DocumentPassword.prototype.getDocPassword(cmd.getDocId(), row.password);
if (docPassword.current) {
cmd.setSavePassword(docPassword.current);
if (docPassword.userId) {
cmd.setUserId(docPassword.userId);
cmd.setUserIndex(docPassword.userIndex);
}
}
yield* addRandomKeyTaskCmd(cmd);
var queueData = getSaveTask(cmd);
Expand Down
4 changes: 2 additions & 2 deletions DocService/sources/gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ var checkDocumentExpire = function() {
for (var i = 0; i < expiredKeys.length; ++i) {
var docId = expiredKeys[i];
if (docId) {
var puckerIndex = yield docsCoServer.getChangesIndexPromise(docId);
if (puckerIndex > 0) {
var hasChanges = yield docsCoServer.hasChanges(docId);
if (hasChanges) {
yield docsCoServer.createSaveTimerPromise(docId, null, null, queue, true);
startSaveCount++;
} else {
Expand Down
2 changes: 1 addition & 1 deletion DocService/sources/mySqlBaseConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let addSqlParam = function (val, values) {
};
exports.addSqlParameter = addSqlParam;
let concatParams = function (val1, val2) {
return `CONCAT(${val1}, ${val2})`;
return `CONCAT(COALESCE(${val1}, ''), COALESCE(${val2}, ''))`;
};
exports.concatParams = concatParams;

Expand Down
9 changes: 7 additions & 2 deletions DocService/sources/taskresult.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ function TaskResultData() {
this.callback = null;
this.baseurl = null;
this.password = null;

this.innerUserId = null;//not a DB field
this.innerUserIndex = null;//not a DB field
}
TaskResultData.prototype.completeDefaults = function() {
if (!this.key) {
Expand Down Expand Up @@ -152,8 +155,10 @@ function toUpdateArray(task, updateTime, isMask, values) {
res.push(`baseurl=${sqlParam}`);
}
if (null != task.password) {
let sqlParam = addSqlParam(task.password, values);
res.push(`password=${sqlParam}`);
var documentPassword = new sqlBase.DocumentPassword();
documentPassword.fromValues(task.password, task.innerUserId, task.innerUserIndex);
let sqlParam = addSqlParam(documentPassword.toSQLInsert(), values);
res.push(`password=${concatParams('password', sqlParam)}`);
}
return res;
}
Expand Down
6 changes: 4 additions & 2 deletions FileConverter/sources/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,10 @@ function* processChanges(tempDirs, cmd, authorProps) {
changesAuthor = forceSave.getAuthorUserId();
changesIndex = forceSave.getAuthorUserIndex();
}
cmd.setUserId(changesAuthor);
cmd.setUserIndex(changesIndex);
if (null != changesAuthor && null != changesIndex) {
cmd.setUserId(changesAuthor);
cmd.setUserIndex(changesIndex);
}
fs.writeFileSync(path.join(tempDirs.result, 'changesHistory.json'), JSON.stringify(changesHistory), 'utf8');
return res;
}
Expand Down

0 comments on commit f60c332

Please sign in to comment.