-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalue_descriptor.go
60 lines (46 loc) · 1.73 KB
/
value_descriptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package restrict
import (
"fmt"
"github.com/el-mike/restrict/v2/internal/utils"
)
// ValueDescriptor - describes a value that will be tested in its parent Condition.
type ValueDescriptor struct {
// Source - source of the value, one of the predefined enum type (ValueSource).
Source ValueSource `json:"source,omitempty" yaml:"source,omitempty"`
// Field - field on the given ValueSource that should hold the value.
Field string `json:"field,omitempty" yaml:"field,omitempty"`
// Value - explicit value taken when using ValueSource.Explicit as value source.
Value interface{} `json:"value,omitempty" yaml:"value,omitempty"`
}
// GetValue - returns real value represented by given ValueDescriptor.
func (vd *ValueDescriptor) GetValue(request *AccessRequest) (interface{}, error) {
if vd == nil {
return nil, newValueDescriptorMalformedError(vd, fmt.Errorf("ValueDescriptor cannot be nil"))
}
if vd.Source == Explicit {
return vd.Value, nil
}
if vd.Field == "" {
return nil, newValueDescriptorMalformedError(vd, fmt.Errorf("Field cannot be empty for Source: \"%s\"", vd.Source.String()))
}
var source interface{}
if vd.Source == SubjectField {
source = request.Subject
}
if vd.Source == ResourceField {
source = request.Resource
}
if vd.Source == ContextField {
source = request.Context
}
if source == nil {
return nil, newValueDescriptorMalformedError(vd, fmt.Errorf("Source could not be find"))
}
if utils.IsMap(source) {
return utils.GetMapValue(source, vd.Field), nil
}
if utils.HasField(source, vd.Field) {
return utils.GetStructFieldValue(source, vd.Field), nil
}
return nil, newValueDescriptorMalformedError(vd, fmt.Errorf("Field \"%s\" does not exist on Source: \"%s\"", vd.Field, vd.Source.String()))
}