visibility.js (814B)
1 // src/lib/visibility.js - who may see a row 2 3 // SQL for lists, so a count and its page cannot disagree. 4 export function visibleSql(user, { visibility, owner }) { 5 if (user?.role === 'admin') return { where: '1 = 1', params: {} }; 6 if (user) { 7 return { 8 where: `(${visibility} = 'public' OR ${owner} = {viewerId})`, 9 params: { viewerId: user.id }, 10 }; 11 } 12 return { where: `${visibility} = 'public'`, params: {} }; 13 } 14 15 // A null owner matches nobody: an unowned row is administered centrally. 16 export function canManage(user, ownerId) { 17 if (!user) return false; 18 if (user.role === 'admin') return true; 19 return ownerId != null && ownerId === user.id; 20 } 21 22 export function canView(user, { visibility, ownerId }) { 23 if (visibility === 'public') return true; 24 return canManage(user, ownerId); 25 }