admin

package
v0.0.0-...-26742bf Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2021 License: MIT Imports: 18 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AdminUserMigration = &gormigrate.Migration{
	ID: "init_admin",
	Migrate: func(tx *gorm.DB) error {
		var err error

		type adminUser struct {
			gorm.Model
			Email     string `gorm:"not null;unique"`
			FirstName string
			LastName  string
			Password  []byte
			LastLogin *time.Time
		}

		if err = tx.CreateTable(&adminUser{}).Error; err != nil {
			return err
		}
		var pwd []byte
		if pwd, err = bcrypt.GenerateFromPassword([]byte("changeme"), bcrypt.DefaultCost); err != nil {
			return err
		}
		usr := adminUser{
			Email:    "you@yourcompany.com",
			Password: pwd,
		}
		return tx.Save(&usr).Error
	},
	Rollback: func(tx *gorm.DB) error {
		return tx.DropTable("admin_users").Error
	},
}

AdminUserMigration is the migration that creates our user model

Functions

This section is empty.

Types

type Admin

type Admin struct {
	// contains filtered or unexported fields
}

Admin abstracts the whole QOR Admin + authentication process

func New

func New(db *gorm.DB, prefix, cookiesecret string) *Admin

New will create a new admin using the provided gorm connection, a prefix for the various routes. Prefix can be an empty string. The cookie secret will be used to encrypt/decrypt the cookie on the backend side.

func (Admin) Bind

func (a Admin) Bind(r *gin.Engine)

Bind will bind the admin interface to an already existing gin router (*gin.Engine).

type User

type User struct {
	gorm.Model
	Email     string `gorm:"not null;unique"`
	FirstName string
	LastName  string
	Password  []byte
	LastLogin *time.Time
}

User defines how an admin user is represented in database

func (User) CheckPassword

func (u User) CheckPassword(raw string) bool

CheckPassword is a simple utility function to check the password given as raw against the user's hashed password

func (User) DisplayName

func (u User) DisplayName() string

DisplayName satisfies the interface for Qor Admin

func (*User) HashPassword

func (u *User) HashPassword() error

HashPassword is a simple utility function to hash the password sent via API before inserting it in database

func (User) TableName

func (u User) TableName() string

TableName allows to override the name of the table

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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