45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package services
|
|
|
|
import (
|
|
"gitea.zjmud.xyz/phyer/rbac/models"
|
|
"gitea.zjmud.xyz/phyer/rbac/repositories"
|
|
// "gitea.zjmud.xyz/phyer/rbac/utils"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func GetAllUsers() ([]models.User, error) {
|
|
return repositories.GetAllUsers()
|
|
}
|
|
|
|
func GetUserByID(id string) (*models.User, error) {
|
|
return repositories.GetUserByID(id)
|
|
}
|
|
|
|
func UpdateUser(id string, updateData map[string]interface{}) (*models.User, error) {
|
|
if password, ok := updateData["password"]; ok {
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password.(string)), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
updateData["password"] = string(hashedPassword)
|
|
}
|
|
return repositories.UpdateUser(id, updateData)
|
|
}
|
|
|
|
func DeleteUser(id string) error {
|
|
return repositories.DeleteUser(id)
|
|
}
|
|
|
|
func RegisterUser(username, password, email string) (*models.User, error) {
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
user := &models.User{
|
|
Username: username,
|
|
Password: string(hashedPassword),
|
|
Email: email,
|
|
}
|
|
return repositories.CreateUser(user)
|
|
}
|