コード例 #1
0
ファイル: auth.go プロジェクト: injuvproject/backofficeinjuv
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

}
コード例 #2
0
func UpdateActivity(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)
	idActivity, _ := strconv.Atoi(utils.GetAndTrim(r, "id"))
	estado := utils.GetAndTrim(r, "estado")

	err := activity.UpdateStatusActivity(db, idActivity, estado)
	if err != nil {
		panic(err)
	}

	template.JSON(w, http.StatusOK, bnd)

}
コード例 #3
0
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)
}
コード例 #4
0
func PostEditAccount(c web.C, w http.ResponseWriter, r *http.Request) {
	bnd := binding.GetDefault(r)
	db := c.Env["mysql"].(*sqlx.DB)
	template := c.Env["render"].(*render.Render)
	id, _ := strconv.Atoi(c.URLParams["uid"])
	cookie, _ := r.Cookie("injuv_auth")
	claims, _ := security.Decode(cookie.Value)

	firstName := utils.GetAndTrim(r, "firstname")
	lastName := utils.GetAndTrim(r, "lastname")
	userName := utils.GetAndTrim(r, "userName")
	email := utils.GetAndTrim(r, "email")
	password := utils.GetAndTrim(r, "password")
	rePassword := utils.GetAndTrim(r, "repassword")
	userAdmin, _ := strconv.ParseBool(utils.GetAndTrim(r, "admin"))

	fmt.Println("%t\n\n", userAdmin)

	if firstName == "" {
		bnd["Error"] = strFirstNameEmpty
	}

	if lastName == "" {
		bnd["Error"] = strLastNameEmpty
	}

	if userName == "" {
		bnd["Error"] = strUserNameEmpty
	}

	if password == "" {
		bnd["Error"] = strPasswordEmpty
	}
	if rePassword == "" {
		bnd["Error"] = strPasswordEmpty
	}
	if password != rePassword {
		bnd["Error"] = strErrorPassword
	}

	if bnd["Error"] != nil {
		template.HTML(w, http.StatusOK, "panel/edit", bnd)
		return
	}

	newUser := &user.User{
		ID:         id,
		FirstName:  firstName,
		LastName:   lastName,
		UserName:   userName,
		Email:      email,
		Password:   password,
		Admin:      userAdmin,
		SignupDate: time.Now(),
	}
	ids := claims["id"].(float64)
	bnd["ADMIN"] = claims["guuid"]
	bnd["User"] = newUser

	newUser.Save(db)

	if claims["guuid"].(bool) == true {
		http.Redirect(w, r, fmt.Sprintf(URLPanelUserByID, int(ids), id, getresponse.ComparableValue), http.StatusFound)
		return
	}

	http.Redirect(w, r, fmt.Sprintf(URLProfileUserByID, int(ids), id, getresponse.ComparableValue), http.StatusFound)
	return

}