From cf02b3ef5cc4d10aec0227eb4ad2fa286ee9898a Mon Sep 17 00:00:00 2001 From: Thomas LAY Date: Wed, 8 Apr 2020 18:10:37 +0200 Subject: [PATCH] Add functions to get a list of publisher restrictions for purpose + check if vendor has consent or LI for a list of purposes --- README.md | 3 ++ segment_core_string.go | 71 ++++++++++++++++++++++++++++++++++++++++++ tcdata.go | 17 ++++++++++ 3 files changed, 91 insertions(+) diff --git a/README.md b/README.md index 20027a2..3faf512 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,9 @@ To verify that a legal basis is established for a purpose or a vendor, use the f | IsPurposeLIAllowed | int | Returns `true` if legitimate interest is established for purpose id and user didn't exercise their right to object | | IsVendorAllowed | int | Returns `true` if user has given consent to vendor id processing their personal data | | IsVendorLIAllowed | int | Returns `true` if transparency for vendor id's legitimate interest is established and user didn't exercise their right to object | +| IsVendorAllowedForPurposes | (int, ...int) | Returns `true` if user has given consent to vendor id processing all purposes ids and publisher hasn't set restrictions for them | +| IsVendorAllowedForPurposesLI | (int, ...int) | Returns `true` if transparency for vendor id's legitimate interest is established for all purpose ids and publisher hasn't set restrictions for them | +| GetPubRestrictionsForPurpose | int | Returns a list of publisher restrictions applied to purpose id | NOTE: For convenience the `CoreString` functions are also available from the `TCData` structure. diff --git a/segment_core_string.go b/segment_core_string.go index 60742f4..52469ab 100644 --- a/segment_core_string.go +++ b/segment_core_string.go @@ -93,6 +93,77 @@ func (c *CoreString) IsVendorLIAllowed(id int) bool { return c.VendorsLITransparency[id] } +// Returns true if user has given consent to vendor id processing all purposes ids +// and publisher hasn't set restrictions for them +func (c *CoreString) IsVendorAllowedForPurposes(id int, purposeIds ...int) bool { + if !c.IsVendorAllowed(id) { + return false + } + + for _, p := range purposeIds { + if !c.IsPurposeAllowed(p) { + return false + } + } + + for _, p := range purposeIds { + pr := c.GetPubRestrictionsForPurpose(p) + for _, r := range pr { + if (r.RestrictionType == RestrictionTypeNotAllowed || r.RestrictionType == RestrictionTypeRequireLI) && r.IsVendorIncluded(id) { + return false + } + } + } + + return true +} + +// Returns true if transparency for vendor id's legitimate interest is established for all purpose ids +// and publisher hasn't set restrictions for them +func (c *CoreString) IsVendorAllowedForPurposesLI(id int, purposeIds ...int) bool { + if !c.IsVendorLIAllowed(id) { + return false + } + + for _, p := range purposeIds { + if !c.IsPurposeLIAllowed(p) { + return false + } + } + + for _, p := range purposeIds { + pr := c.GetPubRestrictionsForPurpose(p) + for _, r := range pr { + if (r.RestrictionType == RestrictionTypeNotAllowed || r.RestrictionType == RestrictionTypeRequireConsent) && r.IsVendorIncluded(id) { + return false + } + } + } + + return true +} + +// Returns a list of publisher restrictions applied to purpose id +func (c *CoreString) GetPubRestrictionsForPurpose(id int) []*PubRestriction { + var pr []*PubRestriction + for _, r := range c.PubRestrictions { + if r.PurposeId == id { + pr = append(pr, r) + } + } + return pr +} + +// Returns true if restriction is applied to vendor id +func (p *PubRestriction) IsVendorIncluded(id int) bool { + for _, entry := range p.RangeEntries { + if entry.StartVendorID <= id && id <= entry.EndVendorID { + return true + } + } + return false +} + // Returns structure as a base64 raw url encoded string func (c *CoreString) Encode() string { bitSize := 230 diff --git a/tcdata.go b/tcdata.go index e448a7c..a64ce9c 100644 --- a/tcdata.go +++ b/tcdata.go @@ -36,6 +36,23 @@ func (t *TCData) IsVendorLIAllowed(id int) bool { return t.CoreString.IsVendorLIAllowed(id) } +// Returns true if user has given consent to vendor id processing all purposes ids +// and publisher hasn't set restrictions for them +func (t *TCData) IsVendorAllowedForPurposes(id int, purposeIds ...int) bool { + return t.CoreString.IsVendorAllowedForPurposes(id, purposeIds...) +} + +// Returns true if transparency for vendor id's legitimate interest is established for all purpose ids +// and publisher hasn't set restrictions for them +func (t *TCData) IsVendorAllowedForPurposesLI(id int, purposeIds ...int) bool { + return t.CoreString.IsVendorAllowedForPurposesLI(id, purposeIds...) +} + +// Returns a list of publisher restrictions applied to purpose id +func (t *TCData) GetPubRestrictionsForPurpose(id int) []*PubRestriction { + return t.CoreString.GetPubRestrictionsForPurpose(id) +} + // Returns structure as a base64 raw url encoded string func (t *TCData) ToTCString() string { var segments []string