@intuitionrobotics/permissions
Version:
242 lines • 11.9 kB
JavaScript
;
/*
* ts-common is the basic building blocks of our typescript projects
*
* Copyright (C) 2020 Intuition Robotics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiPermissionsDB = exports.AccessLevelPermissionsDB = exports.DomainPermissionsDB = exports.ProjectPermissionsDB = exports.ApiDB_Class = exports.LevelDB_Class = exports.DomainDB_Class = exports.ProjectDB_Class = exports.validateStringWithDashesAndSlash = exports.validateProjectName = void 0;
const backend_1 = require("@intuitionrobotics/db-api-generator/backend");
const _imports_1 = require("../_imports");
const ts_common_1 = require("@intuitionrobotics/ts-common");
const assign_1 = require("./assign");
const exceptions_1 = require("@intuitionrobotics/thunderstorm/app-backend/exceptions");
const AccountModule_1 = require("@intuitionrobotics/user-account/app-backend/modules/AccountModule");
const validateProjectId = (0, ts_common_1.validateRegexp)(/^[a-z-]{3,20}$/);
exports.validateProjectName = (0, ts_common_1.validateRegexp)(/^[A-Za-z- ]{3,20}$/);
exports.validateStringWithDashesAndSlash = (0, ts_common_1.validateRegexp)(/^[0-9A-Za-z-:/]+$/);
class ProjectDB_Class extends backend_1.BaseDB_ApiGenerator {
constructor() {
super(_imports_1.CollectionName_Projects, ProjectDB_Class._validator, "project", "ProjectDB");
}
preUpsertProcessing(transaction, dbInstance, request) {
return __awaiter(this, void 0, void 0, function* () {
if (request) {
const account = yield AccountModule_1.AccountModule.validateSession(request, []);
dbInstance._audit = (0, ts_common_1.auditBy)(account.email);
}
});
}
apis(pathPart) {
return [
// new ServerApi_Delete(this, pathPart),
new backend_1.ServerApi_Query(this, pathPart),
new backend_1.ServerApi_Unique(this, pathPart),
];
}
}
exports.ProjectDB_Class = ProjectDB_Class;
ProjectDB_Class._validator = {
_id: validateProjectId,
name: exports.validateProjectName,
customKeys: undefined,
_audit: undefined
};
class DomainDB_Class extends backend_1.BaseDB_ApiGenerator {
constructor() {
super(_imports_1.CollectionName_Domain, DomainDB_Class._validator, "domain", "DomainDB");
this.setLockKeys(['projectId']);
}
assertDeletion(transaction, dbInstance) {
return __awaiter(this, void 0, void 0, function* () {
const accessLevels = yield exports.AccessLevelPermissionsDB.query({ where: { domainId: dbInstance._id } });
if (accessLevels.length) {
throw new exceptions_1.ApiException(403, 'You trying delete domain that associated with accessLevels, you need delete the accessLevels first');
}
});
}
preUpsertProcessing(transaction, dbInstance, request) {
return __awaiter(this, void 0, void 0, function* () {
yield exports.ProjectPermissionsDB.queryUnique({ _id: dbInstance.projectId });
if (request) {
const account = yield AccountModule_1.AccountModule.validateSession(request, []);
dbInstance._audit = (0, ts_common_1.auditBy)(account.email);
}
});
}
}
exports.DomainDB_Class = DomainDB_Class;
DomainDB_Class._validator = {
_id: backend_1.validateOptionalId,
projectId: validateProjectId,
namespace: backend_1.validateNameWithDashesAndDots,
_audit: undefined
};
class LevelDB_Class extends backend_1.BaseDB_ApiGenerator {
constructor() {
super(_imports_1.CollectionName_Level, LevelDB_Class._validator, "level", "LevelDB");
this.setLockKeys(['domainId']);
}
internalFilter(item) {
const { domainId, name, value } = item;
return [{ domainId, name }, { domainId, value }];
}
preUpsertProcessing(transaction, dbInstance, request) {
return __awaiter(this, void 0, void 0, function* () {
yield exports.DomainPermissionsDB.queryUnique({ _id: dbInstance.domainId });
if (request) {
const account = yield AccountModule_1.AccountModule.validateSession(request, []);
dbInstance._audit = (0, ts_common_1.auditBy)(account.email);
}
});
}
upsertImpl_Read(transaction, dbInstance, request) {
const _super = Object.create(null, {
upsertImpl_Read: { get: () => super.upsertImpl_Read }
});
return __awaiter(this, void 0, void 0, function* () {
const existDbLevel = yield transaction.queryUnique(this.collection, { where: { _id: dbInstance._id } });
const groups = yield assign_1.GroupPermissionsDB.query({ where: { accessLevelIds: { $ac: dbInstance._id } } });
const returnWrite = yield _super.upsertImpl_Read.call(this, transaction, dbInstance, request);
if (existDbLevel) {
const callbackfn = (group) => {
var _a, _b;
const index = (_a = group.accessLevelIds) === null || _a === void 0 ? void 0 : _a.indexOf(dbInstance._id);
if (index === undefined)
throw new ts_common_1.MUSTNeverHappenException("Query said it does exists!!");
const accessLevel = (_b = group.__accessLevels) === null || _b === void 0 ? void 0 : _b[index];
if (accessLevel === undefined)
throw new ts_common_1.MUSTNeverHappenException("Query said it does exists!!");
accessLevel.value = dbInstance.value;
};
const asyncs = [];
asyncs.push(...groups.map((group) => __awaiter(this, void 0, void 0, function* () {
yield assign_1.GroupPermissionsDB.validateImpl(group);
yield assign_1.GroupPermissionsDB.assertUniqueness(transaction, group);
callbackfn(group);
})));
const upsertGroups = yield transaction.upsertAll_Read(assign_1.GroupPermissionsDB.collection, groups);
yield Promise.all(asyncs);
// --- writes part
upsertGroups();
}
return returnWrite;
});
}
assertDeletion(transaction, dbInstance) {
return __awaiter(this, void 0, void 0, function* () {
const groups = yield assign_1.GroupPermissionsDB.query({ where: { accessLevelIds: { $ac: dbInstance._id } } });
const apis = yield exports.ApiPermissionsDB.query({ where: { accessLevelIds: { $ac: dbInstance._id } } });
if (groups.length || apis.length)
throw new exceptions_1.ApiException(403, 'You trying delete access level that associated with users/groups/apis, you need delete the associations first');
});
}
setUpdatedLevel(dbLevel, units) {
units.forEach(unit => {
var _a;
let hasGroupDomainLevel = false;
const updatedLevels = ((_a = unit.__accessLevels) === null || _a === void 0 ? void 0 : _a.map(level => {
if (level.domainId === dbLevel.domainId) {
level.value = dbLevel.value;
hasGroupDomainLevel = true;
}
return level;
})) || [];
if (!hasGroupDomainLevel) {
updatedLevels.push({ domainId: dbLevel.domainId, value: dbLevel.value });
}
unit.__accessLevels = updatedLevels;
});
}
}
exports.LevelDB_Class = LevelDB_Class;
LevelDB_Class._validator = {
_id: backend_1.validateOptionalId,
domainId: backend_1.validateUniqueId,
name: backend_1.validateStringWithDashes,
value: (0, ts_common_1.validateRange)([[0, 1000]]),
_audit: undefined
};
class ApiDB_Class extends backend_1.BaseDB_ApiGenerator {
constructor() {
super(_imports_1.CollectionName_Api, ApiDB_Class._validator, "api", "ApiDB");
this.setLockKeys(['projectId', "path"]);
}
externalFilter(item) {
const { projectId, path } = item;
return { projectId, path };
}
internalFilter(item) {
const { projectId, path } = item;
return [{ projectId, path }];
}
preUpsertProcessing(transaction, dbInstance, request) {
return __awaiter(this, void 0, void 0, function* () {
if (request) {
const account = yield AccountModule_1.AccountModule.validateSession(request, []);
dbInstance._audit = (0, ts_common_1.auditBy)(account.email);
}
yield exports.ProjectPermissionsDB.queryUnique({ _id: dbInstance.projectId });
// need to assert that all the permissions levels exists in the db
const _permissionsIds = dbInstance.accessLevelIds;
if (!_permissionsIds || _permissionsIds.length <= 0)
return;
const permissionsIds = (0, ts_common_1.filterDuplicates)(_permissionsIds);
yield Promise.all(permissionsIds.map(id => exports.AccessLevelPermissionsDB.queryUnique({ _id: id })));
dbInstance.accessLevelIds = permissionsIds;
});
}
registerApis(projectId, routes) {
return this.runInTransaction((transaction) => __awaiter(this, void 0, void 0, function* () {
const existingProjectApis = yield exports.ApiPermissionsDB.query({ where: { projectId: projectId } });
const apisToAdd = routes
.filter(path => !existingProjectApis.find(api => api.path === path))
.map(path => ({ path, projectId: projectId }));
return this.upsertAll(apisToAdd, transaction);
}));
}
apis(pathPart) {
return [
new backend_1.ServerApi_Delete(this, pathPart),
new backend_1.ServerApi_Query(this, pathPart),
new backend_1.ServerApi_Unique(this, pathPart),
new backend_1.ServerApi_Update(this, pathPart),
];
}
}
exports.ApiDB_Class = ApiDB_Class;
ApiDB_Class._validator = {
_id: backend_1.validateOptionalId,
projectId: validateProjectId,
path: exports.validateStringWithDashesAndSlash,
accessLevelIds: (0, ts_common_1.validateArray)(backend_1.validateUniqueId, false),
_audit: undefined,
deprecated: undefined,
onlyForApplication: undefined
};
exports.ProjectPermissionsDB = new ProjectDB_Class();
exports.DomainPermissionsDB = new DomainDB_Class();
exports.AccessLevelPermissionsDB = new LevelDB_Class();
exports.ApiPermissionsDB = new ApiDB_Class();
//# sourceMappingURL=managment.js.map