RequireRole checks that the context has a user with at least the given role. Returns the UserInfo on success or an error suitable for gRPC status responses.
(ctx context.Context, required UserRole)
| 69 | // RequireRole checks that the context has a user with at least the given role. |
| 70 | // Returns the UserInfo on success or an error suitable for gRPC status responses. |
| 71 | func RequireRole(ctx context.Context, required UserRole) (*UserInfo, error) { |
| 72 | u := UserFromContext(ctx) |
| 73 | if u == nil { |
| 74 | return nil, fmt.Errorf("no authenticated user in context") |
| 75 | } |
| 76 | if !u.HasRole(required) { |
| 77 | return nil, fmt.Errorf("role %q required, user has %q", required, u.Role) |
| 78 | } |
| 79 | return u, nil |
| 80 | } |
| 81 | |
| 82 | // ParseRole converts a string to UserRole, defaulting to RoleUser for unknown values. |
| 83 | func ParseRole(s string) UserRole { |
nothing calls this directly
no test coverage detected