forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
115 lines (102 loc) · 3.09 KB
/
users.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
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"fmt"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"golang.org/x/crypto/bcrypt"
"time"
)
type User struct {
*types.User
}
// ReturnUser returns *core.User based off a *types.User
func ReturnUser(u *types.User) *User {
return &User{u}
}
// CountUsers returns the amount of users
func CountUsers() int64 {
var amount int64
usersDB().Count(&amount)
return amount
}
// SelectUser returns the User based on the User's ID.
func SelectUser(id int64) (*User, error) {
var user User
err := usersDB().Where("id = ?", id).First(&user)
return &user, err.Error
}
// SelectUsername returns the User based on the User's username
func SelectUsername(username string) (*User, error) {
var user User
res := usersDB().Where("username = ?", username)
err := res.First(&user)
return &user, err.Error
}
// Delete will remove the User record from the database
func (u *User) Delete() error {
return usersDB().Delete(u).Error
}
// Update will update the User's record in database
func (u *User) Update() error {
u.ApiKey = utils.NewSHA1Hash(5)
u.ApiSecret = utils.NewSHA1Hash(10)
return usersDB().Update(u).Error
}
// Create will insert a new User into the database
func (u *User) Create() (int64, error) {
u.CreatedAt = time.Now()
u.Password = utils.HashPassword(u.Password)
u.ApiKey = utils.NewSHA1Hash(5)
u.ApiSecret = utils.NewSHA1Hash(10)
db := usersDB().Create(u)
if db.Error != nil {
return 0, db.Error
}
if u.Id == 0 {
utils.Log(3, fmt.Sprintf("Failed to create User %v. %v", u.Username, db.Error))
return 0, db.Error
}
return u.Id, db.Error
}
// SelectAllUsers returns all users
func SelectAllUsers() ([]*User, error) {
var users []*User
db := usersDB().Find(&users)
if db.Error != nil {
utils.Log(3, fmt.Sprintf("Failed to load all users. %v", db.Error))
return nil, db.Error
}
return users, db.Error
}
// AuthUser will return the User and a boolean if authentication was correct.
// AuthUser accepts username, and password as a string
func AuthUser(username, password string) (*User, bool) {
user, err := SelectUsername(username)
if err != nil {
utils.Log(2, err)
return nil, false
}
if CheckHash(password, user.Password) {
return user, true
}
return nil, false
}
// CheckHash returns true if the password matches with a hashed bcrypt password
func CheckHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}