func postLogin(w http.ResponseWriter, r *http.Request, c *web.Context) { email, password := r.FormValue("email"), r.FormValue("password") user, ok := GetUser(email, password) if !ok || (user.Role != "employee" && user.Role != "admin") { c.SetFlash("alertError", "Incorrect email or password") http.Redirect(w, r, "/login", 303) return } employee, ok := GetEmployee(user.Id) if !ok { c.SetFlash("alertError", "Error finding user") http.Redirect(w, r, "/login", 303) return } c.Login(user.Role) c.SetSession(map[string]interface{}{ "emplyeeId": employee.Id, "email": employee.Email, }) if user.Role == "employee" { http.Redirect(w, r, "/employee/home", 303) return } if user.Role == "admin" { http.Redirect(w, r, "/admin/home", 303) return } return }
// POST submit main login func postLogin(w http.ResponseWriter, r *http.Request, c *web.Context) { employee, role, ok := service.FindOneEmployeeByLogin(r.FormValue("email"), r.FormValue("password")) if role == "developer" { c.Login(role) c.SetSession(map[string]interface{}{ "emplyeeId": "developer", "email": "*****@*****.**", }) http.Redirect(w, r, "/admin/home", 303) return } if !ok { c.SetFlash("alertError", "Incorrect email or password") http.Redirect(w, r, "/login", 303) return } c.Login(role) c.SetSession(map[string]interface{}{ "emplyeeId": employee.Id, "email": employee.Email, }) //if role == "employee" { // http.Redirect(w, r, "/employee/home", 303) // return //} //if role == "admin" { // http.Redirect(w, r, "/admin/home", 303) // return //} http.Redirect(w, r, "/admin/home", 303) return }
func AdminDriverDocumentDelete(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } service.DeleteDocument(c.GetPathVar("documentId")) c.SetFlash("alertSuccess", "Successfully deleted document") http.Redirect(w, r, "/admin/driver/"+c.GetPathVar("driverId")+"/document", 303) }
// POST admin delete company func AdminCompanyDelete(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } service.DeleteCompany(c.GetPathVar("id")) c.SetFlash("alertSuccess", "Successfully deleted company") http.Redirect(w, r, "/admin/company", 303) return }
func AdminVehicleDelete(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } service.DeleteVehicle(c.GetPathVar("vehicleId")) c.SetFlash("alertSuccess", "Successfuly deleted vehicle") http.Redirect(w, r, "/admin/vehicle", 303) return }
func AdminDriverDocumentSave(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } document := service.FindOneDocument(r.FormValue("id")) document.Data = r.FormValue("data") service.SaveDocument(document) c.SetFlash("alertSuccess", "Successfully save document") fmt.Fprintf(w, "/admin/driver/%s/document", c.GetPathVar("driverId")) }
// POSt driver save document func saveDocument(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/"+c.GetPathVar("slug"), "driver") { return } document := service.FindOneDocument(r.FormValue("id")) document.Data = r.FormValue("data") service.SaveDocument(document) c.SetFlash("alertSuccess", "Successfully save document") fmt.Fprintf(w, "/%s/driver", c.GetPathVar("slug")) }
func PostComment(w http.ResponseWriter, r *http.Request, c *web.Context) { comment := service.Comment{ Id: util.UUID4(), Com: r.FormValue("comment"), Url: r.FormValue("return"), Name: r.FormValue("name"), Closed: false, } service.SaveComment(comment) c.SetFlash("alertSuccess", "Successfully save your comment") http.Redirect(w, r, r.FormValue("return"), 303) }
// POST admin edit company func AdminCompanyEdit(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } r.ParseForm() company := service.FindOneCompany(r.FormValue("id")) util.FormToStruct(&company, r.Form, "") service.SaveCompany(company) c.SetFlash("alertSuccess", "Successfully saved company") http.Redirect(w, r, "/admin/company/"+r.FormValue("id"), 303) return }
func AdminVehicleSave(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } r.ParseForm() vehicle := service.FindOneVehicle(r.FormValue("id")) util.FormToStruct(&vehicle, r.Form, "") service.SaveVehicle(vehicle) c.SetFlash("alertSuccess", "Successfully saved vehicle") http.Redirect(w, r, "/admin/vehicle", 303) return }
// POST admin add company func AdminCompanyAdd(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } r.ParseForm() var company service.Company util.FormToStruct(&company, r.Form, "") company.Id = util.UUID4() service.SaveCompany(company) c.SetFlash("alertSuccess", "Successfully saved company") http.Redirect(w, r, "/admin/company", 303) return }
// POST admin save vehilce to company func AdminCompanyVehicleSave(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } r.ParseForm() vehicle := service.FindOneVehicle(r.FormValue("id")) util.FormToStruct(&vehicle, r.Form, "") if vehicle.Id == "" { vehicle.Id = util.UUID4() } service.SaveVehicle(vehicle) c.SetFlash("alertSuccess", "Successfully saved vehicle") http.Redirect(w, r, "/admin/company/"+c.GetPathVar("companyId")+"/vehicle", 303) return }
func saveDocument(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "driver", "/"+c.GetPathVar("slug")) { return } var document Document if ok := db.GetAs("document", r.FormValue("id"), &document); !ok { c.SetFlash("alertError", "Error finding document") http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/driver", 303) return } document.Data = r.FormValue("data") db.Set("document", r.FormValue("id"), document) c.SetFlash("alertSuccess", "Successfully save document") fmt.Fprintf(w, "/%s/driver", c.GetPathVar("slug")) }
// POST admin add driver to company func AdminCompanyDriverAdd(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } if !service.CanUpdateUser("", r.FormValue("email")) { c.SetFlash("alertError", "Email already registered") http.Redirect(w, r, "/admin/company/"+c.GetPathVar("companyId")+"/driver/new", 303) return } r.ParseForm() driver, user := service.NewDriver(r.Form) service.SaveDriver(driver) service.SaveUser(user) c.SetFlash("alertSuccess", "Successfully added driver") http.Redirect(w, r, "/admin/company/"+c.GetPathVar("companyId")+"/driver", 303) return }
// POST add employee func AdminEmployeeAdd(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "developer") { return } if !service.CanUpdateUser("", r.FormValue("email")) { c.SetFlash("alertError", "Email already registered") http.Redirect(w, r, "/admin/employee/new", 303) return } r.ParseForm() employee, user := service.NewEmployee(r.Form) service.SaveUser(user) service.SaveEmployee(employee) c.SetFlash("alertSuccess", "Successfully added employee") http.Redirect(w, r, "/admin/employee", 303) return }
// POST admin upload company logo func AdminUploadCompanyLogo(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } path := "static/logo/" if err := os.MkdirAll(path, 0755); err != nil { c.SetFlash("alertError", "Error uploading file") http.Redirect(w, r, "/admin/company/"+c.GetPathVar("id"), 303) return } r.ParseMultipartForm(32 << 20) // 32 MB file, handler, err := r.FormFile("logo") if err != nil || len(handler.Header["Content-Type"]) < 1 { fmt.Println(err) c.SetFlash("alertError", "Error uploading file") http.Redirect(w, r, "/admin/company/"+c.GetPathVar("id"), 303) return } defer file.Close() if handler.Header["Content-Type"][0] != "image/png" && handler.Header["Content-Type"][0] != "image/jpeg" { fmt.Println(err) c.SetFlash("alertError", "Error uploading file") http.Redirect(w, r, "/admin/company/"+c.GetPathVar("id"), 303) return } company := service.FindOneCompany(c.GetPathVar("id")) f, err := os.OpenFile(path+company.Id+".png", os.O_WRONLY|os.O_CREATE, 0666) if err != nil { fmt.Println(err) c.SetFlash("alertError", "Error uploading file") http.Redirect(w, r, "/admin/company/"+company.Id, 303) return } defer f.Close() io.Copy(f, file) company.Logo = true service.SaveCompany(company) c.SetFlash("alertSuccess", "Successfully uploaded file") http.Redirect(w, r, "/admin/company/"+company.Id, 303) return }
func AdminDriverDocumentAdd(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } driver := service.FindOneDriver(c.GetPathVar("driverId")) docIds := util.SliceString(r.FormValue("docIds"), ',') for _, id := range docIds { document := service.Document{ Id: util.UUID4(), Name: "dqf-" + id, DocumentId: id, Complete: false, CompanyId: driver.CompanyId, DriverId: driver.Id, } service.SaveDocument(document) } c.SetFlash("alertSuccess", "Successfully added documents") http.Redirect(w, r, "/admin/driver/"+c.GetPathVar("driverId")+"/document", 303) }
// POST edit employee func AdminEmployeeEdit(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "developer") { return } if !service.CanUpdateUser(r.FormValue("userId"), r.FormValue("email")) { c.SetFlash("alertError", "Email already registered") http.Redirect(w, r, "/admin/employee/"+r.FormValue("id"), 303) return } r.ParseForm() employee := service.FindOneEmployee(r.FormValue("id")) util.FormToStruct(&employee, r.Form, "") user := service.FindOneUser(employee.UserId) util.FormToStruct(&user, r.Form, "") user.Id = employee.UserId service.SaveEmployee(employee) service.SaveUser(user) c.SetFlash("alertSuccess", "Successfully saved employee") http.Redirect(w, r, "/admin/employee", 303) return }
// POST admin save driver func AdminDriverSave(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } if !service.CanUpdateUser(r.FormValue("userId"), r.FormValue("email")) { c.SetFlash("alertError", "Email already registered") http.Redirect(w, r, "/admin/driver/"+c.GetPathVar("id"), 303) return } r.ParseForm() driver := service.FindOneDriver(r.FormValue("id")) util.FormToStruct(&driver, r.Form, "") user := service.FindOneUser(driver.UserId) util.FormToStruct(&user, r.Form, "") user.Id = driver.UserId service.SaveDriver(driver) service.SaveUser(user) c.SetFlash("alertSuccess", "Successfully saved driver") http.Redirect(w, r, "/admin/driver", 303) return }
func getDocument(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "driver", "/"+c.GetPathVar("slug")) { return } var driver Driver if ok := db.GetAs("driver", c.GetFromSession("id").(string), &driver); !ok { c.SetFlash("alertError", "Error finding driver") c.Logout() http.Redirect(w, r, "/"+c.GetPathVar("slug"), 303) return } var company Company if ok := db.GetAs("company", c.GetFromSession("companyId").(string), &company); !ok { c.SetFlash("alertError", "Error finding company") c.Logout() http.Redirect(w, r, "/"+c.GetPathVar("slug"), 303) return } var document Document if ok := db.GetAs("document", c.GetPathVar("id"), &document); !ok { c.SetFlash("alertError", "Error finding document") //c.Logout() http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/driver", 303) return } ts.Render(w, document.Name+".tmpl", tmpl.Model{ "driver": driver, "company": company, "document": document, }) }
// POST post to cmopany login func postCompanyLogin(w http.ResponseWriter, r *http.Request, c *web.Context) { company, ok := service.FindOneCompanyBySlug(c.GetPathVar("slug")) if !ok || !company.Feature { fmt.Fprintf(w, "404 Not Page Found") return } driver, ok2 := service.FindOneDriverByCompanyLogin(r.FormValue("email"), r.FormValue("password"), company.Id) if !ok2 { c.SetFlash("alertError", "Invalid email or password") http.Redirect(w, r, "/"+company.Slug+"/login", 303) return } c.Login("driver") c.SetSession(map[string]interface{}{ "id": driver.Id, "companyId": driver.CompanyId, "userId": driver.UserId, "email": driver.Email, "slug": company.Slug, }) c.SetFlash("alertSuccess", "Welcome "+driver.FirstName+" "+driver.LastName) http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/driver", 303) return }
func getDriverHome(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "driver", "/"+c.GetPathVar("slug")) { return } if c.GetFromSession("slug") != c.GetPathVar("slug") { http.Redirect(w, r, "/"+c.GetFromSession("slug").(string), 303) } var driver Driver if ok := db.GetAs("driver", c.GetFromSession("id").(string), &driver); !ok { c.SetFlash("alertError", "Error finding driver") c.Logout() http.Redirect(w, r, "/"+c.GetPathVar("slug"), 303) return } msgK, msgV := c.GetFlash() ts.Render(w, "driver-home.tmpl", tmpl.Model{ msgK: msgV, "driver": driver, "slug": c.GetPathVar("slug"), "documents": GetDriverDocuments(driver.Id), }) return }
func postCompanyLogin(w http.ResponseWriter, r *http.Request, c *web.Context) { var company Company if ok := db.GetAs("company", r.FormValue("companyId"), &company); !ok || !company.Feature { fmt.Fprintf(w, "404 Not Page Found") return } var driver Driver if ok := GetDriverFromLogin(r.FormValue("email"), r.FormValue("password"), company.Id, &driver); !ok { c.SetFlash("alertError", "Invalid email or password") http.Redirect(w, r, "/"+company.Slug+"/login", 303) return } c.Login("driver") c.SetSession(map[string]interface{}{ "id": driver.Id, "companyId": driver.CompanyId, "userId": driver.UserId, "email": driver.Email, "slug": company.Slug, }) c.SetFlash("alertSuccess", "Welcome "+driver.FirstName+" "+driver.LastName) http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/driver", 303) return }
func postCompanyRegister(w http.ResponseWriter, r *http.Request, c *web.Context) { company, ok := service.FindOneCompanyBySlug(c.GetPathVar("slug")) if !ok || !company.Feature { fmt.Fprintf(w, "404 Not Page Found") return } if !service.CanUpdate("", r.FormValue("email")) { c.SetFlash("alertError", "Email already registered") http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/register", 303) return } driver, user := service.NewDriver(r.Form) fmt.Println(driver.DOB) dobTS, err := time.Parse("2006-01-02", driver.DOB) if err != nil { c.SetFlash("alertError", "Error invalid date of birth") http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/register", 303) return } if age := service.GetDriverAge(dobTS.UnixNano()); age < company.AgeRequirement { c.SetFlash("alertError", "Not old enough sucker") user.Active = false driver.Status = 0 driver.CompanyId = "" service.SaveUser(user) service.SaveDriver(driver) http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/register", 303) return } document := service.Document{ Id: util.UUID4(), DocumentId: "100", CompanyId: company.Id, DriverId: driver.Id, Name: "dqf-100", Complete: false, } service.SaveUser(user) service.SaveDriver(driver) service.SaveDocument(document) c.SetFlash("alertSuccess", "Successfully Registered for "+company.Name+". Please Login") http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/login", 303) return }
func postCompanyRegister(w http.ResponseWriter, r *http.Request, c *web.Context) { var company Company if ok := db.GetAs("company", r.FormValue("companyId"), &company); !ok || !company.Feature { fmt.Fprintf(w, "404 Not Page Found") return } email := r.FormValue("email") if !CanUpdate("", email) { c.SetFlash("alertError", "Email already registered") http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/register", 303) return } dob := r.FormValue("dateOfBirth") dobTS, err := time.Parse("2006-01-02", dob) if err != nil { c.SetFlash("alertError", "Error invalid date of birth") http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/register", 303) return } userId := web.UUID4() user := User{ Id: userId, Email: email, Password: email, Role: "driver", Active: true, } driverId := web.UUID4() driver := Driver{ Id: driverId, FirstName: r.FormValue("firstName"), LastName: r.FormValue("lastName"), Street: r.FormValue("street"), City: r.FormValue("city"), State: r.FormValue("state"), Zip: r.FormValue("zip"), Email: r.FormValue("email"), DOB: dob, Status: 1, UserId: userId, CompanyId: company.Id, } if age := GetDriverAge(dobTS.UnixNano()); age < company.AgeRequirement { c.SetFlash("alertError", "Not old enough sucker") user.Active = false driver.Status = 0 driver.CompanyId = "" db.Set("user", userId, user) db.Set("driver", driverId, driver) http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/register", 303) return } documentId := web.UUID4() document := Document{ Id: documentId, DocumentId: "100", CompanyId: company.Id, DriverId: driverId, Name: "dqf-100", Complete: false, } db.Set("document", documentId, document) db.Set("user", userId, user) db.Set("driver", driverId, driver) c.SetFlash("alertSuccess", "Successfully Registered for "+company.Name+". Please Login") http.Redirect(w, r, "/"+c.GetPathVar("slug")+"/login", 303) return }