beginning the code for new user account

This commit is contained in:
2025-10-04 16:59:09 -06:00
parent 070afc365e
commit 8ad88c4957
6 changed files with 198 additions and 1 deletions
+25
View File
@@ -0,0 +1,25 @@
/*
* 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
/* AmIsEmailAddressBanned returns true if the given E-mail address is on the "banned" list.
* Parameters:
* address - The E-mail address to be checked.
* Returns:
* true if the address is banned, false if not.
* Standard Go error status.
*/
func AmIsEmailAddressBanned(address string) (bool, error) {
rs, err := amdb.Query("SELECT by_uid FROM emailban WHERE address = ?", address)
if err != nil {
return false, err
}
return rs.Next(), nil
}
+15
View File
@@ -16,6 +16,21 @@ type Sidebox struct {
Param *string `db:"param"`
}
// copySideboxes copies sideboxes from one user to another.
func copySideboxes(toUid int32, fromUid int32) error {
sbox := make([]Sidebox, 0, 3)
err := amdb.Select(sbox, "SELECT * from sideboxes WHERE uid = ?", fromUid)
if err == nil {
for _, sb := range sbox {
_, err := amdb.Exec("INSERT INTO sideboxes (uid, boxid, sequence, param) VALUES (?, ?, ?, ?)", toUid, sb.Boxid, sb.Sequence, sb.Param)
if err != nil {
break
}
}
}
return err
}
/* AmGetSideboxes returns all the configured sideboxes for a user.
* Parameters:
* uid = The ID of the user to retrieve sideboxes for.
+91
View File
@@ -15,6 +15,7 @@ import (
"errors"
"fmt"
"hash/crc32"
"math/rand"
"strconv"
"strings"
"sync"
@@ -46,6 +47,13 @@ type User struct {
DOB *time.Time `db:"dob"`
}
// UserProperties represents a property entry for a user.
type UserProperties struct {
Uid int32 `db:"uid"`
Index int32 `db:"ndx"`
Data *string `db:"data"`
}
// userCache is the cache for User objects.
var userCache *lru.TwoQueueCache = nil
@@ -363,3 +371,86 @@ func AmAuthenticateUserByToken(authString string, remoteIP string) (*User, error
ar = AmNewAudit(AuditLoginOK, user.Uid, remoteIP)
return user, nil
}
// newEmailConfirmationNumber returns a new E-mail confirmation number.
func newEmailConfirmationNumber() int32 {
return rand.Int31n(9000000) + 1000000
}
/* AmCreateNewUser creates a new user record in the database.
* Parameters:
* username - New user name.
* password - New password.
* reminder - Password reminder string.
* dob - User date of birth.
* remoteIP - Remote IP address for audit record.
* Returns:
* Pointer to new user record.
* Standard Go error status.
*/
func AmCreateNewUser(username string, password string, reminder string, dob *time.Time, remoteIP string) (*User, error) {
var ar *AuditRecord = nil
defer func() {
AmStoreAudit(ar)
}()
amdb.Exec("LOCK TABLES users WRITE, userprefs WRITE, propuser WRITE, commmember WRITE, sideboxes WRITE, confhotlist WRITE;")
defer amdb.Exec("UNLOCK TABLES;")
// Test if the user name is already taken.
rs, err := amdb.Query("SELECT uid FROM users WHERE username = ?", username)
if err != nil {
return nil, err
} else if rs.Next() {
log.Warnf("username \"%s\" already exists", username)
return nil, errors.New("that user name already exists. Please try again")
}
// Insert the user record.
_, err2 := amdb.Exec(`INSERT INTO users (username, passhash, verify_email, lockout, email_confnum,
base_lvl, created, lastaccess, passreminder, description, dob) VALUES (?, ?, 0, 0, ?, ?, NOW(), NOW(), ?, '', ?)`,
username, hashPassword(password), newEmailConfirmationNumber(), AmDefaultRole("Global.NewUser").Level(), reminder, *dob)
if err2 != nil {
return nil, err2
}
// Read back the user, which also puts it in the cache.
user, err3 := AmGetUserByName(username)
if err3 != nil {
return nil, err3
}
log.Debugf("...created new user \"%s\" with UID %d", username, user.Uid)
// add user preferences
_, err = amdb.Exec("INSERT INTO userprefs (uid) VALUES (?)", user.Uid)
if err != nil {
return nil, err
}
// add user properties
props := make([]UserProperties, 0)
anon, _ := getAnonUserID()
err = amdb.Select(props, "SELECT * FROM propuser WHERE uid = ?", anon)
if err != nil {
return nil, err
}
for _, p := range props {
_, err := amdb.Exec("INSTERT INTO propuser (uid, ndx, data) VALUES (?, ?, ?)", user.Uid, p.Index, p.Data)
if err != nil {
return nil, err
}
}
// add user sideboxes
err = copySideboxes(user.Uid, anon)
if err != nil {
return nil, err
}
// TODO: auto-join communities
// TODO: copy conference hotlists
// operation was a success - add an audit record
ar = AmNewAudit(AuditAccountCreated, user.Uid, remoteIP)
return user, nil
}