add suspend and unsuspend calls

This commit is contained in:
Your Name 2023-12-13 15:46:02 -05:00
parent b062c4aea1
commit 498f7f7393
2 changed files with 36 additions and 1 deletions

35
auth.go
View File

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"strconv"
"strings"
"time"
"golang.org/x/crypto/bcrypt"
@ -65,3 +66,37 @@ func (u *UserData) ChangePassword(oldpass, newpass string) bool {
}
return u.SetPassword(newcrypt)
}
func (u *UserData) Suspend() bool {
var hash string
u.Set("status", "inactive")
q := fmt.Sprintf("select password from %s_users where id=? limit 1", c.TablePrefix)
err := c.db.Get(&hash, q, u.UserID)
if err != nil {
return false
}
if strings.HasPrefix(hash, "*") {
return true
}
newhash := "*" + hash
q = fmt.Sprintf("update %s_users set password=? where id=?", c.TablePrefix)
_, err = c.db.Exec(q, newhash, u.UserID)
return err == nil
}
func (u *UserData) Unsuspend() bool {
var hash string
u.Set("status", "active")
q := fmt.Sprintf("select password from %s_users where id=? limit 1", c.TablePrefix)
err := c.db.Get(&hash, q, u.UserID)
if err != nil {
return false
}
if !strings.HasPrefix(hash, "*") {
return true
}
newhash := strings.TrimPrefix(hash, "*")
q = fmt.Sprintf("update %s_users set password=? where id=?", c.TablePrefix)
_, err = c.db.Exec(q, newhash, u.UserID)
return err == nil
}

View File

@ -140,7 +140,7 @@ func GetById(id int64) (*UserData, bool) {
if err == sql.ErrNoRows {
return nil, false
}
log.Printf("scsusers.Get: %s", err.Error())
log.Printf("scsusers.GetById: %s", err.Error())
return nil, false
}
ok := u.LoadMeta()