Last active
July 15, 2016 11:22
-
-
Save kavirajk/6bcf9fe586bbebd646d1c406562823ef to your computer and use it in GitHub Desktop.
Resets User's password stored in db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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