MCPcopy Create free account
hub / github.com/diillson/chatcli / RequireRole

Function RequireRole

server/security_context.go:71–80  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
71func 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.
83func ParseRole(s string) UserRole {

Callers

nothing calls this directly

Calls 3

UserFromContextFunction · 0.85
ErrorfMethod · 0.80
HasRoleMethod · 0.80

Tested by

no test coverage detected