Example #1
0
// OauthGithub link connection and user.
func OauthGithub(c *gin.Context) (int, error) {
	var authResponse oauth2.AuthResponse
	var oauthUser OauthUser
	c.BindWith(&authResponse, binding.Form)
	log.Debugf("oauthRedirect form: %v", authResponse)
	response, token, err := oauth2.OauthRequest(github.RequestURL, github.Config, authResponse)
	if err != nil {
		log.Error("get response error")
		return http.StatusInternalServerError, err
	}
	githubUser, err := SetGithubUser(response)
	if err != nil {
		log.Error("SetGithubUser error")
		return http.StatusInternalServerError, err
	}
	modelHelper.AssignValue(&oauthUser, githubUser)
	oauthUser.Id = strconv.Itoa(githubUser.UserId)
	log.Debugf("\noauthUser item : %v", oauthUser)
	status, err := LoginOrCreateOauthUser(c, &oauthUser, github.ProviderId, token)
	if err != nil {
		log.Errorf("LoginOrCreateOauthUser error: %d", status)
		return status, err
	}
	return http.StatusSeeOther, nil
}
Example #2
0
// 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
}
Example #3
0
// CreateLocation creates a location.
func CreateLocation(c *gin.Context) (model.Location, int, error) {
	var form LocationForm
	c.BindWith(&form, binding.Form)
	log.Debugf("struct map : %s\n", form)
	user, _ := userService.CurrentUser(c)
	form.UserId = user.Id
	location := model.Location{}
	modelHelper.AssignValue(&location, &form)
	if db.ORM.Create(&location).Error != nil {
		return location, http.StatusInternalServerError, errors.New("User is not created.")
	}
	return location, http.StatusCreated, nil
}
Example #4
0
// CreateArticle creates an article.
func CreateArticle(c *gin.Context) (model.Article, int, error) {
	var form ArticleForm
	c.BindWith(&form, binding.Form)
	log.Debugf("struct map : %s\n", form)
	user, _ := userService.CurrentUser(c)
	form.UserId = user.Id
	article := model.Article{}
	modelHelper.AssignValue(&article, &form)
	if db.ORM.Create(&article).Error != nil {
		return article, http.StatusInternalServerError, errors.New("User is not created.")
	}
	return article, http.StatusCreated, nil
}
Example #5
0
// UpdateUser updates a user.
func UpdateUser(c *gin.Context) (*model.User, int, error) {
	id := c.Params.ByName("id")
	var user model.User
	if db.ORM.First(&user, id).RecordNotFound() {
		return &user, http.StatusNotFound, errors.New("User is not found.")
	}
	switch c.Request.FormValue("type") {
	case "password":
		var passwordForm PasswordForm
		c.BindWith(&passwordForm, binding.Form)
		log.Debugf("form %+v\n", passwordForm)
		err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(passwordForm.CurrentPassword))
		if err != nil {
			log.Error("Password Incorrect.")
			return &user, http.StatusInternalServerError, errors.New("User is not updated. Password Incorrect.")
		} else {
			newPassword, err := bcrypt.GenerateFromPassword([]byte(passwordForm.Password), 10)
			if err != nil {
				return &user, http.StatusInternalServerError, errors.New("User is not updated. Password not Generated.")
			} else {
				passwordForm.Password = string(newPassword)
				modelHelper.AssignValue(&user, &passwordForm)
			}
		}
	default:
		var form UserForm
		c.BindWith(&form, binding.Form)
		log.Debugf("form %+v\n", form)
		modelHelper.AssignValue(&user, &form)
	}

	log.Debugf("params %+v\n", c.Params)
	status, err := UpdateUserCore(&user)
	if err != nil {
		return &user, status, err
	}
	status, err = SetCookie(c, user.Token)
	return &user, status, err
}
Example #6
0
File: file.go Project: tka/goyangi
// CreateFile creates a file.
func CreateFile(c *gin.Context) (int, error) {
	var form FileForm
	c.BindWith(&form, binding.Form)
	log.Debugf("CreateFile form : %v", form)
	// err = json.Unmarshal(formDataBuffer[:n], &articles)
	// file := model.File{Name: form.Name, Size: form.Size}
	user, _ := userService.CurrentUser(c)
	form.UserId = user.Id
	file := model.File{}
	modelHelper.AssignValue(&file, &form)
	if db.ORM.Create(&file).Error != nil {
		return http.StatusInternalServerError, errors.New("File is not created.")
	}
	return http.StatusCreated, nil
}
Example #7
0
// CreateComment creates a comment.
func CreateComment(c *gin.Context, item interface{}) (int, error) {
	var form CreateCommentForm
	var comment model.Comment
	c.BindWith(&form, binding.Form)
	log.Debugf("comment_form : %v", form)
	status, err := userPermission.CurrentUserIdentical(c, form.UserId)
	if err != nil {
		return status, err
	}
	if db.ORM.First(item, form.ParentId).RecordNotFound() {
		return http.StatusNotFound, errors.New("Item is not found.")
	}
	modelHelper.AssignValue(&comment, &form)
	db.ORM.Model(item).Association("Comments").Append(comment)
	return http.StatusOK, nil
}
Example #8
0
// 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, &registrationForm)
	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
}
Example #9
0
// OauthFacebook link connection and user.
func OauthFacebook(c *gin.Context) (int, error) {
	var authResponse oauth2.AuthResponse
	var oauthUser OauthUser
	c.BindWith(&authResponse, binding.Form)
	log.Debugf("oauthRedirect form: %v", authResponse)
	response, token, err := oauth2.OauthRequest(facebook.RequestURL, facebook.Config, authResponse)
	if err != nil {
		return http.StatusInternalServerError, err
	}
	facebookUser, err := SetFacebookUser(response)
	modelHelper.AssignValue(&oauthUser, facebookUser)
	log.Debugf("\noauthUser item : %v", oauthUser)
	if err != nil {
		return http.StatusInternalServerError, err
	}
	status, err := LoginOrCreateOauthUser(c, &oauthUser, facebook.ProviderId, token)
	if err != nil {
		return status, err
	}
	return http.StatusSeeOther, nil
}
Example #10
0
// OauthGoogle link connection and user.
func OauthGoogle(c *gin.Context) (int, error) {
	log.Debugf("c.Request.URL : %s", c.Request.URL)
	var authResponse oauth2.AuthResponse
	var oauthUser OauthUser
	c.BindWith(&authResponse, binding.Form)
	log.Debugf("oauthRedirect form: %v", authResponse)
	response, token, err := oauth2.OauthRequest(google.RequestURL, google.Config, authResponse)
	if err != nil {
		return http.StatusInternalServerError, err
	}
	googleUser, err := SetGoogleUser(response)
	if err != nil {
		return http.StatusInternalServerError, err
	}
	modelHelper.AssignValue(&oauthUser, googleUser)
	oauthUser.Email = googleUser.Emails[0].Value
	oauthUser.ImageUrl = googleUser.Image.URL
	status, err := LoginOrCreateOauthUser(c, &oauthUser, google.ProviderId, token)
	if err != nil {
		return status, err
	}
	return http.StatusSeeOther, nil
}
Example #11
0
// CreateOauthUser creates oauth user.
func CreateOauthUser(c *gin.Context, oauthUser *OauthUser, connection *model.Connection) (model.User, int, error) {
	var registrationForm userService.RegistrationForm
	var user model.User
	modelHelper.AssignValue(&registrationForm, oauthUser)
	registrationForm.Password = random.GenerateRandomString(12)
	if len(registrationForm.Username) == 0 {
		if len(registrationForm.Email) > 0 {
			registrationForm.Username = strings.Split(registrationForm.Email, "@")[0]
		} else {
			registrationForm.Username = "******"
		}
	}
	registrationForm.Username = userService.SuggestUsername(registrationForm.Username)
	generatedPassword, err := bcrypt.GenerateFromPassword([]byte(registrationForm.Password), 10)
	if err != nil {
		return user, http.StatusInternalServerError, errors.New("Password not generated.")
	}
	registrationForm.Password = string(generatedPassword)
	currentUser, err := userService.CurrentUser(c)
	if err != nil {
		log.Errorf("currentUser : %v", currentUser)
		user, err = userService.CreateUserFromForm(registrationForm)
		if err != nil {
			return user, http.StatusInternalServerError, errors.New("User is not created.")
		}
	} else {
		if db.ORM.Where("id = ?", currentUser.Id).First(&user).RecordNotFound() {
			return user, http.StatusInternalServerError, errors.New("User is not found.")
		}
	}
	db.ORM.Model(&user).Association("Connections").Append(connection)
	if db.ORM.Save(&user).Error != nil {
		return user, http.StatusInternalServerError, errors.New("Connection not appended to user.")
	}
	return user, http.StatusOK, nil
}