fix: user reg
ci-agent / build (push) Failing after 3m0s

This commit is contained in:
d3m0k1d
2026-04-04 18:49:05 +03:00
parent 398c688fed
commit a71fde67e4
9 changed files with 1647 additions and 362 deletions
+8
View File
@@ -25,6 +25,14 @@ type TokenCreate struct {
IsActive bool `json:"is_active"`
}
// UserRegister is the request body for public user registration (all permissions false).
type UserRegister struct {
Name string `json:"name" binding:"required"`
LastName string `json:"last_name" binding:"required"`
Login string `json:"login" binding:"required"`
Password string `json:"password" binding:"required"`
}
// TokenUpdate is the request body for updating an existing user.
type TokenUpdate struct {
Name string `json:"name"`
+28
View File
@@ -64,6 +64,34 @@ func (r *Repository) CreateToken(tc TokenCreate) (string, error) {
return strconv.FormatInt(id, 10), nil
}
// RegisterUser inserts a new user with all permissions set to false and is_active=false.
func (r *Repository) RegisterUser(ur UserRegister) (string, error) {
hashed, err := bcrypt.GenerateFromPassword([]byte(ur.Password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
token, err := utils.RandomToken()
if err != nil {
return "", err
}
result, err := r.DB.Exec(
`INSERT INTO tokens (name, last_name, login, password, token, permission_view, permission_manage_agent, permission_admin, is_active)
VALUES (?, ?, ?, ?, ?, 0, 0, 0, 0)`,
ur.Name, ur.LastName, ur.Login, string(hashed), token,
)
if err != nil {
return "", err
}
id, err := result.LastInsertId()
if err != nil {
return "", err
}
return strconv.FormatInt(id, 10), nil
}
// Login authenticates by login/password, generates a new token, and returns LoginResponse.
func (r *Repository) Login(login, password string) (*LoginResponse, error) {
var t Tokens