@ -16,12 +16,14 @@ import (
"net/http"
"net/http"
"runtime"
"runtime"
"strconv"
"strconv"
"strings"
"time"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/mux"
"github.com/writeas/impart"
"github.com/writeas/impart"
"github.com/writeas/web-core/auth"
"github.com/writeas/web-core/auth"
"github.com/writeas/web-core/log"
"github.com/writeas/web-core/log"
"github.com/writeas/web-core/passgen"
"github.com/writeas/writefreely/appstats"
"github.com/writeas/writefreely/appstats"
"github.com/writeas/writefreely/config"
"github.com/writeas/writefreely/config"
)
)
@ -170,11 +172,12 @@ func handleViewAdminUser(app *App, u *User, w http.ResponseWriter, r *http.Reque
Config config . AppCfg
Config config . AppCfg
Message string
Message string
User * User
User * User
Colls [ ] inspectedCollection
Colls [ ] inspectedCollection
LastPost string
LastPost string
NewPassword string
TotalPosts int64
TotalPosts int64
ClearEmail string
} {
} {
Config : app . cfg . App ,
Config : app . cfg . App ,
Message : r . FormValue ( "m" ) ,
Message : r . FormValue ( "m" ) ,
@ -186,6 +189,14 @@ func handleViewAdminUser(app *App, u *User, w http.ResponseWriter, r *http.Reque
if err != nil {
if err != nil {
return impart . HTTPError { http . StatusInternalServerError , fmt . Sprintf ( "Could not get user: %v" , err ) }
return impart . HTTPError { http . StatusInternalServerError , fmt . Sprintf ( "Could not get user: %v" , err ) }
}
}
flashes , _ := getSessionFlashes ( app , w , r , nil )
for _ , flash := range flashes {
if strings . HasPrefix ( flash , "SUCCESS: " ) {
p . NewPassword = strings . TrimPrefix ( flash , "SUCCESS: " )
p . ClearEmail = p . User . EmailClear ( app . keys )
}
}
p . UserPage = NewUserPage ( app , r , u , p . User . Username , nil )
p . UserPage = NewUserPage ( app , r , u , p . User . Username , nil )
p . TotalPosts = app . db . GetUserPostsCount ( p . User . ID )
p . TotalPosts = app . db . GetUserPostsCount ( p . User . ID )
lp , err := app . db . GetUserLastPostTime ( p . User . ID )
lp , err := app . db . GetUserLastPostTime ( p . User . ID )
@ -230,6 +241,37 @@ func handleViewAdminUser(app *App, u *User, w http.ResponseWriter, r *http.Reque
return nil
return nil
}
}
func handleAdminResetUserPass ( app * App , u * User , w http . ResponseWriter , r * http . Request ) error {
vars := mux . Vars ( r )
username := vars [ "username" ]
if username == "" {
return impart . HTTPError { http . StatusFound , "/admin/users" }
}
// Generate new random password since none supplied
pass := passgen . NewWordish ( )
hashedPass , err := auth . HashPass ( [ ] byte ( pass ) )
if err != nil {
return impart . HTTPError { http . StatusInternalServerError , fmt . Sprintf ( "Could not create password hash: %v" , err ) }
}
userIDVal := r . FormValue ( "user" )
log . Info ( "ADMIN: Changing user %s password" , userIDVal )
id , err := strconv . Atoi ( userIDVal )
if err != nil {
return impart . HTTPError { http . StatusBadRequest , fmt . Sprintf ( "Invalid user ID: %v" , err ) }
}
err = app . db . ChangePassphrase ( int64 ( id ) , true , "" , hashedPass )
if err != nil {
return impart . HTTPError { http . StatusInternalServerError , fmt . Sprintf ( "Could not update passphrase: %v" , err ) }
}
log . Info ( "ADMIN: Successfully changed." )
addSessionFlash ( app , w , r , fmt . Sprintf ( "SUCCESS: %s" , pass ) , nil )
return impart . HTTPError { http . StatusFound , fmt . Sprintf ( "/admin/user/%s" , username ) }
}
func handleViewAdminPages ( app * App , u * User , w http . ResponseWriter , r * http . Request ) error {
func handleViewAdminPages ( app * App , u * User , w http . ResponseWriter , r * http . Request ) error {
p := struct {
p := struct {
* UserPage
* UserPage