utils/accessControl.jsjavascript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.filterAccessibleOwnerIds = exports.checkDeleteAccess = exports.checkWriteAccess = exports.checkReadAccess = exports.canDelete = exports.canWrite = exports.canRead = void 0;
const UserAccessService_1 = __importDefault(require("../services/UserAccessService"));
const FeatureFlagService_1 = __importStar(require("../services/FeatureFlagService"));
const userAccess_1 = require("../db/models/userAccess");
const errors_1 = require("./errors");
/**
 * Check if the acting user has read access to resources owned by the given ownerId.
 * @param ownerId - The ID of the owner whose resources are being accessed
 * @param actingUserId - The ID of the user attempting to access the resource
 * @param isAdmin - Whether the acting user is an admin (admin has full access)
 * @returns Promise<boolean> - True if the user has read access
 */
const canRead = async (ownerId, actingUserId, isAdmin) => {
    if (ownerId === actingUserId || isAdmin) {
        return true;
    }
    const role = await UserAccessService_1.default.getRole(ownerId, actingUserId);
    return role !== null;
};
exports.canRead = canRead;
/**
 * Check if the acting user has write access to resources owned by the given ownerId.
 * Coaches and assistants have write access.
 * @param ownerId - The ID of the owner whose resources are being accessed
 * @param actingUserId - The ID of the user attempting to access the resource
 * @param isAdmin - Whether the acting user is an admin (admin has full access)
 * @returns Promise<boolean> - True if the user has write access
 */
const canWrite = async (ownerId, actingUserId, isAdmin) => {
    if (ownerId === actingUserId || isAdmin) {
        return true;
    }
    const role = await UserAccessService_1.default.getRole(ownerId, actingUserId);
    if (role === userAccess_1.AccessRole.COACH || role === userAccess_1.AccessRole.ASSISTANT) {
        return true;
    }
    return false;
};
exports.canWrite = canWrite;
/**
 * Check if the acting user has delete access to resources owned by the given ownerId.
 * Only coaches have delete access.
 * @param ownerId - The ID of the owner whose resources are being accessed
 * @param actingUserId - The ID of the user attempting to access the resource
 * @param isAdmin - Whether the acting user is an admin (admin has full access)
 * @returns Promise<boolean> - True if the user has delete access
 */
const canDelete = async (ownerId, actingUserId, isAdmin) => {
    if (ownerId === actingUserId || isAdmin) {
        return true;
    }
    const role = await UserAccessService_1.default.getRole(ownerId, actingUserId);
    if (role === userAccess_1.AccessRole.COACH) {
        return true;
    }
    return false;
};
exports.canDelete = canDelete;
/**
 * Verify the acting user has read access to resources owned by the given ownerId.
 * Throws AccessDeniedError if access is denied.
 * @param ownerId - The ID of the owner whose resources are being accessed
 * @param actingUserId - The ID of the user attempting to access the resource
 * @param isAdmin - Whether the acting user is an admin (admin has full access)
 * @returns Promise<void>
 * @throws AccessDeniedError if access is denied
 */
const checkReadAccess = async (ownerId, actingUserId, isAdmin) => {
    const hasAccess = await (0, exports.canRead)(ownerId, actingUserId, isAdmin);
    if (!hasAccess) {
        throw new errors_1.AccessDeniedError("You do not have read access to this resource");
    }
};
exports.checkReadAccess = checkReadAccess;
/**
 * Verify the acting user has write access to resources owned by the given ownerId.
 * Throws AccessDeniedError if access is denied.
 * @param ownerId - The ID of the owner whose resources are being accessed
 * @param actingUserId - The ID of the user attempting to access the resource
 * @param isAdmin - Whether the acting user is an admin (admin has full access)
 * @returns Promise<void>
 * @throws AccessDeniedError if access is denied
 */
const checkWriteAccess = async (ownerId, actingUserId, isAdmin) => {
    const hasAccess = await (0, exports.canWrite)(ownerId, actingUserId, isAdmin);
    if (!hasAccess) {
        throw new errors_1.AccessDeniedError("You do not have write access to this resource");
    }
};
exports.checkWriteAccess = checkWriteAccess;
/**
 * Verify the acting user has delete access to resources owned by the given ownerId.
 * Throws AccessDeniedError if access is denied.
 * @param ownerId - The ID of the owner whose resources are being accessed
 * @param actingUserId - The ID of the user attempting to access the resource
 * @param isAdmin - Whether the acting user is an admin (admin has full access)
 * @returns Promise<void>
 * @throws AccessDeniedError if access is denied
 */
const checkDeleteAccess = async (ownerId, actingUserId, isAdmin) => {
    const hasAccess = await (0, exports.canDelete)(ownerId, actingUserId, isAdmin);
    if (!hasAccess) {
        throw new errors_1.AccessDeniedError("You do not have delete access to this resource");
    }
};
exports.checkDeleteAccess = checkDeleteAccess;
/**
 * Filter ownerIds to only include those the acting user has read access to.
 * This prevents false negatives when checking access for multiple owners.
 * @param ownerIds - Array of owner IDs to filter
 * @param actingUserId - The acting user ID
 * @param isAdmin - Whether the acting user is an admin
 * @returns Promise<string[]> - Filtered array of owner IDs the user can access
 */
const filterAccessibleOwnerIds = async (ownerIds, actingUserId, isAdmin) => {
    const accessSharingEnabled = await FeatureFlagService_1.default.isEnabled(FeatureFlagService_1.FeatureFlagKey.ACCESS_SHARING);
    if (!accessSharingEnabled) {
        return [actingUserId];
    }
    if (ownerIds.length === 0) {
        return [];
    }
    const results = await Promise.all(ownerIds.map(async (ownerId) => {
        const hasAccess = await (0, exports.canRead)(ownerId, actingUserId, isAdmin);
        return hasAccess ? ownerId : null;
    })).then((res) => res.filter((id) => id !== null));
    if (!results.includes(actingUserId))
        results.push(actingUserId);
    return results;
};
exports.filterAccessibleOwnerIds = filterAccessibleOwnerIds;