2022-07-25 01:09:35 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-21 17:45:32 +00:00
|
|
|
"fmt"
|
2022-07-25 01:09:35 +00:00
|
|
|
"time"
|
|
|
|
|
2024-01-14 17:22:06 +00:00
|
|
|
"github.com/jellydator/ttlcache/v3"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2024-04-16 06:04:55 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
2022-07-25 01:09:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MembershipService is a service to check for user membership.
|
|
|
|
type MembershipService interface {
|
|
|
|
// Get returns if the user is a member of the organization.
|
2024-04-16 06:04:55 +00:00
|
|
|
Get(ctx context.Context, _forge forge.Forge, u *model.User, org string) (*model.OrgPerm, error)
|
2022-07-25 01:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type membershipCache struct {
|
2023-07-21 17:45:32 +00:00
|
|
|
cache *ttlcache.Cache[string, *model.OrgPerm]
|
2024-04-16 06:04:55 +00:00
|
|
|
store store.Store
|
2023-07-21 17:45:32 +00:00
|
|
|
ttl time.Duration
|
2022-07-25 01:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMembershipService creates a new membership service.
|
2024-04-16 06:04:55 +00:00
|
|
|
func NewMembershipService(_store store.Store) MembershipService {
|
2022-07-25 01:09:35 +00:00
|
|
|
return &membershipCache{
|
2024-04-16 06:04:55 +00:00
|
|
|
ttl: 10 * time.Minute, //nolint: gomnd
|
|
|
|
store: _store,
|
2023-07-21 17:45:32 +00:00
|
|
|
cache: ttlcache.New(ttlcache.WithDisableTouchOnHit[string, *model.OrgPerm]()),
|
2022-07-25 01:09:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns if the user is a member of the organization.
|
2024-04-16 06:04:55 +00:00
|
|
|
func (c *membershipCache) Get(ctx context.Context, _forge forge.Forge, u *model.User, org string) (*model.OrgPerm, error) {
|
2023-07-21 17:45:32 +00:00
|
|
|
key := fmt.Sprintf("%s-%s", u.ForgeRemoteID, org)
|
2023-08-10 07:17:12 +00:00
|
|
|
item := c.cache.Get(key)
|
2022-07-25 01:09:35 +00:00
|
|
|
if item != nil && !item.IsExpired() {
|
|
|
|
return item.Value(), nil
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
perm, err := _forge.OrgMembership(ctx, u, org)
|
2022-07-25 01:09:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
c.cache.Set(key, perm, c.ttl)
|
2022-07-25 01:09:35 +00:00
|
|
|
return perm, nil
|
|
|
|
}
|