Unify users and team members into a single model
All checks were successful
Deploy / deploy (push) Successful in 13s
All checks were successful
Deploy / deploy (push) Successful in 13s
- Remove team_role filter from GET /api/users/team — all users now appear in the team view - POST /api/users now accepts team_role, brands, phone, modules - PATCH /api/users/:id now accepts team_role, phone, brands, modules - Users without team_role display their system role as fallback Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -781,19 +781,26 @@ app.get('/api/users', requireAuth, requireRole('superadmin'), async (req, res) =
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.post('/api/users', requireAuth, requireRole('superadmin'), async (req, res) => {
|
app.post('/api/users', requireAuth, requireRole('superadmin'), async (req, res) => {
|
||||||
const { name, email, password, role, avatar } = req.body;
|
const { name, email, password, role, avatar, team_role, brands, phone, modules } = req.body;
|
||||||
if (!name || !email || !password || !role) return res.status(400).json({ error: 'Name, email, password, and role are required' });
|
if (!name || !email || !role) return res.status(400).json({ error: 'Name, email, and role are required' });
|
||||||
if (!['superadmin', 'manager', 'contributor'].includes(role)) return res.status(400).json({ error: 'Invalid role' });
|
if (!['superadmin', 'manager', 'contributor'].includes(role)) return res.status(400).json({ error: 'Invalid role' });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const existing = authDb.prepare('SELECT id FROM auth_credentials WHERE email = ?').get(email);
|
const existing = authDb.prepare('SELECT id FROM auth_credentials WHERE email = ?').get(email);
|
||||||
if (existing) return res.status(409).json({ error: 'Email already exists' });
|
if (existing) return res.status(409).json({ error: 'Email already exists' });
|
||||||
|
|
||||||
const created = await nocodb.create('Users', { name, email, role, avatar: avatar || null });
|
const created = await nocodb.create('Users', {
|
||||||
const passwordHash = await bcrypt.hash(password, 10);
|
name, email, role, avatar: avatar || null,
|
||||||
|
team_role: team_role || null,
|
||||||
|
brands: JSON.stringify(brands || []),
|
||||||
|
phone: phone || null,
|
||||||
|
modules: JSON.stringify(modules || ALL_MODULES),
|
||||||
|
});
|
||||||
|
const defaultPassword = password || 'changeme123';
|
||||||
|
const passwordHash = await bcrypt.hash(defaultPassword, 10);
|
||||||
authDb.prepare('INSERT INTO auth_credentials (email, password_hash, nocodb_user_id) VALUES (?, ?, ?)').run(email, passwordHash, created.Id);
|
authDb.prepare('INSERT INTO auth_credentials (email, password_hash, nocodb_user_id) VALUES (?, ?, ?)').run(email, passwordHash, created.Id);
|
||||||
const user = await nocodb.get('Users', created.Id);
|
const user = await nocodb.get('Users', created.Id);
|
||||||
res.status(201).json(user);
|
res.status(201).json({ ...user, id: user.Id, _id: user.Id });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Create user error:', err);
|
console.error('Create user error:', err);
|
||||||
res.status(500).json({ error: 'Failed to create user' });
|
res.status(500).json({ error: 'Failed to create user' });
|
||||||
@@ -808,9 +815,11 @@ app.patch('/api/users/:id', requireAuth, requireRole('superadmin'), async (req,
|
|||||||
if (req.body.role && !['superadmin', 'manager', 'contributor'].includes(req.body.role)) return res.status(400).json({ error: 'Invalid role' });
|
if (req.body.role && !['superadmin', 'manager', 'contributor'].includes(req.body.role)) return res.status(400).json({ error: 'Invalid role' });
|
||||||
|
|
||||||
const data = {};
|
const data = {};
|
||||||
for (const f of ['name', 'email', 'role', 'avatar']) {
|
for (const f of ['name', 'email', 'role', 'avatar', 'team_role', 'phone']) {
|
||||||
if (req.body[f] !== undefined) data[f] = req.body[f];
|
if (req.body[f] !== undefined) data[f] = req.body[f];
|
||||||
}
|
}
|
||||||
|
if (req.body.brands !== undefined) data.brands = JSON.stringify(req.body.brands);
|
||||||
|
if (req.body.modules !== undefined) data.modules = JSON.stringify(req.body.modules);
|
||||||
|
|
||||||
if (req.body.password) {
|
if (req.body.password) {
|
||||||
const hash = await bcrypt.hash(req.body.password, 10);
|
const hash = await bcrypt.hash(req.body.password, 10);
|
||||||
@@ -848,7 +857,6 @@ app.delete('/api/users/:id', requireAuth, requireRole('superadmin'), async (req,
|
|||||||
app.get('/api/users/assignable', requireAuth, async (req, res) => {
|
app.get('/api/users/assignable', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const users = await nocodb.list('Users', {
|
const users = await nocodb.list('Users', {
|
||||||
where: '(team_role,isnot,null)',
|
|
||||||
sort: 'name',
|
sort: 'name',
|
||||||
});
|
});
|
||||||
res.json(users.map(u => ({ ...u, id: u.Id, _id: u.Id })));
|
res.json(users.map(u => ({ ...u, id: u.Id, _id: u.Id })));
|
||||||
@@ -862,7 +870,6 @@ app.get('/api/users/assignable', requireAuth, async (req, res) => {
|
|||||||
app.get('/api/users/team', requireAuth, async (req, res) => {
|
app.get('/api/users/team', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const users = await nocodb.list('Users', {
|
const users = await nocodb.list('Users', {
|
||||||
where: '(team_role,isnot,null)',
|
|
||||||
sort: 'name',
|
sort: 'name',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user