本文整理匯總了Golang中github.com/dorajistyle/goyangi/model.User類的典型用法代碼示例。如果您正苦於以下問題:Golang User類的具體用法?Golang User怎麽用?Golang User使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了User類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: RetrieveUser
// 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
}
示例2: EmailVerification
// 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
}
示例3: ResetPassword
// 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
}
示例4: SendPasswordResetToken
// 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
}
示例5: UpdateUserCore
// 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
}
示例6: ActivateUser
// 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
}
示例7: UpdateUserLikedCount
// 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
}
示例8: CreateUserFromForm
// 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
}
示例9: UpdateUserLikingCount
// 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
}
示例10: SendVerificationToUser
// 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
}