models

package
v0.0.0-...-f03aceb Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Roles for users
	RoleAdmin = "admin"
	RoleUser  = "customer"

	// PermissionViewObjects determines if a role can view standard
	// objects such as jobs
	PermissionViewObjects = "view_objects"
	// PermissionModifyObjects determines if a role can create and modify
	// standard objects.
	PermissionModifyObjects = "modify_objects"
	// PermissionModifySystem determines if a role can manage system-level
	// configuration.
	PermissionModifySystem = "modify_system"
)
View Source
const DateTimeFormat = "2006-01-02 3:4 PM"

Variables

View Source
var ErrModifyingOnlyAdmin = errors.New("Cannot remove the only administrator")

ErrModifyingOnlyAdmin occurs when there is an attempt to modify the only user account with the Admin role in such a way that there will be no user

Functions

func DeleteJob

func DeleteJob(id uint64) error

Delete Job removes the job from table error is thrown if anything happens.

func EnsureEnoughAdmins

func EnsureEnoughAdmins() error

EnsureEnoughAdmins ensures that there is more than one user account with the Admin role. This function is meant to be called before modifying a user account with the Admin role in a non-revokable way.

func GetConfigValueByKey

func GetConfigValueByKey(keyword string) (string, error)

GetConfigValueByKey returns the value of the given key

func GetNumberOfUsers

func GetNumberOfUsers() (int64, error)

GetNumberOfRows returns the number of rows in the specified table.

func HasAnyUsers

func HasAnyUsers() (bool, error)

IsUserExists checks if a user exists

func SaveAppConfig

func SaveAppConfig(a *AppConfig) error

PutUser updates the given user

func SaveJob

func SaveJob(u *Job) error

SaveJob saves the job to the database

func SaveUser

func SaveUser(u *User) error

PutUser updates the given user

func SetupDB

func SetupDB(d *gorm.DB)

func UpdateUserPassword

func UpdateUserPassword(password string, u *User) error

Types

type AppConfig

type AppConfig struct {
	ID      int64  `json:"id"`
	Keyword string `json:"keyword"`
	Value   string `json:"value"`
}

func (AppConfig) TableName

func (AppConfig) TableName() string

TableName overrides the table name used by User to `profiles`

type Job

type Job struct {
	ID                  uint64              `gorm:"default:uuid_short()"`
	Status              jobstatus.JobStatus `json:"status" sql:"not null"`
	StatusText          string              `gorm:"-"`
	ApiURL              string              `json:"api_url" sql:"null"`
	CreatedAt           time.Time           `json:"created_at" sql:"null"`
	CompletedTime       time.Time           `json:"completed_time" sql:"null"`
	CompletedTimeString string              `gorm:"-"`
	UserID              int64               `json:"user_id" sql:"not null"`
	ScanCompleted       bool                `gorm:"-"`
}

Job represents the job model.

func GetByIDAndUser

func GetByIDAndUser(id uint64, userID int64) (Job, error)

GetByIDAndUser returns the job that the given id corresponds to. If no job is found, an error is returned.

func GetJobByID

func GetJobByID(id uint64) (Job, error)

GetJobByID returns the job that the given id corresponds to. If no job is found, an error is returned.

func GetJobs

func GetJobs(u *User) ([]Job, error)

GetJobs returns the jobs

func (*Job) AfterFind

func (j *Job) AfterFind(tx *gorm.DB) (err error)

type Permission

type Permission struct {
	ID          int64  `json:"id"`
	Keyword     string `json:"keyword"`
	Name        string `json:"name"`
	Description string `json:"description"`
}

Permission represents a permission that can be assigned to a role.

type Response

type Response struct {
	Message string      `json:"message"`
	Success bool        `json:"success"`
	Data    interface{} `json:"data"`
}

Response contains the attributes found in an API response

type Role

type Role struct {
	ID          int64        `json:"-"`
	Keyword     string       `json:"keyword"`
	Name        string       `json:"name"`
	Description string       `json:"description"`
	Permissions []Permission `json:"-" gorm:"many2many:role_permissions;"`
}

Role represents a role that can be assigned to a user.

func GetRoleByKeyword

func GetRoleByKeyword(keyword string) (Role, error)

GetRoleByKeyword returns a role that can be assigned to a user.

type Tabler

type Tabler interface {
	TableName() string
}

type User

type User struct {
	ID       int64  `json:"id"`
	Username string `json:"username" sql:"not null;unique"`
	Name     string `json:"name" sql:"not null"`
	Password string `json:"password" sql:"null"`
	Email    string `json:"email" sql:"null:unique"`
	RoleID   int64  `json:"-"`
	Role     Role   `json:"role" gorm:"association_autoupdate:false;association_autocreate:false"`
}

User represents the user model

func GetUserByAPIKey

func GetUserByAPIKey(key string) (User, error)

GetUserByAPIKey returns the user that the given API Key corresponds to. If no user is found, an error is thrown.

func GetUserByID

func GetUserByID(id int64) (User, error)

GetUser returns the user that the given id corresponds to. If no user is found, an error is thrown.

func GetUserByUsername

func GetUserByUsername(username string) (User, error)

GetUserByUsername returns the user that the given username corresponds to. If no user is found, an error is thrown.

func GetUsers

func GetUsers() ([]User, error)

GetUsers returns the users registered

func (*User) HasPermission

func (u *User) HasPermission(keyword string) (bool, error)

HasPermission determines if a role has a given permission.

Jump to

Keyboard shortcuts

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