Admin guest invites: admin inviter, no member cap, no name guessing

The admin panel's /admin/provision-guest now takes ONE field (the guest's
email): no name is collected and none is derived from the email — the
guest chooses their own display name and username during Cloudron account
setup, and the invitation email greets neutrally when no name is known.
The guests ledger records the co-op's ADMIN account (ADMIN_INVITER_EMAIL,
default info@inference.coop) as the inviter. Admin invites provision
immediately (the operator IS the approver) and are not bound by the
member's one-per-30-days cap; recipient-side guards (one guest per
recipient ever, no existing portal row) still run. The member paths
(broker invite, request approval) keep every protection and likewise pass
no derived name.
This commit is contained in:
inference-bot committed 2026-11-08 02:55:10 +00:00
1 parent 7a63b3a728
commit ed50031663
1 file changed
+74 -25
+74 -25
View File
@@ -351,6 +351,10 @@ GUEST_BUDGET = float(os.environ.get("GUEST_BUDGET", "5.0"))
GUEST_DAYS = float(os.environ.get("GUEST_DAYS", "60")) GUEST_DAYS = float(os.environ.get("GUEST_DAYS", "60"))
# One guest per inviting member per rolling 30 days (abuse protection). # One guest per inviting member per rolling 30 days (abuse protection).
GUEST_PER_INVITER_DAYS = float(os.environ.get("GUEST_PER_INVITER_DAYS", "30")) GUEST_PER_INVITER_DAYS = float(os.environ.get("GUEST_PER_INVITER_DAYS", "30"))
# The ADMIN-PANEL guest-invite path records THIS account as the inviter in the
# guests ledger, and admin invites are NOT bound by the member's
# one-per-30-days cap (admins are not members).
ADMIN_INVITER_EMAIL = os.environ.get("ADMIN_INVITER_EMAIL", "info@inference.coop").strip().lower()
# Trial-period approval gate for MEMBER-initiated guest invitations. Off (the # Trial-period approval gate for MEMBER-initiated guest invitations. Off (the
# default): a member's request waits in PENDING until the operator approves or # default): a member's request waits in PENDING until the operator approves or
# rejects it in the panel. On: a member's request provisions immediately, # rejects it in the panel. On: a member's request provisions immediately,
@@ -1129,7 +1133,9 @@ def is_guest_account(email: str) -> bool:
return get_member_account_type(email) == "guest" return get_member_account_type(email) == "guest"
def guest_provision_guard(conn, inviter_email: str, recipient_email: str) -> str | None: def guest_provision_guard(
conn, inviter_email: str, recipient_email: str, admin: bool = False
) -> str | None:
"""Decide whether a guest may be sent (pure — runs on any connection). """Decide whether a guest may be sent (pure — runs on any connection).
Abuse-protection rules (operator decision, 2026-11-08): Abuse-protection rules (operator decision, 2026-11-08):
@@ -1139,6 +1145,12 @@ def guest_provision_guard(conn, inviter_email: str, recipient_email: str) -> str
recipient refuses, as does an existing portal row under that email recipient refuses, as does an existing portal row under that email
(they already have/had a guest account or are a member). (they already have/had a guest account or are a member).
admin=True is the ADMIN-PANEL path: the inviter is the co-op's admin
account, not a member, so the member-side inviter rules (active member,
one-per-30-days) do NOT apply — admins are not bound by the member's
monthly cap. The recipient-side rules still run on every path: one guest
per recipient ever, and no existing portal row.
Returns None when the guest may proceed, else a human-readable refusal. Returns None when the guest may proceed, else a human-readable refusal.
""" """
inviter = (inviter_email or "").strip().lower() inviter = (inviter_email or "").strip().lower()
@@ -1149,6 +1161,7 @@ def guest_provision_guard(conn, inviter_email: str, recipient_email: str) -> str
return "A valid recipient email is required" return "A valid recipient email is required"
if inviter == recipient: if inviter == recipient:
return "You cannot guest the account you are guest access from" return "You cannot guest the account you are guest access from"
if not admin:
row = conn.execute( row = conn.execute(
"SELECT active, account_type FROM members WHERE email = ?", (inviter,) "SELECT active, account_type FROM members WHERE email = ?", (inviter,)
).fetchone() ).fetchone()
@@ -2534,6 +2547,14 @@ def guest_invitation_email(name: str, setup_url: str, inviter_name: str = "") ->
assets. No Loomio/Matrix — guests receive no member invitations. assets. No Loomio/Matrix — guests receive no member invitations.
""" """
subject = "You've been invited to the Inference Cooperative" subject = "You've been invited to the Inference Cooperative"
# Neutral greeting: no name is guessed from the email — the guest chooses
# their own display name during Cloudron account setup.
if name:
greet = f"Hi {name},"
h1 = f"You're invited, {name}"
else:
greet = "Hi there,"
h1 = "You're invited"
if inviter_name: if inviter_name:
invited_line = f"{inviter_name} has invited you to the Inference Cooperative" invited_line = f"{inviter_name} has invited you to the Inference Cooperative"
html_invited = f"<strong style=\"color:#2d3327;\">{inviter_name}</strong> has invited you to the <strong style=\"color:#2d3327;\">Inference Cooperative</strong>" html_invited = f"<strong style=\"color:#2d3327;\">{inviter_name}</strong> has invited you to the <strong style=\"color:#2d3327;\">Inference Cooperative</strong>"
@@ -2541,7 +2562,7 @@ def guest_invitation_email(name: str, setup_url: str, inviter_name: str = "") ->
invited_line = "A member has invited you to the Inference Cooperative" invited_line = "A member has invited you to the Inference Cooperative"
html_invited = "A member has invited you to the <strong style=\"color:#2d3327;\">Inference Cooperative</strong>" html_invited = "A member has invited you to the <strong style=\"color:#2d3327;\">Inference Cooperative</strong>"
text = ( text = (
f"Hi {name},\n\n" f"{greet}\n\n"
f"{invited_line}.\n\n" f"{invited_line}.\n\n"
"It's a member-governed AI cooperative that runs open models on\n" "It's a member-governed AI cooperative that runs open models on\n"
"renewable energy.\n\n" "renewable energy.\n\n"
@@ -2573,7 +2594,7 @@ def guest_invitation_email(name: str, setup_url: str, inviter_name: str = "") ->
</tr> </tr>
<tr> <tr>
<td align="center" style="background:#ffffff;border:1px solid #e8e2d8;border-radius:16px;padding:40px 32px;"> <td align="center" style="background:#ffffff;border:1px solid #e8e2d8;border-radius:16px;padding:40px 32px;">
<h1 style="margin:0 0 12px;font-size:24px;font-weight:700;color:#2d3327;">You're invited, {name}</h1> <h1 style="margin:0 0 12px;font-size:24px;font-weight:700;color:#2d3327;">{h1}</h1>
<p style="margin:0 0 8px;font-size:16px;color:#6b7a62;line-height:1.5;">{html_invited} — a member-governed AI cooperative running open models on renewable energy.</p> <p style="margin:0 0 8px;font-size:16px;color:#6b7a62;line-height:1.5;">{html_invited} — a member-governed AI cooperative running open models on renewable energy.</p>
<p style="margin:0 0 24px;font-size:15px;color:#6b7a62;line-height:1.5;">Your invitation comes with <strong style="color:#2d3327;">$5 of credits</strong> for the co-op's chat and API. Access runs for {int(GUEST_DAYS)} days from today; the credits themselves don't expire early.</p> <p style="margin:0 0 24px;font-size:15px;color:#6b7a62;line-height:1.5;">Your invitation comes with <strong style="color:#2d3327;">$5 of credits</strong> for the co-op's chat and API. Access runs for {int(GUEST_DAYS)} days from today; the credits themselves don't expire early.</p>
<a href="{setup_url}" style="display:inline-block;background:#5b8c5a;color:#ffffff;text-decoration:none;padding:14px 36px;border-radius:10px;font-size:16px;font-weight:600;">Create your account</a> <a href="{setup_url}" style="display:inline-block;background:#5b8c5a;color:#ffffff;text-decoration:none;padding:14px 36px;border-radius:10px;font-size:16px;font-weight:600;">Create your account</a>
@@ -2704,7 +2725,13 @@ def guest_limits_email(name: str, reason: str) -> tuple[str, str, str]:
return subject, text, html return subject, text, html
async def provision_guest_member(email: str, name: str, inviter_email: str) -> str: async def provision_guest_member(
email: str,
name: str,
inviter_email: str,
admin: bool = False,
inviter_display: str | None = None,
) -> str:
"""Provision a GUEST account end-to-end (operator guest access feature). """Provision a GUEST account end-to-end (operator guest access feature).
A guest gives a friend $5 of credits with no Open Collective membership: A guest gives a friend $5 of credits with no Open Collective membership:
@@ -2714,18 +2741,23 @@ async def provision_guest_member(email: str, name: str, inviter_email: str) -> s
(budget_duration None — the cap is the guest's whole life); (budget_duration None — the cap is the guest's whole life);
* the portal row marked account_type='guest' with the $5 held as * the portal row marked account_type='guest' with the $5 held as
non-expiring credit (the credits model) and guest_expires_at set; non-expiring credit (the credits model) and guest_expires_at set;
* the guest INVITATION email (inviter named, the $5 credits, the setup * the guest INVITATION email (inviter named, the $5 credits, the
link — the invitation and the guest welcome in one) and the setup link — the invitation and the guest welcome in one) and the
newsletter subscription; newsletter subscription;
* a guests-ledger row recording inviter → recipient; * a guests-ledger row recording inviter → recipient;
* NO Loomio/Matrix invites, no docs-RAG members group. * NO Loomio/Matrix invites, no docs-RAG members group.
Reuses provision_member's internals (user create, team, key, store) rather Reuses provision_member's internals (user create, team, key, store) rather
than duplicating them. Guards (guest_provision_guard) enforce one guest per than duplicating them. Guards (guest_provision_guard) enforce one guest per
inviter per 30 days and one guest per recipient ever. inviter per 30 days and one guest per recipient ever — except on the
admin path (admin=True), where the inviter is the co-op's admin account
and the member's monthly cap does not apply.
inviter_display overrides the invitation email's inviter line (the admin
path passes a co-op label instead of resolving the inviter's member slug).
""" """
conn = get_db() conn = get_db()
refusal = guest_provision_guard(conn, inviter_email, email) refusal = guest_provision_guard(conn, inviter_email, email, admin=admin)
conn.close() conn.close()
if refusal: if refusal:
raise HTTPException(429, refusal) raise HTTPException(429, refusal)
@@ -2768,7 +2800,12 @@ async def provision_guest_member(email: str, name: str, inviter_email: str) -> s
setup_url = await cloudron_get_invite_link(user_id) setup_url = await cloudron_get_invite_link(user_id)
# Name the inviter in the invitation: their member slug (the username the # Name the inviter in the invitation: their member slug (the username the
# co-op knows them by — the members table has no display-name column). # co-op knows them by — the members table has no display-name column).
# The admin path passes inviter_display instead (a co-op label — the
# invitation comes from the co-op, not from a member).
inviter_name = "" inviter_name = ""
if inviter_display is not None:
inviter_name = inviter_display
else:
conn = get_db() conn = get_db()
row = conn.execute( row = conn.execute(
"SELECT slug FROM members WHERE email = ?", (inviter_email.strip().lower(),) "SELECT slug FROM members WHERE email = ?", (inviter_email.strip().lower(),)
@@ -4174,29 +4211,42 @@ async def admin_provision(request: Request):
@app.post("/admin/provision-guest") @app.post("/admin/provision-guest")
@limiter.limit("20/minute") @limiter.limit("20/minute")
async def admin_provision_guest(request: Request): async def admin_provision_guest(request: Request):
"""Provision a GUEST account (the operator's guest access action). """Provision a GUEST account (the admin panel's invite action).
Same auth as /admin/provision (X-Admin-Token). Body: {"email": "...", Same auth as /admin/provision (X-Admin-Token). Body: {"email": "..."} —
"name": "...", "inviter": "<the guest access member's email>"}. Runs the guest ONE field, the standard guest-invite form. No name is collected and none
pipeline: Cloudron user in the guests group, a $5 LiteLLM team with NO is derived: the guest chooses their own display name and username during
monthly reset, the portal row marked as a guest with a GUEST_DAYS expiry, Cloudron account setup, and the invitation email greets neutrally. The
the guest welcome email, the newsletter subscription, and a guests-ledger inviter is always the co-op's ADMIN account
row recording inviter → recipient. Abuse guards: one guest per inviter per (ADMIN_INVITER_EMAIL, default info@inference.coop) — the panel has no
30 days; one guest per recipient ever; the inviter must be an active inviter input, and the ledger records the admin account.
member. No Loomio/Matrix.
Runs the guest pipeline: Cloudron user in the guests group, a $5 LiteLLM
team with NO monthly reset, the portal row marked as a guest with a
GUEST_DAYS expiry, the guest welcome email, the newsletter subscription,
and a guests-ledger row recording inviter → recipient. Admin invites
provision IMMEDIATELY (no pending state — the operator IS the approver)
and are NOT bound by the member's one-per-30-days cap; the recipient-side
guards still run (one guest per recipient ever, no existing portal row).
No Loomio/Matrix.
""" """
_verify_admin_token(request) _verify_admin_token(request)
body = await request.json() body = await request.json()
email = (body.get("email") or "").strip().lower() email = (body.get("email") or "").strip().lower()
name = (body.get("name") or "").strip() or email.split("@")[0] # No name is collected and none is derived: the guest chooses their own
inviter = (body.get("inviter") or body.get("inviter_email") or "").strip().lower() # display name and username during Cloudron account setup, and the
# invitation email greets neutrally.
name = (body.get("name") or "").strip()
if not email or "@" not in email: if not email or "@" not in email:
raise HTTPException(400, "A valid recipient email is required") raise HTTPException(400, "A valid recipient email is required")
if not inviter or "@" not in inviter: # The inviter is the admin account, never a caller-supplied member email:
raise HTTPException(400, "The inviting member's email ('inviter') is required") # this endpoint speaks for the operator, and admins have no monthly cap.
inviter = ADMIN_INVITER_EMAIL
user_id = await provision_guest_member(email, name, inviter) user_id = await provision_guest_member(
email, name, inviter, admin=True, inviter_display="The co-op"
)
return { return {
"status": "provisioned", "account_type": "guest", "email": email, "status": "provisioned", "account_type": "guest", "email": email,
"inviter": inviter, "user_id": user_id, "budget": GUEST_BUDGET, "inviter": inviter, "user_id": user_id, "budget": GUEST_BUDGET,
@@ -4346,7 +4396,7 @@ async def admin_guest_request_approve(request_id: int, request: Request):
raise HTTPException(409, f"Request #{request_id} is already {status}") raise HTTPException(409, f"Request #{request_id} is already {status}")
try: try:
user_id = await provision_guest_member( user_id = await provision_guest_member(
recipient, name or recipient.split("@")[0], inviter) recipient, name or "", inviter)
except HTTPException as e: except HTTPException as e:
conn = get_db() conn = get_db()
conn.execute( conn.execute(
@@ -6245,8 +6295,7 @@ async def broker_guest_invite(request: Request):
"friend gets their account and invitation email, and " "friend gets their account and invitation email, and "
"the status shows here."), "the status shows here."),
} }
user_id = await provision_guest_member( user_id = await provision_guest_member(recipient, name, email)
recipient, name or recipient.split("@")[0], email)
return { return {
"status": "provisioned", "account_type": "guest", "email": recipient, "status": "provisioned", "account_type": "guest", "email": recipient,
"inviter": email, "user_id": user_id, "budget": GUEST_BUDGET, "inviter": email, "user_id": user_id, "budget": GUEST_BUDGET,