adding admin account reset

This commit is contained in:
Roland Osborne 2022-03-09 15:12:05 -08:00
parent e25b95277c
commit 9af758a82b
3 changed files with 55 additions and 5 deletions

View File

@ -33,8 +33,4 @@ func RemoveNodeAccount(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func SetNodeAccount(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}

View File

@ -8,12 +8,17 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
"databag/internal/store" "databag/internal/store"
"encoding/base64" "encoding/base64"
"github.com/gorilla/mux"
) )
func GetAccountListingImage(w http.ResponseWriter, r *http.Request) { func GetAccountListingImage(w http.ResponseWriter, r *http.Request) {
// get referenced account guid
params := mux.Vars(r)
guid := params["guid"]
var account store.Account var account store.Account
if err := store.DB.Preload("AccountDetail").Where("searchable = ? AND disabled = ?", true, false).First(&account).Error; err != nil { if err := store.DB.Preload("AccountDetail").Where("guid = ? AND searchable = ? AND disabled = ?", guid, true, false).First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err) ErrResponse(w, http.StatusNotFound, err)
} else { } else {

View File

@ -0,0 +1,49 @@
package databag
import (
"net/http"
"encoding/hex"
"time"
"strconv"
"github.com/gorilla/mux"
"databag/internal/store"
"github.com/theckman/go-securerandom"
)
func SetNodeAccount(w http.ResponseWriter, r *http.Request) {
// get referenced account id
params := mux.Vars(r)
accountId, res := strconv.ParseUint(params["accountId"], 10, 32)
if res != nil {
ErrResponse(w, http.StatusBadRequest, res)
return
}
if res = AdminLogin(r); res != nil {
ErrResponse(w, http.StatusUnauthorized, res)
return
}
data, err := securerandom.Bytes(APP_RESETSIZE)
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
token := hex.EncodeToString(data)
accountToken := store.AccountToken{
TokenType: APP_TOKENRESET,
Token: token,
AccountID: uint(accountId),
Expires: time.Now().Unix() + APP_CREATEEXPIRE,
};
if err := store.DB.Create(&accountToken).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
WriteResponse(w, token);
}