check constructs a CheckFunction that performs permission checks based on the type of reference in the entity definition.
( ctx context.Context, request *base.PermissionCheckRequest, en *base.EntityDefinition, )
| 106 | |
| 107 | // check constructs a CheckFunction that performs permission checks based on the type of reference in the entity definition. |
| 108 | func (engine *CheckEngine) check( |
| 109 | ctx context.Context, |
| 110 | request *base.PermissionCheckRequest, |
| 111 | en *base.EntityDefinition, |
| 112 | ) CheckFunction { |
| 113 | // If the request's entity and permission are the same as the subject, return a CheckFunction that always allows the permission. |
| 114 | if tuple.AreQueryAndSubjectEqual(request.GetEntity(), request.GetPermission(), request.GetSubject()) { |
| 115 | return func(ctx context.Context) (*base.PermissionCheckResponse, error) { |
| 116 | return allowed(emptyResponseMetadata()), nil |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Declare a CheckFunction variable that will later be defined based on the type of reference. |
| 121 | var fn CheckFunction |
| 122 | |
| 123 | // Determine the type of the reference by name in the given entity definition. |
| 124 | tor, _ := schema.GetTypeOfReferenceByNameInEntityDefinition(en, request.GetPermission()) |
| 125 | |
| 126 | // Based on the type of the reference, define the CheckFunction in different ways. |
| 127 | switch tor { |
| 128 | case base.EntityDefinition_REFERENCE_PERMISSION: |
| 129 | // Get the permission from the entity definition. |
| 130 | permission, err := schema.GetPermissionByNameInEntityDefinition(en, request.GetPermission()) |
| 131 | if err != nil { |
| 132 | // If an error is encountered while getting the permission, a CheckFunction is returned that always fails with this error. |
| 133 | return checkFail(err) |
| 134 | } |
| 135 | // Get the child of the permission. |
| 136 | child := permission.GetChild() |
| 137 | |
| 138 | // If the child has a rewrite, check the rewrite. |
| 139 | // If not, check the leaf. |
| 140 | if child.GetRewrite() != nil { |
| 141 | fn = engine.checkRewrite(ctx, request, child.GetRewrite()) |
| 142 | } else { |
| 143 | fn = engine.checkLeaf(request, child.GetLeaf()) |
| 144 | } |
| 145 | case base.EntityDefinition_REFERENCE_ATTRIBUTE: |
| 146 | // If the reference is an attribute, check the direct attribute. |
| 147 | fn = engine.checkDirectAttribute(request) |
| 148 | case base.EntityDefinition_REFERENCE_RELATION: |
| 149 | // If the reference is a relation, check the direct relation. |
| 150 | fn = engine.checkDirectRelation(request) |
| 151 | default: |
| 152 | fn = engine.checkDirectCall(request) |
| 153 | } |
| 154 | |
| 155 | // If the CheckFunction is still undefined after the switch, return a CheckFunction that always fails with an error indicating an undefined child kind. |
| 156 | if fn == nil { |
| 157 | return checkFail(errors.New(base.ErrorCode_ERROR_CODE_UNDEFINED_CHILD_KIND.String())) |
| 158 | } |
| 159 | |
| 160 | // Otherwise, return a CheckFunction that checks a union of CheckFunctions with a concurrency limit. |
| 161 | return func(ctx context.Context) (*base.PermissionCheckResponse, error) { |
| 162 | return checkUnion(ctx, []CheckFunction{fn}, engine.concurrencyLimit) |
| 163 | } |
| 164 | } |
| 165 |
no test coverage detected