func RequireUser(ctx context.Context, id ttnpb.UserIdentifiers, required ...ttnpb.Right) (err error) {
	uid := unique.ID(ctx, id)
	rights, err := ListUser(ctx, id)
	if err != nil {
		return err
	}
	if len(rights.GetRights()) == 0 {
		return ErrNoUserRights.WithAttributes("uid", uid)
	}
	missing := ttnpb.RightsFrom(required...).Sub(rights).GetRights()
	if len(missing) > 0 {
		return ErrInsufficientUserRights.WithAttributes("uid", uid, "missing", rightsNames(missing...))
	}
	return nil
}
RequireUser checks that context contains the required rights for the
given user ID.
package rights
import (
	"context"
	"go.thethings.network/lorawan-stack/v3/pkg/errors"
	"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
	"go.thethings.network/lorawan-stack/v3/pkg/unique"
)
var (
	ErrNoUniversalRights = errors.DefinePermissionDenied(
		"no_universal_rights",
		"no universal rights",
	)
	ErrInsufficientUniversalRights = errors.DefinePermissionDenied(
		"insufficient_universal_rights",
		"insufficient universal rights",
		"missing",
	)
	ErrNoAdmin = errors.DefinePermissionDenied(
		"no_admin",
		"no admin",
	)
	ErrNoApplicationRights = errors.DefinePermissionDenied(
		"no_application_rights",
		"no rights for application `{uid}`",
	)
	ErrInsufficientApplicationRights = errors.DefinePermissionDenied(
		"insufficient_application_rights",
		"insufficient rights for application `{uid}`",
		"missing",
	)
	ErrNoClientRights = errors.DefinePermissionDenied(
		"no_client_rights",
		"no rights for client `{uid}`",
	)
	ErrInsufficientClientRights = errors.DefinePermissionDenied(
		"insufficient_client_rights",
		"insufficient rights for client `{uid}`",
		"missing",
	)
	ErrNoGatewayRights = errors.DefinePermissionDenied(
		"no_gateway_rights",
		"no rights for gateway `{uid}`",
	)
	ErrInsufficientGatewayRights = errors.DefinePermissionDenied(
		"insufficient_gateway_rights",
		"insufficient rights for gateway `{uid}`",
		"missing",
	)
	ErrNoOrganizationRights = errors.DefinePermissionDenied(
		"no_organization_rights",
		"no rights for organization `{uid}`",
	)
	ErrInsufficientOrganizationRights = errors.DefinePermissionDenied(
		"insufficient_organization_rights",
		"insufficient rights for organization `{uid}`",
		"missing",
	)
	ErrNoUserRights = errors.DefinePermissionDenied(
		"no_user_rights",
		"no rights for user `{uid}`",
	)
	ErrInsufficientUserRights = errors.DefinePermissionDenied(
		"insufficient_user_rights",
		"insufficient rights for user `{uid}`",
		"missing",
	)
)
func rightsNames(rights ...ttnpb.Right) []string {
	names := make([]string, len(rights))
	for i, right := range rights {
		names[i] = right.String()
	}
	return names
}
func RequireUniversal(ctx context.Context, required ...ttnpb.Right) error {
	authInfo, err := AuthInfo(ctx)
	if err != nil {
		return err
	}
	if rights := authInfo.GetUniversalRights(); len(rights.GetRights()) == 0 {
		return ErrNoUniversalRights.New()
	} else if missing := ttnpb.RightsFrom(required...).Sub(rights).GetRights(); len(missing) > 0 {
		return ErrInsufficientUniversalRights.WithAttributes("missing", rightsNames(missing...))
	}
	return nil
}
func RequireIsAdmin(ctx context.Context) error {
	authInfo, err := AuthInfo(ctx)
	if err != nil {
		return err
	}
	if !authInfo.GetIsAdmin() {
		return ErrNoAdmin.New()
	}
	return nil
}
func RequireApplication(ctx context.Context, id ttnpb.ApplicationIdentifiers, required ...ttnpb.Right) error {
	uid := unique.ID(ctx, id)
	rights, err := ListApplication(ctx, id)
	if err != nil {
		return err
	}
	if len(rights.GetRights()) == 0 {
		return ErrNoApplicationRights.WithAttributes("uid", uid)
	}
	missing := ttnpb.RightsFrom(required...).Sub(rights).GetRights()
	if len(missing) > 0 {
		return ErrInsufficientApplicationRights.WithAttributes("uid", uid, "missing", rightsNames(missing...))
	}
	return nil
}
func RequireClient(ctx context.Context, id ttnpb.ClientIdentifiers, required ...ttnpb.Right) (err error) {
	uid := unique.ID(ctx, id)
	rights, err := ListClient(ctx, id)
	if err != nil {
		return err
	}
	if len(rights.GetRights()) == 0 {
		return ErrNoClientRights.WithAttributes("uid", uid)
	}
	missing := ttnpb.RightsFrom(required...).Sub(rights).GetRights()
	if len(missing) > 0 {
		return ErrInsufficientClientRights.WithAttributes("uid", uid, "missing", rightsNames(missing...))
	}
	return nil
}
func RequireGateway(ctx context.Context, id ttnpb.GatewayIdentifiers, required ...ttnpb.Right) (err error) {
	uid := unique.ID(ctx, id)
	rights, err := ListGateway(ctx, id)
	if err != nil {
		return err
	}
	if len(rights.GetRights()) == 0 {
		return ErrNoGatewayRights.WithAttributes("uid", uid)
	}
	missing := ttnpb.RightsFrom(required...).Sub(rights).GetRights()
	if len(missing) > 0 {
		return ErrInsufficientGatewayRights.WithAttributes("uid", uid, "missing", rightsNames(missing...))
	}
	return nil
}
func RequireOrganization(ctx context.Context, id ttnpb.OrganizationIdentifiers, required ...ttnpb.Right) (err error) {
	uid := unique.ID(ctx, id)
	rights, err := ListOrganization(ctx, id)
	if err != nil {
		return err
	}
	if len(rights.GetRights()) == 0 {
		return ErrNoOrganizationRights.WithAttributes("uid", uid)
	}
	missing := ttnpb.RightsFrom(required...).Sub(rights).GetRights()
	if len(missing) > 0 {
		return ErrInsufficientOrganizationRights.WithAttributes("uid", uid, "missing", rightsNames(missing...))
	}
	return nil
}