hazo_auth 0.1.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hazo_auth_config.example.ini +75 -0
- package/instrumentation.ts +1 -1
- package/next.config.mjs +1 -1
- package/package.json +4 -1
- package/src/app/api/{auth → hazo_auth/auth}/upload_profile_picture/route.ts +2 -2
- package/src/app/api/{auth → hazo_auth}/change_password/route.ts +23 -0
- package/src/app/api/hazo_auth/get_auth/route.ts +89 -0
- package/src/app/api/hazo_auth/invalidate_cache/route.ts +139 -0
- package/src/app/api/{auth → hazo_auth}/library_photos/route.ts +3 -0
- package/src/app/api/{auth → hazo_auth}/logout/route.ts +27 -0
- package/src/app/api/hazo_auth/upload_profile_picture/route.ts +268 -0
- package/src/app/api/hazo_auth/user_management/permissions/route.ts +367 -0
- package/src/app/api/hazo_auth/user_management/roles/route.ts +442 -0
- package/src/app/api/hazo_auth/user_management/users/roles/route.ts +367 -0
- package/src/app/api/hazo_auth/user_management/users/route.ts +239 -0
- package/src/app/api/{auth → hazo_auth}/validate_reset_token/route.ts +3 -0
- package/src/app/api/{auth → hazo_auth}/verify_email/route.ts +3 -0
- package/src/app/globals.css +1 -1
- package/src/app/hazo_auth/user_management/page.tsx +14 -0
- package/src/app/hazo_auth/user_management/user_management_page_client.tsx +16 -0
- package/src/app/hazo_connect/api/sqlite/data/route.ts +7 -1
- package/src/app/hazo_connect/api/sqlite/schema/route.ts +14 -4
- package/src/app/hazo_connect/api/sqlite/tables/route.ts +14 -4
- package/src/app/hazo_connect/sqlite_admin/sqlite-admin-client.tsx +40 -3
- package/src/app/layout.tsx +1 -1
- package/src/app/page.tsx +4 -4
- package/src/components/layouts/email_verification/hooks/use_email_verification.ts +4 -4
- package/src/components/layouts/email_verification/index.tsx +1 -1
- package/src/components/layouts/forgot_password/hooks/use_forgot_password_form.ts +1 -1
- package/src/components/layouts/login/hooks/use_login_form.ts +2 -2
- package/src/components/layouts/my_settings/components/profile_picture_dialog.tsx +1 -1
- package/src/components/layouts/my_settings/components/profile_picture_library_tab.tsx +35 -6
- package/src/components/layouts/my_settings/hooks/use_my_settings.ts +5 -5
- package/src/components/layouts/my_settings/index.tsx +1 -1
- package/src/components/layouts/register/hooks/use_register_form.ts +1 -1
- package/src/components/layouts/reset_password/hooks/use_reset_password_form.ts +3 -3
- package/src/components/layouts/reset_password/index.tsx +2 -2
- package/src/components/layouts/shared/components/logout_button.tsx +1 -1
- package/src/components/layouts/shared/components/profile_pic_menu.tsx +321 -0
- package/src/components/layouts/shared/components/profile_pic_menu_wrapper.tsx +40 -0
- package/src/components/layouts/shared/components/sidebar_layout_wrapper.tsx +22 -72
- package/src/components/layouts/shared/components/unauthorized_guard.tsx +1 -1
- package/src/components/layouts/shared/hooks/use_auth_status.ts +1 -1
- package/src/components/layouts/shared/hooks/use_hazo_auth.ts +158 -0
- package/src/components/layouts/user_management/components/roles_matrix.tsx +607 -0
- package/src/components/layouts/user_management/index.tsx +1295 -0
- package/src/components/ui/alert-dialog.tsx +141 -0
- package/src/components/ui/checkbox.tsx +30 -0
- package/src/components/ui/dropdown-menu.tsx +201 -0
- package/src/components/ui/table.tsx +120 -0
- package/src/lib/auth/auth_cache.ts +220 -0
- package/src/lib/auth/auth_rate_limiter.ts +121 -0
- package/src/lib/auth/auth_types.ts +65 -0
- package/src/lib/auth/hazo_get_auth.server.ts +333 -0
- package/src/lib/auth_utility_config.server.ts +136 -0
- package/src/lib/hazo_connect_setup.server.ts +2 -3
- package/src/lib/my_settings_config.server.ts +1 -1
- package/src/lib/profile_pic_menu_config.server.ts +138 -0
- package/src/lib/reset_password_config.server.ts +5 -5
- package/src/lib/services/email_service.ts +2 -2
- package/src/lib/services/profile_picture_remove_service.ts +1 -1
- package/src/lib/services/token_service.ts +2 -2
- package/src/lib/user_management_config.server.ts +40 -0
- package/src/lib/utils.ts +1 -1
- package/src/middleware.ts +15 -13
- package/src/server/types/express.d.ts +1 -0
- package/src/stories/project_overview.stories.tsx +1 -1
- package/tailwind.config.ts +1 -1
- /package/src/app/api/{auth → hazo_auth}/forgot_password/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/login/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/me/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/profile_picture/[filename]/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/register/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/remove_profile_picture/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/resend_verification/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/reset_password/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/update_user/route.ts +0 -0
- /package/src/app/{forgot_password → hazo_auth/forgot_password}/forgot_password_page_client.tsx +0 -0
- /package/src/app/{forgot_password → hazo_auth/forgot_password}/page.tsx +0 -0
- /package/src/app/{login → hazo_auth/login}/login_page_client.tsx +0 -0
- /package/src/app/{login → hazo_auth/login}/page.tsx +0 -0
- /package/src/app/{my_settings → hazo_auth/my_settings}/my_settings_page_client.tsx +0 -0
- /package/src/app/{my_settings → hazo_auth/my_settings}/page.tsx +0 -0
- /package/src/app/{register → hazo_auth/register}/page.tsx +0 -0
- /package/src/app/{register → hazo_auth/register}/register_page_client.tsx +0 -0
- /package/src/app/{reset_password → hazo_auth/reset_password}/page.tsx +0 -0
- /package/src/app/{reset_password → hazo_auth/reset_password}/reset_password_page_client.tsx +0 -0
- /package/src/app/{verify_email → hazo_auth/verify_email}/page.tsx +0 -0
- /package/src/app/{verify_email → hazo_auth/verify_email}/verify_email_page_client.tsx +0 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
// file_description: API route for managing user roles (assigning roles to users)
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { get_hazo_connect_instance } from "@/lib/hazo_connect_instance.server";
|
|
5
|
+
import { createCrudService, getSqliteAdminService } from "hazo_connect/server";
|
|
6
|
+
import { create_app_logger } from "@/lib/app_logger";
|
|
7
|
+
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
8
|
+
import { get_auth_cache } from "@/lib/auth/auth_cache";
|
|
9
|
+
import { get_auth_utility_config } from "@/lib/auth_utility_config.server";
|
|
10
|
+
|
|
11
|
+
// section: route_config
|
|
12
|
+
export const dynamic = 'force-dynamic';
|
|
13
|
+
|
|
14
|
+
// section: api_handler
|
|
15
|
+
/**
|
|
16
|
+
* GET - Get roles assigned to a user
|
|
17
|
+
* Query params: user_id (string)
|
|
18
|
+
*/
|
|
19
|
+
export async function GET(request: NextRequest) {
|
|
20
|
+
const logger = create_app_logger();
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const { searchParams } = new URL(request.url);
|
|
24
|
+
const user_id = searchParams.get("user_id");
|
|
25
|
+
|
|
26
|
+
if (!user_id || typeof user_id !== "string") {
|
|
27
|
+
return NextResponse.json(
|
|
28
|
+
{ error: "user_id is required as a query parameter" },
|
|
29
|
+
{ status: 400 }
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
34
|
+
const user_roles_service = createCrudService(hazoConnect, "hazo_user_roles");
|
|
35
|
+
|
|
36
|
+
// Get all roles assigned to this user
|
|
37
|
+
const user_roles = await user_roles_service.findBy({
|
|
38
|
+
user_id,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (!Array.isArray(user_roles)) {
|
|
42
|
+
return NextResponse.json(
|
|
43
|
+
{ error: "Failed to fetch user roles" },
|
|
44
|
+
{ status: 500 }
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Extract role IDs
|
|
49
|
+
const role_ids = user_roles.map((ur) => ur.role_id as number).filter((id) => id !== undefined);
|
|
50
|
+
|
|
51
|
+
return NextResponse.json(
|
|
52
|
+
{
|
|
53
|
+
success: true,
|
|
54
|
+
role_ids,
|
|
55
|
+
},
|
|
56
|
+
{ status: 200 }
|
|
57
|
+
);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
60
|
+
logger.error("user_management_user_roles_fetch_failed", {
|
|
61
|
+
filename: get_filename(),
|
|
62
|
+
line_number: get_line_number(),
|
|
63
|
+
error: error_message,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return NextResponse.json(
|
|
67
|
+
{ error: "Failed to fetch user roles" },
|
|
68
|
+
{ status: 500 }
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* POST - Assign a role to a user
|
|
75
|
+
* Body: { user_id: string, role_id: number }
|
|
76
|
+
*/
|
|
77
|
+
export async function POST(request: NextRequest) {
|
|
78
|
+
const logger = create_app_logger();
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const body = await request.json();
|
|
82
|
+
const { user_id, role_id } = body;
|
|
83
|
+
|
|
84
|
+
if (!user_id || typeof user_id !== "string") {
|
|
85
|
+
return NextResponse.json(
|
|
86
|
+
{ error: "user_id is required and must be a string" },
|
|
87
|
+
{ status: 400 }
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!role_id || typeof role_id !== "number") {
|
|
92
|
+
return NextResponse.json(
|
|
93
|
+
{ error: "role_id is required and must be a number" },
|
|
94
|
+
{ status: 400 }
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
99
|
+
const user_roles_service = createCrudService(hazoConnect, "hazo_user_roles");
|
|
100
|
+
|
|
101
|
+
// Check if user exists
|
|
102
|
+
const users_service = createCrudService(hazoConnect, "hazo_users");
|
|
103
|
+
const users = await users_service.findBy({ id: user_id });
|
|
104
|
+
if (!Array.isArray(users) || users.length === 0) {
|
|
105
|
+
return NextResponse.json(
|
|
106
|
+
{ error: "User not found" },
|
|
107
|
+
{ status: 404 }
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Check if role exists
|
|
112
|
+
const roles_service = createCrudService(hazoConnect, "hazo_roles");
|
|
113
|
+
const roles = await roles_service.findBy({ id: role_id });
|
|
114
|
+
if (!Array.isArray(roles) || roles.length === 0) {
|
|
115
|
+
return NextResponse.json(
|
|
116
|
+
{ error: "Role not found" },
|
|
117
|
+
{ status: 404 }
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Check if role is already assigned to user
|
|
122
|
+
const existing_assignments = await user_roles_service.findBy({
|
|
123
|
+
user_id,
|
|
124
|
+
role_id,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
if (Array.isArray(existing_assignments) && existing_assignments.length > 0) {
|
|
128
|
+
return NextResponse.json(
|
|
129
|
+
{ error: "Role is already assigned to this user" },
|
|
130
|
+
{ status: 409 }
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Assign role to user
|
|
135
|
+
const now = new Date().toISOString();
|
|
136
|
+
const new_assignment = await user_roles_service.insert({
|
|
137
|
+
user_id,
|
|
138
|
+
role_id,
|
|
139
|
+
created_at: now,
|
|
140
|
+
changed_at: now,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
logger.info("user_management_user_role_assigned", {
|
|
144
|
+
filename: get_filename(),
|
|
145
|
+
line_number: get_line_number(),
|
|
146
|
+
user_id,
|
|
147
|
+
role_id,
|
|
148
|
+
assignment_id: (new_assignment as { user_id?: string; role_id?: number }).user_id,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
return NextResponse.json(
|
|
152
|
+
{
|
|
153
|
+
success: true,
|
|
154
|
+
assignment: {
|
|
155
|
+
user_id,
|
|
156
|
+
role_id,
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
{ status: 201 }
|
|
160
|
+
);
|
|
161
|
+
} catch (error) {
|
|
162
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
163
|
+
logger.error("user_management_user_role_assign_failed", {
|
|
164
|
+
filename: get_filename(),
|
|
165
|
+
line_number: get_line_number(),
|
|
166
|
+
error: error_message,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
return NextResponse.json(
|
|
170
|
+
{ error: "Failed to assign role to user" },
|
|
171
|
+
{ status: 500 }
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* PUT - Update user roles (bulk assignment/removal)
|
|
178
|
+
* Body: { user_id: string, role_ids: number[] }
|
|
179
|
+
*/
|
|
180
|
+
export async function PUT(request: NextRequest) {
|
|
181
|
+
const logger = create_app_logger();
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
const body = await request.json();
|
|
185
|
+
const { user_id, role_ids } = body;
|
|
186
|
+
|
|
187
|
+
if (!user_id || typeof user_id !== "string") {
|
|
188
|
+
return NextResponse.json(
|
|
189
|
+
{ error: "user_id is required and must be a string" },
|
|
190
|
+
{ status: 400 }
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (!Array.isArray(role_ids)) {
|
|
195
|
+
return NextResponse.json(
|
|
196
|
+
{ error: "role_ids is required and must be an array" },
|
|
197
|
+
{ status: 400 }
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
202
|
+
const user_roles_service = createCrudService(hazoConnect, "hazo_user_roles");
|
|
203
|
+
|
|
204
|
+
// Check if user exists
|
|
205
|
+
const users_service = createCrudService(hazoConnect, "hazo_users");
|
|
206
|
+
const users = await users_service.findBy({ id: user_id });
|
|
207
|
+
if (!Array.isArray(users) || users.length === 0) {
|
|
208
|
+
return NextResponse.json(
|
|
209
|
+
{ error: "User not found" },
|
|
210
|
+
{ status: 404 }
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Get current user roles
|
|
215
|
+
const current_user_roles = await user_roles_service.findBy({
|
|
216
|
+
user_id,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
if (!Array.isArray(current_user_roles)) {
|
|
220
|
+
return NextResponse.json(
|
|
221
|
+
{ error: "Failed to fetch current user roles" },
|
|
222
|
+
{ status: 500 }
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const current_role_ids = current_user_roles.map((ur) => ur.role_id as number).filter((id) => id !== undefined);
|
|
227
|
+
const target_role_ids = role_ids.filter((id) => typeof id === "number");
|
|
228
|
+
|
|
229
|
+
// Find roles to add and remove
|
|
230
|
+
const to_add = target_role_ids.filter((id) => !current_role_ids.includes(id));
|
|
231
|
+
const to_remove = current_role_ids.filter((id) => !target_role_ids.includes(id));
|
|
232
|
+
|
|
233
|
+
const now = new Date().toISOString();
|
|
234
|
+
|
|
235
|
+
// Add new roles
|
|
236
|
+
for (const role_id of to_add) {
|
|
237
|
+
// Check if role exists
|
|
238
|
+
const roles_service = createCrudService(hazoConnect, "hazo_roles");
|
|
239
|
+
const roles = await roles_service.findBy({ id: role_id });
|
|
240
|
+
if (Array.isArray(roles) && roles.length > 0) {
|
|
241
|
+
await user_roles_service.insert({
|
|
242
|
+
user_id,
|
|
243
|
+
role_id,
|
|
244
|
+
created_at: now,
|
|
245
|
+
changed_at: now,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Remove roles
|
|
251
|
+
// Note: hazo_user_roles is a junction table without an id column
|
|
252
|
+
// We need to use SQLite admin service to delete by composite key (user_id, role_id)
|
|
253
|
+
if (to_remove.length > 0) {
|
|
254
|
+
try {
|
|
255
|
+
const admin_service = getSqliteAdminService();
|
|
256
|
+
|
|
257
|
+
for (const role_id of to_remove) {
|
|
258
|
+
// Delete using SQLite admin service with criteria (user_id and role_id)
|
|
259
|
+
await admin_service.deleteRows("hazo_user_roles", {
|
|
260
|
+
user_id,
|
|
261
|
+
role_id,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
} catch (adminError) {
|
|
265
|
+
// Fallback: try using createCrudService deleteById if rowid exists
|
|
266
|
+
// SQLite tables have a hidden rowid column that can be used
|
|
267
|
+
const error_message = adminError instanceof Error ? adminError.message : "Unknown error";
|
|
268
|
+
logger.warn("user_management_user_role_delete_admin_failed", {
|
|
269
|
+
filename: get_filename(),
|
|
270
|
+
line_number: get_line_number(),
|
|
271
|
+
error: error_message,
|
|
272
|
+
note: "Trying fallback method",
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// Fallback: try to find and delete using rowid if available
|
|
276
|
+
for (const role_id of to_remove) {
|
|
277
|
+
const assignments_to_remove = await user_roles_service.findBy({
|
|
278
|
+
user_id,
|
|
279
|
+
role_id,
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
if (Array.isArray(assignments_to_remove) && assignments_to_remove.length > 0) {
|
|
283
|
+
for (const assignment of assignments_to_remove) {
|
|
284
|
+
// Try deleteById with rowid (SQLite has hidden rowid)
|
|
285
|
+
try {
|
|
286
|
+
// Check if assignment has an id field (could be rowid)
|
|
287
|
+
if ((assignment as { id?: number }).id !== undefined) {
|
|
288
|
+
await user_roles_service.deleteById((assignment as { id: number }).id);
|
|
289
|
+
} else if ((assignment as { rowid?: number }).rowid !== undefined) {
|
|
290
|
+
await user_roles_service.deleteById((assignment as { rowid: number }).rowid);
|
|
291
|
+
} else {
|
|
292
|
+
// Last resort: log error
|
|
293
|
+
logger.error("user_management_user_role_delete_no_id", {
|
|
294
|
+
filename: get_filename(),
|
|
295
|
+
line_number: get_line_number(),
|
|
296
|
+
user_id,
|
|
297
|
+
role_id,
|
|
298
|
+
assignment,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
} catch (deleteError) {
|
|
302
|
+
const delete_error_message = deleteError instanceof Error ? deleteError.message : "Unknown error";
|
|
303
|
+
logger.error("user_management_user_role_delete_failed", {
|
|
304
|
+
filename: get_filename(),
|
|
305
|
+
line_number: get_line_number(),
|
|
306
|
+
user_id,
|
|
307
|
+
role_id,
|
|
308
|
+
error: delete_error_message,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Invalidate user cache after role assignment changes
|
|
318
|
+
try {
|
|
319
|
+
const config = get_auth_utility_config();
|
|
320
|
+
const cache = get_auth_cache(
|
|
321
|
+
config.cache_max_users,
|
|
322
|
+
config.cache_ttl_minutes,
|
|
323
|
+
config.cache_max_age_minutes,
|
|
324
|
+
);
|
|
325
|
+
cache.invalidate_user(user_id);
|
|
326
|
+
} catch (cache_error) {
|
|
327
|
+
// Log but don't fail role update if cache invalidation fails
|
|
328
|
+
const cache_error_message =
|
|
329
|
+
cache_error instanceof Error ? cache_error.message : "Unknown error";
|
|
330
|
+
logger.warn("user_management_user_roles_cache_invalidation_failed", {
|
|
331
|
+
filename: get_filename(),
|
|
332
|
+
line_number: get_line_number(),
|
|
333
|
+
user_id,
|
|
334
|
+
error: cache_error_message,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
logger.info("user_management_user_roles_updated", {
|
|
339
|
+
filename: get_filename(),
|
|
340
|
+
line_number: get_line_number(),
|
|
341
|
+
user_id,
|
|
342
|
+
added: to_add.length,
|
|
343
|
+
removed: to_remove.length,
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
return NextResponse.json(
|
|
347
|
+
{
|
|
348
|
+
success: true,
|
|
349
|
+
added: to_add.length,
|
|
350
|
+
removed: to_remove.length,
|
|
351
|
+
},
|
|
352
|
+
{ status: 200 }
|
|
353
|
+
);
|
|
354
|
+
} catch (error) {
|
|
355
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
356
|
+
logger.error("user_management_user_roles_update_failed", {
|
|
357
|
+
filename: get_filename(),
|
|
358
|
+
line_number: get_line_number(),
|
|
359
|
+
error: error_message,
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
return NextResponse.json(
|
|
363
|
+
{ error: "Failed to update user roles" },
|
|
364
|
+
{ status: 500 }
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// file_description: API route for user management operations (list users, deactivate, reset password)
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { get_hazo_connect_instance } from "@/lib/hazo_connect_instance.server";
|
|
5
|
+
import { createCrudService } from "hazo_connect/server";
|
|
6
|
+
import { create_app_logger } from "@/lib/app_logger";
|
|
7
|
+
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
8
|
+
import { request_password_reset } from "@/lib/services/password_reset_service";
|
|
9
|
+
import { get_auth_cache } from "@/lib/auth/auth_cache";
|
|
10
|
+
import { get_auth_utility_config } from "@/lib/auth_utility_config.server";
|
|
11
|
+
|
|
12
|
+
// section: route_config
|
|
13
|
+
export const dynamic = 'force-dynamic';
|
|
14
|
+
|
|
15
|
+
// section: api_handler
|
|
16
|
+
/**
|
|
17
|
+
* GET - Fetch all users with details or a specific user by id
|
|
18
|
+
* Query params: id (optional) - if provided, returns only that user
|
|
19
|
+
*/
|
|
20
|
+
export async function GET(request: NextRequest) {
|
|
21
|
+
const logger = create_app_logger();
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const { searchParams } = new URL(request.url);
|
|
25
|
+
const user_id = searchParams.get("id");
|
|
26
|
+
|
|
27
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
28
|
+
const users_service = createCrudService(hazoConnect, "hazo_users");
|
|
29
|
+
|
|
30
|
+
// Fetch users - filter by id if provided, otherwise get all
|
|
31
|
+
const users = await users_service.findBy(user_id ? { id: user_id } : {});
|
|
32
|
+
|
|
33
|
+
if (!Array.isArray(users)) {
|
|
34
|
+
return NextResponse.json(
|
|
35
|
+
{ error: "Failed to fetch users" },
|
|
36
|
+
{ status: 500 }
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
logger.info("user_management_users_fetched", {
|
|
41
|
+
filename: get_filename(),
|
|
42
|
+
line_number: get_line_number(),
|
|
43
|
+
user_count: users.length,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return NextResponse.json(
|
|
47
|
+
{
|
|
48
|
+
success: true,
|
|
49
|
+
users: users.map((user) => ({
|
|
50
|
+
id: user.id,
|
|
51
|
+
name: user.name || null,
|
|
52
|
+
email_address: user.email_address,
|
|
53
|
+
email_verified: user.email_verified || false,
|
|
54
|
+
is_active: user.is_active !== false,
|
|
55
|
+
last_logon: user.last_logon || null,
|
|
56
|
+
created_at: user.created_at || null,
|
|
57
|
+
profile_picture_url: user.profile_picture_url || null,
|
|
58
|
+
profile_source: user.profile_source || null,
|
|
59
|
+
})),
|
|
60
|
+
},
|
|
61
|
+
{ status: 200 }
|
|
62
|
+
);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
65
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
66
|
+
|
|
67
|
+
logger.error("user_management_users_fetch_error", {
|
|
68
|
+
filename: get_filename(),
|
|
69
|
+
line_number: get_line_number(),
|
|
70
|
+
error_message,
|
|
71
|
+
error_stack,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
return NextResponse.json(
|
|
75
|
+
{ error: "Failed to fetch users" },
|
|
76
|
+
{ status: 500 }
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* PATCH - Update user (deactivate: set is_active to false)
|
|
83
|
+
*/
|
|
84
|
+
export async function PATCH(request: NextRequest) {
|
|
85
|
+
const logger = create_app_logger();
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
const body = await request.json();
|
|
89
|
+
const { user_id, is_active } = body;
|
|
90
|
+
|
|
91
|
+
if (!user_id || typeof is_active !== "boolean") {
|
|
92
|
+
return NextResponse.json(
|
|
93
|
+
{ error: "user_id and is_active (boolean) are required" },
|
|
94
|
+
{ status: 400 }
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
99
|
+
const users_service = createCrudService(hazoConnect, "hazo_users");
|
|
100
|
+
|
|
101
|
+
// Update user with changed_at timestamp
|
|
102
|
+
const now = new Date().toISOString();
|
|
103
|
+
await users_service.updateById(user_id, {
|
|
104
|
+
is_active,
|
|
105
|
+
changed_at: now,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Invalidate user cache after deactivation
|
|
109
|
+
if (is_active === false) {
|
|
110
|
+
try {
|
|
111
|
+
const config = get_auth_utility_config();
|
|
112
|
+
const cache = get_auth_cache(
|
|
113
|
+
config.cache_max_users,
|
|
114
|
+
config.cache_ttl_minutes,
|
|
115
|
+
config.cache_max_age_minutes,
|
|
116
|
+
);
|
|
117
|
+
cache.invalidate_user(user_id);
|
|
118
|
+
} catch (cache_error) {
|
|
119
|
+
// Log but don't fail user update if cache invalidation fails
|
|
120
|
+
const cache_error_message =
|
|
121
|
+
cache_error instanceof Error ? cache_error.message : "Unknown error";
|
|
122
|
+
logger.warn("user_management_user_cache_invalidation_failed", {
|
|
123
|
+
filename: get_filename(),
|
|
124
|
+
line_number: get_line_number(),
|
|
125
|
+
user_id,
|
|
126
|
+
error: cache_error_message,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
logger.info("user_management_user_updated", {
|
|
132
|
+
filename: get_filename(),
|
|
133
|
+
line_number: get_line_number(),
|
|
134
|
+
user_id,
|
|
135
|
+
is_active,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return NextResponse.json(
|
|
139
|
+
{ success: true },
|
|
140
|
+
{ status: 200 }
|
|
141
|
+
);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
144
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
145
|
+
|
|
146
|
+
logger.error("user_management_user_update_error", {
|
|
147
|
+
filename: get_filename(),
|
|
148
|
+
line_number: get_line_number(),
|
|
149
|
+
error_message,
|
|
150
|
+
error_stack,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
return NextResponse.json(
|
|
154
|
+
{ error: "Failed to update user" },
|
|
155
|
+
{ status: 500 }
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* POST - Send password reset email to user
|
|
162
|
+
*/
|
|
163
|
+
export async function POST(request: NextRequest) {
|
|
164
|
+
const logger = create_app_logger();
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
const body = await request.json();
|
|
168
|
+
const { user_id } = body;
|
|
169
|
+
|
|
170
|
+
if (!user_id) {
|
|
171
|
+
return NextResponse.json(
|
|
172
|
+
{ error: "user_id is required" },
|
|
173
|
+
{ status: 400 }
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
178
|
+
const users_service = createCrudService(hazoConnect, "hazo_users");
|
|
179
|
+
|
|
180
|
+
// Get user by ID
|
|
181
|
+
const users = await users_service.findBy({ id: user_id });
|
|
182
|
+
|
|
183
|
+
if (!Array.isArray(users) || users.length === 0) {
|
|
184
|
+
return NextResponse.json(
|
|
185
|
+
{ error: "User not found" },
|
|
186
|
+
{ status: 404 }
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const user = users[0];
|
|
191
|
+
const email = user.email_address as string;
|
|
192
|
+
|
|
193
|
+
// Request password reset using existing service
|
|
194
|
+
const result = await request_password_reset(hazoConnect, { email });
|
|
195
|
+
|
|
196
|
+
if (!result.success) {
|
|
197
|
+
logger.warn("user_management_password_reset_failed", {
|
|
198
|
+
filename: get_filename(),
|
|
199
|
+
line_number: get_line_number(),
|
|
200
|
+
user_id,
|
|
201
|
+
email,
|
|
202
|
+
error: result.error,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
return NextResponse.json(
|
|
206
|
+
{ error: result.error || "Failed to send password reset email" },
|
|
207
|
+
{ status: 500 }
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
logger.info("user_management_password_reset_sent", {
|
|
212
|
+
filename: get_filename(),
|
|
213
|
+
line_number: get_line_number(),
|
|
214
|
+
user_id,
|
|
215
|
+
email,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
return NextResponse.json(
|
|
219
|
+
{ success: true },
|
|
220
|
+
{ status: 200 }
|
|
221
|
+
);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
224
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
225
|
+
|
|
226
|
+
logger.error("user_management_password_reset_error", {
|
|
227
|
+
filename: get_filename(),
|
|
228
|
+
line_number: get_line_number(),
|
|
229
|
+
error_message,
|
|
230
|
+
error_stack,
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
return NextResponse.json(
|
|
234
|
+
{ error: "Failed to send password reset email" },
|
|
235
|
+
{ status: 500 }
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
@@ -6,6 +6,9 @@ import { validate_password_reset_token } from "@/lib/services/password_reset_ser
|
|
|
6
6
|
import { create_app_logger } from "@/lib/app_logger";
|
|
7
7
|
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
8
8
|
|
|
9
|
+
// section: route_config
|
|
10
|
+
export const dynamic = 'force-dynamic';
|
|
11
|
+
|
|
9
12
|
// section: api_handler
|
|
10
13
|
export async function GET(request: NextRequest) {
|
|
11
14
|
const logger = create_app_logger();
|
|
@@ -6,6 +6,9 @@ import { create_app_logger } from "@/lib/app_logger";
|
|
|
6
6
|
import { verify_email_token } from "@/lib/services/email_verification_service";
|
|
7
7
|
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
8
8
|
|
|
9
|
+
// section: route_config
|
|
10
|
+
export const dynamic = 'force-dynamic';
|
|
11
|
+
|
|
9
12
|
// section: api_handler
|
|
10
13
|
export async function GET(request: NextRequest) {
|
|
11
14
|
const logger = create_app_logger();
|
package/src/app/globals.css
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// file_description: render the user management page shell and mount the user management layout component within sidebar
|
|
2
|
+
// section: imports
|
|
3
|
+
import { SidebarLayoutWrapper } from "@/components/layouts/shared/components/sidebar_layout_wrapper";
|
|
4
|
+
import { UserManagementPageClient } from "./user_management_page_client";
|
|
5
|
+
|
|
6
|
+
// section: component
|
|
7
|
+
export default function user_management_page() {
|
|
8
|
+
return (
|
|
9
|
+
<SidebarLayoutWrapper>
|
|
10
|
+
<UserManagementPageClient />
|
|
11
|
+
</SidebarLayoutWrapper>
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// file_description: client component for user management page
|
|
2
|
+
// section: client_directive
|
|
3
|
+
"use client";
|
|
4
|
+
|
|
5
|
+
// section: imports
|
|
6
|
+
import { UserManagementLayout } from "@/components/layouts/user_management";
|
|
7
|
+
|
|
8
|
+
// section: component
|
|
9
|
+
/**
|
|
10
|
+
* Client component for user management page
|
|
11
|
+
* @returns User Management layout component
|
|
12
|
+
*/
|
|
13
|
+
export function UserManagementPageClient() {
|
|
14
|
+
return <UserManagementLayout className="w-full" />;
|
|
15
|
+
}
|
|
16
|
+
|
|
@@ -25,7 +25,13 @@ const allowedOperators: SqliteFilterOperator[] = [
|
|
|
25
25
|
function ensureAdminServiceInitialized() {
|
|
26
26
|
// Get singleton hazo_connect instance (initializes admin service if needed)
|
|
27
27
|
get_hazo_connect_instance();
|
|
28
|
-
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
return getSqliteAdminService();
|
|
31
|
+
} catch (serviceError) {
|
|
32
|
+
const errorMessage = serviceError instanceof Error ? serviceError.message : "Unknown error";
|
|
33
|
+
throw new Error(`SQLite Admin Service not available: ${errorMessage}. Make sure enable_admin_ui is set to true in hazo_auth_config.ini.`);
|
|
34
|
+
}
|
|
29
35
|
}
|
|
30
36
|
|
|
31
37
|
export async function GET(request: NextRequest) {
|