Skip to content

Commit

Permalink
fixed issue CMU-17313Q#52 - file translation complete
Browse files Browse the repository at this point in the history
  • Loading branch information
dananaimi committed Aug 28, 2023
1 parent 0b7891e commit a87d472
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 96 deletions.
226 changes: 130 additions & 96 deletions src/api/categories.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,136 @@
'use strict';

const categories = require('../categories');
const events = require('../events');
const user = require('../user');
const groups = require('../groups');
const privileges = require('../privileges');

const categoriesAPI = module.exports;

categoriesAPI.get = async function (caller, data) {
const [userPrivileges, category] = await Promise.all([
privileges.categories.get(data.cid, caller.uid),
categories.getCategoryData(data.cid),
]);
if (!category || !userPrivileges.read) {
return null;
}

return category;
};

categoriesAPI.create = async function (caller, data) {
const response = await categories.create(data);
const categoryObjs = await categories.getCategories([response.cid], caller.uid);
return categoryObjs[0];
};

categoriesAPI.update = async function (caller, data) {
if (!data) {
throw new Error('[[error:invalid-data]]');
}
await categories.update(data);
};

categoriesAPI.delete = async function (caller, data) {
const name = await categories.getCategoryField(data.cid, 'name');
await categories.purge(data.cid, caller.uid);
await events.log({
type: 'category-purge',
uid: caller.uid,
ip: caller.ip,
cid: data.cid,
name: name,
"use strict";
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());
});
};

categoriesAPI.getPrivileges = async (caller, cid) => {
let responsePayload;

if (cid === 'admin') {
responsePayload = await privileges.admin.list(caller.uid);
} else if (!parseInt(cid, 10)) {
responsePayload = await privileges.global.list();
} else {
responsePayload = await privileges.categories.list(cid);
}

return responsePayload;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};

categoriesAPI.setPrivilege = async (caller, data) => {
const [userExists, groupExists] = await Promise.all([
user.exists(data.member),
groups.exists(data.member),
]);

if (!userExists && !groupExists) {
throw new Error('[[error:no-user-or-group]]');
}
const privs = Array.isArray(data.privilege) ? data.privilege : [data.privilege];
const type = data.set ? 'give' : 'rescind';
if (!privs.length) {
throw new Error('[[error:invalid-data]]');
}
if (parseInt(data.cid, 10) === 0) {
const adminPrivList = await privileges.admin.getPrivilegeList();
const adminPrivs = privs.filter(priv => adminPrivList.includes(priv));
if (adminPrivs.length) {
await privileges.admin[type](adminPrivs, data.member);
Object.defineProperty(exports, "__esModule", { value: true });
exports.delete = exports.setPrivilege = exports.getPrivileges = exports._delete = exports.update = exports.create = exports.get = void 0;
const categories_1 = __importDefault(require("../categories"));
const events_1 = __importDefault(require("../events"));
const user_1 = __importDefault(require("../user"));
const groups_1 = __importDefault(require("../groups"));
const privileges_1 = __importDefault(require("../privileges"));
function get(caller, data) {
return __awaiter(this, void 0, void 0, function* () {
const userPrivileges = yield privileges_1.default.categories.get(data.cid, caller.uid);
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const category = yield categories_1.default.getCategoryData(data.cid);
if (!category || !userPrivileges.read) {
return null;
}
const globalPrivList = await privileges.global.getPrivilegeList();
const globalPrivs = privs.filter(priv => globalPrivList.includes(priv));
if (globalPrivs.length) {
await privileges.global[type](globalPrivs, data.member);
return category;
});
}
exports.get = get;
function create(caller, data) {
return __awaiter(this, void 0, void 0, function* () {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const response = yield categories_1.default.create(data);
const categoryIds = [response.cid];
const userUid = caller.uid;
const categoryObjs = yield categories_1.default.getCategories(categoryIds, userUid);
return categoryObjs[0];
});
}
exports.create = create;
function update(caller, data) {
return __awaiter(this, void 0, void 0, function* () {
if (!data) {
throw new Error('[[error:invalid-data]]');
}
} else {
const categoryPrivList = await privileges.categories.getPrivilegeList();
const categoryPrivs = privs.filter(priv => categoryPrivList.includes(priv));
await privileges.categories[type](categoryPrivs, data.cid, data.member);
}

await events.log({
uid: caller.uid,
type: 'privilege-change',
ip: caller.ip,
privilege: data.privilege.toString(),
cid: data.cid,
action: data.set ? 'grant' : 'rescind',
target: data.member,
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
yield categories_1.default.update(data);
});
};
}
exports.update = update;
function _delete(caller, data) {
return __awaiter(this, void 0, void 0, function* () {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const name = yield categories_1.default.getCategoryField(data.cid, 'name');
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
yield categories_1.default.purge(data.cid, caller.uid);
yield events_1.default.log({
type: 'category-purge',
uid: caller.uid,
ip: caller.ip,
cid: data.cid,
name: name,
});
});
}
exports._delete = _delete;
exports.delete = _delete;
function getPrivileges(caller, cid) {
return __awaiter(this, void 0, void 0, function* () {
if (cid === 'admin') {
return yield privileges_1.default.admin.list(caller.uid);
}
if (!parseInt(cid, 10)) {
return yield privileges_1.default.global.list();
}
return yield privileges_1.default.categories.list(cid);
});
}
exports.getPrivileges = getPrivileges;
function setPrivilege(caller, data) {
return __awaiter(this, void 0, void 0, function* () {
const [userExists, groupExists] = yield Promise.all([
user_1.default.exists(data.member),
groups_1.default.exists(data.member),
]);
if (!userExists && !groupExists) {
throw new Error('[[error:no-user-or-group]]');
}
const privs = Array.isArray(data.privilege) ? data.privilege : [data.privilege];
const type = data.set ? 'give' : 'rescind';
if (!privs.length) {
throw new Error('[[error:invalid-data]]');
}
if (parseInt(data.cid, 10) === 0) {
const adminPrivList = yield privileges_1.default.admin.getPrivilegeList();
const adminPrivs = privs.filter(priv => adminPrivList.includes(priv));
if (adminPrivs.length) {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
yield privileges_1.default.admin[type](adminPrivs, data.member);
}
const globalPrivList = yield privileges_1.default.global.getPrivilegeList();
const globalPrivs = privs.filter(priv => globalPrivList.includes(priv));
if (globalPrivs.length) {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
yield privileges_1.default.global[type](globalPrivs, data.member);
}
}
else {
const categoryPrivList = yield privileges_1.default.categories.getPrivilegeList();
const categoryPrivs = privs.filter(priv => categoryPrivList.includes(priv));
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
yield privileges_1.default.categories[type](categoryPrivs, data.cid, data.member);
}
yield events_1.default.log({
uid: caller.uid,
type: 'privilege-change',
ip: caller.ip,
privilege: data.privilege.toString(),
cid: data.cid,
action: data.set ? 'grant' : 'rescind',
target: data.member,
});
});
}
exports.setPrivilege = setPrivilege;
131 changes: 131 additions & 0 deletions src/api/categories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import categories from '../categories';
import events from '../events';
import user from '../user';
import groups from '../groups';
import privileges from '../privileges';
import { CategoryObject } from '../types/category';

interface Caller {
uid: string ;
ip: string
}

interface Data {
uid: string;
cid: string;
member: string | number;
privilege: string | string[];
set: string
}
interface UserPrivilege {
[key: string]: boolean,
read?: boolean,
}

export async function get(caller: Caller, data: Data) : Promise<CategoryObject> {
const userPrivileges: UserPrivilege = await privileges.categories.get(data.cid, caller.uid) as UserPrivilege;
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const category: CategoryObject = await categories.getCategoryData(data.cid) as CategoryObject;
if (!category || !userPrivileges.read) {
return null;
}
return category;
}

export async function create(caller: Caller, data: Data) {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const response : CategoryObject = await categories.create(data) as CategoryObject;
const categoryIds: number[] = [response.cid];
const userUid : string = caller.uid;
const categoryObjs:CategoryObject[] = await categories.getCategories(categoryIds, userUid) as CategoryObject[];
return categoryObjs[0];
}

export async function update(caller: Caller, data: Data) {
if (!data) {
throw new Error('[[error:invalid-data]]');
}
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
await categories.update(data);
}

export async function _delete(caller: Caller, data: Data): Promise<void> {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const name: string = await categories.getCategoryField(data.cid, 'name') as string;
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
await categories.purge(data.cid, caller.uid);
await events.log({
type: 'category-purge',
uid: caller.uid,
ip: caller.ip,
cid: data.cid,
name: name,
});
}

export async function getPrivileges(caller: Caller, cid : string) {
if (cid === 'admin') {
return await privileges.admin.list(caller.uid);
}
if (!parseInt(cid, 10)) {
return await privileges.global.list();
}
return await privileges.categories.list(cid);
}

export async function setPrivilege(caller : Caller, data : Data) {
const [userExists, groupExists] = await Promise.all([
user.exists(data.member) as boolean,
groups.exists(data.member) as boolean,
]);

if (!userExists && !groupExists) {
throw new Error('[[error:no-user-or-group]]');
}
const privs = Array.isArray(data.privilege) ? data.privilege : [data.privilege];
const type :string = data.set ? 'give' : 'rescind';
if (!privs.length) {
throw new Error('[[error:invalid-data]]');
}
if (parseInt(data.cid, 10) === 0) {
const adminPrivList : string[] = await privileges.admin.getPrivilegeList() as string[];
const adminPrivs : string[] = privs.filter(priv => adminPrivList.includes(priv));
if (adminPrivs.length) {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
await privileges.admin[type](adminPrivs, data.member);
}
const globalPrivList : string[] = await privileges.global.getPrivilegeList() as string[];
const globalPrivs : string[] = privs.filter(priv => globalPrivList.includes(priv));
if (globalPrivs.length) {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
await privileges.global[type](globalPrivs, data.member);
}
} else {
const categoryPrivList : string[] = await privileges.categories.getPrivilegeList() as string[];
const categoryPrivs: string[] = privs.filter(priv => categoryPrivList.includes(priv));
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
await privileges.categories[type](categoryPrivs, data.cid, data.member);
}

await events.log({
uid: caller.uid,
type: 'privilege-change',
ip: caller.ip,
privilege: data.privilege.toString(),
cid: data.cid,
action: data.set ? 'grant' : 'rescind',
target: data.member,
});
}

export {
_delete as delete,
};

0 comments on commit a87d472

Please sign in to comment.