database

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2021 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DBMux *sync.Mutex
	Db    *gorm.DB
)
View Source
var (
	BEAST_GLOBAL_DIR string = filepath.Join(os.Getenv("HOME"), ".beast")
	BEAST_DATABASE   string = "beast.db"
)

Functions

func AddNotification

func AddNotification(notification *Notification) error

Create an entry for the notification in the Notification table It returns an error if anything wrong happen during the transaction.

func BatchUpdateChallenge

func BatchUpdateChallenge(whereMap map[string]interface{}, chall Challenge) error

This function updates a challenge entry in the database, whereMap is the map which contains key value pairs of column and values to filter out the record to update. chall is the Challenge variable with the values to update with. This function returns any error that might occur while updating the challenge entry which includes the error in case the challenge already does not exist in the database.

func CheckPreviousSubmissions

func CheckPreviousSubmissions(userId uint, challId uint) (bool, error)

Check whether challenge is submitted by the user

func CreateChallengeEntry

func CreateChallengeEntry(challenge *Challenge) error

Create an entry for the challenge in the Challenge table It returns an error if anything wrong happen during the transaction.

func CreateUserEntry

func CreateUserEntry(user *User) error

Create an entry for the user in the User table It returns an error if anything wrong happen during the transaction.

func DeleteChallengeEntry

func DeleteChallengeEntry(challenge *Challenge) error

func DeleteNotification

func DeleteNotification(notification *Notification) error

Remove entry for the notification in the Notification table

func DeleteRelatedPorts

func DeleteRelatedPorts(portList []Port) error

func GetUserRank

func GetUserRank(userID uint, userScore uint, updatedAt time.Time) (rank int64, error error)

func QueryOrCreateTagEntry

func QueryOrCreateTagEntry(tag *Tag) error

Queries or Create if not Exist

func SaveFlagSubmission

func SaveFlagSubmission(user_challenges *UserChallenges) error

func SaveTransaction

func SaveTransaction(transaction *Transaction) error

func UpdateChallenge

func UpdateChallenge(chall *Challenge, m map[string]interface{}) error

Update an entry for the challenge in the Challenge table

func UpdateNotification

func UpdateNotification(notify *Notification, m map[string]interface{}) error

Update an entry for the notification in the Notification table

func UpdateUser

func UpdateUser(user *User, m map[string]interface{}) error

Update an entry for the user in the User table

Types

type AuthorizedKeyTemplate

type AuthorizedKeyTemplate struct {
	UserID  string
	Command string
	PubKey  string
}

type Challenge

type Challenge struct {
	gorm.Model

	Name        string `gorm:"not null;type:varchar(64);unique"`
	Flag        string `gorm:"not null;type:text"`
	Type        string `gorm:"type:varchar(64)"`
	Sidecar     string `gorm:"type:varchar(64)"`
	Hints       string `gorm:"type:text"`
	Description string `gorm:"type:text"`
	Format      string `gorm:"not null"`
	ContainerId string `gorm:"size:64;unique"`
	ImageId     string `gorm:"size:64;unique"`
	Status      string `gorm:"not null;default:'Undeployed'"`
	AuthorID    uint   `gorm:"not null"`
	HealthCheck uint   `gorm:"not null;default:1"`
	Points      uint   `gorm:"default:0"`
	Ports       []Port
	Tags        []*Tag  `gorm:"many2many:tag_challenges;"`
	Users       []*User `gorm:"many2many:user_challenges;"`
}

The `challenges` table has the following columns name author format container_id image_id status

Some hooks needs to be attached to these database transaction, and on the basis of the type of the transaction that is performed on the challenge table, we need to perform some action.

Use gorm hooks for these purpose, currently the following hooks are implemented. * AfterUpdate * AfterCreate * AfterSave * AfterDelete

All these hooks are used for generating the access shell script for the challenge to the challenge author

func GetRelatedChallenges

func GetRelatedChallenges(user *User) ([]Challenge, error)

Get Related Challenges

func QueryAllChallenges

func QueryAllChallenges() ([]Challenge, error)

Query challenges table to get all the entries in the table

func QueryChallengeEntries

func QueryChallengeEntries(key string, value string) ([]Challenge, error)

Queries all the challenges entries where the column represented by key have the value in value.

func QueryChallengeEntriesMap

func QueryChallengeEntriesMap(whereMap map[string]interface{}) ([]Challenge, error)

Queries all the challenges entries where the column matches

func QueryFirstChallengeEntry

func QueryFirstChallengeEntry(key string, value string) (Challenge, error)

Using the column value in key and value in value get the first result of the query.

func QueryRelatedChallenges

func QueryRelatedChallenges(tag *Tag) ([]Challenge, error)

Query Related Challenges

func (*Challenge) AfterCreate

func (challenge *Challenge) AfterCreate(tx *gorm.DB) error

hook after create of challenge

func (*Challenge) AfterDelete

func (challenge *Challenge) AfterDelete(tx *gorm.DB) error

hook after deleting the challenge

func (*Challenge) AfterUpdate

func (challenge *Challenge) AfterUpdate(tx *gorm.DB) error

hook after update of challenge

type Notification

type Notification struct {
	gorm.Model

	Title       string `gorm:not null;unique`
	Description string `gorm:not null`
}

func QueryAllNotification

func QueryAllNotification() ([]Notification, error)

Query Notification table to get all the entries in the table

func QueryFirstNotificationEntry

func QueryFirstNotificationEntry(key string, value string) (Notification, error)

Using the column value in key and value in value get the first result of the query.

func QueryNotificationEntries

func QueryNotificationEntries(key string, value string) ([]Notification, error)

Queries all the challenges entries where the column represented by key have the value in value.

type Port

type Port struct {
	gorm.Model

	ChallengeID uint   `gorm:"not null"`
	PortNo      uint32 `gorm:"not null;unique"`
}

func GetAllocatedPorts

func GetAllocatedPorts(challenge Challenge) ([]Port, error)

func PortEntryGetOrCreate

func PortEntryGetOrCreate(port *Port) (Port, error)

Create an entry for the port in the Port table It returns an error if anything wrong happen during the transaction. If the entry already exists then it does not do anything and returns.

type ScriptFile

type ScriptFile struct {
	User       string
	Challenges map[string]string
}

type Tag

type Tag struct {
	gorm.Model

	Challenges []*Challenge `gorm:"many2many:tag_challenges;"`
	TagName    string       `gorm:"not null;unique"`
}

func GetRelatedTags

func GetRelatedTags(challenge *Challenge) ([]Tag, error)

Get Related Tags

func QueryTags

func QueryTags(whereMap map[string]interface{}) ([]*Tag, error)

Query using map

type Transaction

type Transaction struct {
	gorm.Model

	Action      string
	UserID      uint `gorm:"not null"`
	ChallengeID uint `gorm:"not null"`
}

type User

type User struct {
	gorm.Model
	auth.AuthModel

	Challenges []*Challenge `gorm:"many2many:user_challenges;"`
	Name       string       `gorm:"not null"`
	Email      string       `gorm:"non null;unique"`
	SshKey     string
	Status     uint `gorm:"not null;default:0"` // 0 for unbanned, 1 for banned
	Score      uint `gorm:"default:0"`
}

func GetRelatedUsers

func GetRelatedUsers(challenge *Challenge) ([]User, error)

Get Related Users

func QueryAllUsers

func QueryAllUsers() ([]User, error)

Query all the entries in the User table

func QueryFirstUserEntry

func QueryFirstUserEntry(key string, value string) (User, error)

Using the column value in key and value in value get the first result of the query.

func QueryUserById

func QueryUserById(authorID uint) (User, error)

func QueryUserEntries

func QueryUserEntries(key string, value string) ([]User, error)

Queries all the users entries where the column represented by key have the value in value.

func (*User) AfterCreate

func (user *User) AfterCreate(tx *gorm.DB) error

hook after create

func (*User) AfterDelete

func (user *User) AfterDelete(tx *gorm.DB) error

Updating data in same transaction

func (*User) AfterUpdate

func (user *User) AfterUpdate(tx *gorm.DB) error

hook after update

type UserChallenges

type UserChallenges struct {
	CreatedAt   time.Time
	UserID      uint
	ChallengeID uint
}

func QueryAllSubmissions

func QueryAllSubmissions() ([]UserChallenges, error)

Query challenges table to get all the entries in the table

Jump to

Keyboard shortcuts

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