func (controller *contactsController) editGet(rw http.ResponseWriter, req *http.Request) (int, error) { vars := mux.Vars(req) contactID, err := strconv.ParseInt(vars["contactID"], 10, 64) if err != nil { return http.StatusInternalServerError, err } // Get the contact to edit contact := new(database.Contact) err = contact.GetContact(controller.DB, contactID) if err != nil { return http.StatusInternalServerError, err } isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer) contactEdit := new(viewmodels.ContactsEditViewModel) contactEdit.Name = contact.Name contactEdit.ContactID = contact.ContactID contactEdit.EmailAddress = contact.EmailAddress contactEdit.SmsNumber = contact.SmsNumber contactEdit.EmailActive = contact.EmailActive contactEdit.SmsActive = contact.SmsActive contactEdit.SelectedSites, err = getContactSiteIDs(controller, contact) if err != nil { return http.StatusInternalServerError, err } sites, errGet := getAllSites(controller) if errGet != nil { return http.StatusInternalServerError, errGet } vm := viewmodels.EditContactViewModel(contactEdit, sites, isAuthenticated, user, make(map[string]string)) vm.CsrfField = csrf.TemplateField(req) return http.StatusOK, controller.editTemplate.Execute(rw, vm) }
func (controller *contactsController) deletePost(rw http.ResponseWriter, req *http.Request) (int, error) { err := req.ParseForm() if err != nil { return http.StatusInternalServerError, err } decoder := schema.NewDecoder() // Ignore unknown keys to prevent errors from the CSRF token. decoder.IgnoreUnknownKeys(true) formContact := new(viewmodels.ContactsEditViewModel) err = decoder.Decode(formContact, req.PostForm) if err != nil { return http.StatusInternalServerError, err } valErrors := validateContactForm(formContact) if len(valErrors) > 0 { isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer) var noSites = []database.Site{} vm := viewmodels.EditContactViewModel(formContact, noSites, isAuthenticated, user, valErrors) vm.CsrfField = csrf.TemplateField(req) return http.StatusOK, controller.deleteTemplate.Execute(rw, vm) } // Get the contact to delete contact := new(database.Contact) err = contact.GetContact(controller.DB, formContact.ContactID) if err != nil { return http.StatusInternalServerError, err } mapContacts(contact, formContact) err = contact.DeleteContact(controller.DB) if err != nil { return http.StatusInternalServerError, err } // Refresh the pinger with the changes. // TODO: Check whether this contact is associated with any active site first. err = controller.pinger.UpdateSiteSettings() if err != nil { return http.StatusInternalServerError, err } http.Redirect(rw, req, "/settings/contacts", http.StatusSeeOther) return http.StatusSeeOther, nil }
func (controller *contactsController) deleteGet(rw http.ResponseWriter, req *http.Request) (int, error) { vars := mux.Vars(req) contactID, err := strconv.ParseInt(vars["contactID"], 10, 64) if err != nil { return http.StatusInternalServerError, err } // Get the contact to edit contact := new(database.Contact) err = contact.GetContact(controller.DB, contactID) if err != nil { return http.StatusInternalServerError, err } isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer) contactDelete := new(viewmodels.ContactsEditViewModel) contactDelete.Name = contact.Name contactDelete.ContactID = contact.ContactID contactDelete.EmailAddress = contact.EmailAddress var noSites = []database.Site{} vm := viewmodels.EditContactViewModel(contactDelete, noSites, isAuthenticated, user, make(map[string]string)) vm.CsrfField = csrf.TemplateField(req) return http.StatusOK, controller.deleteTemplate.Execute(rw, vm) }
func (controller *contactsController) editPost(rw http.ResponseWriter, req *http.Request) (int, error) { err := req.ParseForm() if err != nil { return http.StatusInternalServerError, err } decoder := schema.NewDecoder() // Ignore unknown keys to prevent errors from the CSRF token. decoder.IgnoreUnknownKeys(true) formContact := new(viewmodels.ContactsEditViewModel) err = decoder.Decode(formContact, req.PostForm) if err != nil { return http.StatusInternalServerError, err } valErrors := validateContactForm(formContact) if len(valErrors) > 0 { isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer) sites, errGet := getAllSites(controller) if errGet != nil { return http.StatusInternalServerError, err } vm := viewmodels.EditContactViewModel(formContact, sites, isAuthenticated, user, valErrors) vm.CsrfField = csrf.TemplateField(req) return http.StatusOK, controller.editTemplate.Execute(rw, vm) } // Get the contact to update contact := new(database.Contact) err = contact.GetContact(controller.DB, formContact.ContactID) if err != nil { return http.StatusInternalServerError, err } mapContacts(contact, formContact) err = contact.UpdateContact(controller.DB) if err != nil { return http.StatusInternalServerError, err } contactSiteIDS, getErr := getContactSiteIDs(controller, contact) if getErr != nil { return http.StatusInternalServerError, getErr } //Loop selected ones first and if it's not already in the site then add it. for _, siteSelID := range formContact.SelectedSites { if !int64InSlice(int64(siteSelID), contactSiteIDS) { err = addContactToSite(controller, contact.ContactID, siteSelID) if err != nil { return http.StatusInternalServerError, err } } } // Loop existing contact sites and if it's not in the selected items then remove it. for _, contactSiteID := range contactSiteIDS { if !int64InSlice(int64(contactSiteID), formContact.SelectedSites) { err = removeContactFromSite(controller, contact.ContactID, contactSiteID) if err != nil { return http.StatusInternalServerError, err } } } // Refresh the pinger with the changes. // TODO: Check whether this contact is associated with any active site first. err = controller.pinger.UpdateSiteSettings() if err != nil { return http.StatusInternalServerError, err } http.Redirect(rw, req, "/settings/contacts", http.StatusSeeOther) return http.StatusSeeOther, nil }