174 lines
3.9 KiB
Go
174 lines
3.9 KiB
Go
|
package db
|
||
|
|
||
|
import "log"
|
||
|
|
||
|
func CreateUser(name string, passhash string, email string, displayName string) error {
|
||
|
user := User{
|
||
|
Name: name,
|
||
|
PassHash: passhash,
|
||
|
Email: email,
|
||
|
DisplayName: displayName,
|
||
|
}
|
||
|
result := conn.Create(&user)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could not add user %s: %v", name, result.Error)
|
||
|
return result.Error
|
||
|
} else {
|
||
|
log.Printf("Created User %s", name)
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func AddUserToGroup(username string, groupname string) error {
|
||
|
user, err := GetUserByName(username)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
group, err := GetGroupByName(groupname)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
result := conn.Where("name = ?", groupname).First(&user)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could find user %s: %v", username, result.Error)
|
||
|
return result.Error
|
||
|
}
|
||
|
|
||
|
err = conn.Model(&user).Association("Groups").Append(&group)
|
||
|
|
||
|
if err != nil {
|
||
|
log.Printf("Could add user %s to group %s: %v", username, groupname, result.Error)
|
||
|
return result.Error
|
||
|
} else {
|
||
|
log.Printf("Added user %s to Group %s", username, groupname)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func DeleteUserFromGroup(username string, groupname string) error {
|
||
|
user, err := GetUserByName(username)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
group, err := GetGroupByName(groupname)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
result := conn.Where("name = ?", groupname).First(&user)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could find user %s: %v", username, result.Error)
|
||
|
return result.Error
|
||
|
}
|
||
|
|
||
|
err = conn.Model(&user).Association("Groups").Delete(&group)
|
||
|
|
||
|
if err != nil {
|
||
|
log.Printf("Could delete user %s from group %s: %v", username, groupname, result.Error)
|
||
|
return result.Error
|
||
|
} else {
|
||
|
log.Printf("Deleted user %s from group %s", username, groupname)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func GetUserByName(name string) (User, error) {
|
||
|
var user User
|
||
|
user.Name = name
|
||
|
result := conn.Where("name = ?", name).First(&user)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could find user %s: %v", name, result.Error)
|
||
|
return user, result.Error
|
||
|
} else {
|
||
|
log.Printf("retrieved user %s", name)
|
||
|
return user, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func GetUserByID(id int) (User, error) {
|
||
|
var group User
|
||
|
result := conn.Where("id = ?", id).First(&group)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could find group %d: %v", id, result.Error)
|
||
|
return group, result.Error
|
||
|
} else {
|
||
|
log.Printf("retrieved group %d", id)
|
||
|
return group, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func GetAllUsers() ([]User, error) {
|
||
|
var user []User
|
||
|
result := conn.Find(&user)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could find roles: %v", result.Error)
|
||
|
return user, result.Error
|
||
|
} else {
|
||
|
return user, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func SetUserName(user User, name string) error {
|
||
|
user.Name = name
|
||
|
result := conn.Save(&user)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could find user %s: %v", name, result.Error)
|
||
|
return result.Error
|
||
|
} else {
|
||
|
log.Printf("retrieved user %s", name)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func SetUserDisplayName(user User, name string) error {
|
||
|
user.DisplayName = name
|
||
|
result := conn.Save(&user)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could find user %s: %v", name, result.Error)
|
||
|
return result.Error
|
||
|
} else {
|
||
|
log.Printf("retrieved user %s", name)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func SetUserPassHash(user User, hash string) error {
|
||
|
user.PassHash = hash
|
||
|
result := conn.Save(&user)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could find user %s: %v", hash, result.Error)
|
||
|
return result.Error
|
||
|
} else {
|
||
|
log.Printf("retrieved user %s", hash)
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func SetUserEmail(user User, email string) error {
|
||
|
user.Email = email
|
||
|
result := conn.Save(&user)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could find user %s: %v", email, result.Error)
|
||
|
return result.Error
|
||
|
} else {
|
||
|
log.Printf("retrieved user %s", email)
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func DeleteUser(user User) error {
|
||
|
result := conn.First(&user).Delete(&user)
|
||
|
if result.Error != nil {
|
||
|
log.Printf("Could find user %d: %v", user.ID, result.Error)
|
||
|
return result.Error
|
||
|
} else {
|
||
|
log.Printf("deleted user %d", user.ID)
|
||
|
return nil
|
||
|
}
|
||
|
}
|