Documentation ¶
Index ¶
- Constants
- Variables
- func Auth(e *casbin.Enforcer, userSvc *Service, userRepo *Repository) gin.HandlerFunc
- type Controller
- type Model
- type Repository
- func (r *Repository) Create(ctx context.Context, user *Model) (*Model, error)
- func (r *Repository) Delete(ctx context.Context, user *Model) (*Model, error)
- func (r *Repository) Get(ctx context.Context, user *Model) (*Model, error)
- func (r *Repository) List(ctx context.Context, filter interface{}) ([]Model, error)
- func (r *Repository) Update(ctx context.Context, user *Model) (*Model, error)
- type Service
- func (s *Service) ComparePassword(hashedPassword, password []byte) error
- func (s *Service) DeleteUserIDSessionContext(ctx *gin.Context) error
- func (s *Service) HashPassword(password []byte) ([]byte, error)
- func (s *Service) IsPasswordHashed(hashedPassword []byte) bool
- func (s *Service) SetUserIDSessionContext(ctx *gin.Context, userID string) error
- func (s *Service) UserFromContext(ctx *gin.Context) (*Model, bool)
- func (s *Service) UserIDFromSessionContext(ctx *gin.Context) (primitive.ObjectID, error)
Constants ¶
const ( AdminRole = ":admin:" NormalRole = ":normal:" GuestRole = ":guest:" )
User Roles
const CollectionName = "users"
CollectionName is the mongodb collection name
const ContextKey = ":user:key:context"
ContextKey is the user context key
const SessionKey = ":user:key:session"
SessionKey is the user session key
Variables ¶
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
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
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
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service represents a user service
func NewService ¶
NewService returns a new user service
func (*Service) ComparePassword ¶
ComparePassword is a wrapper over bcrypt.CompareHashAndPassword
func (*Service) DeleteUserIDSessionContext ¶
DeleteUserIDSessionContext sets user ID in gin.Context
func (*Service) HashPassword ¶
HashPassword is a wrapper over bcrypt.GenerateFromPassword
func (*Service) IsPasswordHashed ¶
IsPasswordHashed checks if password is hashed
func (*Service) SetUserIDSessionContext ¶
SetUserIDSessionContext sets user ID in gin.Context
func (*Service) UserFromContext ¶
UserFromContext returns the user entity from gin.Context