|
|
| package users
|
|
|
| import (
|
| "log"
|
| "net/http"
|
|
|
| "github.com/henrygd/beszel/internal/migrations"
|
|
|
| "github.com/pocketbase/dbx"
|
| "github.com/pocketbase/pocketbase/core"
|
| )
|
|
|
| type UserManager struct {
|
| app core.App
|
| }
|
|
|
| func NewUserManager(app core.App) *UserManager {
|
| return &UserManager{
|
| app: app,
|
| }
|
| }
|
|
|
|
|
| func (um *UserManager) InitializeUserRole(e *core.RecordEvent) error {
|
| if e.Record.GetString("role") == "" {
|
| e.Record.Set("role", "user")
|
| }
|
| return e.Next()
|
| }
|
|
|
|
|
| func (um *UserManager) InitializeUserSettings(e *core.RecordEvent) error {
|
| record := e.Record
|
|
|
| settings := struct {
|
| ChartTime string `json:"chartTime"`
|
| Emails []string `json:"emails"`
|
| }{
|
| ChartTime: "1h",
|
| }
|
| record.UnmarshalJSONField("settings", &settings)
|
|
|
| var user struct {
|
| Email string `db:"email"`
|
| }
|
| err := e.App.DB().NewQuery("SELECT email FROM users WHERE id = {:id}").Bind(dbx.Params{
|
| "id": record.GetString("user"),
|
| }).One(&user)
|
| if err != nil {
|
| log.Println("failed to get user email", "err", err)
|
| return err
|
| }
|
| settings.Emails = []string{user.Email}
|
| record.Set("settings", settings)
|
| return e.Next()
|
| }
|
|
|
|
|
|
|
| func (um *UserManager) CreateFirstUser(e *core.RequestEvent) error {
|
|
|
| totalUsers, err := um.app.CountRecords("users")
|
| if err != nil || totalUsers > 0 {
|
| return e.JSON(http.StatusForbidden, map[string]string{"err": "Forbidden"})
|
| }
|
|
|
| adminUsers, err := um.app.FindAllRecords(core.CollectionNameSuperusers)
|
| if err != nil || len(adminUsers) != 1 || adminUsers[0].GetString("email") != migrations.TempAdminEmail {
|
| return e.JSON(http.StatusForbidden, map[string]string{"err": "Forbidden"})
|
| }
|
|
|
| data := struct {
|
| Email string `json:"email"`
|
| Password string `json:"password"`
|
| }{}
|
| if err := e.BindBody(&data); err != nil {
|
| return e.JSON(http.StatusBadRequest, map[string]string{"err": err.Error()})
|
| }
|
| if data.Email == "" || data.Password == "" {
|
| return e.JSON(http.StatusBadRequest, map[string]string{"err": "Bad request"})
|
| }
|
|
|
| collection, _ := um.app.FindCollectionByNameOrId("users")
|
| user := core.NewRecord(collection)
|
| user.SetEmail(data.Email)
|
| user.SetPassword(data.Password)
|
| user.Set("role", "admin")
|
| user.Set("verified", true)
|
| if err := um.app.Save(user); err != nil {
|
| return e.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
|
| }
|
|
|
| collection, _ = um.app.FindCollectionByNameOrId(core.CollectionNameSuperusers)
|
| adminUser := core.NewRecord(collection)
|
| adminUser.SetEmail(data.Email)
|
| adminUser.SetPassword(data.Password)
|
| if err := um.app.Save(adminUser); err != nil {
|
| return e.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
|
| }
|
|
|
| if err := um.app.Delete(adminUsers[0]); err != nil {
|
| return e.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
|
| }
|
| return e.JSON(http.StatusOK, map[string]string{"msg": "User created"})
|
| }
|
|
|