MCPcopy Create free account
hub / github.com/Permify/permify / check

Method check

internal/engines/check.go:108–164  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

106
107// check constructs a CheckFunction that performs permission checks based on the type of reference in the entity definition.
108func (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

Callers 1

CheckMethod · 0.95

Calls 15

checkRewriteMethod · 0.95
checkLeafMethod · 0.95
checkDirectAttributeMethod · 0.95
checkDirectRelationMethod · 0.95
checkDirectCallMethod · 0.95
AreQueryAndSubjectEqualFunction · 0.92
allowedFunction · 0.85
emptyResponseMetadataFunction · 0.85
checkFailFunction · 0.85
checkUnionFunction · 0.85

Tested by

no test coverage detected