feat: add ownership transfer support

This commit is contained in:
Aarnav Tale
2026-03-14 15:17:45 -04:00
parent 55ddbe0987
commit a26faab139
6 changed files with 142 additions and 3 deletions
+40
View File
@@ -434,6 +434,46 @@ export class AuthService {
return (user.role in Roles ? user.role : "member") as Role;
}
/**
* Transfer ownership from the current owner to another user.
* The current owner is demoted to admin and the target is promoted
* to owner. Both users must exist. Returns false if the caller is
* not actually the owner or the target doesn't exist.
*/
async transferOwnership(currentOwnerSubject: string, newOwnerSubject: string): Promise<boolean> {
const [current] = await this.opts.db
.select()
.from(users)
.where(eq(users.sub, currentOwnerSubject))
.limit(1);
if (!current || current.role !== "owner") {
return false;
}
const [target] = await this.opts.db
.select()
.from(users)
.where(eq(users.sub, newOwnerSubject))
.limit(1);
if (!target || target.id === current.id) {
return false;
}
await this.opts.db
.update(users)
.set({ role: "admin", caps: capsForRole("admin"), updated_at: new Date() })
.where(eq(users.id, current.id));
await this.opts.db
.update(users)
.set({ role: "owner", caps: capsForRole("owner"), updated_at: new Date() })
.where(eq(users.id, target.id));
return true;
}
/**
* Reassign the role of a user identified by their OIDC subject.
* Cannot reassign the owner role.