105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
/*
|
|
* Amsterdam Web Communities System
|
|
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
// The database package contains database management and storage logic.
|
|
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
)
|
|
|
|
// User represents a user in the Amsterdam database.
|
|
type User struct {
|
|
Uid int32 `db:"uid"`
|
|
Username string `db:"username"`
|
|
Passhash string `db:"passhash"`
|
|
Tokenauth *string `db:"tokenauth"`
|
|
ContactID int32 `db:"contactid"`
|
|
IsAnon bool `db:"is_anon"`
|
|
VerifyEMail bool `db:"verify_email"`
|
|
Lockout bool `db:"lockout"`
|
|
AccessTries int16 `db:"access_tries"`
|
|
EmailConfNum int32 `db:"email_confnum"`
|
|
BaseLevel uint16 `db:"base_lvl"`
|
|
Created time.Time `db:"created"`
|
|
LastAccess *time.Time `db:"lastaccess"`
|
|
PassReminder string `db:"passreminder"`
|
|
Description *string `db:"description"`
|
|
DOB *time.Time `db:"dob"`
|
|
}
|
|
|
|
// userCache is the cache for User objects.
|
|
var userCache *lru.TwoQueueCache = nil
|
|
|
|
// anonUid is the UID of the "anonymous" user.
|
|
var anonUid int32 = -1
|
|
|
|
/* AmGetUser returns a reference to the specified user.
|
|
* Parameters:
|
|
* uid - The UID of the user.
|
|
* Returns:
|
|
* Pointer to User containing user data, or nil
|
|
* Standard Go error status
|
|
*/
|
|
func AmGetUser(uid int32) (*User, error) {
|
|
var err error = nil
|
|
if userCache == nil {
|
|
userCache, err = lru.New2Q(100)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
rc, ok := userCache.Get(uid)
|
|
if !ok {
|
|
var dbdata []User
|
|
err = amdb.Select(&dbdata, "SELECT * from users WHERE uid = ?", uid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(dbdata) > 1 {
|
|
return nil, fmt.Errorf("AmGetUser(%d): too many responses(%d)", uid, len(dbdata))
|
|
}
|
|
rc = &(dbdata[0])
|
|
userCache.Add(uid, rc)
|
|
}
|
|
return rc.(*User), err
|
|
}
|
|
|
|
/* AmGetAnonUser returns a reference to the anonymous user.
|
|
* Returns:
|
|
* Pointer to User containing anonymous user data, or nil
|
|
* Standard Go error status
|
|
*/
|
|
func AmGetAnonUser() (*User, error) {
|
|
var err error = nil
|
|
if anonUid < 0 {
|
|
var rows *sql.Rows
|
|
rows, err = amdb.Query("SELECT uid FROM users WHERE is_anon = 1")
|
|
if err == nil {
|
|
defer rows.Close()
|
|
if rows.Next() {
|
|
err = rows.Scan(&anonUid)
|
|
if err == nil && rows.Next() {
|
|
err = fmt.Errorf("should be only one anonymous user in Amsterdam database")
|
|
}
|
|
} else {
|
|
err = fmt.Errorf("no anonymous user in Amsterdam database")
|
|
}
|
|
}
|
|
}
|
|
var rc *User = nil
|
|
if err == nil {
|
|
rc, err = AmGetUser(anonUid)
|
|
}
|
|
return rc, err
|
|
}
|