-
Notifications
You must be signed in to change notification settings - Fork 12
/
User.go
121 lines (93 loc) · 3.25 KB
/
User.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"errors"
"github.com/LiamDotPro/Go-Multitenancy/helpers"
"github.com/jinzhu/gorm"
)
// User
type User struct {
gorm.Model
Email string
Password string `json:",omitempty"`
AccountType int
FirstName string
LastName string
PhoneNumber string
RecoveryEmail string
}
// Creates a standard user in the database.
// Returns the inserted user id
func createUser(email string, password string, accountType int, connection *gorm.DB) (uint, error) {
// Slice for found users.
var foundUsers []User
if err := connection.Select("email").Where("email = ?", email).Find(&foundUsers).Error; err != nil {
return 0, err
}
// If duplicate email address has been found return.
if len(foundUsers) > 0 {
return 0, errors.New("A user with that email address already exists")
}
// Hash the password so it's not clear text.
// Run outside of the if statements so we can grab the result outside of local scope.
hash, hashErr := helpers.HashPassword([]byte(password))
if hashErr != nil {
return 0, hashErr
}
var user = User{Email: email, Password: hash, AccountType: accountType}
// Run create
if err := connection.Create(&user).Error; err != nil {
// Error Handler
return 0, err
}
// Return newly created user ID
return user.ID, nil
}
// Logs a user in.
func loginUser(email string, password string, connection *gorm.DB) (uint, bool, error) {
// Create local state user
var user User
// Find the user by email, return error if input is malformed.
if err := connection.First(&user, "email = ?", email).Error; err != nil {
return 0, false, err
}
// Now we've found a user send off the hashed password and sent password for decoding.
if result := helpers.CheckPasswordHash(password, user.Password); result != true {
// Passwords do not match
return 0, false, errors.New("passwords did not match")
}
// Checks have bee passed return true
return user.ID, true, nil
}
// Updates a user in the database.
// A separate method is called when updating a company id
func updateUser(id uint, email string, accountType int, firstName string, lastName string, phoneNumber string, recoveryEmail string, connection *gorm.DB) (string, error) {
var user User
// Update the basic user information, anything that was set as nil will not be changed.
if err := connection.Model(&user).Where("id = ?", id).Updates(User{
Email: email,
AccountType: accountType,
FirstName: firstName,
LastName: lastName,
PhoneNumber: phoneNumber,
RecoveryEmail: recoveryEmail,
}).Error; err != nil {
return "", err
}
return "User Information Successfully Updated.", nil
}
// Deletes a user in the database.
func deleteUser(id uint, connection *gorm.DB) (string, error) {
var user User
if err := connection.Where("id = ?", id).Delete(&user).Error; err != nil {
return "An error occurred when trying to delete the user", err
}
return "The user has been successfully deleted", nil
}
// Get a specific user from the database.
func getUser(id uint, connection *gorm.DB) (*User, error) {
var user User
if err := connection.Select("id, created_at, updated_at, deleted_at, email, account_type, company_id, first_name, last_name").Where("id = ? ", id).First(&user).Error; err != nil {
return nil, err
}
return &user, nil
}