// RetrieveUser retrieves a user. func RetrieveUser(c *gin.Context) (*model.PublicUser, bool, int64, int, error) { var user model.User var currentUserId int64 var isAuthor bool // var publicUser *model.PublicUser // publicUser.User = &user id := c.Params.ByName("id") if db.ORM.Select(config.UserPublicFields).First(&user, id).RecordNotFound() { return &model.PublicUser{User: &user}, isAuthor, currentUserId, http.StatusNotFound, errors.New("User is not found.") } currentUser, err := CurrentUser(c) if err == nil { currentUserId = currentUser.Id isAuthor = currentUser.Id == user.Id } var likingList model.LikingList likings, currentPage, hasPrev, hasNext, _ := likingRetriever.RetrieveLikings(user) likingList.Likings = likings currentUserlikedCount := db.ORM.Model(&user).Where("id =?", currentUserId).Association("Likings").Count() log.Debugf("Current user like count : %d", currentUserlikedCount) likingMeta.SetLikingPageMeta(&likingList, currentPage, hasPrev, hasNext, user.LikingCount, currentUserlikedCount) user.LikingList = likingList var likedList model.LikedList liked, currentPage, hasPrev, hasNext, _ := likingRetriever.RetrieveLiked(user) likedList.Liked = liked likingMeta.SetLikedPageMeta(&likedList, currentPage, hasPrev, hasNext, user.LikedCount) user.LikedList = likedList return &model.PublicUser{User: &user}, isAuthor, currentUserId, http.StatusOK, nil }
// EmailVerification verifies an email of user. func EmailVerification(c *gin.Context) (int, error) { var user model.User var verifyEmailForm VerifyEmailForm c.BindWith(&verifyEmailForm, binding.Form) log.Debugf("verifyEmailForm.ActivationToken : %s", verifyEmailForm.ActivationToken) if db.ORM.Where(&model.User{ActivationToken: verifyEmailForm.ActivationToken}).First(&user).RecordNotFound() { return http.StatusNotFound, errors.New("User is not found.") } isExpired := timeHelper.IsExpired(user.ActivateUntil) log.Debugf("passwordResetUntil : %s", user.ActivateUntil.UTC()) log.Debugf("expired : %t", isExpired) if isExpired { return http.StatusForbidden, errors.New("token not valid.") } user.ActivationToken = "" user.ActivateUntil = time.Now() user.ActivatedAt = time.Now() user.Activation = true status, err := UpdateUserCore(&user) if err != nil { return status, err } status, err = SetCookie(c, user.Token) return status, err }
// ResetPassword resets a password of user. func ResetPassword(c *gin.Context) (int, error) { var user model.User var passwordResetForm PasswordResetForm c.BindWith(&passwordResetForm, binding.Form) if db.ORM.Where(&model.User{PasswordResetToken: passwordResetForm.PasswordResetToken}).First(&user).RecordNotFound() { return http.StatusNotFound, errors.New("User is not found.") } isExpired := timeHelper.IsExpired(user.PasswordResetUntil) log.Debugf("passwordResetUntil : %s", user.PasswordResetUntil.UTC()) log.Debugf("expired : %t", isExpired) if isExpired { return http.StatusForbidden, errors.New("token not valid.") } newPassword, err := bcrypt.GenerateFromPassword([]byte(passwordResetForm.Password), 10) if err != nil { return http.StatusInternalServerError, errors.New("User is not updated. Password not Generated.") } passwordResetForm.Password = string(newPassword) log.Debugf("user password before : %s ", user.Password) modelHelper.AssignValue(&user, &passwordResetForm) user.PasswordResetToken = "" user.PasswordResetUntil = time.Now() log.Debugf("user password after : %s ", user.Password) status, err := UpdateUserCore(&user) if err != nil { return status, err } status, err = SetCookie(c, user.Token) return status, err }
// SendPasswordResetToken sends a password reset token. func SendPasswordResetToken(c *gin.Context) (int, error) { var user model.User var sendPasswordResetForm SendPasswordResetForm var err error log.Debugf("c.Params : %v", c.Params) c.BindWith(&sendPasswordResetForm, binding.Form) if db.ORM.Where(&model.User{Email: sendPasswordResetForm.Email}).First(&user).RecordNotFound() { return http.StatusNotFound, errors.New("User is not found. Please Check the email.") } user.PasswordResetUntil = timeHelper.TwentyFourHoursLater() user.PasswordResetToken, err = crypto.GenerateRandomToken16() if err != nil { return http.StatusInternalServerError, err } log.Debugf("generated token : %s", user.PasswordResetToken) status, err := UpdateUserCore(&user) if err != nil { return status, err } err = SendEmailPasswordResetToken(user.Email, user.PasswordResetToken, "en-us") if err != nil { return http.StatusInternalServerError, err } return http.StatusOK, nil }
// UpdateUserCore updates a user. (Applying the modifed data of user). func UpdateUserCore(user *model.User) (int, error) { user.Md5 = crypto.GenerateMD5Hash(user.Email) token, err := crypto.GenerateRandomToken32() if err != nil { return http.StatusInternalServerError, errors.New("Token not generated.") } user.Token = token user.TokenExpiration = timeHelper.FewDaysLater(config.AuthTokenExpirationDay) if db.ORM.Save(user).Error != nil { return http.StatusInternalServerError, errors.New("User is not updated.") } return http.StatusOK, nil }
// ActivateUser toggle activation of a user. func ActivateUser(c *gin.Context) (model.User, int, error) { id := c.Params.ByName("id") var user model.User var form ActivateForm c.BindWith(&form, binding.Form) if db.ORM.First(&user, id).RecordNotFound() { return user, http.StatusNotFound, errors.New("User is not found.") } user.Activation = form.Activation if db.ORM.Save(&user).Error != nil { return user, http.StatusInternalServerError, errors.New("User not activated.") } return user, http.StatusOK, nil }
// UpdateUserLikedCount updates user liked count. func UpdateUserLikedCount(c *gin.Context) (int, error) { log.Debug("UpdateUserLikedCount performed") currentUserSrc, err := userService.CurrentUser(c) var currentUser model.User if err != nil { return http.StatusUnauthorized, err } db.ORM.First(¤tUser, currentUserSrc.Id) currentUser.LikedCount = db.ORM.Model(currentUser).Association("Liked").Count() log.Debugf("LikedCount : %d", currentUser.LikedCount) if db.ORM.Save(currentUser).Error != nil { return http.StatusInternalServerError, errors.New("User liked count is not updated.") } return http.StatusOK, nil }
// CreateUserFromForm creates a user from a registration form. func CreateUserFromForm(registrationForm RegistrationForm) (model.User, error) { var user model.User log.Debugf("registrationForm %+v\n", registrationForm) modelHelper.AssignValue(&user, ®istrationForm) user.Md5 = crypto.GenerateMD5Hash(user.Email) token, err := crypto.GenerateRandomToken32() if err != nil { return user, errors.New("Token not generated.") } user.Token = token user.TokenExpiration = timeHelper.FewDaysLater(config.AuthTokenExpirationDay) log.Debugf("user %+v\n", user) if db.ORM.Create(&user).Error != nil { return user, errors.New("User is not created.") } return user, nil }
// UpdateUserLikingCount updates user liking count. func UpdateUserLikingCount(user *model.User) (int, error) { log.Debug("UpdateUserLikingCount performed") user.LikingCount = db.ORM.Model(user).Association("Likings").Count() if db.ORM.Save(user).Error != nil { return http.StatusInternalServerError, errors.New("User liking count is not updated.") } return http.StatusOK, nil }
// SendVerificationToUser sends an email verification token to user. func SendVerificationToUser(user model.User) (int, error) { var status int var err error user.ActivateUntil = timeHelper.TwentyFourHoursLater() user.ActivationToken, err = crypto.GenerateRandomToken32() if err != nil { return http.StatusInternalServerError, err } user.Activation = false log.Debugf("generated token : %s", user.ActivationToken) status, err = UpdateUserCore(&user) if err != nil { return status, err } err = SendEmailVerfication(user.Email, user.ActivationToken, "en-us") if err != nil { return http.StatusInternalServerError, err } return http.StatusOK, err }