2017-11-10 03:33:11 +00:00
package common
2017-06-13 07:12:58 +00:00
2017-08-06 15:22:18 +00:00
import (
2017-09-03 04:50:31 +00:00
"database/sql"
2017-08-06 15:22:18 +00:00
"errors"
2017-09-03 04:50:31 +00:00
"log"
2017-08-06 15:22:18 +00:00
"strconv"
2017-09-03 04:50:31 +00:00
"sync"
2017-09-18 17:03:52 +00:00
"sync/atomic"
2017-08-06 15:22:18 +00:00
2017-11-11 04:06:16 +00:00
"../query_gen/lib"
2017-08-06 15:22:18 +00:00
"golang.org/x/crypto/bcrypt"
)
2017-06-13 07:12:58 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Add the watchdog goroutine
2017-09-15 22:20:01 +00:00
// TODO: Add some sort of update method
2017-11-11 04:06:16 +00:00
var Users UserStore
var ErrAccountExists = errors . New ( "this username is already in use" )
2017-06-13 07:12:58 +00:00
type UserStore interface {
2017-11-11 04:06:16 +00:00
DirtyGet ( id int ) * User
2017-06-13 07:12:58 +00:00
Get ( id int ) ( * User , error )
2017-09-15 22:20:01 +00:00
Exists ( id int ) bool
//BulkGet(ids []int) ([]*User, error)
BulkGetMap ( ids [ ] int ) ( map [ int ] * User , error )
2017-06-13 07:12:58 +00:00
BypassGet ( id int ) ( * User , error )
2017-10-21 00:27:47 +00:00
Create ( username string , password string , email string , group int , active bool ) ( int , error )
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
GlobalCount ( ) int
2017-09-15 22:20:01 +00:00
}
type UserCache interface {
CacheGet ( id int ) ( * User , error )
CacheGetUnsafe ( id int ) ( * User , error )
CacheSet ( item * User ) error
CacheAdd ( item * User ) error
CacheAddUnsafe ( item * User ) error
CacheRemove ( id int ) error
CacheRemoveUnsafe ( id int ) error
2017-09-18 17:03:52 +00:00
Flush ( )
2017-09-22 02:21:17 +00:00
Reload ( id int ) error
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
Length ( ) int
2017-09-15 22:20:01 +00:00
SetCapacity ( capacity int )
2017-06-13 07:12:58 +00:00
GetCapacity ( ) int
}
2017-06-15 11:40:35 +00:00
type MemoryUserStore struct {
2017-09-10 16:57:22 +00:00
items map [ int ] * User
2017-09-18 17:03:52 +00:00
length int64
2017-09-10 16:57:22 +00:00
capacity int
get * sql . Stmt
2017-09-15 22:20:01 +00:00
exists * sql . Stmt
2017-09-10 16:57:22 +00:00
register * sql . Stmt
usernameExists * sql . Stmt
userCount * sql . Stmt
2017-06-13 07:12:58 +00:00
sync . RWMutex
}
2017-09-13 15:09:13 +00:00
// NewMemoryUserStore gives you a new instance of MemoryUserStore
2017-11-02 13:35:19 +00:00
func NewMemoryUserStore ( capacity int ) ( * MemoryUserStore , error ) {
2017-11-06 07:23:32 +00:00
acc := qgen . Builder . Accumulator ( )
// TODO: Add an admin version of registerStmt with more flexibility?
2017-06-15 11:40:35 +00:00
return & MemoryUserStore {
2017-09-10 16:57:22 +00:00
items : make ( map [ int ] * User ) ,
capacity : capacity ,
2017-11-06 07:23:32 +00:00
get : acc . SimpleSelect ( "users" , "name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip, temp_group" , "uid = ?" , "" , "" ) ,
exists : acc . SimpleSelect ( "users" , "uid" , "uid = ?" , "" , "" ) ,
register : acc . SimpleInsert ( "users" , "name, email, password, salt, group, is_super_admin, session, active, message, createdAt, lastActiveAt" , "?,?,?,?,?,0,'',?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()" ) ,
usernameExists : acc . SimpleSelect ( "users" , "name" , "name = ?" , "" , "" ) ,
userCount : acc . SimpleCount ( "users" , "" , "" ) ,
} , acc . FirstError ( )
2017-06-13 07:12:58 +00:00
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) CacheGet ( id int ) ( * User , error ) {
mus . RLock ( )
item , ok := mus . items [ id ]
mus . RUnlock ( )
2017-06-13 07:12:58 +00:00
if ok {
return item , nil
}
2017-06-28 12:05:26 +00:00
return item , ErrNoRows
2017-06-13 07:12:58 +00:00
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) CacheGetUnsafe ( id int ) ( * User , error ) {
item , ok := mus . items [ id ]
2017-06-13 07:12:58 +00:00
if ok {
return item , nil
}
2017-06-28 12:05:26 +00:00
return item , ErrNoRows
2017-06-13 07:12:58 +00:00
}
2017-11-11 04:06:16 +00:00
func ( mus * MemoryUserStore ) DirtyGet ( id int ) * User {
mus . RLock ( )
user , ok := mus . items [ id ]
mus . RUnlock ( )
if ok {
return user
}
user = & User { ID : id , Loggedin : true }
err := mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . IsSuperAdmin , & user . Session , & user . Email , & user . Avatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . LastIP , & user . TempGroup )
user . Init ( )
if err == nil {
mus . CacheSet ( user )
return user
}
return BlankUser ( )
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) Get ( id int ) ( * User , error ) {
mus . RLock ( )
user , ok := mus . items [ id ]
mus . RUnlock ( )
2017-06-13 07:12:58 +00:00
if ok {
return user , nil
}
2017-09-03 04:50:31 +00:00
user = & User { ID : id , Loggedin : true }
2017-09-18 17:03:52 +00:00
err := mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . IsSuperAdmin , & user . Session , & user . Email , & user . Avatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . LastIP , & user . TempGroup )
2017-06-13 07:12:58 +00:00
2017-11-02 13:35:19 +00:00
user . Init ( )
2017-06-13 07:12:58 +00:00
if err == nil {
2017-09-18 17:03:52 +00:00
mus . CacheSet ( user )
2017-06-13 07:12:58 +00:00
}
return user , err
}
2017-08-06 15:22:18 +00:00
// WARNING: We did a little hack to make this as thin and quick as possible to reduce lock contention, use the * Cascade* methods instead for normal use
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) bulkGet ( ids [ ] int ) ( list [ ] * User ) {
2017-09-03 04:50:31 +00:00
list = make ( [ ] * User , len ( ids ) )
2017-09-18 17:03:52 +00:00
mus . RLock ( )
2017-08-06 15:22:18 +00:00
for i , id := range ids {
2017-09-18 17:03:52 +00:00
list [ i ] = mus . items [ id ]
2017-08-06 15:22:18 +00:00
}
2017-09-18 17:03:52 +00:00
mus . RUnlock ( )
2017-08-06 15:22:18 +00:00
return list
}
2017-09-10 16:57:22 +00:00
// TODO: Optimise the query to avoid preparing it on the spot? Maybe, use knowledge of the most common IN() parameter counts?
// TODO: ID of 0 should always error?
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) BulkGetMap ( ids [ ] int ) ( list map [ int ] * User , err error ) {
2017-09-10 16:57:22 +00:00
var idCount = len ( ids )
2017-08-06 15:22:18 +00:00
list = make ( map [ int ] * User )
2017-09-03 04:50:31 +00:00
if idCount == 0 {
2017-08-06 15:22:18 +00:00
return list , nil
}
2017-09-03 04:50:31 +00:00
var stillHere [ ] int
2017-09-18 17:03:52 +00:00
sliceList := mus . bulkGet ( ids )
2017-09-03 04:50:31 +00:00
for i , sliceItem := range sliceList {
if sliceItem != nil {
list [ sliceItem . ID ] = sliceItem
2017-08-06 15:22:18 +00:00
} else {
2017-09-03 04:50:31 +00:00
stillHere = append ( stillHere , ids [ i ] )
2017-08-06 15:22:18 +00:00
}
}
2017-09-03 04:50:31 +00:00
ids = stillHere
2017-08-06 15:22:18 +00:00
// If every user is in the cache, then return immediately
if len ( ids ) == 0 {
return list , nil
}
2017-09-24 00:49:41 +00:00
// TODO: Add a function for the qlist stuff
2017-08-06 15:22:18 +00:00
var qlist string
var uidList [ ] interface { }
for _ , id := range ids {
2017-09-03 04:50:31 +00:00
uidList = append ( uidList , strconv . Itoa ( id ) )
2017-08-06 15:22:18 +00:00
qlist += "?,"
}
2017-09-03 04:50:31 +00:00
qlist = qlist [ 0 : len ( qlist ) - 1 ]
2017-08-06 15:22:18 +00:00
2017-11-11 23:34:27 +00:00
acc := qgen . Builder . Accumulator ( )
rows , err := acc . Select ( "users" ) . Columns ( "uid, name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip, temp_group" ) . Where ( "uid IN(" + qlist + ")" ) . Query ( uidList ... )
2017-08-06 15:22:18 +00:00
if err != nil {
return nil , err
}
for rows . Next ( ) {
2017-09-03 04:50:31 +00:00
user := & User { Loggedin : true }
err := rows . Scan ( & user . ID , & user . Name , & user . Group , & user . IsSuperAdmin , & user . Session , & user . Email , & user . Avatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . LastIP , & user . TempGroup )
2017-08-06 15:22:18 +00:00
if err != nil {
return nil , err
}
2017-11-02 13:35:19 +00:00
user . Init ( )
2017-11-11 23:34:27 +00:00
mus . CacheSet ( user )
2017-08-06 15:22:18 +00:00
list [ user . ID ] = user
}
// Did we miss any users?
2017-09-03 04:50:31 +00:00
if idCount > len ( list ) {
var sidList string
2017-08-06 15:22:18 +00:00
for _ , id := range ids {
_ , ok := list [ id ]
if ! ok {
2017-09-03 04:50:31 +00:00
sidList += strconv . Itoa ( id ) + ","
2017-08-06 15:22:18 +00:00
}
}
// We probably don't need this, but it might be useful in case of bugs in BulkCascadeGetMap
2017-09-03 04:50:31 +00:00
if sidList == "" {
2017-11-11 04:06:16 +00:00
if Dev . DebugMode {
2017-08-13 11:22:34 +00:00
log . Print ( "This data is sampled later in the BulkCascadeGetMap function, so it might miss the cached IDs" )
2017-09-03 04:50:31 +00:00
log . Print ( "idCount" , idCount )
log . Print ( "ids" , ids )
log . Print ( "list" , list )
2017-08-06 15:22:18 +00:00
}
return list , errors . New ( "We weren't able to find a user, but we don't know which one" )
}
2017-09-03 04:50:31 +00:00
sidList = sidList [ 0 : len ( sidList ) - 1 ]
2017-08-06 15:22:18 +00:00
2017-11-11 23:34:27 +00:00
err = errors . New ( "Unable to find the users with the following IDs: " + sidList )
2017-08-06 15:22:18 +00:00
}
2017-11-11 23:34:27 +00:00
return list , err
2017-08-06 15:22:18 +00:00
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) BypassGet ( id int ) ( * User , error ) {
2017-09-03 04:50:31 +00:00
user := & User { ID : id , Loggedin : true }
2017-09-18 17:03:52 +00:00
err := mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . IsSuperAdmin , & user . Session , & user . Email , & user . Avatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . LastIP , & user . TempGroup )
2017-06-13 07:12:58 +00:00
2017-11-02 13:35:19 +00:00
user . Init ( )
2017-06-13 07:12:58 +00:00
return user , err
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) Reload ( id int ) error {
2017-09-03 04:50:31 +00:00
user := & User { ID : id , Loggedin : true }
2017-09-18 17:03:52 +00:00
err := mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . IsSuperAdmin , & user . Session , & user . Email , & user . Avatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . LastIP , & user . TempGroup )
2017-06-13 07:12:58 +00:00
if err != nil {
2017-09-18 17:03:52 +00:00
mus . CacheRemove ( id )
2017-06-13 07:12:58 +00:00
return err
}
2017-11-02 13:35:19 +00:00
user . Init ( )
2017-09-18 17:03:52 +00:00
_ = mus . CacheSet ( user )
2017-06-13 07:12:58 +00:00
return nil
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) Exists ( id int ) bool {
2017-10-16 07:32:58 +00:00
err := mus . exists . QueryRow ( id ) . Scan ( & id )
if err != nil && err != ErrNoRows {
LogError ( err )
}
return err != ErrNoRows
2017-09-15 22:20:01 +00:00
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) CacheSet ( item * User ) error {
mus . Lock ( )
user , ok := mus . items [ item . ID ]
2017-06-13 07:12:58 +00:00
if ok {
2017-09-18 17:03:52 +00:00
mus . Unlock ( )
2017-06-13 07:12:58 +00:00
* user = * item
2017-09-18 17:03:52 +00:00
} else if int ( mus . length ) >= mus . capacity {
mus . Unlock ( )
2017-06-13 07:12:58 +00:00
return ErrStoreCapacityOverflow
} else {
2017-09-18 17:03:52 +00:00
mus . items [ item . ID ] = item
mus . Unlock ( )
atomic . AddInt64 ( & mus . length , 1 )
2017-06-13 07:12:58 +00:00
}
return nil
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) CacheAdd ( item * User ) error {
if int ( mus . length ) >= mus . capacity {
2017-06-13 07:12:58 +00:00
return ErrStoreCapacityOverflow
}
2017-09-18 17:03:52 +00:00
mus . Lock ( )
mus . items [ item . ID ] = item
2017-10-16 07:32:58 +00:00
mus . length = int64 ( len ( mus . items ) )
2017-09-18 17:03:52 +00:00
mus . Unlock ( )
2017-06-13 07:12:58 +00:00
return nil
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) CacheAddUnsafe ( item * User ) error {
if int ( mus . length ) >= mus . capacity {
2017-06-13 07:12:58 +00:00
return ErrStoreCapacityOverflow
}
2017-09-18 17:03:52 +00:00
mus . items [ item . ID ] = item
2017-10-16 07:32:58 +00:00
mus . length = int64 ( len ( mus . items ) )
2017-06-13 07:12:58 +00:00
return nil
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) CacheRemove ( id int ) error {
mus . Lock ( )
2017-10-16 07:32:58 +00:00
_ , ok := mus . items [ id ]
if ! ok {
mus . Unlock ( )
return ErrNoRows
}
2017-09-18 17:03:52 +00:00
delete ( mus . items , id )
mus . Unlock ( )
atomic . AddInt64 ( & mus . length , - 1 )
2017-06-13 07:12:58 +00:00
return nil
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) CacheRemoveUnsafe ( id int ) error {
2017-10-16 07:32:58 +00:00
_ , ok := mus . items [ id ]
if ! ok {
return ErrNoRows
}
2017-09-18 17:03:52 +00:00
delete ( mus . items , id )
atomic . AddInt64 ( & mus . length , - 1 )
2017-06-13 07:12:58 +00:00
return nil
}
2017-10-16 07:32:58 +00:00
// TODO: Change active to a bool?
2017-10-21 00:27:47 +00:00
func ( mus * MemoryUserStore ) Create ( username string , password string , email string , group int , active bool ) ( int , error ) {
2017-06-25 09:56:39 +00:00
// Is this username already taken..?
2017-09-18 17:03:52 +00:00
err := mus . usernameExists . QueryRow ( username ) . Scan ( & username )
2017-06-28 12:05:26 +00:00
if err != ErrNoRows {
2017-11-11 04:06:16 +00:00
return 0 , ErrAccountExists
2017-06-25 09:56:39 +00:00
}
2017-11-11 04:06:16 +00:00
salt , err := GenerateSafeString ( SaltLength )
2017-06-25 09:56:39 +00:00
if err != nil {
return 0 , err
}
2017-09-03 04:50:31 +00:00
hashedPassword , err := bcrypt . GenerateFromPassword ( [ ] byte ( password + salt ) , bcrypt . DefaultCost )
2017-06-25 09:56:39 +00:00
if err != nil {
return 0 , err
}
2017-09-18 17:03:52 +00:00
res , err := mus . register . Exec ( username , email , string ( hashedPassword ) , salt , group , active )
2017-06-25 09:56:39 +00:00
if err != nil {
return 0 , err
}
2017-09-03 04:50:31 +00:00
lastID , err := res . LastInsertId ( )
return int ( lastID ) , err
2017-06-25 09:56:39 +00:00
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) Flush ( ) {
mus . Lock ( )
mus . items = make ( map [ int ] * User )
mus . length = 0
mus . Unlock ( )
2017-06-13 07:12:58 +00:00
}
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
// ! Is this concurrent?
// Length returns the number of users in the memory cache
func ( mus * MemoryUserStore ) Length ( ) int {
2017-09-18 17:03:52 +00:00
return int ( mus . length )
2017-06-13 07:12:58 +00:00
}
2017-09-18 17:03:52 +00:00
func ( mus * MemoryUserStore ) SetCapacity ( capacity int ) {
mus . capacity = capacity
}
func ( mus * MemoryUserStore ) GetCapacity ( ) int {
return mus . capacity
2017-06-13 07:12:58 +00:00
}
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
// GlobalCount returns the total number of users registered on the forums
2017-10-21 00:27:47 +00:00
func ( mus * MemoryUserStore ) GlobalCount ( ) ( ucount int ) {
2017-09-18 17:03:52 +00:00
err := mus . userCount . QueryRow ( ) . Scan ( & ucount )
2017-08-15 13:47:56 +00:00
if err != nil {
LogError ( err )
}
return ucount
}
2017-09-10 16:57:22 +00:00
type SQLUserStore struct {
2017-09-03 04:50:31 +00:00
get * sql . Stmt
2017-09-15 22:20:01 +00:00
exists * sql . Stmt
2017-09-03 04:50:31 +00:00
register * sql . Stmt
usernameExists * sql . Stmt
userCount * sql . Stmt
2017-06-13 07:12:58 +00:00
}
2017-11-02 13:35:19 +00:00
func NewSQLUserStore ( ) ( * SQLUserStore , error ) {
2017-11-06 07:23:32 +00:00
acc := qgen . Builder . Accumulator ( )
// TODO: Add an admin version of registerStmt with more flexibility?
2017-09-10 16:57:22 +00:00
return & SQLUserStore {
2017-11-06 07:23:32 +00:00
get : acc . SimpleSelect ( "users" , "name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip, temp_group" , "uid = ?" , "" , "" ) ,
exists : acc . SimpleSelect ( "users" , "uid" , "uid = ?" , "" , "" ) ,
register : acc . SimpleInsert ( "users" , "name, email, password, salt, group, is_super_admin, session, active, message, createdAt, lastActiveAt" , "?,?,?,?,?,0,'',?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()" ) ,
usernameExists : acc . SimpleSelect ( "users" , "name" , "name = ?" , "" , "" ) ,
userCount : acc . SimpleCount ( "users" , "" , "" ) ,
} , acc . FirstError ( )
2017-06-13 07:12:58 +00:00
}
2017-11-11 04:06:16 +00:00
func ( mus * SQLUserStore ) DirtyGet ( id int ) * User {
user := & User { ID : id , Loggedin : true }
err := mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . IsSuperAdmin , & user . Session , & user . Email , & user . Avatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . LastIP , & user . TempGroup )
user . Init ( )
if err != nil {
return BlankUser ( )
}
return user
}
2017-09-18 17:03:52 +00:00
func ( mus * SQLUserStore ) Get ( id int ) ( * User , error ) {
2017-09-22 02:21:17 +00:00
user := & User { ID : id , Loggedin : true }
2017-09-18 17:03:52 +00:00
err := mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . IsSuperAdmin , & user . Session , & user . Email , & user . Avatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . LastIP , & user . TempGroup )
2017-06-13 07:12:58 +00:00
2017-11-02 13:35:19 +00:00
user . Init ( )
2017-09-22 02:21:17 +00:00
return user , err
2017-06-13 07:12:58 +00:00
}
2017-09-10 16:57:22 +00:00
// TODO: Optimise the query to avoid preparing it on the spot? Maybe, use knowledge of the most common IN() parameter counts?
2017-09-18 17:03:52 +00:00
func ( mus * SQLUserStore ) BulkGetMap ( ids [ ] int ) ( list map [ int ] * User , err error ) {
2017-08-06 15:22:18 +00:00
var qlist string
var uidList [ ] interface { }
for _ , id := range ids {
2017-09-03 04:50:31 +00:00
uidList = append ( uidList , strconv . Itoa ( id ) )
2017-08-06 15:22:18 +00:00
qlist += "?,"
}
2017-09-03 04:50:31 +00:00
qlist = qlist [ 0 : len ( qlist ) - 1 ]
2017-08-06 15:22:18 +00:00
2017-11-11 23:34:27 +00:00
acc := qgen . Builder . Accumulator ( )
rows , err := acc . Select ( "users" ) . Columns ( "uid, name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip, temp_group" ) . Where ( "uid IN(" + qlist + ")" ) . Query ( uidList ... )
2017-08-06 15:22:18 +00:00
if err != nil {
return nil , err
}
list = make ( map [ int ] * User )
for rows . Next ( ) {
2017-09-03 04:50:31 +00:00
user := & User { Loggedin : true }
err := rows . Scan ( & user . ID , & user . Name , & user . Group , & user . IsSuperAdmin , & user . Session , & user . Email , & user . Avatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . LastIP , & user . TempGroup )
2017-08-06 15:22:18 +00:00
if err != nil {
return nil , err
}
2017-11-02 13:35:19 +00:00
user . Init ( )
2017-08-06 15:22:18 +00:00
list [ user . ID ] = user
}
return list , nil
}
2017-09-18 17:03:52 +00:00
func ( mus * SQLUserStore ) BypassGet ( id int ) ( * User , error ) {
2017-09-22 02:21:17 +00:00
user := & User { ID : id , Loggedin : true }
2017-09-18 17:03:52 +00:00
err := mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . IsSuperAdmin , & user . Session , & user . Email , & user . Avatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . LastIP , & user . TempGroup )
2017-06-13 07:12:58 +00:00
2017-11-02 13:35:19 +00:00
user . Init ( )
2017-09-22 02:21:17 +00:00
return user , err
2017-09-15 22:20:01 +00:00
}
2017-09-18 17:03:52 +00:00
func ( mus * SQLUserStore ) Exists ( id int ) bool {
2017-10-16 07:32:58 +00:00
err := mus . exists . QueryRow ( id ) . Scan ( & id )
if err != nil && err != ErrNoRows {
LogError ( err )
}
return err != ErrNoRows
2017-06-13 07:12:58 +00:00
}
2017-10-21 00:27:47 +00:00
func ( mus * SQLUserStore ) Create ( username string , password string , email string , group int , active bool ) ( int , error ) {
2017-06-25 09:56:39 +00:00
// Is this username already taken..?
2017-09-18 17:03:52 +00:00
err := mus . usernameExists . QueryRow ( username ) . Scan ( & username )
2017-06-28 12:05:26 +00:00
if err != ErrNoRows {
2017-11-11 04:06:16 +00:00
return 0 , ErrAccountExists
2017-06-25 09:56:39 +00:00
}
2017-11-11 04:06:16 +00:00
salt , err := GenerateSafeString ( SaltLength )
2017-06-25 09:56:39 +00:00
if err != nil {
return 0 , err
}
2017-09-03 04:50:31 +00:00
hashedPassword , err := bcrypt . GenerateFromPassword ( [ ] byte ( password + salt ) , bcrypt . DefaultCost )
2017-06-25 09:56:39 +00:00
if err != nil {
return 0 , err
}
2017-09-18 17:03:52 +00:00
res , err := mus . register . Exec ( username , email , string ( hashedPassword ) , salt , group , active )
2017-06-25 09:56:39 +00:00
if err != nil {
return 0 , err
}
2017-09-03 04:50:31 +00:00
lastID , err := res . LastInsertId ( )
return int ( lastID ) , err
2017-06-25 09:56:39 +00:00
}
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
// GlobalCount returns the total number of users registered on the forums
2017-10-21 00:27:47 +00:00
func ( mus * SQLUserStore ) GlobalCount ( ) ( ucount int ) {
2017-09-18 17:03:52 +00:00
err := mus . userCount . QueryRow ( ) . Scan ( & ucount )
2017-08-15 13:47:56 +00:00
if err != nil {
LogError ( err )
}
return ucount
2017-06-13 07:12:58 +00:00
}
2017-10-21 00:27:47 +00:00
// TODO: MockUserStore
// NullUserStore is here for tests because Go doesn't have short-circuiting
type NullUserStore struct {
}
func ( nus * NullUserStore ) CacheGet ( _ int ) ( * User , error ) {
return nil , ErrNoRows
}
func ( nus * NullUserStore ) CacheGetUnsafe ( _ int ) ( * User , error ) {
return nil , ErrNoRows
}
func ( nus * NullUserStore ) CacheSet ( _ * User ) error {
return ErrStoreCapacityOverflow
}
func ( nus * NullUserStore ) CacheAdd ( _ * User ) error {
return ErrStoreCapacityOverflow
}
func ( nus * NullUserStore ) CacheAddUnsafe ( _ * User ) error {
return ErrStoreCapacityOverflow
}
func ( nus * NullUserStore ) CacheRemove ( _ int ) error {
return ErrNoRows
}
func ( nus * NullUserStore ) CacheRemoveUnsafe ( _ int ) error {
return ErrNoRows
}
func ( nus * NullUserStore ) Flush ( ) {
}
func ( nus * NullUserStore ) Reload ( _ int ) error {
return ErrNoRows
}
func ( nus * NullUserStore ) Length ( ) int {
return 0
}
func ( nus * NullUserStore ) SetCapacity ( _ int ) {
}
func ( nus * NullUserStore ) GetCapacity ( ) int {
return 0
}