user

package
v0.0.0-...-118fa08 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AdminRole  = ":admin:"
	NormalRole = ":normal:"
	GuestRole  = ":guest:"
)

User Roles

View Source
const CollectionName = "users"

CollectionName is the mongodb collection name

View Source
const ContextKey = ":user:key:context"

ContextKey is the user context key

View Source
const SessionKey = ":user:key:session"

SessionKey is the user session key

Variables

View Source
var (
	ErrCreateUser   = errors.New("can't create user")
	ErrGetUser      = errors.New("can't retrieve user")
	ErrListUsers    = errors.New("can't retrieve users")
	ErrUpdateUser   = errors.New("can't update user")
	ErrDeleteUser   = errors.New("can't delete user")
	ErrUserNotFound = errors.New("user not found")
)

Repository related errors

Functions

func Auth

func Auth(e *casbin.Enforcer, userSvc *Service, userRepo *Repository) gin.HandlerFunc

Auth is the auth middleware

Types

type Controller

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

Controller represents the user controller

func NewController

func NewController(userRepo *Repository) *Controller

NewController returns a new User

func (*Controller) AttachRoutes

func (c *Controller) AttachRoutes(r *gin.RouterGroup)

AttachRoutes attaches the controller's routes to gin.RouterGroup

func (*Controller) Create

func (c *Controller) Create(ctx *gin.Context)

Create creates a new user

func (*Controller) Delete

func (c *Controller) Delete(ctx *gin.Context)

Delete deletes a user

func (*Controller) Get

func (c *Controller) Get(ctx *gin.Context)

Get retrieves a single user

func (*Controller) List

func (c *Controller) List(ctx *gin.Context)

List retrieves a list of user

func (*Controller) Update

func (c *Controller) Update(ctx *gin.Context)

Update updates a user

type Model

type Model struct {
	ID       primitive.ObjectID `bson:"_id,omitempty"`
	Username string             `bson:"username,omitempty"`
	Password string             `bson:"password,omitempty"`
	Email    string             `bson:"email,omitempty"`
	Verified bool               `bson:"verified,omitempty"`
	Role     string             `bson:"role,omitempty"`
}

Model represents a single user date structure

func (*Model) SetDefaults

func (m *Model) SetDefaults()

SetDefaults sets default values

type Repository

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

Repository represents the user repository

func NewRepository

func NewRepository(db *mongo.Database, userSvc *Service) *Repository

NewRepository returns a new Repository

func (*Repository) Create

func (r *Repository) Create(ctx context.Context, user *Model) (*Model, error)

Create creates a new user

func (*Repository) Delete

func (r *Repository) Delete(ctx context.Context, user *Model) (*Model, error)

Delete deletes a user

func (*Repository) Get

func (r *Repository) Get(ctx context.Context, user *Model) (*Model, error)

Get retrieves a single user

func (*Repository) List

func (r *Repository) List(ctx context.Context, filter interface{}) ([]Model, error)

List retrieves a list of user

func (*Repository) Update

func (r *Repository) Update(ctx context.Context, user *Model) (*Model, error)

Update updates a user

type Service

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

Service represents a user service

func NewService

func NewService(passwordHash string) *Service

NewService returns a new user service

func (*Service) ComparePassword

func (s *Service) ComparePassword(hashedPassword, password []byte) error

ComparePassword is a wrapper over bcrypt.CompareHashAndPassword

func (*Service) DeleteUserIDSessionContext

func (s *Service) DeleteUserIDSessionContext(ctx *gin.Context) error

DeleteUserIDSessionContext sets user ID in gin.Context

func (*Service) HashPassword

func (s *Service) HashPassword(password []byte) ([]byte, error)

HashPassword is a wrapper over bcrypt.GenerateFromPassword

func (*Service) IsPasswordHashed

func (s *Service) IsPasswordHashed(hashedPassword []byte) bool

IsPasswordHashed checks if password is hashed

func (*Service) SetUserIDSessionContext

func (s *Service) SetUserIDSessionContext(ctx *gin.Context, userID string) error

SetUserIDSessionContext sets user ID in gin.Context

func (*Service) UserFromContext

func (s *Service) UserFromContext(ctx *gin.Context) (*Model, bool)

UserFromContext returns the user entity from gin.Context

func (*Service) UserIDFromSessionContext

func (s *Service) UserIDFromSessionContext(ctx *gin.Context) (primitive.ObjectID, error)

UserIDFromSessionContext returns the user ID from gin.Context

Jump to

Keyboard shortcuts

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