UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

224 lines (223 loc) 10.1 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const runtime_1 = require("@tsoa/runtime"); const lodash_1 = require("lodash"); const BaseController_1 = __importDefault(require("../controllers/BaseController")); const interfaces_1 = require("../interfaces"); const mongodb_1 = require("../plugins/mongodb"); const user_utils_1 = require("../plugins/user-utils"); const UserService_1 = require("../services/UserService"); let UserController = class UserController extends BaseController_1.default { constructor() { super(new UserService_1.UserService()); } /** * List of users */ async read(queryParams) { const res = await super.read(); // console.log("[1] res.data :>> ", res.data); if ((0, lodash_1.isArray)(res.data)) { res.data = await (0, user_utils_1.filterUsersByWorkspaceRole)(mongodb_1.MongoDB.toString(this.workspace._id), res.data); res.data = (0, user_utils_1.filterSensitiveInfo)(res.data); } else { res.data = await (0, user_utils_1.filterUsersByWorkspaceRole)(mongodb_1.MongoDB.toString(this.workspace._id), [res.data]); res.data = (0, user_utils_1.filterSensitiveInfo)([res.data]); } // console.log("[2] res.data :>> ", res.data); return res; } async profile(queryParams) { console.log("[USER_CONTROLLER] profile() > this.user :>> ", this.user); if (!this.user.username) { // create username from slug (if not exists) await this.service.updateOne({ _id: this.user._id }, { username: this.user.slug }).catch((e) => { console.error(`Unable to update "username" of this user (${this.user._id}): ${e}`); }); } return this.user ? (0, interfaces_1.respondSuccess)({ data: this.user }) : (0, interfaces_1.respondFailure)(`Unauthenticated.`); } async create(body, queryParams) { try { const newUser = await this.service.create(body, this.options); return newUser ? (0, interfaces_1.respondSuccess)({ data: newUser }) : (0, interfaces_1.respondFailure)(`Failed to create user.`); } catch (e) { return (0, interfaces_1.respondFailure)(`Failed to create user: ${e}`); } } async update(body, queryParams) { // console.log("body.roles :>> ", body.roles); try { if (body.roles) { try { // find list of affected users const users = await this.service.find(this.filter, { populate: ["roles"] }); users.forEach(async (user) => { if ((0, lodash_1.isArray)(body.roles)) { await Promise.all(body.roles.map((roleId) => (0, user_utils_1.assignRoleWithoutCheckingPermissions)(mongodb_1.MongoDB.toString(roleId), user, this.ownership))); } else if (mongodb_1.MongoDB.isValidObjectId(body.roles)) { const roleId = body.roles; return (0, user_utils_1.assignRoleWithoutCheckingPermissions)(mongodb_1.MongoDB.toString(roleId), user, this.ownership); } }); delete body.roles; } catch (e) { return (0, interfaces_1.respondFailure)(`Unable to update role: ${e}`); } } // ! [MAGIC] if the item to be updated is the current logged in user -> allow it to happen! if (this.filter.owner && mongodb_1.MongoDB.toString(this.filter.owner) === mongodb_1.MongoDB.toString(this.user._id)) delete this.filter.owner; const updatedUsers = await this.service.update(this.filter, body, this.options); return updatedUsers && updatedUsers.length > 0 ? (0, interfaces_1.respondSuccess)({ data: updatedUsers }) : (0, interfaces_1.respondFailure)(`Failed to update users.`); } catch (e) { return (0, interfaces_1.respondFailure)(`Failed to update users: ${e}`); } } delete(queryParams) { return super.delete(); } async assignRole(body) { try { if (!body.roleId) throw new Error(`Param "roleId" is required.`); if (!body.userId) throw new Error(`Param "userId" is required.`); const { user, role } = await (0, user_utils_1.assignRoleByID)(body.roleId, body.userId); return (0, interfaces_1.respondSuccess)({ data: { user, role } }); } catch (e) { return (0, interfaces_1.respondFailure)(e.toString()); } } async joinWorkspace(body) { // console.log("body :>> ", body); try { const user = await this.service.joinWorkspace(body, this.options); return (0, interfaces_1.respondSuccess)({ data: user }); } catch (e) { console.log(e); return (0, interfaces_1.respondFailure)({ msg: `Failed to join a workspace: ${e.message}` }); } } /** * Update user's access permissions * @param body - Example: `{ userId: "000", resource: { "projects": "1,2,3,4", "apps": "4,5,6" } }` * @returns */ async updateAccessPermissions(body) { try { if (!body.userSlug) throw new Error(`Param "userSlug" is required.`); if (!body.resource) throw new Error(`Param "resource" is required.`); const { userSlug, resource } = body; const updatedUser = await this.service.updateAccessPermissions(userSlug, resource); return (0, interfaces_1.respondSuccess)({ data: updatedUser }); } catch (e) { return (0, interfaces_1.respondFailure)(e.toString()); } } }; __decorate([ (0, runtime_1.Security)("api_key"), (0, runtime_1.Security)("jwt"), (0, runtime_1.Get)("/"), __param(0, (0, runtime_1.Queries)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], UserController.prototype, "read", null); __decorate([ (0, runtime_1.Security)("api_key"), (0, runtime_1.Security)("jwt"), (0, runtime_1.Get)("/profile"), __param(0, (0, runtime_1.Queries)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], UserController.prototype, "profile", null); __decorate([ (0, runtime_1.Security)("api_key2"), (0, runtime_1.Security)("jwt"), (0, runtime_1.Post)("/"), __param(0, (0, runtime_1.Body)()), __param(1, (0, runtime_1.Queries)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserController.prototype, "create", null); __decorate([ (0, runtime_1.Security)("api_key"), (0, runtime_1.Security)("jwt"), (0, runtime_1.Patch)("/"), __param(0, (0, runtime_1.Body)()), __param(1, (0, runtime_1.Queries)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserController.prototype, "update", null); __decorate([ (0, runtime_1.Security)("api_key"), (0, runtime_1.Security)("jwt"), (0, runtime_1.Delete)("/"), __param(0, (0, runtime_1.Queries)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], UserController.prototype, "delete", null); __decorate([ (0, runtime_1.Security)("api_key"), (0, runtime_1.Security)("jwt"), (0, runtime_1.Patch)("/assign-role"), __param(0, (0, runtime_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], UserController.prototype, "assignRole", null); __decorate([ (0, runtime_1.Security)("api_key"), (0, runtime_1.Security)("jwt"), (0, runtime_1.Patch)("/join-workspace"), __param(0, (0, runtime_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], UserController.prototype, "joinWorkspace", null); __decorate([ (0, runtime_1.Security)("api_key"), (0, runtime_1.Security)("jwt"), (0, runtime_1.Patch)("/permissions"), __param(0, (0, runtime_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], UserController.prototype, "updateAccessPermissions", null); UserController = __decorate([ (0, runtime_1.Tags)("User"), (0, runtime_1.Route)("user"), __metadata("design:paramtypes", []) ], UserController); exports.default = UserController;