func GetAccount(c web.C, w http.ResponseWriter, r *http.Request) { template := c.Env["render"].(*render.Render) db := c.Env["mysql"].(*sqlx.DB) bnd := binding.GetDefault(r) id, _ := strconv.Atoi(c.URLParams["uid"]) cookie, _ := r.Cookie("injuv_auth") claims, _ := security.Decode(cookie.Value) u, err := user.Get(db, id) if err != nil { panic(err) } if r.FormValue("edit") == getresponse.ComparableValue { bnd["Success"] = strEditSuccess } bnd["User"] = u bnd["ID"] = claims["id"] bnd["ADMIN"] = claims["guuid"] template.HTML(w, http.StatusOK, "panel/show", bnd, render.HTMLOptions{ Layout: "panel/layout", }) }
func PostLogin(c web.C, w http.ResponseWriter, r *http.Request) { db := c.Env["mysql"].(*sqlx.DB) logInEmail := utils.GetAndTrim(r, "email") logInPassword := utils.GetAndTrim(r, "password") fmt.Printf("%s\n\n", logInEmail) newUser := &user.User{ Email: logInEmail, } exist := newUser.EmailExist(db) if exist == false { http.Redirect(w, r, fmt.Sprintf("/login.html?false=%s", getresponse.LogInErrEmalOrPassword), http.StatusFound) return } err := newUser.LoadID(db) if err != nil { http.Redirect(w, r, fmt.Sprintf("/login.html?false=%s", getresponse.LogInErrEmalOrPassword), http.StatusFound) return } u, err := user.Get(db, newUser.ID) if err != nil { panic(err) } if logInEmail == u.Email && logInPassword == u.Password { claims := map[string]interface{}{ config.ConstID: u.ID, config.ConstName: u.UserName, config.ConstAdmin: u.Admin, } // Esto tiene el contenido de la cookie contenidoCookie := security.Encode(claims) // Expiración del próximo año fechaExpiracion := time.Now().AddDate(0, 0, 1) // Creo la cookie cookie := http.Cookie{ Name: "injuv_auth", Value: contenidoCookie, Path: "/", Expires: fechaExpiracion, RawExpires: fechaExpiracion.Format(time.UnixDate), } http.SetCookie(w, &cookie) http.Redirect(w, r, fmt.Sprintf("/panel/%d.html", newUser.ID), http.StatusFound) return } http.Redirect(w, r, fmt.Sprintf("/login.html?false=%s", getresponse.LogInErrEmalOrPassword), http.StatusFound) return }
func LoadUser(c *web.C, h http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { db := c.Env["mysql"].(*sqlx.DB) userID := c.Env[config.ConstID].(float64) id, _ := strconv.Atoi(c.URLParams["id"]) u, err := user.Get(db, id) if err != nil { http.Redirect(w, r, formatURLlogin, http.StatusFound) return } if int(userID) != u.ID { http.Redirect(w, r, formatURLlogin, http.StatusFound) return } c.Env[config.ConstID] = user.User{ ID: u.ID, FirstName: u.FirstName, LastName: u.LastName, Email: u.Email, } h.ServeHTTP(w, r) return } return http.HandlerFunc(fn) }
func NewActivitie(c web.C, w http.ResponseWriter, r *http.Request) { template := c.Env["render"].(*render.Render) db := c.Env["mysql"].(*sqlx.DB) bnd := binding.GetDefault(r) name := utils.GetAndTrim(r, "name") description := utils.GetAndTrim(r, "description") dateExpire := utils.GetAndTrim(r, "fechaExpiracion") recursos := utils.GetAndTrim(r, "recurso") estado := utils.GetAndTrim(r, "estado") pioridad := utils.GetAndTrim(r, "pioridad") // adjunto := utils.GetAndTrim(r, "adjunto") fmt.Println("\n\n\n%s", dateExpire) userid, _ := strconv.Atoi(recursos) if name == "" { bnd["Error"] = StrUserEmpty } if description == "" { bnd["Error"] = StrDescriptionEmpty } activityNew := &activity.Activity{ Title: name, Description: description, ExpireDate: dateExpire, User: userid, Estate: estado, Adjuntos: 0, Pioridad: pioridad, } activityNew.Create(db) userAdd, _ := user.Get(db, userid) //TO_DO LOAD EMAIL FOR USER=user emailSender := fmt.Sprintf(strSenderFormat, "injuv araucania", config.SendTo) emailFullSubject := fmt.Sprintf("%s", "Notificacion nueva tarea") emailMessage := fmt.Sprintf("Se a creado una nueva Tarea %s fecha termino tarea %s", name, dateExpire) emailFullName := fmt.Sprintf("Asignada a %s %s", userAdd.FirstName, userAdd.LastName) emailBody := fmt.Sprintf( strEmailFormat, emailMessage, emailFullName, ) err := sendEmail(emailBody, emailSender, emailFullSubject, userAdd.Email) if err == errBadMail { bnd["Error"] = errStrUnableToSend template.JSON(w, http.StatusOK, bnd) return } template.JSON(w, http.StatusOK, bnd) }