Go · 1234 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package policy
4
5 // Actor is the authenticated identity asking for a decision. The web
6 // layer constructs one from middleware.CurrentUserFromContext + a
7 // suspended/admin check. SSH and HTTP git transports build their own
8 // from the resolved auth principal.
9 //
10 // An anonymous request has UserID == 0; IsAnonymous == true. Convention
11 // is that callers fill IsAnonymous explicitly even when UserID == 0
12 // implies it — duplication is cheap and keeps the boolean visible at
13 // every call site.
14 type Actor struct {
15 UserID int64
16 Username string
17 IsAnonymous bool
18 IsSuspended bool
19 IsSiteAdmin bool
20 }
21
22 // AnonymousActor returns the canonical anonymous Actor. Use in tests
23 // and at unauthenticated entrypoints.
24 func AnonymousActor() Actor {
25 return Actor{IsAnonymous: true}
26 }
27
28 // UserActor wraps a logged-in user. Suspension and site-admin flags
29 // must be loaded from the DB by the caller — the policy package does
30 // not query users on its own to keep the decision pure.
31 func UserActor(userID int64, username string, suspended, siteAdmin bool) Actor {
32 return Actor{
33 UserID: userID,
34 Username: username,
35 IsSuspended: suspended,
36 IsSiteAdmin: siteAdmin,
37 }
38 }
39