2023-01-24 19:57:15 +00:00
|
|
|
// Copyright 2023 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
package identity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-02-25 20:40:02 +00:00
|
|
|
"sync/atomic"
|
2019-11-27 12:42:36 +00:00
|
|
|
)
|
|
|
|
|
2023-05-18 09:05:56 +00:00
|
|
|
// NewManager creates a new Manager starting at id.
|
2019-11-27 12:42:36 +00:00
|
|
|
func NewManager(id Provider) Manager {
|
|
|
|
return &identityManager{
|
|
|
|
Provider: id,
|
|
|
|
ids: Identities{id.GetIdentity(): id},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPathIdentity creates a new Identity with the two identifiers
|
|
|
|
// type and path.
|
|
|
|
func NewPathIdentity(typ, pat string) PathIdentity {
|
|
|
|
pat = strings.ToLower(strings.TrimPrefix(filepath.ToSlash(pat), "/"))
|
|
|
|
return PathIdentity{Type: typ, Path: pat}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Identities stores identity providers.
|
|
|
|
type Identities map[Identity]Provider
|
|
|
|
|
2020-02-29 11:05:06 +00:00
|
|
|
func (ids Identities) search(depth int, id Identity) Provider {
|
|
|
|
if v, found := ids[id.GetIdentity()]; found {
|
2019-11-27 12:42:36 +00:00
|
|
|
return v
|
|
|
|
}
|
2020-02-29 11:05:06 +00:00
|
|
|
|
|
|
|
depth++
|
|
|
|
|
|
|
|
// There may be infinite recursion in templates.
|
|
|
|
if depth > 100 {
|
|
|
|
// Bail out.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
for _, v := range ids {
|
|
|
|
switch t := v.(type) {
|
|
|
|
case IdentitiesProvider:
|
2020-02-29 11:05:06 +00:00
|
|
|
if nested := t.GetIdentities().search(depth, id); nested != nil {
|
2019-11-27 12:42:36 +00:00
|
|
|
return nested
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdentitiesProvider provides all Identities.
|
|
|
|
type IdentitiesProvider interface {
|
|
|
|
GetIdentities() Identities
|
|
|
|
}
|
|
|
|
|
|
|
|
// Identity represents an thing that can provide an identify. This can be
|
|
|
|
// any Go type, but the Identity returned by GetIdentify must be hashable.
|
|
|
|
type Identity interface {
|
|
|
|
Provider
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manager manages identities, and is itself a Provider of Identity.
|
|
|
|
type Manager interface {
|
2020-11-26 07:32:49 +00:00
|
|
|
SearchProvider
|
2019-11-27 12:42:36 +00:00
|
|
|
Add(ids ...Provider)
|
|
|
|
Reset()
|
|
|
|
}
|
|
|
|
|
2020-11-26 07:32:49 +00:00
|
|
|
// SearchProvider provides access to the chained set of identities.
|
|
|
|
type SearchProvider interface {
|
|
|
|
Provider
|
|
|
|
IdentitiesProvider
|
|
|
|
Search(id Identity) Provider
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
// A PathIdentity is a common identity identified by a type and a path, e.g. "layouts" and "_default/single.html".
|
|
|
|
type PathIdentity struct {
|
|
|
|
Type string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIdentity returns itself.
|
|
|
|
func (id PathIdentity) GetIdentity() Identity {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the Path.
|
|
|
|
func (id PathIdentity) Name() string {
|
|
|
|
return id.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
// A KeyValueIdentity a general purpose identity.
|
|
|
|
type KeyValueIdentity struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIdentity returns itself.
|
|
|
|
func (id KeyValueIdentity) GetIdentity() Identity {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the Key.
|
|
|
|
func (id KeyValueIdentity) Name() string {
|
|
|
|
return id.Key
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:57:15 +00:00
|
|
|
// Provider provides the comparable Identity.
|
2019-11-27 12:42:36 +00:00
|
|
|
type Provider interface {
|
2022-04-21 08:59:13 +00:00
|
|
|
// GetIdentity is for internal use.
|
2019-11-27 12:42:36 +00:00
|
|
|
GetIdentity() Identity
|
|
|
|
}
|
|
|
|
|
|
|
|
type identityManager struct {
|
|
|
|
sync.Mutex
|
|
|
|
Provider
|
|
|
|
ids Identities
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *identityManager) Add(ids ...Provider) {
|
|
|
|
im.Lock()
|
|
|
|
for _, id := range ids {
|
|
|
|
im.ids[id.GetIdentity()] = id
|
|
|
|
}
|
|
|
|
im.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *identityManager) Reset() {
|
|
|
|
im.Lock()
|
|
|
|
id := im.GetIdentity()
|
|
|
|
im.ids = Identities{id.GetIdentity(): id}
|
|
|
|
im.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:07:10 +00:00
|
|
|
// TODO(bep) these identities are currently only read on server reloads
|
|
|
|
// so there should be no concurrency issues, but that may change.
|
2019-11-27 12:42:36 +00:00
|
|
|
func (im *identityManager) GetIdentities() Identities {
|
|
|
|
im.Lock()
|
|
|
|
defer im.Unlock()
|
|
|
|
return im.ids
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *identityManager) Search(id Identity) Provider {
|
|
|
|
im.Lock()
|
|
|
|
defer im.Unlock()
|
2020-02-29 11:05:06 +00:00
|
|
|
return im.ids.search(0, id.GetIdentity())
|
2019-11-27 12:42:36 +00:00
|
|
|
}
|
2020-02-25 20:40:02 +00:00
|
|
|
|
|
|
|
// Incrementer increments and returns the value.
|
|
|
|
// Typically used for IDs.
|
|
|
|
type Incrementer interface {
|
|
|
|
Incr() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// IncrementByOne implements Incrementer adding 1 every time Incr is called.
|
|
|
|
type IncrementByOne struct {
|
|
|
|
counter uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *IncrementByOne) Incr() int {
|
|
|
|
return int(atomic.AddUint64(&c.counter, uint64(1)))
|
|
|
|
}
|