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-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" )
2018-03-17 08:16:43 +00:00
var ErrLongUsername = errors . New ( "this username is too long" )
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
2018-05-27 09:36:35 +00:00
GetOffset ( offset int , perPage int ) ( users [ ] * User , err error )
2017-09-15 22:20:01 +00:00
//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 )
2017-11-23 05:37:08 +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
GlobalCount ( ) int
2017-09-15 22:20:01 +00:00
2017-11-23 05:37:08 +00:00
SetCache ( cache UserCache )
GetCache ( ) UserCache
2017-06-13 07:12:58 +00:00
}
2017-11-23 05:37:08 +00:00
type DefaultUserStore struct {
cache UserCache
2017-09-10 16:57:22 +00:00
get * sql . Stmt
2018-05-27 09:36:35 +00:00
getOffset * 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
}
2017-11-23 05:37:08 +00:00
// NewDefaultUserStore gives you a new instance of DefaultUserStore
func NewDefaultUserStore ( cache UserCache ) ( * DefaultUserStore , error ) {
2018-08-04 11:46:36 +00:00
acc := qgen . NewAcc ( )
2017-11-23 05:37:08 +00:00
if cache == nil {
cache = NewNullUserCache ( )
}
2017-11-06 07:23:32 +00:00
// TODO: Add an admin version of registerStmt with more flexibility?
2017-11-23 05:37:08 +00:00
return & DefaultUserStore {
cache : cache ,
2018-03-31 05:25:27 +00:00
get : acc . SimpleSelect ( "users" , "name, group, active, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, liked, last_ip, temp_group" , "uid = ?" , "" , "" ) ,
2018-05-27 09:36:35 +00:00
getOffset : acc . Select ( "users" ) . Columns ( "uid, name, group, active, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, liked, last_ip, temp_group" ) . Orderby ( "uid ASC" ) . Limit ( "?,?" ) . Prepare ( ) ,
2017-11-06 07:23:32 +00:00
exists : acc . SimpleSelect ( "users" , "uid" , "uid = ?" , "" , "" ) ,
2018-02-10 15:07:21 +00:00
register : acc . SimpleInsert ( "users" , "name, email, password, salt, group, is_super_admin, session, active, message, createdAt, lastActiveAt" , "?,?,?,?,?,0,'',?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()" ) , // TODO: Implement user_count on users_groups here
2017-11-06 07:23:32 +00:00
usernameExists : acc . SimpleSelect ( "users" , "name" , "name = ?" , "" , "" ) ,
userCount : acc . SimpleCount ( "users" , "" , "" ) ,
} , acc . FirstError ( )
2017-06-13 07:12:58 +00:00
}
2017-11-23 05:37:08 +00:00
func ( mus * DefaultUserStore ) DirtyGet ( id int ) * User {
user , err := mus . cache . Get ( id )
if err == nil {
2017-11-11 04:06:16 +00:00
return user
}
2018-09-13 07:41:01 +00:00
/ * if mus . OutOfBounds ( id ) {
return BlankUser ( )
} * /
2017-11-11 04:06:16 +00:00
user = & User { ID : id , Loggedin : true }
2018-07-28 12:52:23 +00:00
err = mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . Active , & user . IsSuperAdmin , & user . Session , & user . Email , & user . RawAvatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . Liked , & user . LastIP , & user . TempGroup )
2017-11-11 04:06:16 +00:00
user . Init ( )
if err == nil {
2017-11-23 05:37:08 +00:00
mus . cache . Set ( user )
2017-11-11 04:06:16 +00:00
return user
}
return BlankUser ( )
}
2017-11-23 05:37:08 +00:00
// TODO: Log weird cache errors? Not just here but in every *Cache?
func ( mus * DefaultUserStore ) Get ( id int ) ( * User , error ) {
user , err := mus . cache . Get ( id )
if err == nil {
2017-06-13 07:12:58 +00:00
return user , nil
}
2017-09-03 04:50:31 +00:00
user = & User { ID : id , Loggedin : true }
2018-07-28 12:52:23 +00:00
err = mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . Active , & user . IsSuperAdmin , & user . Session , & user . Email , & user . RawAvatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . Liked , & 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-11-23 05:37:08 +00:00
mus . cache . Set ( user )
2017-06-13 07:12:58 +00:00
}
return user , err
}
2018-05-27 09:36:35 +00:00
// TODO: Optimise this, so we don't wind up hitting the database every-time for small gaps
// TODO: Make this a little more consistent with DefaultGroupStore's GetRange method
func ( store * DefaultUserStore ) GetOffset ( offset int , perPage int ) ( users [ ] * User , err error ) {
rows , err := store . getOffset . Query ( offset , perPage )
if err != nil {
return users , err
}
defer rows . Close ( )
for rows . Next ( ) {
user := & User { Loggedin : true }
2018-07-28 12:52:23 +00:00
err := rows . Scan ( & user . ID , & user . Name , & user . Group , & user . Active , & user . IsSuperAdmin , & user . Session , & user . Email , & user . RawAvatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . Liked , & user . LastIP , & user . TempGroup )
2018-05-27 09:36:35 +00:00
if err != nil {
return nil , err
}
user . Init ( )
store . cache . Set ( user )
users = append ( users , user )
}
return users , rows . Err ( )
}
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-11-23 05:37:08 +00:00
func ( mus * DefaultUserStore ) 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-11-23 05:37:08 +00:00
sliceList := mus . cache . BulkGet ( ids )
2018-06-06 06:13:55 +00:00
if len ( sliceList ) > 0 {
for i , sliceItem := range sliceList {
if sliceItem != nil {
list [ sliceItem . ID ] = sliceItem
} else {
stillHere = append ( stillHere , ids [ i ] )
}
2017-08-06 15:22:18 +00:00
}
2018-06-06 06:13:55 +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
2018-09-13 07:41:01 +00:00
} else if len ( ids ) == 1 {
topic , err := mus . Get ( ids [ 0 ] )
if err != nil {
return list , err
}
list [ topic . ID ] = topic
return list , nil
2017-08-06 15:22:18 +00:00
}
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
2018-08-04 11:46:36 +00:00
rows , err := qgen . NewAcc ( ) . Select ( "users" ) . Columns ( "uid, name, group, active, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, liked, last_ip, temp_group" ) . Where ( "uid IN(" + qlist + ")" ) . Query ( uidList ... )
2017-08-06 15:22:18 +00:00
if err != nil {
2018-02-15 13:15:27 +00:00
return list , err
2017-08-06 15:22:18 +00:00
}
for rows . Next ( ) {
2017-09-03 04:50:31 +00:00
user := & User { Loggedin : true }
2018-07-28 12:52:23 +00:00
err := rows . Scan ( & user . ID , & user . Name , & user . Group , & user . Active , & user . IsSuperAdmin , & user . Session , & user . Email , & user . RawAvatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . Liked , & user . LastIP , & user . TempGroup )
2017-08-06 15:22:18 +00:00
if err != nil {
2018-02-15 13:15:27 +00:00
return list , err
2017-08-06 15:22:18 +00:00
}
2017-11-02 13:35:19 +00:00
user . Init ( )
2017-11-23 05:37:08 +00:00
mus . cache . Set ( 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 == "" {
2018-02-19 04:26:01 +00:00
// TODO: Bulk log this
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-11-23 05:37:08 +00:00
func ( mus * DefaultUserStore ) BypassGet ( id int ) ( * User , error ) {
2017-09-03 04:50:31 +00:00
user := & User { ID : id , Loggedin : true }
2018-07-28 12:52:23 +00:00
err := mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . Active , & user . IsSuperAdmin , & user . Session , & user . Email , & user . RawAvatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . Liked , & 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-11-23 05:37:08 +00:00
func ( mus * DefaultUserStore ) Reload ( id int ) error {
2017-09-03 04:50:31 +00:00
user := & User { ID : id , Loggedin : true }
2018-07-28 12:52:23 +00:00
err := mus . get . QueryRow ( id ) . Scan ( & user . Name , & user . Group , & user . Active , & user . IsSuperAdmin , & user . Session , & user . Email , & user . RawAvatar , & user . Message , & user . URLPrefix , & user . URLName , & user . Level , & user . Score , & user . Liked , & user . LastIP , & user . TempGroup )
2017-06-13 07:12:58 +00:00
if err != nil {
2017-11-23 05:37:08 +00:00
mus . cache . Remove ( id )
2017-06-13 07:12:58 +00:00
return err
}
2017-11-02 13:35:19 +00:00
user . Init ( )
2017-11-23 05:37:08 +00:00
_ = mus . cache . Set ( user )
2017-06-13 07:12:58 +00:00
return nil
}
2017-11-23 05:37:08 +00:00
func ( mus * DefaultUserStore ) 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-10-16 07:32:58 +00:00
// TODO: Change active to a bool?
2018-05-28 06:27:12 +00:00
// TODO: Use unique keys for the usernames
2017-11-23 05:37:08 +00:00
func ( mus * DefaultUserStore ) Create ( username string , password string , email string , group int , active bool ) ( int , error ) {
2018-03-17 08:16:43 +00:00
// TODO: Strip spaces?
// ? This number might be a little screwy with Unicode, but it's the only consistent thing we have, as Unicode characters can be any number of bytes in theory?
if len ( username ) > Config . MaxUsernameLength {
return 0 , ErrLongUsername
}
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-11-23 05:37:08 +00:00
func ( mus * DefaultUserStore ) 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-11-23 05:37:08 +00:00
func ( mus * DefaultUserStore ) SetCache ( cache UserCache ) {
mus . cache = cache
2017-06-13 07:12:58 +00:00
}
2017-11-23 05:37:08 +00:00
// TODO: We're temporarily doing this so that you can do ucache != nil in getTopicUser. Refactor it.
func ( mus * DefaultUserStore ) GetCache ( ) UserCache {
_ , ok := mus . cache . ( * NullUserCache )
if ok {
return nil
2017-08-15 13:47:56 +00:00
}
2017-11-23 05:37:08 +00:00
return mus . cache
2017-10-21 00:27:47 +00:00
}