models

package
v0.0.0-...-2e80e89 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseModel

type BaseModel struct {
	// Default values for PostgreSQL, change it for other DBMS
	ID          uuid.UUID  `gorm:"primary_key;type:uuid;default:uuid_generate_v4()"`
	CreatedByID *uuid.UUID `gorm:"type:uuid"`
	UpdatedByID *uuid.UUID `gorm:"type:uuid"`
	CreatedAt   *time.Time `gorm:"index;not null;default:current_timestamp"`
	UpdatedAt   *time.Time `gorm:"index"`
}

BaseModel defines the common columns that all db structs should hold, usually db structs based on this have no soft delete

type BaseModelSeq

type BaseModelSeq struct {
	// Default values for PostgreSQL, change it for other DBMS
	ID          int        `gorm:"primary_key,auto_increment"`
	CreatedByID *uuid.UUID `gorm:"type:uuid"`
	UpdatedByID *uuid.UUID `gorm:"type:uuid"`
	CreatedAt   *time.Time `gorm:"index;not null;default:current_timestamp"`
	UpdatedAt   *time.Time `gorm:"index"`
}

BaseModelSeq defines the common columns that all db structs should hold, with an INT key

type BaseModelSeqSoftDelete

type BaseModelSeqSoftDelete struct {
	BaseModelSeq
	DeletedByID *uuid.UUID `gorm:"type:uuid"`
	DeletedAt   *time.Time `gorm:"index"`
}

BaseModelSeqSoftDelete defines the common columns that all db structs should hold, usually. This struct also defines the fields for GORM triggers to detect the entity should soft delete

type BaseModelSoftDelete

type BaseModelSoftDelete struct {
	BaseModel
	DeletedByID *uuid.UUID `gorm:"type:uuid"`
	DeletedAt   *time.Time `gorm:"index"`
}

BaseModelSoftDelete defines the common columns that all db structs should hold, usually. This struct also defines the fields for GORM triggers to detect the entity should soft delete

type Permission

type Permission struct {
	BaseModelSeq
	Tag         string `gorm:"not null;unique_index"`
	Description string `gorm:"size:1024"`
}

Permission defines a permission scope for the user

type Role

type Role struct {
	BaseModelSeq
	Name        string       `gorm:"not null"`
	Description string       `gorm:"size:1024"`
	ParentRoles []Role       `gorm:"many2many:role_parents;association_jointable_foreignkey:parent_role_id"`
	ChildRoles  []Role       `gorm:"many2many:role_parents;association_jointable_foreignkey:role_id"`
	Permissions []Permission `gorm:"many2many:role_permissions;association_autoupdate:false;association_autocreate:false"`
}

Role defines a role for the user

type User

type User struct {
	BaseModelSoftDelete        // We don't to actually delete the users, audit
	Email               string `gorm:"not null;index"`
	Password            string
	Name                *string `gorm:"null"`
	NickName            *string
	FirstName           *string
	LastName            *string
	Location            *string
	AvatarURL           *string       `gorm:"size:1024"`
	Description         *string       `gorm:"size:1024"`
	UserProfiles        []UserProfile `gorm:"association_autocreate:false;association_autoupdate:false"`
	Roles               []Role        `gorm:"many2many:user_roles;association_autocreate:false;association_autoupdate:false"`
	Permissions         []Permission  `gorm:"many2many:user_permissions;association_autocreate:false;association_autoupdate:false"`
	CreatedBy           *User         `gorm:"association_autoupdate:false;association_autocreate:false"`
	UpdatedBy           *User         `gorm:"association_autoupdate:false;association_autocreate:false"`
}

User defines a user for the app

func (*User) AfterSave

func (u *User) AfterSave(scope *gorm.Scope) error

AfterSave hook for User

func (*User) BeforeSave

func (u *User) BeforeSave(scope *gorm.Scope) error

BeforeSave hook for User

func (*User) GetDisplayName

func (u *User) GetDisplayName() string

GetDisplayName returns the displayName if not nil, or the first + last name

func (*User) HasPermission

func (u *User) HasPermission(permission string, entity string) (bool, error)

HasPermission verifies if user has a specific permission

func (*User) HasPermissionBool

func (u *User) HasPermissionBool(permission string, entity string) bool

HasPermissionBool verifies if user has a specific permission - returns t/f

func (*User) HasPermissionTag

func (u *User) HasPermissionTag(tag string) (bool, error)

HasPermissionTag verifies if user has a specific permission tag

func (*User) HasRole

func (u *User) HasRole(roleID int) (bool, error)

HasRole verifies if user possesses a role

type UserAPIKey

type UserAPIKey struct {
	BaseModelSeq
	Name        string
	User        User         `gorm:"association_autocreate:false;association_autoupdate:false"`
	UserID      uuid.UUID    `gorm:"not null;index"`
	APIKey      string       `gorm:"size:128;unique_index"`
	Permissions []Permission `gorm:"many2many:user_api_key_permissions;association_autocreate:false;association_autoupdate:false"`
}

UserAPIKey generated api keys for the users

func (*UserAPIKey) BeforeSave

func (k *UserAPIKey) BeforeSave(scope *gorm.Scope) error

BeforeSave hook for UserAPIKey

type UserPermission

type UserPermission struct {
	UserID       uuid.UUID `gorm:"index"`
	PermissionID int       `gorm:"index"`
}

UserPermission relation between an user and its permissions

type UserProfile

type UserProfile struct {
	BaseModelSeq
	Email          string    `gorm:"unique_index:idx_email_provider_external_user_id"`
	UserID         uuid.UUID `gorm:"not null;index"`
	User           User      `gorm:"association_autocreate:false;association_autoupdate:false"`
	Provider       string    `gorm:"not null;index;unique_index:idx_email_provider_external_user_id;default:'DB'"` // DB means database or no ExternalUserID
	ExternalUserID string    `gorm:"not null;index;unique_index:idx_email_provider_external_user_id"`              // User ID
	Name           string
	NickName       string
	FirstName      string
	LastName       string
	Location       string `gorm:"size:512"`
	AvatarURL      string `gorm:"size:1024"`
	Description    string `gorm:"size:1024"`
	CreatedBy      *User  `gorm:"association_autoupdate:false;association_autocreate:false"`
	UpdatedBy      *User  `gorm:"association_autoupdate:false;association_autocreate:false"`
}

UserProfile saves all the related OAuth Profiles

type UserRole

type UserRole struct {
	UserID uuid.UUID `gorm:"index"`
	RoleID int       `gorm:"index"`
}

UserRole relation between an user and its roles

func (*UserRole) AfterSave

func (ur *UserRole) AfterSave(scope *gorm.Scope) error

AfterSave hook (assigning roles, fill all permissions for example)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL