2021-05-09 22:08:10 +02:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2021-07-13 08:27:22 +02:00
|
|
|
"fmt"
|
2024-10-26 14:56:17 +02:00
|
|
|
"github.com/semaphoreui/semaphore/db"
|
2021-05-09 22:08:10 +02:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *BoltDb) CreateUserWithoutPassword(user db.User) (newUser db.User, err error) {
|
|
|
|
|
2021-12-18 14:16:34 +01:00
|
|
|
err = db.ValidateUser(user)
|
2021-05-09 22:08:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:27:22 +02:00
|
|
|
_, err = d.GetUserByLoginOrEmail(user.Username, user.Email)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("user already exists")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != db.ErrNotFound {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
user.Password = ""
|
|
|
|
user.Created = db.GetParsedTime(time.Now())
|
|
|
|
|
2021-05-13 21:45:54 +02:00
|
|
|
usr, err := d.createObject(0, db.UserProps, user)
|
2021-05-09 22:08:10 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
newUser = usr.(db.User)
|
2021-05-09 22:08:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) CreateUser(user db.UserWithPwd) (newUser db.User, err error) {
|
|
|
|
|
2021-12-18 14:16:34 +01:00
|
|
|
err = db.ValidateUser(user.User)
|
2021-05-09 22:08:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:27:22 +02:00
|
|
|
_, err = d.GetUserByLoginOrEmail(user.Username, user.Email)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("user already exists")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != db.ErrNotFound {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
pwdHash, err := bcrypt.GenerateFromPassword([]byte(user.Pwd), 11)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Password = string(pwdHash)
|
|
|
|
user.Created = db.GetParsedTime(time.Now())
|
|
|
|
|
2021-05-13 21:45:54 +02:00
|
|
|
usr, err := d.createObject(0, db.UserProps, user)
|
2021-05-09 22:08:10 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:45:54 +02:00
|
|
|
newUser = usr.(db.UserWithPwd).User
|
2021-05-09 22:08:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) DeleteUser(userID int) error {
|
2021-05-16 23:44:42 +02:00
|
|
|
projects, err := d.GetProjects(userID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add transaction
|
|
|
|
|
|
|
|
for _, p := range projects {
|
|
|
|
_ = d.DeleteProjectUser(p.ID, userID)
|
|
|
|
}
|
|
|
|
|
2022-01-31 23:16:00 +01:00
|
|
|
return d.deleteObject(0, db.UserProps, intObjectID(userID), nil)
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) UpdateUser(user db.UserWithPwd) error {
|
2021-05-13 00:56:31 +02:00
|
|
|
var password string
|
2021-05-09 22:08:10 +02:00
|
|
|
|
|
|
|
if user.Pwd != "" {
|
|
|
|
var pwdHash []byte
|
2021-05-13 00:56:31 +02:00
|
|
|
pwdHash, err := bcrypt.GenerateFromPassword([]byte(user.Pwd), 11)
|
2021-05-09 22:08:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-13 00:56:31 +02:00
|
|
|
password = string(pwdHash)
|
2021-05-09 22:08:10 +02:00
|
|
|
} else {
|
2021-05-13 00:56:31 +02:00
|
|
|
oldUser, err := d.GetUser(user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
password = oldUser.Password
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
user.Password = password
|
|
|
|
|
2021-05-13 21:45:54 +02:00
|
|
|
return d.updateObject(0, db.UserProps, user)
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) SetUserPassword(userID int, password string) error {
|
2021-05-13 00:56:31 +02:00
|
|
|
pwdHash, err := bcrypt.GenerateFromPassword([]byte(password), 11)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user, err := d.GetUser(userID)
|
2021-05-09 22:08:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-13 00:56:31 +02:00
|
|
|
user.Password = string(pwdHash)
|
2021-05-13 21:45:54 +02:00
|
|
|
return d.updateObject(0, db.UserProps, user)
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
func (d *BoltDb) CreateProjectUser(projectUser db.ProjectUser) (db.ProjectUser, error) {
|
2021-05-13 21:45:54 +02:00
|
|
|
newProjectUser, err := d.createObject(projectUser.ProjectID, db.ProjectUserProps, projectUser)
|
2021-05-09 22:08:10 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2021-05-13 00:56:31 +02:00
|
|
|
return db.ProjectUser{}, err
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
return newProjectUser.(db.ProjectUser), nil
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
func (d *BoltDb) GetProjectUser(projectID, userID int) (user db.ProjectUser, err error) {
|
2021-05-16 23:44:42 +02:00
|
|
|
err = d.getObject(projectID, db.ProjectUserProps, intObjectID(userID), &user)
|
2021-05-13 00:56:31 +02:00
|
|
|
return
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 12:41:57 +02:00
|
|
|
func (d *BoltDb) GetProjectUsers(projectID int, params db.RetrieveQueryParams) (users []db.UserWithProjectRole, err error) {
|
2021-05-14 01:23:23 +02:00
|
|
|
var projectUsers []db.ProjectUser
|
|
|
|
err = d.getObjects(projectID, db.ProjectUserProps, params, nil, &projectUsers)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, projUser := range projectUsers {
|
|
|
|
var usr db.User
|
|
|
|
usr, err = d.GetUser(projUser.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-07-08 12:41:57 +02:00
|
|
|
var usrWithRole = db.UserWithProjectRole{
|
|
|
|
User: usr,
|
|
|
|
Role: projUser.Role,
|
|
|
|
}
|
|
|
|
users = append(users, usrWithRole)
|
2021-05-14 01:23:23 +02:00
|
|
|
}
|
2021-05-09 22:08:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) UpdateProjectUser(projectUser db.ProjectUser) error {
|
2021-05-13 21:45:54 +02:00
|
|
|
return d.updateObject(projectUser.ProjectID, db.ProjectUserProps, projectUser)
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) DeleteProjectUser(projectID, userID int) error {
|
2022-01-31 23:16:00 +01:00
|
|
|
return d.deleteObject(projectID, db.ProjectUserProps, intObjectID(userID), nil)
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2023-07-07 23:15:30 +02:00
|
|
|
// GetUser retrieves a user from the database by ID
|
2021-05-13 00:56:31 +02:00
|
|
|
func (d *BoltDb) GetUser(userID int) (user db.User, err error) {
|
2021-05-13 21:45:54 +02:00
|
|
|
err = d.getObject(0, db.UserProps, intObjectID(userID), &user)
|
2021-05-13 00:56:31 +02:00
|
|
|
return
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:28:28 +02:00
|
|
|
func (d *BoltDb) GetUserCount() (count int, err error) {
|
2024-11-19 12:17:36 +01:00
|
|
|
var users []db.User
|
|
|
|
err = d.getObjects(0, db.UserProps, db.RetrieveQueryParams{}, nil, &users)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
count = len(users)
|
2024-04-25 13:28:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
func (d *BoltDb) GetUsers(params db.RetrieveQueryParams) (users []db.User, err error) {
|
2021-05-13 21:45:54 +02:00
|
|
|
err = d.getObjects(0, db.UserProps, params, nil, &users)
|
2021-05-13 00:56:31 +02:00
|
|
|
return
|
|
|
|
}
|
2021-05-09 22:08:10 +02:00
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
func (d *BoltDb) GetUserByLoginOrEmail(login string, email string) (existingUser db.User, err error) {
|
|
|
|
var users []db.User
|
2021-05-13 21:45:54 +02:00
|
|
|
err = d.getObjects(0, db.UserProps, db.RetrieveQueryParams{}, nil, &users)
|
2021-05-09 22:08:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
for _, user := range users {
|
|
|
|
if user.Username == login || user.Email == email {
|
|
|
|
existingUser = user
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 22:08:10 +02:00
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
err = db.ErrNotFound
|
2021-05-09 22:08:10 +02:00
|
|
|
return
|
|
|
|
}
|
2024-04-02 23:50:52 +02:00
|
|
|
|
|
|
|
func (d *BoltDb) GetAllAdmins() (users []db.User, err error) {
|
|
|
|
err = d.getObjects(0, db.UserProps, db.RetrieveQueryParams{}, func(i interface{}) bool {
|
|
|
|
user := i.(db.User)
|
|
|
|
return user.Admin
|
|
|
|
}, &users)
|
|
|
|
return
|
|
|
|
}
|