Handle first user differently from the rest
This commit is contained in:
parent
ed51d28c65
commit
8711ba0629
28
cmd/main.go
28
cmd/main.go
@ -54,34 +54,28 @@ func main() {
|
|||||||
mux.HandleFunc("GET /edit-user/", view.EditUser(args, db, store))
|
mux.HandleFunc("GET /edit-user/", view.EditUser(args, db, store))
|
||||||
mux.HandleFunc("GET /hub/", view.ShowHub(args, db, store))
|
mux.HandleFunc("GET /hub/", view.ShowHub(args, db, store))
|
||||||
mux.HandleFunc("GET /logout/", view.Logout(args, store))
|
mux.HandleFunc("GET /logout/", view.Logout(args, store))
|
||||||
mux.HandleFunc("GET /publish-issue/",
|
mux.HandleFunc("GET /publish-issue/", view.PublishLatestIssue(args, db, store))
|
||||||
view.PublishLatestIssue(args, db, store))
|
mux.HandleFunc("GET /rejected-articles/", view.ShowRejectedArticles(args, db, store))
|
||||||
mux.HandleFunc("GET /rejected-articles/",
|
mux.HandleFunc("GET /review-rejected-article/{id}/", view.ReviewRejectedArticle(args, db, store))
|
||||||
view.ShowRejectedArticles(args, db, store))
|
mux.HandleFunc("GET /review-unpublished-article/{id}/", view.ReviewUnpublishedArticle(args, db, store))
|
||||||
mux.HandleFunc("GET /review-rejected-article/{id}/",
|
mux.HandleFunc("GET /rss/", view.ShowRSS(
|
||||||
view.ReviewRejectedArticle(args, db, store))
|
args,
|
||||||
mux.HandleFunc("GET /review-unpublished-article/{id}/",
|
|
||||||
view.ReviewUnpublishedArticle(args, db, store))
|
|
||||||
mux.HandleFunc("GET /rss/", view.ShowRSS(args,
|
|
||||||
db,
|
db,
|
||||||
"Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt",
|
"Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt",
|
||||||
"https://distrikt-ni-st.de",
|
"https://distrikt-ni-st.de",
|
||||||
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität",
|
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität",
|
||||||
))
|
))
|
||||||
mux.HandleFunc("GET /this-issue/", view.ShowCurrentArticles(args, db))
|
mux.HandleFunc("GET /this-issue/", view.ShowCurrentArticles(args, db))
|
||||||
mux.HandleFunc("GET /unpublished-articles/",
|
mux.HandleFunc("GET /unpublished-articles/", view.ShowUnpublishedArticles(args, db))
|
||||||
view.ShowUnpublishedArticles(args, db))
|
|
||||||
mux.HandleFunc("GET /write-article/", view.WriteArticle(args, db))
|
mux.HandleFunc("GET /write-article/", view.WriteArticle(args, db))
|
||||||
|
|
||||||
|
mux.HandleFunc("POST /add-first-user/", view.AddFirstUser(args, db, store))
|
||||||
mux.HandleFunc("POST /add-tag/", view.AddTag(args, db, store))
|
mux.HandleFunc("POST /add-tag/", view.AddTag(args, db, store))
|
||||||
mux.HandleFunc("POST /add-user/", view.AddUser(args, db, store))
|
mux.HandleFunc("POST /add-user/", view.AddUser(args, db, store))
|
||||||
mux.HandleFunc("POST /login/", view.Login(args, db, store))
|
mux.HandleFunc("POST /login/", view.Login(args, db, store))
|
||||||
mux.HandleFunc("POST /publish-article/{id}/",
|
mux.HandleFunc("POST /publish-article/{id}/", view.PublishArticle(args, db, store))
|
||||||
view.PublishArticle(args, db, store))
|
mux.HandleFunc("POST /reject-article/{id}/", view.RejectArticle(args, db, store))
|
||||||
mux.HandleFunc("POST /reject-article/{id}/",
|
mux.HandleFunc("POST /resubmit-article/{id}/", view.ResubmitArticle(args, db, store))
|
||||||
view.RejectArticle(args, db, store))
|
|
||||||
mux.HandleFunc("POST /resubmit-article/{id}/",
|
|
||||||
view.ResubmitArticle(args, db, store))
|
|
||||||
mux.HandleFunc("POST /submit-article/", view.SubmitArticle(args, db, store))
|
mux.HandleFunc("POST /submit-article/", view.SubmitArticle(args, db, store))
|
||||||
mux.HandleFunc("POST /update-user/", view.UpdateUser(args, db, store))
|
mux.HandleFunc("POST /update-user/", view.UpdateUser(args, db, store))
|
||||||
mux.HandleFunc("POST /upload-image/", view.UploadImage(args))
|
mux.HandleFunc("POST /upload-image/", view.UploadImage(args))
|
||||||
|
@ -36,7 +36,7 @@ func HomePage(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.Han
|
|||||||
|
|
||||||
files := []string{c.WebDir + "/templates/index.html"}
|
files := []string{c.WebDir + "/templates/index.html"}
|
||||||
if numRows == 0 {
|
if numRows == 0 {
|
||||||
files = append(files, c.WebDir+"/templates/add-user.html")
|
files = append(files, c.WebDir+"/templates/first-user.html")
|
||||||
tmpl, err := template.ParseFiles(files...)
|
tmpl, err := template.ParseFiles(files...)
|
||||||
template.Must(tmpl, err).Execute(w, nil)
|
template.Must(tmpl, err).Execute(w, nil)
|
||||||
} else {
|
} else {
|
||||||
|
@ -88,37 +88,13 @@ func AddUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.Hand
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
htmlData.ID, err = db.AddUser(htmlData.User, pass)
|
_, err = db.AddUser(htmlData.User, pass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if htmlData.ID == 1 {
|
|
||||||
htmlData.Role = model.Admin
|
|
||||||
|
|
||||||
if err = db.UpdateAttributes(
|
|
||||||
&model.Attribute{Table: "users", ID: id, AttName: "role", Value: htmlData.Role},
|
|
||||||
); err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := saveSession(w, r, s, htmlData.User); err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := db.AddIssue(); err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", 0)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", 0)
|
||||||
}
|
}
|
||||||
@ -214,3 +190,72 @@ func UpdateUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.H
|
|||||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddFirstUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
htmlData := UserData{
|
||||||
|
User: &model.User{
|
||||||
|
UserName: r.PostFormValue("username"),
|
||||||
|
FirstName: r.PostFormValue("first-name"),
|
||||||
|
LastName: r.PostFormValue("last-name"),
|
||||||
|
Role: model.Admin,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
pass := r.PostFormValue("password")
|
||||||
|
pass2 := r.PostFormValue("password2")
|
||||||
|
|
||||||
|
if len(htmlData.UserName) == 0 || len(htmlData.FirstName) == 0 ||
|
||||||
|
len(htmlData.LastName) == 0 || len(pass) == 0 || len(pass2) == 0 {
|
||||||
|
htmlData.Msg = "Alle Felder müssen ausgefüllt werden."
|
||||||
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userString, stringLen, ok := checkUserStrings(htmlData.User)
|
||||||
|
if !ok {
|
||||||
|
htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ",
|
||||||
|
stringLen, " Zeichen erlaubt.")
|
||||||
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
id, _ := db.GetID(htmlData.UserName)
|
||||||
|
if id != 0 {
|
||||||
|
htmlData.Msg = fmt.Sprint(htmlData.UserName,
|
||||||
|
" ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
|
||||||
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if pass != pass2 {
|
||||||
|
htmlData.Msg = "Die Passwörter stimmen nicht überein."
|
||||||
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
htmlData.ID, err = db.AddUser(htmlData.User, pass)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := saveSession(w, r, s, htmlData.User); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := db.AddIssue(); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
38
web/templates/first-user.html
Normal file
38
web/templates/first-user.html
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
{{define "page-content"}}
|
||||||
|
<h2>Neuer Benutzer</h2>
|
||||||
|
<form>
|
||||||
|
<div class="grid grid-cols-3 gap-4">
|
||||||
|
<div>
|
||||||
|
<label for="username">Benutzername</label>
|
||||||
|
<input class="w-full" required name="username" type="text" value="{{.UserName}}" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="password">Passwort</label>
|
||||||
|
<input class="w-full" required name="password" placeholder="***" type="password" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="password2">Passwort wiederholen</label>
|
||||||
|
<input class="w-full" required name="password2" placeholder="***" type="password" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="first-name">Vorname</label>
|
||||||
|
<input class="w-full" required name="first-name" type="text" value="{{.FirstName}}" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="last-name">Nachname</label>
|
||||||
|
<input class="w-full" required name="last-name" type="text" value="{{.LastName}}" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="btn-area">
|
||||||
|
<input class="action-btn" type="submit" value="Anlegen" hx-post="/add-first-user/" hx-target="#page-content" />
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
var msg = "{{.Msg}}";
|
||||||
|
if (msg != "") {
|
||||||
|
alert(msg);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
{{end}}
|
@ -14,12 +14,10 @@
|
|||||||
<button class="btn" hx-get="logout" hx-target="#page-content">Abmelden</button>
|
<button class="btn" hx-get="logout" hx-target="#page-content">Abmelden</button>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main>
|
<main class="mx-4">
|
||||||
<div id="page-content">
|
<div id="page-content">
|
||||||
{{template "page-content" .}}
|
{{template "page-content" .}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="/web/static/js/htmx.min.js"></script>
|
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<footer class="my-8">
|
<footer class="my-8">
|
||||||
@ -27,6 +25,8 @@
|
|||||||
© 2024 Jason Streifling. Alle Rechte vorbehalten.
|
© 2024 Jason Streifling. Alle Rechte vorbehalten.
|
||||||
</p>
|
</p>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
|
<script src="/web/static/js/htmx.min.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user