Nach Aufräumen von Paket session kann dieses nur zu server umbenannt werden
This commit is contained in:
84
packages/server/login.go
Normal file
84
packages/server/login.go
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Sicherheitsunterweisung
|
||||
* Copyright (C) 2023 Jason Streifling
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
)
|
||||
|
||||
func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
instructors, err := db.GetInstructors()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
for _, i := range instructors {
|
||||
if r.PostFormValue("login") == fmt.Sprint(i.ID) {
|
||||
session := Session{
|
||||
UUID: uuid.New(),
|
||||
Instructor: *i,
|
||||
Briefings: make([]*Briefing, 0),
|
||||
}
|
||||
(*ss) = append((*ss), &session)
|
||||
cs <- &session
|
||||
|
||||
data := tableHTMLData{SessionID: session.UUID}
|
||||
data.OTD, err = db.GetAllOverviewTableData()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", data)
|
||||
return
|
||||
}
|
||||
}
|
||||
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
|
||||
}
|
||||
}
|
||||
|
||||
func findCorrectLogin(l string, ss *[]*Session) (*Briefing, *Participant, bool) {
|
||||
for _, s := range *ss {
|
||||
for _, b := range s.Briefings {
|
||||
for _, p := range b.Participants {
|
||||
if l == p.Login {
|
||||
return b, p, true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil, false
|
||||
}
|
||||
|
||||
func HandleExternalLogin(ss *[]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
briefing, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
|
||||
if loginCorrect {
|
||||
data := participantHTMLData{
|
||||
BriefingID: briefing.UUID,
|
||||
Participant: Participant{Login: participant.Login},
|
||||
}
|
||||
|
||||
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
|
||||
} else {
|
||||
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user