checkDirectAttribute constructs a CheckFunction that checks if a direct attribute permission check request is allowed or denied.
( request *base.PermissionCheckRequest, )
| 443 | // checkDirectAttribute constructs a CheckFunction that checks if a direct attribute |
| 444 | // permission check request is allowed or denied. |
| 445 | func (engine *CheckEngine) checkDirectAttribute( |
| 446 | request *base.PermissionCheckRequest, |
| 447 | ) CheckFunction { |
| 448 | // We're returning a function here - this is the actual CheckFunction. |
| 449 | return func(ctx context.Context) (*base.PermissionCheckResponse, error) { |
| 450 | // Initial error declaration |
| 451 | var err error |
| 452 | |
| 453 | // Create a new AttributeFilter with the entity type and ID from the request |
| 454 | // and the requested permission. |
| 455 | filter := &base.AttributeFilter{ |
| 456 | Entity: &base.EntityFilter{ |
| 457 | Type: request.GetEntity().GetType(), |
| 458 | Ids: []string{request.GetEntity().GetId()}, |
| 459 | }, |
| 460 | Attributes: []string{request.GetPermission()}, |
| 461 | } |
| 462 | |
| 463 | var val *base.Attribute |
| 464 | |
| 465 | // storageContext.NewContextualAttributes creates a new instance of ContextualAttributes based on the attributes |
| 466 | // retrieved from the request context. |
| 467 | val, err = storageContext.NewContextualAttributes(request.GetContext().GetAttributes()...).QuerySingleAttribute(filter) |
| 468 | // An error occurred while querying the single attribute, so we return a denied response with empty metadata |
| 469 | // and the error. |
| 470 | if err != nil { |
| 471 | return denied(emptyResponseMetadata()), err |
| 472 | } |
| 473 | |
| 474 | if val == nil { |
| 475 | // Use the data reader's QuerySingleAttribute method to find the relevant attribute |
| 476 | val, err = engine.dataReader.QuerySingleAttribute(ctx, request.GetTenantId(), filter, request.GetMetadata().GetSnapToken()) |
| 477 | // If there was an error, return a denied response and the error. |
| 478 | if err != nil { |
| 479 | return denied(emptyResponseMetadata()), err |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // No attribute was found matching the provided filter. In this case, we return a denied response with empty metadata |
| 484 | // and no error. |
| 485 | if val == nil { |
| 486 | return denied(emptyResponseMetadata()), nil |
| 487 | } |
| 488 | |
| 489 | // Unmarshal the attribute value into a BoolValue message. |
| 490 | var msg base.BooleanValue |
| 491 | if err := val.GetValue().UnmarshalTo(&msg); err != nil { |
| 492 | // If there was an error unmarshaling, return a denied response and the error. |
| 493 | return denied(emptyResponseMetadata()), err |
| 494 | } |
| 495 | |
| 496 | // If the attribute's value is true, return an allowed response. |
| 497 | if msg.Data { |
| 498 | return allowed(emptyResponseMetadata()), nil |
| 499 | } |
| 500 | |
| 501 | // If the attribute's value is not true, return a denied response. |
| 502 | return denied(emptyResponseMetadata()), nil |
no test coverage detected