Documentation
¶
Overview ¶
Package usermodel provides user account models and operations for the Weblens system.
Index ¶
- Constants
- Variables
- func DeleteAllUsers(ctx context.Context) (err error)
- func DoesUserExist(ctx context.Context, username string) (bool, error)
- func SaveUser(ctx context.Context, u *User) (err error)
- func ValidateUser(ctx context.Context, user *User) error
- func WithUser(ctx context.Context, user *User) context.Context
- type Permissions
- type User
- func FromContext(ctx context.Context) (*User, bool)
- func GetAllUsers(ctx context.Context) (us []*User, err error)
- func GetPublicUser() *User
- func GetServerOwner(ctx context.Context) (u *User, err error)
- func GetUnknownUser() *User
- func GetUserByUsername(ctx context.Context, username string) (u *User, err error)
- func SearchByUsername(ctx context.Context, partialUsername string) ([]*User, error)
- func (u *User) CheckLogin(attempt string) bool
- func (u *User) Delete(ctx context.Context) (err error)
- func (u *User) GetDisplayName() string
- func (u *User) GetUsername() string
- func (u *User) IsActive() bool
- func (u *User) IsAdmin() bool
- func (u *User) IsOwner() bool
- func (u *User) IsPublic() bool
- func (u *User) IsSystemUser() bool
- func (u *User) SetDisplayName(fullName string)
- func (u *User) SocketType() websocket.ClientType
- func (u *User) UpdateActivationStatus(ctx context.Context, active bool) (err error)
- func (u *User) UpdateDisplayName(ctx context.Context, newName string) (err error)
- func (u *User) UpdateHomeID(ctx context.Context, newHomeID string) (err error)
- func (u *User) UpdatePassword(ctx context.Context, newPass string) (err error)
- func (u *User) UpdatePermissionLevel(ctx context.Context, newPermissionLevel Permissions) (err error)
- func (u *User) UpdateTrashID(ctx context.Context, newTrashID string) (err error)
Constants ¶
const PublicUserName = "PUBLIC"
PublicUserName is the username used for public access.
const UnknownUserName = "UNKNOWN"
UnknownUserName is the username used for unknown users.
const UserCollectionKey = "users"
UserCollectionKey is the MongoDB collection name for users.
Variables ¶
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") )
var ErrUserNotFound = wlerrors.New("user not found")
ErrUserNotFound is returned when a user cannot be found.
Functions ¶
func DeleteAllUsers ¶
DeleteAllUsers removes all users from the database.
func DoesUserExist ¶
DoesUserExist checks if a user with the given username exists in the database.
func SaveUser ¶
SaveUser validates and saves a new user to the database, hashing the password before storage.
func ValidateUser ¶
ValidateUser checks if the user's username and password meet the required criteria.
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 ¶
FromContext retrieves the user from the context if present.
func GetAllUsers ¶
GetAllUsers retrieves all users from the database.
func GetPublicUser ¶
func GetPublicUser() *User
GetPublicUser returns the shared public user instance.
func GetServerOwner ¶
GetServerOwner retrieves the user with owner permissions from the database.
func GetUnknownUser ¶
func GetUnknownUser() *User
GetUnknownUser returns the shared unknown user instance.
func GetUserByUsername ¶
GetUserByUsername retrieves a user from the database by their username.
func SearchByUsername ¶
SearchByUsername searches for users whose username matches the partial string.
func (*User) CheckLogin ¶
CheckLogin verifies a login attempt by comparing the provided password against the stored hash. Returns false if the user is not activated.
func (*User) GetDisplayName ¶
GetDisplayName returns the user's display name (full name).
func (*User) GetUsername ¶
GetUsername returns the user's unique username.
func (*User) IsSystemUser ¶
IsSystemUser returns true if the user is a system-level user with highest privileges.
func (*User) SetDisplayName ¶
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 ¶
UpdateActivationStatus updates the user's account activation status in the database.
func (*User) UpdateDisplayName ¶
UpdateDisplayName changes the user's display name in the database.
func (*User) UpdateHomeID ¶
UpdateHomeID updates the user's home directory ID in the database.
func (*User) UpdatePassword ¶
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.