2020-07-11 12:00:49 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2020-08-07 12:55:02 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2020-06-11 16:36:47 -04:00
|
|
|
package iam
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
2023-12-21 12:54:27 -08:00
|
|
|
"github.com/hashicorp/boundary/internal/db/timestamp"
|
2020-11-05 16:54:03 -05:00
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/iam/store"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/types/action"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/types/resource"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/types/scope"
|
2020-06-11 16:36:47 -04:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
)
|
|
|
|
|
|
2020-07-17 16:20:56 -04:00
|
|
|
const (
|
|
|
|
|
defaultGroupTableName = "iam_group"
|
|
|
|
|
)
|
|
|
|
|
|
2020-07-11 12:00:49 -04:00
|
|
|
// Group is made up of principals which are scoped to an org.
|
2020-06-11 16:36:47 -04:00
|
|
|
type Group struct {
|
|
|
|
|
*store.Group
|
|
|
|
|
tableName string `gorm:"-"`
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 12:55:02 -04:00
|
|
|
// ensure that Group implements the interfaces of: Resource, Cloneable, and db.VetForWriter.
|
2021-01-07 10:02:32 -05:00
|
|
|
var (
|
|
|
|
|
_ Resource = (*Group)(nil)
|
|
|
|
|
_ Cloneable = (*Group)(nil)
|
|
|
|
|
_ db.VetForWriter = (*Group)(nil)
|
|
|
|
|
)
|
2020-06-11 16:36:47 -04:00
|
|
|
|
2020-07-11 12:00:49 -04:00
|
|
|
// NewGroup creates a new in memory group with a scope (project/org)
|
2024-01-24 15:43:49 -05:00
|
|
|
// and allowed options include: withDescription, WithName.
|
2023-06-06 17:09:08 -04:00
|
|
|
func NewGroup(ctx context.Context, scopeId string, opt ...Option) (*Group, error) {
|
2020-12-23 11:00:00 -08:00
|
|
|
const op = "iam.NewGroup"
|
2020-06-11 16:36:47 -04:00
|
|
|
if scopeId == "" {
|
2023-06-06 17:09:08 -04:00
|
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
|
2020-06-11 16:36:47 -04:00
|
|
|
}
|
|
|
|
|
opts := getOpts(opt...)
|
|
|
|
|
g := &Group{
|
|
|
|
|
Group: &store.Group{
|
|
|
|
|
Name: opts.withName,
|
|
|
|
|
Description: opts.withDescription,
|
|
|
|
|
ScopeId: scopeId,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return g, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clone creates a clone of the Group.
|
2022-10-25 18:29:38 -05:00
|
|
|
func (g *Group) Clone() any {
|
2020-06-11 16:36:47 -04:00
|
|
|
cp := proto.Clone(g.Group)
|
|
|
|
|
return &Group{
|
|
|
|
|
Group: cp.(*store.Group),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func allocGroup() Group {
|
|
|
|
|
return Group{
|
|
|
|
|
Group: &store.Group{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VetForWrite implements db.VetForWrite() interface and validates the group
|
|
|
|
|
// before it's written.
|
|
|
|
|
func (g *Group) VetForWrite(ctx context.Context, r db.Reader, opType db.OpType, opt ...db.Option) error {
|
2020-12-23 11:00:00 -08:00
|
|
|
const op = "iam.(Group).VetForWrite"
|
2020-06-11 16:36:47 -04:00
|
|
|
if g.PublicId == "" {
|
2021-07-29 18:52:56 -06:00
|
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing public id")
|
2020-06-11 16:36:47 -04:00
|
|
|
}
|
|
|
|
|
if err := validateScopeForWrite(ctx, r, g, opType, opt...); err != nil {
|
2021-07-29 18:52:56 -06:00
|
|
|
return errors.Wrap(ctx, err, op)
|
2020-06-11 16:36:47 -04:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 14:33:09 -05:00
|
|
|
func (g *Group) validScopeTypes() []scope.Type {
|
2020-08-12 00:02:26 -04:00
|
|
|
return []scope.Type{scope.Global, scope.Org, scope.Project}
|
2020-06-11 16:36:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetScope returns the scope for the Group.
|
|
|
|
|
func (g *Group) GetScope(ctx context.Context, r db.Reader) (*Scope, error) {
|
|
|
|
|
return LookupScope(ctx, r, g)
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 15:53:29 -07:00
|
|
|
// GetResourceType returns the type of the Group.
|
|
|
|
|
func (*Group) GetResourceType() resource.Type { return resource.Group }
|
2020-06-11 16:36:47 -04:00
|
|
|
|
|
|
|
|
// Actions returns the available actions for Group
|
2020-06-26 11:00:14 -04:00
|
|
|
func (*Group) Actions() map[string]action.Type {
|
2020-06-11 16:36:47 -04:00
|
|
|
return CrudActions()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName returns the tablename to override the default gorm table name.
|
|
|
|
|
func (g *Group) TableName() string {
|
|
|
|
|
if g.tableName != "" {
|
|
|
|
|
return g.tableName
|
|
|
|
|
}
|
2020-07-17 16:20:56 -04:00
|
|
|
return defaultGroupTableName
|
2020-06-11 16:36:47 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-17 16:20:56 -04:00
|
|
|
// SetTableName sets the tablename and satisfies the ReplayableMessage
|
|
|
|
|
// interface. If the caller attempts to set the name to "" the name will be
|
|
|
|
|
// reset to the default name.
|
2020-06-11 16:36:47 -04:00
|
|
|
func (g *Group) SetTableName(n string) {
|
2020-07-23 17:58:41 -04:00
|
|
|
g.tableName = n
|
2020-06-11 16:36:47 -04:00
|
|
|
}
|
2023-12-21 12:54:27 -08:00
|
|
|
|
|
|
|
|
type deletedGroup struct {
|
|
|
|
|
PublicId string `gorm:"primary_key"`
|
|
|
|
|
DeleteTime *timestamp.Timestamp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName returns the tablename to override the default gorm table name
|
|
|
|
|
func (u *deletedGroup) TableName() string {
|
|
|
|
|
return "iam_group_deleted"
|
|
|
|
|
}
|