2017-06-13 07:12:58 +00:00
|
|
|
package main
|
|
|
|
|
2017-06-13 08:56:48 +00:00
|
|
|
import "log"
|
2017-06-13 07:12:58 +00:00
|
|
|
import "sync"
|
|
|
|
import "strings"
|
|
|
|
import "strconv"
|
|
|
|
import "database/sql"
|
2017-06-13 08:56:48 +00:00
|
|
|
import "./query_gen/lib"
|
2017-06-13 07:12:58 +00:00
|
|
|
|
|
|
|
var users UserStore
|
|
|
|
|
|
|
|
type UserStore interface {
|
|
|
|
Load(id int) error
|
|
|
|
Get(id int) (*User, error)
|
|
|
|
GetUnsafe(id int) (*User, error)
|
|
|
|
CascadeGet(id int) (*User, error)
|
|
|
|
BypassGet(id int) (*User, error)
|
|
|
|
Set(item *User) error
|
|
|
|
Add(item *User) error
|
|
|
|
AddUnsafe(item *User) error
|
|
|
|
//SetConn(conn interface{}) error
|
|
|
|
//GetConn() interface{}
|
|
|
|
Remove(id int) error
|
|
|
|
RemoveUnsafe(id int) error
|
|
|
|
GetLength() int
|
|
|
|
GetCapacity() int
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
type MemoryUserStore struct {
|
2017-06-13 07:12:58 +00:00
|
|
|
items map[int]*User
|
|
|
|
length int
|
|
|
|
capacity int
|
2017-06-13 08:56:48 +00:00
|
|
|
get *sql.Stmt
|
2017-06-13 07:12:58 +00:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func NewMemoryUserStore(capacity int) *MemoryUserStore {
|
|
|
|
stmt, err := qgen.Builder.SimpleSelect("users","name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip","uid = ?","","")
|
2017-06-13 08:56:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-06-15 11:40:35 +00:00
|
|
|
return &MemoryUserStore{
|
2017-06-13 08:56:48 +00:00
|
|
|
items:make(map[int]*User),
|
|
|
|
capacity:capacity,
|
|
|
|
get:stmt,
|
|
|
|
}
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) Get(id int) (*User, error) {
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.RLock()
|
|
|
|
item, ok := sus.items[id]
|
|
|
|
sus.RUnlock()
|
2017-06-13 07:12:58 +00:00
|
|
|
if ok {
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
return item, sql.ErrNoRows
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) GetUnsafe(id int) (*User, error) {
|
2017-06-13 08:56:48 +00:00
|
|
|
item, ok := sus.items[id]
|
2017-06-13 07:12:58 +00:00
|
|
|
if ok {
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
return item, sql.ErrNoRows
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) CascadeGet(id int) (*User, error) {
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.RLock()
|
|
|
|
user, ok := sus.items[id]
|
|
|
|
sus.RUnlock()
|
2017-06-13 07:12:58 +00:00
|
|
|
if ok {
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
user = &User{ID:id,Loggedin:true}
|
2017-06-13 08:56:48 +00:00
|
|
|
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
2017-06-13 07:12:58 +00:00
|
|
|
|
|
|
|
if user.Avatar != "" {
|
|
|
|
if user.Avatar[0] == '.' {
|
|
|
|
user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + user.Avatar
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Avatar = strings.Replace(noavatar,"{id}",strconv.Itoa(user.ID),1)
|
|
|
|
}
|
|
|
|
user.Tag = groups[user.Group].Tag
|
|
|
|
init_user_perms(user)
|
|
|
|
if err == nil {
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.Set(user)
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) BypassGet(id int) (*User, error) {
|
2017-06-13 07:12:58 +00:00
|
|
|
user := &User{ID:id,Loggedin:true}
|
2017-06-13 08:56:48 +00:00
|
|
|
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
2017-06-13 07:12:58 +00:00
|
|
|
|
|
|
|
if user.Avatar != "" {
|
|
|
|
if user.Avatar[0] == '.' {
|
|
|
|
user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + user.Avatar
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Avatar = strings.Replace(noavatar,"{id}",strconv.Itoa(user.ID),1)
|
|
|
|
}
|
|
|
|
user.Tag = groups[user.Group].Tag
|
|
|
|
init_user_perms(user)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) Load(id int) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
user := &User{ID:id,Loggedin:true}
|
2017-06-13 08:56:48 +00:00
|
|
|
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
2017-06-13 07:12:58 +00:00
|
|
|
if err != nil {
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.Remove(id)
|
2017-06-13 07:12:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Avatar != "" {
|
|
|
|
if user.Avatar[0] == '.' {
|
|
|
|
user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + user.Avatar
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Avatar = strings.Replace(noavatar,"{id}",strconv.Itoa(user.ID),1)
|
|
|
|
}
|
|
|
|
user.Tag = groups[user.Group].Tag
|
|
|
|
init_user_perms(user)
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.Set(user)
|
2017-06-13 07:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) Set(item *User) error {
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.Lock()
|
|
|
|
user, ok := sus.items[item.ID]
|
2017-06-13 07:12:58 +00:00
|
|
|
if ok {
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.Unlock()
|
2017-06-13 07:12:58 +00:00
|
|
|
*user = *item
|
2017-06-13 08:56:48 +00:00
|
|
|
} else if sus.length >= sus.capacity {
|
|
|
|
sus.Unlock()
|
2017-06-13 07:12:58 +00:00
|
|
|
return ErrStoreCapacityOverflow
|
|
|
|
} else {
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.items[item.ID] = item
|
|
|
|
sus.Unlock()
|
|
|
|
sus.length++
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) Add(item *User) error {
|
2017-06-13 08:56:48 +00:00
|
|
|
if sus.length >= sus.capacity {
|
2017-06-13 07:12:58 +00:00
|
|
|
return ErrStoreCapacityOverflow
|
|
|
|
}
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.Lock()
|
|
|
|
sus.items[item.ID] = item
|
|
|
|
sus.Unlock()
|
|
|
|
sus.length++
|
2017-06-13 07:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) AddUnsafe(item *User) error {
|
2017-06-13 08:56:48 +00:00
|
|
|
if sus.length >= sus.capacity {
|
2017-06-13 07:12:58 +00:00
|
|
|
return ErrStoreCapacityOverflow
|
|
|
|
}
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.items[item.ID] = item
|
|
|
|
sus.length++
|
2017-06-13 07:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) Remove(id int) error {
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.Lock()
|
|
|
|
delete(sus.items,id)
|
|
|
|
sus.Unlock()
|
|
|
|
sus.length--
|
2017-06-13 07:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) RemoveUnsafe(id int) error {
|
2017-06-13 08:56:48 +00:00
|
|
|
delete(sus.items,id)
|
|
|
|
sus.length--
|
2017-06-13 07:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) GetLength() int {
|
2017-06-13 08:56:48 +00:00
|
|
|
return sus.length
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) SetCapacity(capacity int) {
|
2017-06-13 08:56:48 +00:00
|
|
|
sus.capacity = capacity
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sus *MemoryUserStore) GetCapacity() int {
|
2017-06-13 08:56:48 +00:00
|
|
|
return sus.capacity
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SqlUserStore struct {
|
2017-06-14 07:09:44 +00:00
|
|
|
get *sql.Stmt
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSqlUserStore() *SqlUserStore {
|
2017-06-15 11:40:35 +00:00
|
|
|
stmt, err := qgen.Builder.SimpleSelect("users","name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip","uid = ?","","")
|
2017-06-13 08:56:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return &SqlUserStore{stmt}
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sus *SqlUserStore) Get(id int) (*User, error) {
|
|
|
|
user := User{ID:id,Loggedin:true}
|
2017-06-13 08:56:48 +00:00
|
|
|
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
2017-06-13 07:12:58 +00:00
|
|
|
|
|
|
|
if user.Avatar != "" {
|
|
|
|
if user.Avatar[0] == '.' {
|
|
|
|
user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + user.Avatar
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Avatar = strings.Replace(noavatar,"{id}",strconv.Itoa(user.ID),1)
|
|
|
|
}
|
|
|
|
user.Tag = groups[user.Group].Tag
|
|
|
|
init_user_perms(&user)
|
|
|
|
return &user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sus *SqlUserStore) GetUnsafe(id int) (*User, error) {
|
|
|
|
user := User{ID:id,Loggedin:true}
|
2017-06-13 08:56:48 +00:00
|
|
|
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
2017-06-13 07:12:58 +00:00
|
|
|
|
|
|
|
if user.Avatar != "" {
|
|
|
|
if user.Avatar[0] == '.' {
|
|
|
|
user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + user.Avatar
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Avatar = strings.Replace(noavatar,"{id}",strconv.Itoa(user.ID),1)
|
|
|
|
}
|
|
|
|
user.Tag = groups[user.Group].Tag
|
|
|
|
init_user_perms(&user)
|
|
|
|
return &user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sus *SqlUserStore) CascadeGet(id int) (*User, error) {
|
|
|
|
user := User{ID:id,Loggedin:true}
|
2017-06-13 08:56:48 +00:00
|
|
|
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
2017-06-13 07:12:58 +00:00
|
|
|
|
|
|
|
if user.Avatar != "" {
|
|
|
|
if user.Avatar[0] == '.' {
|
|
|
|
user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + user.Avatar
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Avatar = strings.Replace(noavatar,"{id}",strconv.Itoa(user.ID),1)
|
|
|
|
}
|
|
|
|
user.Tag = groups[user.Group].Tag
|
|
|
|
init_user_perms(&user)
|
|
|
|
return &user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sus *SqlUserStore) BypassGet(id int) (*User, error) {
|
|
|
|
user := User{ID:id,Loggedin:true}
|
2017-06-13 08:56:48 +00:00
|
|
|
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
2017-06-13 07:12:58 +00:00
|
|
|
|
|
|
|
if user.Avatar != "" {
|
|
|
|
if user.Avatar[0] == '.' {
|
|
|
|
user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + user.Avatar
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Avatar = strings.Replace(noavatar,"{id}",strconv.Itoa(user.ID),1)
|
|
|
|
}
|
|
|
|
user.Tag = groups[user.Group].Tag
|
|
|
|
init_user_perms(&user)
|
|
|
|
return &user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sus *SqlUserStore) Load(id int) error {
|
|
|
|
user := &User{ID:id}
|
|
|
|
// Simplify this into a quick check whether the user exists
|
2017-06-13 08:56:48 +00:00
|
|
|
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
2017-06-13 07:12:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Placeholder methods, the actual queries are done elsewhere
|
|
|
|
func (sus *SqlUserStore) Set(item *User) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sus *SqlUserStore) Add(item *User) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sus *SqlUserStore) AddUnsafe(item *User) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sus *SqlUserStore) Remove(id int) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sus *SqlUserStore) RemoveUnsafe(id int) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sus *SqlUserStore) GetCapacity() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sus *SqlUserStore) GetLength() int {
|
|
|
|
return 0 // Return the total number of users registered on the forums?
|
|
|
|
}
|