cerca

lean forum software (pmc local branch)
git clone http://git.permacomputing.net/repos/cerca.git # read-only access
Log | Files | Refs | README | LICENSE

main.go (1776B)


      1 package main
      2 
      3 import (
      4 	"cerca/constants"
      5 	"cerca/database"
      6 	"flag"
      7 	"fmt"
      8 	"os"
      9 )
     10 
     11 func inform(msg string, args ...interface{}) {
     12 	if len(args) > 0 {
     13 		fmt.Printf("admin-reset: %s\n", fmt.Sprintf(msg, args...))
     14 	} else {
     15 		fmt.Printf("admin-reset: %s\n", msg)
     16 	}
     17 }
     18 
     19 func complain(msg string, args ...interface{}) {
     20 	if len(args) > 0 {
     21 		inform(msg, args)
     22 	} else {
     23 		inform(msg)
     24 	}
     25 	os.Exit(0)
     26 }
     27 
     28 func main() {
     29 	var username string
     30 	var dbPath string
     31 	flag.StringVar(&username, "username", "", "username whose credentials should be reset")
     32 	flag.StringVar(&dbPath, "database", "./data/forum.db", "full path to the forum database; e.g. ./data/forum.db")
     33 	flag.Parse()
     34 
     35 	usage := `usage
     36   admin-reset --database ./data/forum.db --username <username to reset>
     37   admin-reset --help for more information
     38 
     39   # example
     40   ./admin-reset --database ../../testdata/forum.db --username bambas 
     41   `
     42 
     43 	if username == "" {
     44 		complain(usage)
     45 	}
     46 
     47 	// check if database exists! we dont wanna create a new db in this case ':)
     48 	if !database.CheckExists(dbPath) {
     49 		complain("couldn't find database at %s", dbPath)
     50 	}
     51 
     52 	db := database.InitDB(dbPath)
     53 
     54 	userid, err := db.GetUserID(username)
     55 	if err != nil {
     56 		complain("reset password failed (%w)", err)
     57 	}
     58 	newPassword, err := db.ResetPassword(userid)
     59 
     60 	if err != nil {
     61 		complain("reset password failed (%w)", err)
     62 	}
     63 
     64 	// log cmd actions just as admin web-actions are logged
     65 	systemUserid := db.GetSystemUserid()
     66 	err = db.AddModerationLog(systemUserid, userid, constants.MODLOG_RESETPW)
     67 	if err != nil {
     68 		complain("adding mod log for password reset failed (%w)", err)
     69 	}
     70 
     71 	inform("Successfully updated %s's password hash", username)
     72 	inform("New temporary password: %s", newPassword)
     73 	inform("Admin action has been logged to /moderations")
     74 }