func (t *Device) save(response http.ResponseWriter, request *http.Request) { // Load the device with the MAC Address. macAddress := request.FormValue("macAddress") deviceHelper, err := datastore.GetDeviceHelper() if err != nil { http.Error(response, err.Error(), 500) return } MACAddressHardware, err := net.ParseMAC(macAddress) if err != nil { log.Println("Mac Error:" + err.Error()) http.Error(response, err.Error(), 500) return } device, err := deviceHelper.GetDevice(MACAddressHardware) // Check for error when loading device. if err != nil { http.Error(response, err.Error(), 500) return } device.Nickname = request.FormValue("nickname") if request.FormValue("defaultUser") != "" { userHelper, err := datastore.GetUserHelper() if err != nil { log.Println("Error: Getting User Helper :" + err.Error()) http.Error(response, err.Error(), 500) return } user, err := userHelper.GetUser(request.FormValue("defaultUser")) if err != nil { log.Println("Error: Getting User:"******"" { device.DefaultUser = &user } else { device.DefaultUser = nil } } else { device.DefaultUser = nil } err = deviceHelper.SetDevice(&device) // Check for error when saving device. if err != nil { http.Error(response, err.Error(), 500) return } http.Redirect(response, request, t.Base()+"?savecomplete=true", http.StatusMovedPermanently) }
func (t *Device) edit(response http.ResponseWriter, request *http.Request) { // Get the current session user. myuser := t.Sessions.CurrentUser(response, request) // Get the device. macAddress := mux.Vars(request)["macAddress"] deviceDataStoreHelper, err := datastore.GetDeviceHelper() if err != nil { http.Error(response, err.Error(), 500) return } MACAddressHardware, err := net.ParseMAC(macAddress) if err != nil { http.Error(response, err.Error(), 500) return } mydevice, err := deviceDataStoreHelper.GetDevice(MACAddressHardware) // Check for error when loading device. if err != nil { http.Error(response, err.Error(), 500) return } // Get all users. userDataStoreHelper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } allUsers, err := userDataStoreHelper.GetUsers() // Check for error when loading users. if err != nil { http.Error(response, err.Error(), 500) return } // Setup the data structure to pass to the page. data := struct { Action string Device datastore.Device User datastore.User AllUsers []datastore.User }{ "deviceSettings", mydevice, myuser, allUsers, } // Parse the page and execute the template. tpl, err := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/devices/edit.tpl") tpl.Execute(response, data) }
/** * Handles saving a user. */ func (t *User) save(response http.ResponseWriter, request *http.Request) { // Try and load a user with the Username. existingUsername := request.FormValue("idUsername") userDataStoreHelper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } myuser := datastore.User{} if existingUsername != "" { myuser, err = userDataStoreHelper.GetUser(existingUsername) // Check for error when loading user. if err != nil { http.Error(response, err.Error(), 500) return } } // Set the values on the object. myuser.Username = request.FormValue("username") myuser.DisplayName = request.FormValue("name") myuser.SetShortDOB(request.FormValue("dob")) if request.FormValue("isAdmin") == "true" { myuser.IsAdmin = true } else { myuser.IsAdmin = false } err = userDataStoreHelper.SetUser(&myuser) // Check for error when saving user. if err != nil { // There was an error, so report that to the screen. http.Redirect(response, request, t.Base()+"?saveerror=true", http.StatusMovedPermanently) } else { // No error, so report a successful save to the screen and remove the previous version // if username has been edited. // If the new username is different to the existing username, delete the old // version before saving the new version. This prevents duplicates. if existingUsername != "" && myuser.Username != existingUsername { err = userDataStoreHelper.DeleteUser(existingUsername) } http.Redirect(response, request, t.Base()+"?savecomplete=true", http.StatusMovedPermanently) } }
/** * Displays the user access list. */ func (t *UsersCollections) index(response http.ResponseWriter, request *http.Request) { // Get the current session user. myuser := t.Sessions.CurrentUser(response, request) // Get all users. userHelper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } allUsers, err := userHelper.GetUsers() // Check for error when loading users. if err != nil { http.Error(response, err.Error(), 500) return } // Retrieve the savecomplete parameter from the URL and determine if it is true or not. saveCompleteParam := request.URL.Query().Get("savecomplete") saveComplete := saveCompleteParam == "true" // Retrieve the saveerror parameter from the URL and determine if it is true or not. saveErrorParam := request.URL.Query().Get("saveerror") saveError := saveErrorParam == "true" // Setup the data structure to pass to the page. data := struct { Action string User datastore.User AllUsers []datastore.User SaveComplete bool SaveError bool }{ "userAccessSettings", myuser, allUsers, saveComplete, saveError, } // Parse the page and execute the template. tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/access/index.tpl") tpl.Execute(response, data) }
/** * Handles deleting a user. */ func (t *User) remove(response http.ResponseWriter, request *http.Request) { // Try and load a user with the Username. username := mux.Vars(request)["username"] userDataStoreHelper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } err = userDataStoreHelper.DeleteUser(username) // Check for error when loading user. if err != nil { http.Error(response, err.Error(), 500) return } http.Redirect(response, request, t.Base(), http.StatusMovedPermanently) }
/* * Create a User in the Database with the username specified in the URL and the password "administrator". */ func (t *Developer) createadmin(response http.ResponseWriter, request *http.Request) { myuser := datastore.User{} myuser.Username = mux.Vars(request)["username"] myuser.Password = "******" myuser.IsAdmin = true helper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } err = helper.SetUser(&myuser) if err != nil { log.Println("Error:" + err.Error()) http.Error(response, err.Error(), 500) return } return }
/** * Handles editing a user. */ func (t *User) edit(response http.ResponseWriter, request *http.Request) { // Get the current session user. user := t.Sessions.CurrentUser(response, request) // Get the user. username := mux.Vars(request)["username"] userDataStoreHelper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } userToEdit, err := userDataStoreHelper.GetUser(username) // Check for error when loading user. if err != nil { http.Error(response, err.Error(), 500) return } // Setup the data structure to pass to the page. data := struct { Action string User datastore.User UserToEdit *datastore.User EditUser bool NewUser bool }{ "userSettings", user, &userToEdit, true, false, } // Parse the page and execute the template. tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/users/edit.tpl") tpl.Execute(response, data) }
/** * Handles editing a user filter collections object. */ func (t *UsersCollections) edit(response http.ResponseWriter, request *http.Request) { // Get the current session user. myuser := t.Sessions.CurrentUser(response, request) // Get the user filter collections. username := mux.Vars(request)["username"] userFilterCollectionsHelper, err := datastore.GetUserFilterCollectionsHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } userFilterCollections, err := userFilterCollectionsHelper.GetUserFilterCollections(username) // Check for error when loading user filter collection. if err != nil { http.Error(response, err.Error(), 500) return } // Get the user relating to the filter collection. userDataStoreHelper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } filterCollectionsUser, err := userDataStoreHelper.GetUser(username) // Check for error when loading user. if err != nil { http.Error(response, err.Error(), 500) return } // Get all collections to display on the page. filterCollectionHelper, err := datastore.GetFilterCollectionHelper() allCollections, err := filterCollectionHelper.GetFilterCollections() // Check for error when loading collections. if err != nil { http.Error(response, err.Error(), 500) return } // Setup the data structure to pass to the page. data := struct { Action string User datastore.User UserFilterCollections datastore.UserFilterCollections FilterCollectionsUser *datastore.User AllCollections []datastore.FilterCollection }{ "userAccessSettings", myuser, userFilterCollections, &filterCollectionsUser, allCollections, } // Parse the page and execute the template. tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/access/edit.tpl") tpl.Execute(response, data) }
func (t *Root) login(response http.ResponseWriter, request *http.Request) { if request.Method != "POST" { // Retrieve the loginFailed parameter from the URL and determine if it is true or not. loginFailedParam := request.URL.Query().Get("loginFailed") loginFailed := loginFailedParam == "true" // Add the loginFailed value to a data struct to pass to the template. data := struct { LoginFailed bool }{ loginFailed, } tpl, _ := template.ParseFiles("static/main.tpl", "static/login.tpl") tpl.Execute(response, data) } else { request.ParseForm() hash := sha1.New() hash.Write([]byte(request.FormValue("password"))) passwordHash := fmt.Sprintf("%x", hash.Sum(nil)) helper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } myuser, err := helper.GetUser(request.FormValue("username")) if err != nil { log.Println("Error Looking Up User:"******"" && myuser.Username == request.FormValue("username") && myuser.Password == passwordHash { //Setup our session here. t.Sessions.SetCurrentUser(response, request, myuser) remoteIP, _, err := net.SplitHostPort(request.RemoteAddr) //We now need to assosiate the user with the current device :D deviceHelper, err := datastore.GetDeviceHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } device, err := deviceHelper.GetDeviceByIP(net.ParseIP(remoteIP)) if err != nil { log.Println("Error Looking Up Device:" + err.Error()) http.Error(response, err.Error(), 500) return } //Make sure the device is within our network. if device.MACAddress.String() != "" { //Device is within our network (It might have been external) device.CurrentUser = &myuser deviceHelper.SetDevice(&device) } // Login successful, display the user dashboard. t.Sessions.SessionInfo.SaveSession(response, request) http.Redirect(response, request, "/", http.StatusMovedPermanently) return } else { // Login unsuccessful, return to login screen. log.Println("Login Failed:" + myuser.Username) http.Redirect(response, request, "/login?loginFailed=true", http.StatusMovedPermanently) return } } }