feat: refine global search RBAC and fix image loading
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m24s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m24s
- Restricted Agent search to Attendances only. - Enabled Super Admin search for Organizations (Tenants). - Fixed user avatar URL construction in search results. - Added Organizations category to search dropdown for Super Admins.
This commit is contained in:
@@ -460,30 +460,29 @@ apiRouter.post('/users/:id/avatar', upload.single('avatar'), async (req, res) =>
|
||||
// --- Global Search ---
|
||||
apiRouter.get('/search', async (req, res) => {
|
||||
const { q } = req.query;
|
||||
if (!q || q.length < 2) return res.json({ members: [], teams: [], attendances: [] });
|
||||
if (!q || q.length < 2) return res.json({ members: [], teams: [], attendances: [], organizations: [] });
|
||||
|
||||
const queryStr = `%${q}%`;
|
||||
const results = { members: [], teams: [], attendances: [] };
|
||||
const results = { members: [], teams: [], attendances: [], organizations: [] };
|
||||
|
||||
try {
|
||||
// 1. Search Members
|
||||
let membersQ = 'SELECT id, name, email, slug, role, team_id, avatar_url FROM users WHERE (name LIKE ? OR email LIKE ?)';
|
||||
const membersParams = [queryStr, queryStr];
|
||||
// 1. Search Members (only for roles above agent)
|
||||
if (req.user.role !== 'agent') {
|
||||
let membersQ = 'SELECT id, name, email, slug, role, team_id, avatar_url FROM users WHERE (name LIKE ? OR email LIKE ?)';
|
||||
const membersParams = [queryStr, queryStr];
|
||||
|
||||
if (req.user.role === 'super_admin') {
|
||||
// No extra filters
|
||||
} else if (req.user.role === 'admin' || req.user.role === 'owner') {
|
||||
membersQ += ' AND tenant_id = ?';
|
||||
membersParams.push(req.user.tenant_id);
|
||||
} else if (req.user.role === 'manager') {
|
||||
membersQ += ' AND tenant_id = ? AND (team_id = ? OR id = ?)';
|
||||
membersParams.push(req.user.tenant_id, req.user.team_id, req.user.id);
|
||||
} else {
|
||||
membersQ += ' AND id = ?';
|
||||
membersParams.push(req.user.id);
|
||||
if (req.user.role === 'super_admin') {
|
||||
// No extra filters
|
||||
} else if (req.user.role === 'admin' || req.user.role === 'owner') {
|
||||
membersQ += ' AND tenant_id = ?';
|
||||
membersParams.push(req.user.tenant_id);
|
||||
} else if (req.user.role === 'manager') {
|
||||
membersQ += ' AND tenant_id = ? AND (team_id = ? OR id = ?)';
|
||||
membersParams.push(req.user.tenant_id, req.user.team_id, req.user.id);
|
||||
}
|
||||
const [members] = await pool.query(membersQ, membersParams);
|
||||
results.members = members;
|
||||
}
|
||||
const [members] = await pool.query(membersQ, membersParams);
|
||||
results.members = members;
|
||||
|
||||
// 2. Search Teams (only for roles above agent)
|
||||
if (req.user.role !== 'agent') {
|
||||
@@ -503,7 +502,13 @@ apiRouter.get('/search', async (req, res) => {
|
||||
results.teams = teams;
|
||||
}
|
||||
|
||||
// 3. Search Attendances
|
||||
// 3. Search Organizations (only for super_admin)
|
||||
if (req.user.role === 'super_admin') {
|
||||
const [orgs] = await pool.query('SELECT id, name, slug, status FROM tenants WHERE name LIKE ? OR slug LIKE ?', [queryStr, queryStr]);
|
||||
results.organizations = orgs;
|
||||
}
|
||||
|
||||
// 4. Search Attendances
|
||||
let attendancesQ = 'SELECT a.id, a.summary, a.created_at, u.name as user_name FROM attendances a JOIN users u ON a.user_id = u.id WHERE a.summary LIKE ?';
|
||||
const attendancesParams = [queryStr];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user