Compare commits

...

2 commits

Author SHA1 Message Date
b6447d4d21 minor 2025-04-26 11:22:40 -05:00
4dcd03c426 fixed admin promotion, stopped self from demoting self 2025-04-26 11:22:28 -05:00
2 changed files with 27 additions and 7 deletions

View file

@ -89,11 +89,11 @@ module.exports = (pool, query, authMiddleware) => {
} }
}); });
// Update user (admin can update is_disabled and internal_notes) // Update user (admin can update is_disabled, is_admin and internal_notes)
router.patch('/:id', async (req, res, next) => { router.patch('/:id', async (req, res, next) => {
try { try {
const { id } = req.params; const { id } = req.params;
const { is_disabled, internal_notes } = req.body; const { is_disabled, internal_notes, is_admin} = req.body;
// Check if user is admin // Check if user is admin
if (!req.user.is_admin) { if (!req.user.is_admin) {
@ -118,12 +118,14 @@ module.exports = (pool, query, authMiddleware) => {
UPDATE users UPDATE users
SET SET
is_disabled = $1, is_disabled = $1,
internal_notes = $2 internal_notes = $2,
WHERE id = $3 is_admin = $3
WHERE id = $4
RETURNING id, email, first_name, last_name, is_admin, is_disabled, internal_notes RETURNING id, email, first_name, last_name, is_admin, is_disabled, internal_notes
`, [ `, [
is_disabled !== undefined ? is_disabled : userCheck.rows[0].is_disabled, is_disabled !== undefined ? is_disabled : userCheck.rows[0].is_disabled,
internal_notes !== undefined ? internal_notes : userCheck.rows[0].internal_notes, internal_notes !== undefined ? internal_notes : userCheck.rows[0].internal_notes,
is_admin !== undefined ? is_admin : userCheck.rows[0].is_admin,
id id
]); ]);

View file

@ -37,8 +37,9 @@ import {
import { useAdminUsers, useUpdateUser } from '@hooks/adminHooks'; import { useAdminUsers, useUpdateUser } from '@hooks/adminHooks';
import { format } from 'date-fns'; import { format } from 'date-fns';
import EmailDialog from '@components/EmailDialog'; import EmailDialog from '@components/EmailDialog';
import { useAuth } from '@hooks/reduxHooks';
const AdminCustomersPage = () => { const AdminCustomersPage = () => {
const { user, isAuthenticated } = useAuth();
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(10); const [rowsPerPage, setRowsPerPage] = useState(10);
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
@ -48,6 +49,7 @@ const AdminCustomersPage = () => {
const [emailRecipient, setEmailRecipient] = useState(null); const [emailRecipient, setEmailRecipient] = useState(null);
const [formData, setFormData] = useState({ const [formData, setFormData] = useState({
is_disabled: false, is_disabled: false,
is_admin: false,
internal_notes: '' internal_notes: ''
}); });
@ -99,6 +101,7 @@ const AdminCustomersPage = () => {
setCurrentUser(user); setCurrentUser(user);
setFormData({ setFormData({
is_disabled: user.is_disabled, is_disabled: user.is_disabled,
is_admin: user.is_admin,
internal_notes: user.internal_notes || '' internal_notes: user.internal_notes || ''
}); });
setEditDialogOpen(true); setEditDialogOpen(true);
@ -127,7 +130,7 @@ const AdminCustomersPage = () => {
const { name, value, checked } = e.target; const { name, value, checked } = e.target;
setFormData(prev => ({ setFormData(prev => ({
...prev, ...prev,
[name]: name === 'is_disabled' ? checked : value [name]: name === 'is_disabled' || name === 'is_admin' ? checked : value
})); }));
}; };
@ -326,13 +329,28 @@ const AdminCustomersPage = () => {
<Switch <Switch
checked={formData.is_disabled} checked={formData.is_disabled}
onChange={handleFormChange} onChange={handleFormChange}
disabled={user === currentUser.id}
name="is_disabled" name="is_disabled"
color="error" color="error"
/> />
} }
label={formData.is_disabled ? "Account is disabled" : "Account is active"} label={`${formData.is_disabled ? "Account is disabled" : "Account is active"}` + `${user === currentUser.id && formData.is_admin? " (Current user can\'t disabled themselves)" : "" }`}
sx={{ my: 2, display: 'block' }} sx={{ my: 2, display: 'block' }}
/> />
<FormControlLabel
control={
<Switch
checked={formData.is_admin}
onChange={handleFormChange}
disabled={user === currentUser.id && formData.is_admin}
name="is_admin"
color="error"
/>
}
label={`${formData.is_admin ? "Account is Admin" : "Account is not Admin"}` + `${user === currentUser.id && formData.is_admin? " (Admin can't downgrade themselves)" : "" }`}
sx={{ my: 2, display: 'block' }}
/>
<TextField <TextField
autoFocus autoFocus