Refactor creating user into seperate file
This commit is contained in:
parent
1fb7625ca2
commit
7d3718396e
114
pkg/user/user.go
114
pkg/user/user.go
|
@ -23,10 +23,6 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/config"
|
|
||||||
"code.vikunja.io/api/pkg/mail"
|
|
||||||
"code.vikunja.io/api/pkg/metrics"
|
|
||||||
"code.vikunja.io/api/pkg/utils"
|
|
||||||
"code.vikunja.io/web"
|
"code.vikunja.io/web"
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
@ -226,116 +222,6 @@ func GetUserFromClaims(claims jwt.MapClaims) (user *User, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateUser creates a new user and inserts it into the database
|
|
||||||
func CreateUser(user *User) (newUser *User, err error) {
|
|
||||||
|
|
||||||
newUser = user
|
|
||||||
|
|
||||||
if newUser.Issuer == "" {
|
|
||||||
newUser.Issuer = "local"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if we have all needed informations
|
|
||||||
if (newUser.Issuer != "local" && newUser.Subject == "") ||
|
|
||||||
(newUser.Issuer == "local" && (newUser.Password == "" ||
|
|
||||||
newUser.Username == "" ||
|
|
||||||
newUser.Email == "")) {
|
|
||||||
return &User{}, ErrNoUsernamePassword{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the user already existst with that username
|
|
||||||
exists := true
|
|
||||||
_, err = GetUserByUsername(newUser.Username)
|
|
||||||
if err != nil {
|
|
||||||
if IsErrUserDoesNotExist(err) {
|
|
||||||
exists = false
|
|
||||||
} else {
|
|
||||||
return &User{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if exists {
|
|
||||||
return &User{}, ErrUsernameExists{newUser.ID, newUser.Username}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the user already existst with that email
|
|
||||||
exists = true
|
|
||||||
userToCheck := &User{
|
|
||||||
Email: newUser.Email,
|
|
||||||
Issuer: newUser.Issuer,
|
|
||||||
Subject: newUser.Subject,
|
|
||||||
}
|
|
||||||
|
|
||||||
if newUser.Issuer != "local" {
|
|
||||||
userToCheck.Email = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = GetUser(userToCheck)
|
|
||||||
if err != nil {
|
|
||||||
if IsErrUserDoesNotExist(err) {
|
|
||||||
exists = false
|
|
||||||
} else {
|
|
||||||
return &User{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if exists && newUser.Issuer == "local" {
|
|
||||||
return &User{}, ErrUserEmailExists{newUser.ID, newUser.Email}
|
|
||||||
}
|
|
||||||
|
|
||||||
if newUser.Issuer == "local" {
|
|
||||||
// Hash the password
|
|
||||||
newUser.Password, err = hashPassword(user.Password)
|
|
||||||
if err != nil {
|
|
||||||
return &User{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
newUser.IsActive = true
|
|
||||||
if config.MailerEnabled.GetBool() && newUser.Issuer == "local" {
|
|
||||||
// The new user should not be activated until it confirms his mail address
|
|
||||||
newUser.IsActive = false
|
|
||||||
// Generate a confirm token
|
|
||||||
newUser.EmailConfirmToken = utils.MakeRandomString(60)
|
|
||||||
}
|
|
||||||
|
|
||||||
newUser.AvatarProvider = "initials"
|
|
||||||
|
|
||||||
// Insert it
|
|
||||||
_, err = x.Insert(newUser)
|
|
||||||
if err != nil {
|
|
||||||
return &User{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the metrics
|
|
||||||
metrics.UpdateCount(1, metrics.ActiveUsersKey)
|
|
||||||
|
|
||||||
// Get the full new User
|
|
||||||
newUserOut, err := GetUser(newUser)
|
|
||||||
if err != nil {
|
|
||||||
return &User{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dont send a mail if we're testing
|
|
||||||
if !config.MailerEnabled.GetBool() {
|
|
||||||
return newUserOut, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send the user a mail with a link to confirm the mail
|
|
||||||
data := map[string]interface{}{
|
|
||||||
"User": newUserOut,
|
|
||||||
"IsNew": true,
|
|
||||||
}
|
|
||||||
|
|
||||||
mail.SendMailWithTemplate(user.Email, newUserOut.Username+" + Vikunja = <3", "confirm-email", data)
|
|
||||||
|
|
||||||
return newUserOut, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// HashPassword hashes a password
|
|
||||||
func hashPassword(password string) (string, error) {
|
|
||||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 11)
|
|
||||||
return string(bytes), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateUser updates a user
|
// UpdateUser updates a user
|
||||||
func UpdateUser(user *User) (updatedUser *User, err error) {
|
func UpdateUser(user *User) (updatedUser *User, err error) {
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,135 @@
|
||||||
|
// Vikunja is a to-do list application to facilitate your life.
|
||||||
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"code.vikunja.io/api/pkg/config"
|
||||||
|
"code.vikunja.io/api/pkg/mail"
|
||||||
|
"code.vikunja.io/api/pkg/metrics"
|
||||||
|
"code.vikunja.io/api/pkg/utils"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CreateUser creates a new user and inserts it into the database
|
||||||
|
func CreateUser(user *User) (newUser *User, err error) {
|
||||||
|
|
||||||
|
newUser = user
|
||||||
|
|
||||||
|
if newUser.Issuer == "" {
|
||||||
|
newUser.Issuer = "local"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we have all needed informations
|
||||||
|
if (newUser.Issuer != "local" && newUser.Subject == "") ||
|
||||||
|
(newUser.Issuer == "local" && (newUser.Password == "" ||
|
||||||
|
newUser.Username == "" ||
|
||||||
|
newUser.Email == "")) {
|
||||||
|
return &User{}, ErrNoUsernamePassword{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the user already existst with that username
|
||||||
|
exists := true
|
||||||
|
_, err = GetUserByUsername(newUser.Username)
|
||||||
|
if err != nil {
|
||||||
|
if IsErrUserDoesNotExist(err) {
|
||||||
|
exists = false
|
||||||
|
} else {
|
||||||
|
return &User{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
return &User{}, ErrUsernameExists{newUser.ID, newUser.Username}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the user already existst with that email
|
||||||
|
exists = true
|
||||||
|
userToCheck := &User{
|
||||||
|
Email: newUser.Email,
|
||||||
|
Issuer: newUser.Issuer,
|
||||||
|
Subject: newUser.Subject,
|
||||||
|
}
|
||||||
|
|
||||||
|
if newUser.Issuer != "local" {
|
||||||
|
userToCheck.Email = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = GetUser(userToCheck)
|
||||||
|
if err != nil {
|
||||||
|
if IsErrUserDoesNotExist(err) {
|
||||||
|
exists = false
|
||||||
|
} else {
|
||||||
|
return &User{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if exists && newUser.Issuer == "local" {
|
||||||
|
return &User{}, ErrUserEmailExists{newUser.ID, newUser.Email}
|
||||||
|
}
|
||||||
|
|
||||||
|
if newUser.Issuer == "local" {
|
||||||
|
// Hash the password
|
||||||
|
newUser.Password, err = hashPassword(user.Password)
|
||||||
|
if err != nil {
|
||||||
|
return &User{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newUser.IsActive = true
|
||||||
|
if config.MailerEnabled.GetBool() && newUser.Issuer == "local" {
|
||||||
|
// The new user should not be activated until it confirms his mail address
|
||||||
|
newUser.IsActive = false
|
||||||
|
// Generate a confirm token
|
||||||
|
newUser.EmailConfirmToken = utils.MakeRandomString(60)
|
||||||
|
}
|
||||||
|
|
||||||
|
newUser.AvatarProvider = "initials"
|
||||||
|
|
||||||
|
// Insert it
|
||||||
|
_, err = x.Insert(newUser)
|
||||||
|
if err != nil {
|
||||||
|
return &User{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the metrics
|
||||||
|
metrics.UpdateCount(1, metrics.ActiveUsersKey)
|
||||||
|
|
||||||
|
// Get the full new User
|
||||||
|
newUserOut, err := GetUser(newUser)
|
||||||
|
if err != nil {
|
||||||
|
return &User{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dont send a mail if we're testing
|
||||||
|
if !config.MailerEnabled.GetBool() {
|
||||||
|
return newUserOut, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the user a mail with a link to confirm the mail
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"User": newUserOut,
|
||||||
|
"IsNew": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
mail.SendMailWithTemplate(user.Email, newUserOut.Username+" + Vikunja = <3", "confirm-email", data)
|
||||||
|
|
||||||
|
return newUserOut, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// HashPassword hashes a password
|
||||||
|
func hashPassword(password string) (string, error) {
|
||||||
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 11)
|
||||||
|
return string(bytes), err
|
||||||
|
}
|
Loading…
Reference in New Issue