TGGXF43HKM7KPF4OS4ANJTSSS3LKMJKPXL7PLY5CZJGIFQW4CQYAC
AUEY3QXCUPYXL354YRB7AQBEOYX3GN5XGE47KX7TE6XT2QFP7G6AC
IUHUM6OZ5KYEAYQCIYNG5Q4QLQRAQNMBWKYGV2ZDJFNY5W4DOUNQC
ONSQYCF6NFUEA24ORB62W4P62LKUMV7C5PLYRZQULHFDNROEY2HQC
OBXY6BHNROIV7W4O3MMQ6GTQXU2XUKF5A4DCJYDLUEUHV72E7BNAC
HVD2NGYM4J2PKQ72SFMXLBGQPKPCGDRB54IK3ISOQZDWKGM3WGQQC
4FBIL6IZUDNCXTM6EUHTEOJRHVI4LIIX4BU2IXPXKR362GKIAJMQC
O7W4FZVRKDQDAAXEW4T7P262PPRILRCSSACODMUTQZ6VNR36PVCQC
if (clusterResult.length === 0) {
return c.json({
error: "Cluster not found"
}, 404);
}
const datasetId = clusterResult[0].datasetId;
// Check if user has READ permission for this dataset
const hasPermission = await checkUserPermission(db, userId, datasetId, 'READ');
if (!hasPermission) {
return c.json({
error: "Access denied: No READ permission for this dataset"
}, 403);
}
// Query to get datasets with permissions
// This complex query gets datasets where the user has READ access either through:
// 1. Direct user-specific grants
// 2. Role-based grants for their role
// 3. Being the owner of the dataset
const results = await db
// If user has no role, return empty result with helpful message
if (!userRoleName) {
return c.json({
data: [],
userId: userId,
userRole: null,
message: "No access granted. Please contact an administrator to assign you a role."
});
}
// Get datasets with explicit READ grants - owners must also have grants
const grantedDatasets = await db
}
// Now get all permissions for each dataset
for (const datasetId of datasetMap.keys()) {
const allPermissions = await db
.select({
permission: accessGrant.permission
})
.from(accessGrant)
.where(sqlExpr`
${accessGrant.datasetId} = ${datasetId} AND
${accessGrant.active} = true AND
(
(${accessGrant.userId} = ${userId}) OR
(${accessGrant.userId} IS NULL AND ${userRoleName} IS NOT NULL AND ${accessGrant.role} = ${userRoleName})
)
`);
// Add permission from access grants (avoid duplicates)
if (row.permission && !datasetMap.get(row.id)!.permissions.includes(row.permission)) {
datasetMap.get(row.id)!.permissions.push(row.permission);
}
});
const datasetData = datasetMap.get(datasetId)!;
// Collect unique permissions from grants - applies to ALL users including owners
const permissions = new Set(['READ']); // We know they have read access
allPermissions.forEach(p => permissions.add(p.permission));
datasetData.permissions = Array.from(permissions);
}
// Filter datasets to only include those with READ permission
const datasetsWithReadAccess = Array.from(datasetMap.values())
.filter(dataset => dataset.permissions.includes('READ'))
.slice(0, 20); // Limit to 20 datasets
const datasetsWithReadAccess = Array.from(datasetMap.values()).slice(0, 20);
if (locationResult.length === 0) {
return c.json({
error: "Location not found"
}, 404);
}
const datasetId = locationResult[0].datasetId;
// Check if user has READ permission for this dataset
const hasPermission = await checkUserPermission(db, userId, datasetId, 'READ');
if (!hasPermission) {
return c.json({
error: "Access denied: No READ permission for this dataset"
}, 403);
}
// First check if user is the owner (owners have all permissions)
const ownerCheck = await db
.select({ owner: dataset.owner })
.from(dataset)
.where(eq(dataset.id, datasetId))
.limit(1);
if (ownerCheck.length > 0 && ownerCheck[0].owner === userId) {
return true;
}
// Get user's role
// Get user's role - ALL users must have a valid role to access data
{error && <p className="text-red-600 mb-4">Error: {error}</p>}
{error && (
<p className={`mb-4 ${error.includes('No access granted') || error.includes('contact an administrator') ? 'text-amber-600' : 'text-red-600'}`}>
{error.includes('Error: ') ? error : `Info: ${error}`}
</p>
)}