Documentation ¶
Index ¶
- Constants
- func BoolFiltering(filterVal bool, filedNames ...string) func(*gorm.DB) *gorm.DB
- func FixWhereClausesForStatementModifier(stmt *gorm.Statement)
- func SkipBoolFilter(filedNames ...string) func(*gorm.DB) *gorm.DB
- func SkipTenancyCheck() func(*gorm.DB) *gorm.DB
- func TenancyCheck(flags ...TenancyCheckFlag) func(*gorm.DB) *gorm.DB
- type Audit
- type FilterBool
- type NoopStatementModifier
- type SoftDelete
- type Tenancy
- type TenancyCheckFlag
- type TenantPath
- func (t TenantPath) DeleteClauses(f *schema.Field) []clause.Interface
- func (t TenantPath) GormDataType() string
- func (t TenantPath) QueryClauses(f *schema.Field) []clause.Interface
- func (t *TenantPath) Scan(src interface{}) error
- func (t TenantPath) UpdateClauses(f *schema.Field) []clause.Interface
- func (t TenantPath) Value() (driver.Value, error)
Constants ¶
const (
TagFilter = "filter"
)
Variables ¶
This section is empty.
Functions ¶
func BoolFiltering ¶
BoolFiltering is a gorm scope that change default/tag behavior of FilterBool field filtering with given field names e.g. db.WithContext(ctx).Scopes(BoolFiltering(false, "FieldName1", "FieldName2")).Find(...)
would filter out any model with "FieldName1" or "FieldName2" equals to "false"
To override all FilterBool filtering, provide no params or "*" e.g. db.WithContext(ctx).Scopes(BoolFiltering()).Find(...)
Note using this scope without context would panic
func FixWhereClausesForStatementModifier ¶
FixWhereClausesForStatementModifier applies special fix for db.Model(&model{}).Where(&model{f1:v1}).Or(&model{f2:v2})... Ref: https://github.com/go-gorm/gorm/issues/3627
https://github.com/go-gorm/gorm/commit/9b2181199d88ed6f74650d73fa9d20264dd134c0#diff-e3e9193af67f3a706b3fe042a9f121d3609721da110f6a585cdb1d1660fd5a3c
Important: utility function for go-lanai internal use
func SkipBoolFilter ¶
SkipBoolFilter is a gorm scope that can be used to skip filtering of FilterBool fields with given field names e.g. db.WithContext(ctx).Scopes(SkipBoolFilter("FieldName1", "FieldName2")).Find(...)
To disable all FilterBool filtering, provide no params or "*" e.g. db.WithContext(ctx).Scopes(SkipBoolFilter()).Find(...)
Note using this scope without context would panic
func SkipTenancyCheck ¶
SkipTenancyCheck is used as a scope for gorm.DB to skip tenancy check e.g. db.WithContext(ctx).Scopes(SkipTenancyCheck()).Find(...) Note using this scope without context would panic
func TenancyCheck ¶
func TenancyCheck(flags ...TenancyCheckFlag) func(*gorm.DB) *gorm.DB
TenancyCheck is used as a scope for gorm.DB to override tenancy check e.g. db.WithContext(ctx).Scopes(TenancyCheck()).Find(...) Note using this scope without context would panic
Types ¶
type FilterBool ¶
type FilterBool bool
FilterBool implements - schema.GormDataTypeInterface - schema.QueryClausesInterface this data type adds "WHERE" clause in SELECT statements for filtering out models based on this field's value
FilterBool by default filter out true values (WHERE filter_bool_col IS NOT TRUE AND ....). this behavior can be changed to using tag `filter:"<-|true|false>"`
- `filter:"-"`: disables the filtering at model declaration level. Can be enabled on per query basis using scopes or repo options (if applicable)
- `filter:"true"`: filter out "true" values, the default behavior Can be overridden on per query basis using scopes or repo options (if applicable)
- `filter:"false"`: filter out "false" values Can be overridden on per query basis using scopes or repo options (if applicable)
See SkipBoolFilter and BoolFiltering for filtering behaviour overriding
func (FilterBool) GormDataType ¶
func (t FilterBool) GormDataType() string
func (FilterBool) QueryClauses ¶
func (t FilterBool) QueryClauses(f *schema.Field) []clause.Interface
QueryClauses implements schema.QueryClausesInterface,
func (*FilterBool) Scan ¶
func (t *FilterBool) Scan(src interface{}) error
Scan implements sql.Scanner
type NoopStatementModifier ¶
type NoopStatementModifier struct{}
NoopStatementModifier used to be embedded of any StatementModifier implementation. This type implement dummy clause.Interface methods
func (NoopStatementModifier) Build ¶
func (sm NoopStatementModifier) Build(clause.Builder)
func (NoopStatementModifier) MergeClause ¶
func (sm NoopStatementModifier) MergeClause(*clause.Clause)
func (NoopStatementModifier) Name ¶
func (sm NoopStatementModifier) Name() string
type SoftDelete ¶
type Tenancy ¶
type Tenancy struct { TenantID uuid.UUID `gorm:"type:KeyID;not null"` TenantPath TenantPath `gorm:"type:uuid[];index:,type:gin;not null" json:"-"` }
Tenancy is an embedded type for data model. It's responsible for populating TenantPath and check for Tenancy related data when crating/updating. Tenancy implements - callbacks.BeforeCreateInterface - callbacks.BeforeUpdateInterface When used as an embedded type, tag `filter` can be used to override default tenancy check behavior:
- `filter:"w"`: create/update/delete are enforced (Default mode)
- `filter:"rw"`: CRUD operations are all enforced, this mode filters result of any Select/Update/Delete query based on current security context
- `filter:"-"`: filtering is disabled. Note: setting TenantID to in-accessible tenant is still enforced. to disable TenantID value check, use SkipTenancyCheck
e.g. <code>
type TenancyModel struct { ID uuid.UUID `gorm:"primaryKey;type:uuid;default:gen_random_uuid();"` Tenancy `filter:"rw"` }
</code>
func (*Tenancy) BeforeUpdate ¶
BeforeUpdate Check if user is allowed to update this item's tenancy to the target tenant. (i.e. if user has access to the target tenant) We don't check the original tenancy because we don't have that information in this hook. That check has to be done in application code.
func (Tenancy) SkipTenancyCheck ¶
SkipTenancyCheck is used for embedding models to override tenancy check behavior. It should be called within model's hooks. this function would panic if context is not set yet
type TenancyCheckFlag ¶
type TenancyCheckFlag uint
TenancyCheckFlag bitwise Flag of tenancy flag mode
const ( TenancyCheckFlagWriteValueCheck TenancyCheckFlag = 1 << iota TenancyCheckFlagWriteFiltering TenancyCheckFlagReadFiltering )
type TenantPath ¶
TenantPath implements - schema.GormDataTypeInterface - schema.QueryClausesInterface - schema.UpdateClausesInterface - schema.DeleteClausesInterface this data type adds "WHERE" clause for tenancy filtering
func (TenantPath) DeleteClauses ¶
func (t TenantPath) DeleteClauses(f *schema.Field) []clause.Interface
DeleteClauses implements schema.DeleteClausesInterface,
func (TenantPath) GormDataType ¶
func (t TenantPath) GormDataType() string
func (TenantPath) QueryClauses ¶
func (t TenantPath) QueryClauses(f *schema.Field) []clause.Interface
QueryClauses implements schema.QueryClausesInterface,
func (*TenantPath) Scan ¶
func (t *TenantPath) Scan(src interface{}) error
Scan implements sql.Scanner
func (TenantPath) UpdateClauses ¶
func (t TenantPath) UpdateClauses(f *schema.Field) []clause.Interface
UpdateClauses implements schema.UpdateClausesInterface,