-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
117 lines (106 loc) · 2.78 KB
/
repository.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
package credential
import (
"database/sql"
"errors"
"time"
"github.com/mrexmelle/connect-authx/internal/config"
"gorm.io/gorm"
)
type Repository interface {
CreateWithDb(db *gorm.DB, employeeId string, password string) error
ExistsByEmployeeIdAndPassword(employeeId string, password string) (bool, error)
DeleteByEmployeeId(employeeId string) error
UpdatePasswordByEmployeeIdAndPassword(newPassword string, employeeId string, currentPassword string) error
ResetPasswordByEmployeeId(employeeId string) error
}
type RepositoryImpl struct {
ConfigService *config.Service
TableName string
}
func NewRepository(cfg *config.Service) Repository {
return &RepositoryImpl{
ConfigService: cfg,
TableName: "credentials",
}
}
func (r *RepositoryImpl) CreateWithDb(
db *gorm.DB,
employeeId string,
password string,
) error {
result := db.Exec(
"INSERT INTO "+r.TableName+"(employee_id, password_hash, "+
"created_at, updated_at) "+
"VALUES(?, CRYPT(?, GEN_SALT('bf', 8)), NOW(), NOW())",
employeeId,
password,
)
return result.Error
}
func (r *RepositoryImpl) ExistsByEmployeeIdAndPassword(
employeeId string,
password string,
) (bool, error) {
var idResult string
err := r.ConfigService.ReadDb.
Select("employee_id").
Table(r.TableName).
Where("deleted_at IS NULL").
Where("employee_id = ?", employeeId).
Where("password_hash = CRYPT(?, password_hash)", password).
Row().
Scan(&idResult)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
return (idResult == employeeId), err
}
func (r *RepositoryImpl) DeleteByEmployeeId(employeeId string) error {
now := time.Now()
result := r.ConfigService.WriteDb.
Table(r.TableName).
Where("deleted_at IS NULL").
Where("employee_id = ?", employeeId).
Updates(
map[string]interface{}{
"deleted_at": now,
"updated_at": now,
},
)
return result.Error
}
func (r *RepositoryImpl) UpdatePasswordByEmployeeIdAndPassword(
newPassword string,
employeeId string,
currentPassword string,
) error {
result := r.ConfigService.WriteDb.Exec(
"UPDATE "+r.TableName+" SET "+
"password_hash = CRYPT(?, GEN_SALT('bf', 8)), "+
"updated_at = NOW() "+
"WHERE employee_id = ? AND password_hash = CRYPT(?, password_hash) AND deleted_at IS NULL",
newPassword,
employeeId,
currentPassword,
)
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return result.Error
}
func (r *RepositoryImpl) ResetPasswordByEmployeeId(
employeeId string,
) error {
result := r.ConfigService.WriteDb.Exec(
"UPDATE "+r.TableName+" SET "+
"password_hash = CRYPT(?, GEN_SALT('bf', 8)), "+
"updated_at = NOW() "+
"WHERE employee_id = ? AND deleted_at IS NULL",
r.ConfigService.GetDefaultUserPassword(),
employeeId,
)
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return result.Error
}