usermodel

package
v0.0.0-...-dd4be11 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package usermodel provides user account models and operations for the Weblens system.

Index

Constants

View Source
const PublicUserName = "PUBLIC"

PublicUserName is the username used for public access.

View Source
const UnknownUserName = "UNKNOWN"

UnknownUserName is the username used for unknown users.

View Source
const UserCollectionKey = "users"

UserCollectionKey is the MongoDB collection name for users.

Variables

View Source
var (
	// ErrUsernameTooShort is returned when the username is too short.
	ErrUsernameTooShort = wlerrors.Statusf(http.StatusBadRequest, "username is too short")
	// ErrUsernameTooLong is returned when the username exceeds the maximum length.
	ErrUsernameTooLong = wlerrors.Statusf(http.StatusBadRequest, "username is too long")
	// ErrUsernameInvalidChars is returned when the username contains invalid characters.
	ErrUsernameInvalidChars = wlerrors.Statusf(http.StatusBadRequest, "username contains invalid characters, only alphanumeric, _ and - are allowed")
	// ErrUsernameNotAllowed is returned when the username is reserved or disallowed.
	ErrUsernameNotAllowed = wlerrors.Statusf(http.StatusBadRequest, "username is not allowed")

	// ErrPasswordTooShort is returned when the password is too short.
	ErrPasswordTooShort = wlerrors.Statusf(http.StatusBadRequest, "password is too short")
	// ErrPasswordNoDigits is returned when the password contains no digits.
	ErrPasswordNoDigits = wlerrors.Statusf(http.StatusBadRequest, "password must contain at least one digit")
)
View Source
var ErrUserNotFound = wlerrors.New("user not found")

ErrUserNotFound is returned when a user cannot be found.

Functions

func DeleteAllUsers

func DeleteAllUsers(ctx context.Context) (err error)

DeleteAllUsers removes all users from the database.

func DoesUserExist

func DoesUserExist(ctx context.Context, username string) (bool, error)

DoesUserExist checks if a user with the given username exists in the database.

func SaveUser

func SaveUser(ctx context.Context, u *User) (err error)

SaveUser validates and saves a new user to the database, hashing the password before storage.

func ValidateUser

func ValidateUser(ctx context.Context, user *User) error

ValidateUser checks if the user's username and password meet the required criteria.

func WithUser

func WithUser(ctx context.Context, user *User) context.Context

WithUser attaches a user to the context.

Types

type Permissions

type Permissions int

Permissions defines the permission level for a user in the system.

const (
	UserPermissionPublic Permissions = iota
	UserPermissionBasic
	UserPermissionAdmin
	UserPermissionOwner
	UserPermissionSystem
)

User permission levels in ascending order of privilege.

type User

type User struct {
	// Database id of the user
	ID primitive.ObjectID `bson:"_id"`

	// Username is the unique identifier for the user. can only contain alphanumeric characters, underscores, and hyphens
	Username string `bson:"username"`

	// DisplayName is the name shown in the gui for the user, typically the full name of the user
	DisplayName string `bson:"fullName"`

	// Password is the bcrypt hash of the user's password
	Password string `bson:"password"`

	// The id of the user's home folder
	HomeID string `bson:"homeID"`

	// The id of the user's trash folder
	TrashID string `bson:"trashID"`

	// The id of the server instance that created this user
	CreatedBy string `bson:"createdBy"`

	// Level of user permissions: basic, admin, or owner
	UserPerms Permissions `bson:"userPerms"`

	// Is the user activated
	Activated bool `bson:"activated"`

	// Timestamp of when the user was updated last
	UpdatedAt int64 `bson:"updatedAt"`
}

User represents a user account in the Weblens system with authentication and permission information.

func FromContext

func FromContext(ctx context.Context) (*User, bool)

FromContext retrieves the user from the context if present.

func GetAllUsers

func GetAllUsers(ctx context.Context) (us []*User, err error)

GetAllUsers retrieves all users from the database.

func GetPublicUser

func GetPublicUser() *User

GetPublicUser returns the shared public user instance.

func GetServerOwner

func GetServerOwner(ctx context.Context) (u *User, err error)

GetServerOwner retrieves the user with owner permissions from the database.

func GetUnknownUser

func GetUnknownUser() *User

GetUnknownUser returns the shared unknown user instance.

func GetUserByUsername

func GetUserByUsername(ctx context.Context, username string) (u *User, err error)

GetUserByUsername retrieves a user from the database by their username.

func SearchByUsername

func SearchByUsername(ctx context.Context, partialUsername string) ([]*User, error)

SearchByUsername searches for users whose username matches the partial string.

func (*User) CheckLogin

func (u *User) CheckLogin(attempt string) bool

CheckLogin verifies a login attempt by comparing the provided password against the stored hash. Returns false if the user is not activated.

func (*User) Delete

func (u *User) Delete(ctx context.Context) (err error)

Delete removes the user from the database.

func (*User) GetDisplayName

func (u *User) GetDisplayName() string

GetDisplayName returns the user's display name (full name).

func (*User) GetUsername

func (u *User) GetUsername() string

GetUsername returns the user's unique username.

func (*User) IsActive

func (u *User) IsActive() bool

IsActive returns true if the user account is activated.

func (*User) IsAdmin

func (u *User) IsAdmin() bool

IsAdmin returns true if the user has administrator permissions or higher.

func (*User) IsOwner

func (u *User) IsOwner() bool

IsOwner returns true if the user has owner permissions or higher.

func (*User) IsPublic

func (u *User) IsPublic() bool

IsPublic returns true if the user has public (unauthenticated) permissions.

func (*User) IsSystemUser

func (u *User) IsSystemUser() bool

IsSystemUser returns true if the user is a system-level user with highest privileges.

func (*User) SetDisplayName

func (u *User) SetDisplayName(fullName string)

SetDisplayName updates the user's display name.

func (*User) SocketType

func (u *User) SocketType() websocket.ClientType

SocketType returns the websocket client type for this user.

func (*User) UpdateActivationStatus

func (u *User) UpdateActivationStatus(ctx context.Context, active bool) (err error)

UpdateActivationStatus updates the user's account activation status in the database.

func (*User) UpdateDisplayName

func (u *User) UpdateDisplayName(ctx context.Context, newName string) (err error)

UpdateDisplayName changes the user's display name in the database.

func (*User) UpdateHomeID

func (u *User) UpdateHomeID(ctx context.Context, newHomeID string) (err error)

UpdateHomeID updates the user's home directory ID in the database.

func (*User) UpdatePassword

func (u *User) UpdatePassword(ctx context.Context, newPass string) (err error)

UpdatePassword validates and updates the user's password, hashing it before storage.

func (*User) UpdatePermissionLevel

func (u *User) UpdatePermissionLevel(ctx context.Context, newPermissionLevel Permissions) (err error)

UpdatePermissionLevel changes the user's permission level in the database.

func (*User) UpdateTrashID

func (u *User) UpdateTrashID(ctx context.Context, newTrashID string) (err error)

UpdateTrashID updates the user's trash directory ID in the database.

Jump to

Keyboard shortcuts

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