101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
package iam
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/watchtower/internal/db"
|
|
"github.com/hashicorp/watchtower/internal/iam/store"
|
|
"github.com/hashicorp/watchtower/internal/types/action"
|
|
"github.com/hashicorp/watchtower/internal/types/resource"
|
|
"github.com/hashicorp/watchtower/internal/types/scope"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// User defines watchtower users which are scoped to an Organization
|
|
type User struct {
|
|
*store.User
|
|
tableName string `gorm:"-"`
|
|
}
|
|
|
|
// ensure that User implements the interfaces of: Resource, Clonable and db.VetForWriter
|
|
var _ Resource = (*User)(nil)
|
|
var _ Clonable = (*User)(nil)
|
|
var _ db.VetForWriter = (*User)(nil)
|
|
|
|
// NewUser creates a new in memory user and allows options:
|
|
// WithName - to specify the user's friendly name and WithDescription - to
|
|
// specify a user description
|
|
func NewUser(organizationPublicId string, opt ...Option) (*User, error) {
|
|
opts := getOpts(opt...)
|
|
if organizationPublicId == "" {
|
|
return nil, fmt.Errorf("new user: missing organization id %w", db.ErrInvalidParameter)
|
|
}
|
|
u := &User{
|
|
User: &store.User{
|
|
Name: opts.withName,
|
|
Description: opts.withDescription,
|
|
ScopeId: organizationPublicId,
|
|
},
|
|
}
|
|
return u, nil
|
|
}
|
|
|
|
func allocUser() User {
|
|
return User{
|
|
User: &store.User{},
|
|
}
|
|
}
|
|
|
|
// Clone creates a clone of the User
|
|
func (u *User) Clone() interface{} {
|
|
cp := proto.Clone(u.User)
|
|
return &User{
|
|
User: cp.(*store.User),
|
|
}
|
|
}
|
|
|
|
// VetForWrite implements db.VetForWrite() interface and validates the user
|
|
// before it's written.
|
|
func (u *User) VetForWrite(ctx context.Context, r db.Reader, opType db.OpType, opt ...db.Option) error {
|
|
if u.PublicId == "" {
|
|
return fmt.Errorf("user vet for write: missing public id: %w", db.ErrNilParameter)
|
|
}
|
|
if err := validateScopeForWrite(ctx, r, u, opType, opt...); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (u *User) validScopeTypes() []scope.Type {
|
|
return []scope.Type{scope.Organization}
|
|
}
|
|
|
|
// GetScope returns the scope for the User
|
|
func (u *User) GetScope(ctx context.Context, r db.Reader) (*Scope, error) {
|
|
return LookupScope(ctx, r, u)
|
|
}
|
|
|
|
// ResourceType returns the type of the User
|
|
func (*User) ResourceType() resource.Type { return resource.User }
|
|
|
|
// Actions returns the available actions for Users
|
|
func (*User) Actions() map[string]action.Type {
|
|
return CrudActions()
|
|
}
|
|
|
|
// TableName returns the tablename to override the default gorm table name
|
|
func (u *User) TableName() string {
|
|
if u.tableName != "" {
|
|
return u.tableName
|
|
}
|
|
return "iam_user"
|
|
}
|
|
|
|
// SetTableName sets the tablename and satisfies the ReplayableMessage interface
|
|
func (u *User) SetTableName(n string) {
|
|
if n != "" {
|
|
u.tableName = n
|
|
}
|
|
}
|