Files
threshold/internal/iam/user.go
T

105 lines
2.8 KiB
Go
Raw Normal View History

2020-06-04 20:05:08 -04:00
package iam
import (
"context"
"fmt"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/db"
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-04 20:05:08 -04:00
"google.golang.org/protobuf/proto"
)
const (
defaultUserTableName = "iam_user"
)
2020-08-10 17:15:02 -04:00
// User defines boundary users which are scoped to an Org
2020-06-04 20:05:08 -04:00
type User struct {
*store.User
tableName string `gorm:"-"`
}
2020-08-07 12:55:02 -04:00
// ensure that User implements the interfaces of: Resource, Cloneable and db.VetForWriter
2020-06-04 20:05:08 -04:00
var _ Resource = (*User)(nil)
2020-08-07 12:55:02 -04:00
var _ Cloneable = (*User)(nil)
2020-06-04 20:05:08 -04:00
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
2020-07-10 15:46:26 -04:00
func NewUser(scopeId string, opt ...Option) (*User, error) {
2020-06-04 20:05:08 -04:00
opts := getOpts(opt...)
2020-07-10 15:46:26 -04:00
if scopeId == "" {
2020-11-05 16:54:03 -05:00
return nil, fmt.Errorf("new user: missing scope id %w", errors.ErrInvalidParameter)
2020-06-04 20:05:08 -04:00
}
u := &User{
User: &store.User{
Name: opts.withName,
Description: opts.withDescription,
2020-07-10 15:46:26 -04:00
ScopeId: scopeId,
2020-06-04 20:05:08 -04:00
},
}
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 == "" {
2020-11-05 16:54:03 -05:00
return fmt.Errorf("user vet for write: missing public id: %w", errors.ErrInvalidParameter)
2020-06-04 20:05:08 -04:00
}
if err := validateScopeForWrite(ctx, r, u, opType, opt...); err != nil {
return err
}
return nil
}
func (u *User) validScopeTypes() []scope.Type {
return []scope.Type{scope.Global, scope.Org}
2020-06-04 20:05:08 -04:00
}
// 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 }
2020-06-04 20:05:08 -04:00
// Actions returns the available actions for Users
func (*User) Actions() map[string]action.Type {
2020-06-04 20:05:08 -04:00
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 defaultUserTableName
2020-06-04 20:05:08 -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-04 20:05:08 -04:00
func (u *User) SetTableName(n string) {
2020-07-23 17:58:41 -04:00
u.tableName = n
2020-06-04 20:05:08 -04:00
}