diff --git a/back/methods/account/change-password.js b/back/methods/account/change-password.js index 25b63b9a8..c0956b193 100644 --- a/back/methods/account/change-password.js +++ b/back/methods/account/change-password.js @@ -5,17 +5,17 @@ module.exports = Self => { accepts: [ { arg: 'id', - type: 'Number', + type: 'number', description: 'The user id', http: {source: 'path'} }, { arg: 'oldPassword', - type: 'String', + type: 'string', description: 'The old password', required: true }, { arg: 'newPassword', - type: 'String', + type: 'string', description: 'The new password', required: true } diff --git a/db/changes/10470-family/00-aclMdb.sql b/db/changes/10470-family/00-aclMdb.sql new file mode 100644 index 000000000..c57f60eb3 --- /dev/null +++ b/db/changes/10470-family/00-aclMdb.sql @@ -0,0 +1,14 @@ +CREATE TABLE `vn`.`mdbBranch` ( + `name` VARCHAR(255), + PRIMARY KEY(`name`) +); + +CREATE TABLE `vn`.`mdbVersion` ( + `app` VARCHAR(255) NOT NULL, + `branchFk` VARCHAR(255) NOT NULL, + `version` INT, + CONSTRAINT `mdbVersion_branchFk` FOREIGN KEY (`branchFk`) REFERENCES `vn`.`mdbBranch` (`name`) ON DELETE CASCADE ON UPDATE CASCADE +); + +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES('MdbVersion', '*', '*', 'ALLOW', 'ROLE', 'developer'); diff --git a/db/changes/10470-family/delete.keep b/db/changes/10470-family/delete.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index c69012f41..c91ba71c2 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2577,3 +2577,12 @@ INSERT INTO `vn`.`machineWorker` (`workerFk`, `machineFk`, `inTimed`, `outTimed` (1106, 2, CURDATE(), NULL), (1106, 2, DATE_ADD(CURDATE(), INTERVAL + 1 DAY), DATE_ADD(CURDATE(), INTERVAL +1 DAY)); +INSERT INTO `vn`.`mdbBranch` (`name`) + VALUES + ('test'), + ('master'); + +INSERT INTO `vn`.`mdbVersion` (`app`, `branchFk`, `version`) + VALUES + ('tpv', 'test', '1'), + ('lab', 'master', '1'); \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 1d80a4b62..4fc5dc811 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,6 +32,7 @@ services: - /mnt/appdata/pdfs:/var/lib/salix/pdfs - /mnt/appdata/dms:/var/lib/salix/dms - /mnt/appdata/image:/var/lib/salix/image + - /mnt/appdata/vn-access:/var/lib/salix/vn-access deploy: replicas: ${BACK_REPLICAS:?} placement: diff --git a/loopback/server/datasources.json b/loopback/server/datasources.json index f51beeb19..5dade9c2e 100644 --- a/loopback/server/datasources.json +++ b/loopback/server/datasources.json @@ -98,5 +98,15 @@ "image/jpg", "video/mp4" ] + }, + "accessStorage": { + "name": "accessStorage", + "connector": "loopback-component-storage", + "provider": "filesystem", + "root": "./storage/access", + "maxFileSize": "524288000", + "allowedContentTypes": [ + "application/x-7z-compressed" + ] } } \ No newline at end of file diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js new file mode 100644 index 000000000..3d54c0250 --- /dev/null +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -0,0 +1,121 @@ +const fs = require('fs-extra'); +const path = require('path'); +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('upload', { + description: 'Upload and attach a access file', + accepts: [ + { + arg: 'appName', + type: 'string', + required: true, + description: 'The app name' + }, + { + arg: 'newVersion', + type: 'number', + required: true, + description: `The new version number` + }, + { + arg: 'branch', + type: 'string', + required: true, + description: `The branch name` + } + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/upload`, + verb: 'POST' + } + }); + + Self.upload = async(ctx, appName, newVersion, branch, options) => { + const models = Self.app.models; + const myOptions = {}; + + const TempContainer = models.TempContainer; + const AccessContainer = models.AccessContainer; + const fileOptions = {}; + + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + let srcFile; + try { + const tempContainer = await TempContainer.container('access'); + const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions); + const files = Object.values(uploaded.files).map(file => { + return file[0]; + }); + const uploadedFile = files[0]; + + const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name); + srcFile = path.join(file.client.root, file.container, file.name); + + const accessContainer = await AccessContainer.container('.archive'); + const destinationFile = path.join( + accessContainer.client.root, accessContainer.name, appName, `${newVersion}.7z`); + + if (process.env.NODE_ENV == 'test') + await fs.unlink(srcFile); + else { + await fs.move(srcFile, destinationFile, { + overwrite: true + }); + await fs.chmod(destinationFile, 0o644); + + const existBranch = await models.MdbBranch.findOne({ + where: {name: branch} + }); + + if (!existBranch) + throw new UserError('Not exist this branch'); + + const branchPath = path.join(accessContainer.client.root, 'branches', branch); + await fs.mkdir(branchPath, {recursive: true}); + + const destinationBranch = path.join(branchPath, `${appName}.7z`); + const destinationRoot = path.join(accessContainer.client.root, `${appName}.7z`); + try { + await fs.unlink(destinationBranch); + } catch (e) {} + await fs.symlink(destinationFile, destinationBranch); + + if (branch == 'master') { + try { + await fs.unlink(destinationRoot); + } catch (e) {} + await fs.symlink(destinationFile, destinationRoot); + } + } + + await models.MdbVersion.upsert({ + app: appName, + branchFk: branch, + version: newVersion + }); + + if (tx) await tx.commit(); + } catch (e) { + if (tx) await tx.rollback(); + + if (fs.existsSync(srcFile)) + await fs.unlink(srcFile); + + throw e; + } + }; +}; diff --git a/modules/mdb/back/model-config.json b/modules/mdb/back/model-config.json new file mode 100644 index 000000000..d5be8de87 --- /dev/null +++ b/modules/mdb/back/model-config.json @@ -0,0 +1,11 @@ +{ + "MdbBranch": { + "dataSource": "vn" + }, + "MdbVersion": { + "dataSource": "vn" + }, + "AccessContainer": { + "dataSource": "accessStorage" + } +} diff --git a/modules/mdb/back/models/mdb-container.json b/modules/mdb/back/models/mdb-container.json new file mode 100644 index 000000000..a927b30f1 --- /dev/null +++ b/modules/mdb/back/models/mdb-container.json @@ -0,0 +1,10 @@ +{ + "name": "AccessContainer", + "base": "Container", + "acls": [{ + "accessType": "*", + "principalType": "ROLE", + "principalId": "developer", + "permission": "ALLOW" + }] +} \ No newline at end of file diff --git a/modules/mdb/back/models/mdbBranch.json b/modules/mdb/back/models/mdbBranch.json new file mode 100644 index 000000000..486dfaf25 --- /dev/null +++ b/modules/mdb/back/models/mdbBranch.json @@ -0,0 +1,16 @@ +{ + "name": "MdbBranch", + "base": "VnModel", + "options": { + "mysql": { + "table": "mdbBranch" + } + }, + "properties": { + "name": { + "id": true, + "type": "string", + "description": "Identifier" + } + } +} \ No newline at end of file diff --git a/modules/mdb/back/models/mdbVersion.js b/modules/mdb/back/models/mdbVersion.js new file mode 100644 index 000000000..b36ee2a60 --- /dev/null +++ b/modules/mdb/back/models/mdbVersion.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/mdbVersion/upload')(Self); +}; diff --git a/modules/mdb/back/models/mdbVersion.json b/modules/mdb/back/models/mdbVersion.json new file mode 100644 index 000000000..02635ff8a --- /dev/null +++ b/modules/mdb/back/models/mdbVersion.json @@ -0,0 +1,26 @@ +{ + "name": "MdbVersion", + "base": "VnModel", + "options": { + "mysql": { + "table": "mdbVersion" + } + }, + "properties": { + "app": { + "type": "string", + "description": "The app name", + "id": true + }, + "version": { + "type": "number" + } + }, + "relations": { + "branch": { + "type": "belongsTo", + "model": "MdbBranch", + "foreignKey": "branchFk" + } + } +} \ No newline at end of file diff --git a/storage/access/.keep b/storage/access/.keep new file mode 100644 index 000000000..8e2556896 --- /dev/null +++ b/storage/access/.keep @@ -0,0 +1 @@ +Forces tmp folder creation! \ No newline at end of file