2017-09-13 15:09:13 +00:00
/ *
*
* Gosora User File
* Copyright Azareal 2017 - 2018
*
* /
2016-12-02 07:38:54 +00:00
package main
2017-05-11 13:04:43 +00:00
2017-06-05 11:57:27 +00:00
import (
2017-08-27 09:33:45 +00:00
//"log"
2017-06-05 11:57:27 +00:00
//"fmt"
2017-09-03 04:50:31 +00:00
"strconv"
"time"
2017-06-28 12:05:26 +00:00
2017-06-05 11:57:27 +00:00
"golang.org/x/crypto/bcrypt"
)
2016-12-02 07:38:54 +00:00
2017-09-03 04:50:31 +00:00
var guestUser = User { ID : 0 , Link : "#" , Group : 6 , Perms : GuestPerms }
2017-07-12 11:05:18 +00:00
2017-09-03 04:50:31 +00:00
//func(real_password string, password string, salt string) (err error)
var CheckPassword = BcryptCheckPassword
//func(password string) (hashed_password string, salt string, err error)
var GeneratePassword = BcryptGeneratePassword
type User struct {
ID int
Link string
Name string
Email string
Group int
Active bool
IsMod bool
IsSuperMod bool
IsAdmin bool
IsSuperAdmin bool
IsBanned bool
Perms Perms
PluginPerms map [ string ] bool
Session string
Loggedin bool
Avatar string
Message string
URLPrefix string // Move this to another table? Create a user lite?
URLName string
Tag string
Level int
Score int
LastIP string
TempGroup int
2016-12-02 07:38:54 +00:00
}
2017-09-03 04:50:31 +00:00
type Email struct {
UserID int
Email string
2017-01-03 07:47:31 +00:00
Validated bool
2017-09-03 04:50:31 +00:00
Primary bool
Token string
2017-01-03 07:47:31 +00:00
}
2017-08-27 09:33:45 +00:00
func ( user * User ) Ban ( duration time . Duration , issuedBy int ) error {
2017-09-03 04:50:31 +00:00
return user . ScheduleGroupUpdate ( 4 , issuedBy , duration )
2017-08-27 09:33:45 +00:00
}
func ( user * User ) Unban ( ) error {
err := user . RevertGroupUpdate ( )
2017-09-22 02:21:17 +00:00
ucache , ok := users . ( UserCache )
if ok {
ucache . CacheRemove ( user . ID )
2017-08-27 09:33:45 +00:00
}
2017-09-22 02:21:17 +00:00
return err
2017-08-27 09:33:45 +00:00
}
2017-09-10 16:57:22 +00:00
// TODO: Use a transaction to avoid race conditions
2017-08-27 09:33:45 +00:00
// Make this more stateless?
func ( user * User ) ScheduleGroupUpdate ( gid int , issuedBy int , duration time . Duration ) error {
var temporary bool
if duration . Nanoseconds ( ) != 0 {
temporary = true
}
revertAt := time . Now ( ) . Add ( duration )
2017-09-18 17:03:52 +00:00
_ , err := replaceScheduleGroupStmt . Exec ( user . ID , gid , issuedBy , revertAt , temporary )
2017-08-27 09:33:45 +00:00
if err != nil {
return err
}
2017-09-18 17:03:52 +00:00
_ , err = setTempGroupStmt . Exec ( gid , user . ID )
2017-09-22 02:21:17 +00:00
ucache , ok := users . ( UserCache )
if ok {
ucache . CacheRemove ( user . ID )
2017-08-27 09:33:45 +00:00
}
2017-09-22 02:21:17 +00:00
return err
2017-08-27 09:33:45 +00:00
}
2017-09-10 16:57:22 +00:00
// TODO: Use a transaction to avoid race conditions
2017-08-27 09:33:45 +00:00
func ( user * User ) RevertGroupUpdate ( ) error {
2017-09-18 17:03:52 +00:00
_ , err := replaceScheduleGroupStmt . Exec ( user . ID , 0 , 0 , time . Now ( ) , false )
2017-08-27 09:33:45 +00:00
if err != nil {
return err
}
2017-09-18 17:03:52 +00:00
_ , err = setTempGroupStmt . Exec ( 0 , user . ID )
2017-09-22 02:21:17 +00:00
ucache , ok := users . ( UserCache )
if ok {
ucache . CacheRemove ( user . ID )
2016-12-02 07:38:54 +00:00
}
2017-09-22 02:21:17 +00:00
return err
2017-07-12 11:05:18 +00:00
}
2017-06-06 14:41:06 +00:00
2017-09-22 02:21:17 +00:00
// TODO: Use a transaction here
2017-09-25 00:48:35 +00:00
// ? - Add a Deactivate method? Not really needed, if someone's been bad you could do a ban, I guess it might be useful, if someone says that email x isn't actually owned by the user in question?
2017-09-22 02:21:17 +00:00
func ( user * User ) Activate ( ) ( err error ) {
_ , err = activateUserStmt . Exec ( user . ID )
2016-12-02 07:38:54 +00:00
if err != nil {
return err
}
2017-09-22 02:21:17 +00:00
_ , err = changeGroupStmt . Exec ( config . DefaultGroup , user . ID )
ucache , ok := users . ( UserCache )
if ok {
ucache . CacheRemove ( user . ID )
2017-02-10 13:39:13 +00:00
}
2017-09-22 02:21:17 +00:00
return err
2017-02-10 13:39:13 +00:00
}
2017-09-10 16:57:22 +00:00
func ( user * User ) increasePostStats ( wcount int , topic bool ) error {
2017-01-12 02:55:08 +00:00
var mod int
2017-09-03 04:50:31 +00:00
baseScore := 1
2017-01-12 02:55:08 +00:00
if topic {
2017-09-18 17:03:52 +00:00
_ , err := incrementUserTopicsStmt . Exec ( 1 , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
2017-09-03 04:50:31 +00:00
baseScore = 2
2017-01-12 02:55:08 +00:00
}
2017-06-06 14:41:06 +00:00
2017-08-20 10:11:49 +00:00
settings := settingBox . Load ( ) . ( SettingBox )
2017-06-10 07:58:15 +00:00
if wcount >= settings [ "megapost_min_words" ] . ( int ) {
2017-09-18 17:03:52 +00:00
_ , err := incrementUserMegapostsStmt . Exec ( 1 , 1 , 1 , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
mod = 4
2017-06-10 07:58:15 +00:00
} else if wcount >= settings [ "bigpost_min_words" ] . ( int ) {
2017-09-18 17:03:52 +00:00
_ , err := incrementUserBigpostsStmt . Exec ( 1 , 1 , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
mod = 1
} else {
2017-09-18 17:03:52 +00:00
_ , err := incrementUserPostsStmt . Exec ( 1 , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
}
2017-09-18 17:03:52 +00:00
_ , err := incrementUserScoreStmt . Exec ( baseScore + mod , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
2017-08-13 11:22:34 +00:00
//log.Print(user.Score + base_score + mod)
//log.Print(getLevel(user.Score + base_score + mod))
2017-09-10 16:57:22 +00:00
// TODO: Use a transaction to prevent level desyncs?
2017-09-18 17:03:52 +00:00
_ , err = updateUserLevelStmt . Exec ( getLevel ( user . Score + baseScore + mod ) , user . ID )
2017-01-12 02:55:08 +00:00
return err
}
2017-09-10 16:57:22 +00:00
func ( user * User ) decreasePostStats ( wcount int , topic bool ) error {
2017-01-12 02:55:08 +00:00
var mod int
2017-09-03 04:50:31 +00:00
baseScore := - 1
2017-01-12 02:55:08 +00:00
if topic {
2017-09-18 17:03:52 +00:00
_ , err := incrementUserTopicsStmt . Exec ( - 1 , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
2017-09-03 04:50:31 +00:00
baseScore = - 2
2017-01-12 02:55:08 +00:00
}
2017-06-06 14:41:06 +00:00
2017-08-20 10:11:49 +00:00
settings := settingBox . Load ( ) . ( SettingBox )
2017-06-12 09:03:14 +00:00
if wcount >= settings [ "megapost_min_words" ] . ( int ) {
2017-09-18 17:03:52 +00:00
_ , err := incrementUserMegapostsStmt . Exec ( - 1 , - 1 , - 1 , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
mod = 4
2017-06-12 09:03:14 +00:00
} else if wcount >= settings [ "bigpost_min_words" ] . ( int ) {
2017-09-18 17:03:52 +00:00
_ , err := incrementUserBigpostsStmt . Exec ( - 1 , - 1 , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
mod = 1
} else {
2017-09-18 17:03:52 +00:00
_ , err := incrementUserPostsStmt . Exec ( - 1 , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
}
2017-09-18 17:03:52 +00:00
_ , err := incrementUserScoreStmt . Exec ( baseScore - mod , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
2017-09-10 16:57:22 +00:00
// TODO: Use a transaction to prevent level desyncs?
2017-09-18 17:03:52 +00:00
_ , err = updateUserLevelStmt . Exec ( getLevel ( user . Score - baseScore - mod ) , user . ID )
2017-01-12 02:55:08 +00:00
return err
}
2017-02-15 10:49:30 +00:00
2017-09-28 22:16:34 +00:00
func ( user * User ) Copy ( ) User {
return * user
}
2017-09-25 00:48:35 +00:00
// TODO: Write unit tests for this
2017-09-22 02:21:17 +00:00
func ( user * User ) initPerms ( ) {
2017-09-15 22:20:01 +00:00
if user . TempGroup != 0 {
user . Group = user . TempGroup
}
group := gstore . DirtyGet ( user . Group )
2017-09-03 04:50:31 +00:00
if user . IsSuperAdmin {
2017-08-06 15:22:18 +00:00
user . Perms = AllPerms
user . PluginPerms = AllPluginPerms
} else {
2017-09-15 22:20:01 +00:00
user . Perms = group . Perms
user . PluginPerms = group . PluginPerms
2017-08-27 09:33:45 +00:00
}
2017-09-15 22:20:01 +00:00
user . IsAdmin = user . IsSuperAdmin || group . IsAdmin
user . IsSuperMod = user . IsAdmin || group . IsMod
2017-09-03 04:50:31 +00:00
user . IsMod = user . IsSuperMod
2017-09-15 22:20:01 +00:00
user . IsBanned = group . IsBanned
2017-09-03 04:50:31 +00:00
if user . IsBanned && user . IsSuperMod {
user . IsBanned = false
2017-02-15 10:49:30 +00:00
}
2017-02-28 09:27:28 +00:00
}
2017-09-22 02:21:17 +00:00
func BcryptCheckPassword ( realPassword string , password string , salt string ) ( err error ) {
return bcrypt . CompareHashAndPassword ( [ ] byte ( realPassword ) , [ ] byte ( password + salt ) )
}
// Investigate. Do we need the extra salt?
func BcryptGeneratePassword ( password string ) ( hashedPassword string , salt string , err error ) {
salt , err = GenerateSafeString ( saltLength )
if err != nil {
return "" , "" , err
}
password = password + salt
hashedPassword , err = BcryptGeneratePasswordNoSalt ( password )
if err != nil {
return "" , "" , err
}
return hashedPassword , salt , nil
}
func BcryptGeneratePasswordNoSalt ( password string ) ( hash string , err error ) {
hashedPassword , err := bcrypt . GenerateFromPassword ( [ ] byte ( password ) , bcrypt . DefaultCost )
if err != nil {
return "" , err
}
return string ( hashedPassword ) , nil
}
func SetPassword ( uid int , password string ) error {
hashedPassword , salt , err := GeneratePassword ( password )
if err != nil {
return err
}
_ , err = setPasswordStmt . Exec ( hashedPassword , salt , uid )
return err
}
func SendValidationEmail ( username string , email string , token string ) bool {
var schema = "http"
if site . EnableSsl {
schema += "s"
}
// TODO: Move these to the phrase system
subject := "Validate Your Email @ " + site . Name
msg := "Dear " + username + ", following your registration on our forums, we ask you to validate your email, so that we can confirm that this email actually belongs to you.\n\nClick on the following link to do so. " + schema + "://" + site . URL + "/user/edit/token/" + token + "\n\nIf you haven't created an account here, then please feel free to ignore this email.\nWe're sorry for the inconvenience this may have caused."
return SendEmail ( email , subject , msg )
}
2017-09-25 00:48:35 +00:00
// TODO: Write units tests for this
2017-09-22 02:21:17 +00:00
func wordsToScore ( wcount int , topic bool ) ( score int ) {
if topic {
score = 2
} else {
score = 1
}
settings := settingBox . Load ( ) . ( SettingBox )
if wcount >= settings [ "megapost_min_words" ] . ( int ) {
score += 4
} else if wcount >= settings [ "bigpost_min_words" ] . ( int ) {
score ++
}
return score
}
2017-09-28 22:16:34 +00:00
// For use in tests and to help generate dummy users for forums which don't have last posters
func getDummyUser ( ) * User {
return & User { ID : 0 , Name : "" }
}
2017-09-25 00:48:35 +00:00
// TODO: Write unit tests for this
2017-09-03 04:50:31 +00:00
func buildProfileURL ( slug string , uid int ) string {
2017-06-28 12:05:26 +00:00
if slug == "" {
return "/user/" + strconv . Itoa ( uid )
}
return "/user/" + slug + "." + strconv . Itoa ( uid )
2017-02-28 09:27:28 +00:00
}