fix: Enforce role-based permissions in frontend
Some checks failed
ParentFlow CI/CD Pipeline / Backend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Frontend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Security Scanning (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-app/maternal-app-backend dockerfile:Dockerfile.production name:backend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-web dockerfile:Dockerfile.production name:frontend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Development (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled

Fixed critical permission bypass where viewers could:
- Remove family members (now only parents can)
- Invite new members (now only parents can)
- Generate share codes (now only parents can)
- Add children (now only parents can)
- Edit children (now only parents and caregivers can)
- Delete children (now only parents can)

Changes:
- Family page: Added isParent checks for all admin actions
- Children page: Added canAddChildren, canEditChildren, canDeleteChildren checks
- Both pages now use useSelectedFamily hook for consistent role access

Backend already had correct permission checks - this fixes the frontend to respect them.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Andrei
2025-10-09 12:59:33 +00:00
parent ce939b909b
commit 2be0e90f19
2 changed files with 77 additions and 56 deletions

View File

@@ -26,11 +26,13 @@ import { DeleteConfirmDialog } from '@/components/children/DeleteConfirmDialog';
import { motion } from 'framer-motion'; import { motion } from 'framer-motion';
import { useTranslation } from '@/hooks/useTranslation'; import { useTranslation } from '@/hooks/useTranslation';
import { useLocalizedDate } from '@/hooks/useLocalizedDate'; import { useLocalizedDate } from '@/hooks/useLocalizedDate';
import { useSelectedFamily } from '@/hooks/useSelectedFamily';
export default function ChildrenPage() { export default function ChildrenPage() {
const { t } = useTranslation('children'); const { t } = useTranslation('children');
const { user } = useAuth(); const { user } = useAuth();
const { format: formatDate } = useLocalizedDate(); const { format: formatDate } = useLocalizedDate();
const { familyId, familyRole } = useSelectedFamily();
const [children, setChildren] = useState<Child[]>([]); const [children, setChildren] = useState<Child[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [error, setError] = useState<string>(''); const [error, setError] = useState<string>('');
@@ -40,8 +42,10 @@ export default function ChildrenPage() {
const [childToDelete, setChildToDelete] = useState<Child | null>(null); const [childToDelete, setChildToDelete] = useState<Child | null>(null);
const [actionLoading, setActionLoading] = useState(false); const [actionLoading, setActionLoading] = useState(false);
// Get familyId from user // Permission checks based on role
const familyId = user?.families?.[0]?.familyId; const canAddChildren = familyRole === 'parent';
const canEditChildren = familyRole === 'parent' || familyRole === 'caregiver';
const canDeleteChildren = familyRole === 'parent';
useEffect(() => { useEffect(() => {
if (familyId) { if (familyId) {
@@ -164,6 +168,7 @@ export default function ChildrenPage() {
{t('subtitle')} {t('subtitle')}
</Typography> </Typography>
</Box> </Box>
{canAddChildren && (
<Button <Button
variant="contained" variant="contained"
startIcon={<Add />} startIcon={<Add />}
@@ -177,6 +182,7 @@ export default function ChildrenPage() {
> >
{t('addChild')} {t('addChild')}
</Button> </Button>
)}
</Box> </Box>
{error && ( {error && (
@@ -201,6 +207,7 @@ export default function ChildrenPage() {
<Typography variant="body2" color="text.secondary" sx={{ mb: 3 }}> <Typography variant="body2" color="text.secondary" sx={{ mb: 3 }}>
{t('noChildrenSubtitle')} {t('noChildrenSubtitle')}
</Typography> </Typography>
{canAddChildren && (
<Button <Button
variant="contained" variant="contained"
startIcon={<Add />} startIcon={<Add />}
@@ -209,6 +216,7 @@ export default function ChildrenPage() {
> >
{t('addFirstChild')} {t('addFirstChild')}
</Button> </Button>
)}
</CardContent> </CardContent>
</Card> </Card>
</Grid> </Grid>
@@ -263,12 +271,16 @@ export default function ChildrenPage() {
</Box> </Box>
<Box sx={{ display: 'flex', gap: 1, mt: 2, justifyContent: { xs: 'center', sm: 'flex-start' } }}> <Box sx={{ display: 'flex', gap: 1, mt: 2, justifyContent: { xs: 'center', sm: 'flex-start' } }}>
{canEditChildren && (
<IconButton size="medium" color="primary" onClick={() => handleEditChild(child)} sx={{ minWidth: 48, minHeight: 48 }}> <IconButton size="medium" color="primary" onClick={() => handleEditChild(child)} sx={{ minWidth: 48, minHeight: 48 }}>
<Edit /> <Edit />
</IconButton> </IconButton>
)}
{canDeleteChildren && (
<IconButton size="medium" color="error" onClick={() => handleDeleteClick(child)} sx={{ minWidth: 48, minHeight: 48 }}> <IconButton size="medium" color="error" onClick={() => handleDeleteClick(child)} sx={{ minWidth: 48, minHeight: 48 }}>
<Delete /> <Delete />
</IconButton> </IconButton>
)}
</Box> </Box>
</Paper> </Paper>
</motion.div> </motion.div>

View File

@@ -41,7 +41,7 @@ import { useSelectedFamily } from '@/hooks/useSelectedFamily';
export default function FamilyPage() { export default function FamilyPage() {
const { t } = useTranslation('family'); const { t } = useTranslation('family');
const { user, refreshUser } = useAuth(); const { user, refreshUser } = useAuth();
const { familyId, selectedIndex, setSelectedIndex, userFamilies, hasMultipleFamilies } = useSelectedFamily(); const { familyId, familyRole, selectedIndex, setSelectedIndex, userFamilies, hasMultipleFamilies } = useSelectedFamily();
const [family, setFamily] = useState<Family | null>(null); const [family, setFamily] = useState<Family | null>(null);
const [members, setMembers] = useState<FamilyMember[]>([]); const [members, setMembers] = useState<FamilyMember[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
@@ -54,6 +54,9 @@ export default function FamilyPage() {
const [snackbar, setSnackbar] = useState({ open: false, message: '' }); const [snackbar, setSnackbar] = useState({ open: false, message: '' });
const [familyNames, setFamilyNames] = useState<Record<string, string>>({}); const [familyNames, setFamilyNames] = useState<Record<string, string>>({});
// Check if current user is a parent (has admin permissions)
const isParent = familyRole === 'parent';
useEffect(() => { useEffect(() => {
if (familyId) { if (familyId) {
fetchFamilyData(); fetchFamilyData();
@@ -229,6 +232,7 @@ export default function FamilyPage() {
> >
{t('buttons.joinFamily')} {t('buttons.joinFamily')}
</Button> </Button>
{isParent && (
<Button <Button
variant="contained" variant="contained"
startIcon={<PersonAdd />} startIcon={<PersonAdd />}
@@ -238,6 +242,7 @@ export default function FamilyPage() {
> >
{t('buttons.inviteMember')} {t('buttons.inviteMember')}
</Button> </Button>
)}
</Box> </Box>
</Box> </Box>
@@ -318,6 +323,7 @@ export default function FamilyPage() {
{t('buttons.copyCode')} {t('buttons.copyCode')}
</Button> </Button>
</Box> </Box>
{isParent && (
<Button <Button
variant="text" variant="text"
size="small" size="small"
@@ -327,6 +333,7 @@ export default function FamilyPage() {
> >
Generate New Code Generate New Code
</Button> </Button>
)}
</Paper> </Paper>
</Grid> </Grid>
)} )}
@@ -347,6 +354,7 @@ export default function FamilyPage() {
<Typography variant="body2" color="text.secondary" sx={{ mb: 2 }}> <Typography variant="body2" color="text.secondary" sx={{ mb: 2 }}>
{t('members.noMembersDescription')} {t('members.noMembersDescription')}
</Typography> </Typography>
{isParent && (
<Button <Button
variant="outlined" variant="outlined"
startIcon={<PersonAdd />} startIcon={<PersonAdd />}
@@ -354,6 +362,7 @@ export default function FamilyPage() {
> >
{t('buttons.inviteFirstMember')} {t('buttons.inviteFirstMember')}
</Button> </Button>
)}
</Box> </Box>
) : ( ) : (
<List sx={{ p: 0 }}> <List sx={{ p: 0 }}>
@@ -400,7 +409,7 @@ export default function FamilyPage() {
color={getRoleColor(member.role)} color={getRoleColor(member.role)}
sx={{ borderRadius: 1, mr: 1 }} sx={{ borderRadius: 1, mr: 1 }}
/> />
{!isCurrentUser(member.userId) && ( {isParent && !isCurrentUser(member.userId) && (
<IconButton <IconButton
size="small" size="small"
onClick={() => handleRemoveClick(member)} onClick={() => handleRemoveClick(member)}