feature: signup and login work
This commit is contained in:
parent
a4a750b35c
commit
3d42591ee5
18 changed files with 956 additions and 65 deletions
|
@ -1,19 +1,24 @@
|
|||
import { get_session, get_user, PRECHECK_TABLE, require_user } from '../../../../utils/prechecks.ts';
|
||||
import { PASSWORD_ENTRIES, PASSWORD_ENTRY } from '../../../../models/password_entry.ts';
|
||||
import { SESSIONS } from '../../../../models/session.ts';
|
||||
import { USER, USERS } from '../../../../models/user.ts';
|
||||
import { PERMISSIONS_STORE, USER_PERMISSIONS } from '../../../../models/user_permissions.ts';
|
||||
import parse_body from '../../../../utils/bodyparser.ts';
|
||||
import { CANNED_RESPONSES } from '../../../../utils/canned_responses.ts';
|
||||
|
||||
export const PERMISSIONS: Record<string, (req: Request, meta: Record<string, any>) => Promise<boolean>> = {};
|
||||
export const PRECHECKS: PRECHECK_TABLE = {};
|
||||
|
||||
// GET /api/users/:id - Get single user
|
||||
PERMISSIONS.GET = (_req: Request, meta: Record<string, any>): Promise<boolean> => {
|
||||
PRECHECKS.GET = [get_session, get_user, require_user, (_req: Request, meta: Record<string, any>): Response | undefined => {
|
||||
const user_is_self = !!meta.user && !!meta.params && meta.user.id === meta.params.id;
|
||||
const can_read_self = meta.user_permissions?.permissions.includes('self.read');
|
||||
const can_read_others = meta.user_permissions?.permissions?.includes('users.read');
|
||||
|
||||
return can_read_others || (can_read_self && user_is_self);
|
||||
};
|
||||
const has_permission = can_read_others || (can_read_self && user_is_self);
|
||||
if (!has_permission) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export async function GET(_req: Request, meta: Record<string, any>): Promise<Response> {
|
||||
const user_id: string = meta.params?.id?.toLowerCase().trim() ?? '';
|
||||
const user: USER | null = user_id.length === 49 ? await USERS.get(user_id) : null; // lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
|
@ -29,34 +34,22 @@ export async function GET(_req: Request, meta: Record<string, any>): Promise<Res
|
|||
});
|
||||
}
|
||||
|
||||
const user_is_self = meta.user?.id === user.id;
|
||||
const has_permission_to_read = (user_is_self && meta.user_permissions?.permissions?.includes('self.read')) ||
|
||||
(meta.user_permissions?.permissions?.includes('users.read'));
|
||||
|
||||
if (!has_permission_to_read) {
|
||||
return Response.json({
|
||||
error: {
|
||||
message: 'Permission denied.',
|
||||
cause: 'permission_denied'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
return Response.json(user, {
|
||||
status: 200
|
||||
});
|
||||
}
|
||||
|
||||
// PUT /api/users/:id - Update user
|
||||
PERMISSIONS.PUT = (_req: Request, meta: Record<string, any>): Promise<boolean> => {
|
||||
PRECHECKS.PUT = [get_session, get_user, require_user, (_req: Request, meta: Record<string, any>): Response | undefined => {
|
||||
const user_is_self = !!meta.user && !!meta.params && meta.user.id === meta.params.id;
|
||||
const can_write_self = meta.user_permissions?.permissions.includes('self.write');
|
||||
const can_write_others = meta.user_permissions?.permissions?.includes('users.write');
|
||||
|
||||
return can_write_others || (can_write_self && user_is_self);
|
||||
};
|
||||
const has_permission = can_write_others || (can_write_self && user_is_self);
|
||||
if (!has_permission) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export async function PUT(req: Request, meta: { params: Record<string, any> }): Promise<Response> {
|
||||
const now = new Date().toISOString();
|
||||
const id: string = meta.params.id ?? '';
|
||||
|
@ -101,13 +94,16 @@ export async function PUT(req: Request, meta: { params: Record<string, any> }):
|
|||
}
|
||||
|
||||
// DELETE /api/users/:id - Delete user
|
||||
PERMISSIONS.DELETE = (_req: Request, meta: Record<string, any>): Promise<boolean> => {
|
||||
PRECHECKS.DELETE = [get_session, get_user, require_user, (_req: Request, meta: Record<string, any>): Response | undefined => {
|
||||
const user_is_self = !!meta.user && !!meta.params && meta.user.id === meta.params.id;
|
||||
const can_write_self = meta.user_permissions?.permissions.includes('self.write');
|
||||
const can_write_others = meta.user_permissions?.permissions?.includes('users.write');
|
||||
|
||||
return can_write_others || (can_write_self && user_is_self);
|
||||
};
|
||||
const has_permission = can_write_others || (can_write_self && user_is_self);
|
||||
if (!has_permission) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export async function DELETE(_req: Request, meta: { params: Record<string, any> }): Promise<Response> {
|
||||
const user_id: string = meta.params.id ?? '';
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue