2019-11-29 12:44:43 +01:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
|
|
|
"github.com/sethvargo/go-password/password"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
gorm.Model
|
|
|
|
|
|
|
|
Username string
|
2020-01-11 08:03:56 +01:00
|
|
|
Password string `csv:"-"`
|
2019-11-29 12:44:43 +01:00
|
|
|
Role string
|
|
|
|
}
|
|
|
|
|
2019-12-07 08:58:30 +01:00
|
|
|
var (
|
|
|
|
SubscriberUser = User{Username: "subscriber", Role: "subscriber"}
|
|
|
|
AdministratorUser = User{Username: "admin", Role: "administrator"}
|
|
|
|
)
|
|
|
|
|
2019-11-29 12:44:43 +01:00
|
|
|
func (model *User) GetID() uint { return model.ID }
|
|
|
|
|
|
|
|
func (model *User) String() string {
|
2020-12-29 19:18:18 +01:00
|
|
|
return model.Username
|
2019-11-29 12:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func genPassword() (string, error) {
|
|
|
|
password, err := password.Generate(8, 2, 0, false, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *User) BeforeSave(tx *gorm.DB) error {
|
|
|
|
if model.Password == "" {
|
|
|
|
password, err := genPassword()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
model.Password = password
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *User) Create(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-29 12:44:43 +01:00
|
|
|
if r.Method == "GET" {
|
|
|
|
user := new(User)
|
|
|
|
return user, nil
|
|
|
|
} else {
|
|
|
|
user := new(User)
|
|
|
|
err := renderer.Decode(user, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
user, err = CreateUser(db, user)
|
2019-11-29 12:44:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *User) Read(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-29 12:44:43 +01:00
|
|
|
var user User
|
|
|
|
|
|
|
|
id := args["id"]
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db. /*.Preload("Something")*/ First(&user, id).Error; err != nil {
|
2019-11-29 12:44:43 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *User) ReadAll(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-29 12:44:43 +01:00
|
|
|
var users []*User
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db. /*.Preload("Something")*/ Order("created_at").Find(&users).Error; err != nil {
|
2019-11-29 12:44:43 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *User) Update(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-29 12:44:43 +01:00
|
|
|
if r.Method == "GET" {
|
2020-01-15 11:27:00 +01:00
|
|
|
result, err := model.Read(db, args, w, r)
|
2019-11-29 12:44:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user := result.(*User)
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
// if err := db._db.Find(&user.AllElements).Error; err != nil {
|
2019-11-29 12:44:43 +01:00
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// user.SelectedElement = make(map[uint]string)
|
|
|
|
// user.SelectedElement[user.ElementID] = "selected"
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
} else {
|
2020-01-15 11:27:00 +01:00
|
|
|
user, err := model.Read(db, args, w, r)
|
2019-11-29 12:44:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = renderer.Decode(user, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
_, err = SaveUser(db, user)
|
2019-11-29 12:44:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
user, err = model.Read(db, args, w, r)
|
2019-11-29 12:44:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return user.(*User), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *User) Delete(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
|
|
user, err := model.Read(db, args, w, r)
|
2019-11-29 12:44:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Unscoped().Delete(user.(*User)).Error; err != nil {
|
2019-11-29 12:44:43 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return user.(*User), nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func CreateUser(db *Database, user *User) (*User, error) {
|
|
|
|
if err := db._db.Create(user).Error; err != nil {
|
2019-11-29 12:44:43 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func SaveUser(db *Database, user interface{}) (interface{}, error) {
|
|
|
|
if err := db._db. /*.Omit("Something")*/ Save(user).Error; err != nil {
|
2019-11-29 12:44:43 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|