Skip to content

Instantly share code, notes, and snippets.

@kavirajk
Last active July 15, 2016 11:22
Show Gist options
  • Save kavirajk/6bcf9fe586bbebd646d1c406562823ef to your computer and use it in GitHub Desktop.
Save kavirajk/6bcf9fe586bbebd646d1c406562823ef to your computer and use it in GitHub Desktop.
Resets User's password stored in db
// WARNING: This command is supposed to be used only by admin
package main
import (
"flag"
"fmt"
"log"
"code.launchyard.com/root/myserver/config"
"code.launchyard.com/root/myserver/models"
"code.launchyard.com/root/myserver/utils"
)
// Saver is a interface that wraps the 'Save' behaviour of user
type Saver interface {
Save() *models.AppError
}
var defaultUserGetter = func(email string) *models.User {
user, err := models.GetUserByEmail(email)
if err != nil {
log.Fatalf("failed password-reset: %v\n", err)
}
return user
}
var defaultPasswordSaver = func(s Saver) {
if err := s.Save(); err != nil {
log.Fatalf("error saving new password: %v\n", err)
}
}
var (
email = flag.String("email", "", "user's email to reset the password")
password = flag.String("password", "", "new password")
userGetter = defaultUserGetter
savePassword = defaultPasswordSaver
)
func main() {
flag.Parse()
if *email == "" {
fmt.Print("email: ")
fmt.Scanln(email)
}
if *password == "" {
fmt.Print("new-password: ")
fmt.Scanln(password)
}
models.InitModel(config.DBDriver, fmt.Sprintf(config.DBDataSource))
ResetPassword(*email, *password)
fmt.Println("Password reset success.")
}
// ResetPassword takes email and new-password and resets the password with new-password
func ResetPassword(email, newPassword string) {
user := userGetter(email)
user.Password = utils.HashString(newPassword)
savePassword(user)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment