go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/chirp/pkg/controller/smurf.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package controller
     9  
    10  import (
    11  	"net/http"
    12  	"time"
    13  
    14  	"go.charczuk.com/projects/chirp/pkg/config"
    15  	"go.charczuk.com/projects/chirp/pkg/model"
    16  	"go.charczuk.com/sdk/apputil"
    17  	"go.charczuk.com/sdk/uuid"
    18  	"go.charczuk.com/sdk/web"
    19  )
    20  
    21  // Smurf is a controller that lets admins create
    22  // or log into "smurf" accounts that are used for testing.
    23  type Smurf struct {
    24  	apputil.BaseController
    25  
    26  	Config config.Config
    27  	Model  model.Manager
    28  }
    29  
    30  // Register registers the controller with the app.
    31  func (s Smurf) Register(app *web.App) {
    32  	app.Get("/smurf/new", web.NestMiddleware(s.new, web.SessionRequired))
    33  	app.Post("/smurf/new", web.NestMiddleware(s.postNew, web.SessionRequired))
    34  	app.Get("/smurf/as", web.NestMiddleware(s.as, web.SessionRequired))
    35  	app.Post("/smurf/as", web.NestMiddleware(s.postAs, web.SessionRequired))
    36  }
    37  
    38  func (s Smurf) new(r web.Context) web.Result {
    39  	userID := s.GetUserID(r)
    40  	u, found, err := s.Model.User(r, userID, userID)
    41  	if err != nil {
    42  		return r.Views().InternalError(err)
    43  	}
    44  	if !found || !u.Info.IsAdmin {
    45  		return web.Redirect("/home")
    46  	}
    47  	return r.Views().View("smurf_new", model.UserFull{})
    48  }
    49  
    50  func (s Smurf) as(r web.Context) web.Result {
    51  	userID := s.GetUserID(r)
    52  	u, found, err := s.Model.User(r, userID, userID)
    53  	if err != nil {
    54  		return r.Views().InternalError(err)
    55  	}
    56  	if !found || !u.Info.IsAdmin {
    57  		return web.Redirect("/home")
    58  	}
    59  	return r.Views().View("smurf_as", nil)
    60  }
    61  
    62  func (s Smurf) postAs(r web.Context) web.Result {
    63  	userID := s.GetUserID(r)
    64  	u, found, err := s.Model.User(r, userID, userID)
    65  	if err != nil {
    66  		return r.Views().InternalError(err)
    67  	}
    68  	if !found || !u.Info.IsAdmin {
    69  		return web.Redirect("/home")
    70  	}
    71  	username, err := web.FormValue[string](r, "username")
    72  	if err != nil {
    73  		return r.Views().BadRequest(err)
    74  	}
    75  
    76  	user, found, err := s.Model.UserByUsername(r, userID, username)
    77  	if !found {
    78  		return r.Views().NotFound()
    79  	}
    80  	if err != nil {
    81  		return r.Views().InternalError(err)
    82  	}
    83  	if err = r.App().Logout(r); err != nil {
    84  		return r.Views().InternalError(err)
    85  	}
    86  	_, err = r.App().Login(user.ID.String(), r)
    87  	if err != nil {
    88  		return r.Views().InternalError(err)
    89  	}
    90  	return web.RedirectWithMethod(http.MethodGet, "/home")
    91  }
    92  
    93  func (s Smurf) postNew(r web.Context) web.Result {
    94  	userID := s.GetUserID(r)
    95  	u, found, err := s.Model.User(r, userID, userID)
    96  	if err != nil {
    97  		return r.Views().InternalError(err)
    98  	}
    99  	if !found || !u.Info.IsAdmin {
   100  		return web.Redirect("/home")
   101  	}
   102  
   103  	var user apputil.User
   104  	user.Email, err = web.FormValue[string](r, "Email")
   105  	if err != nil {
   106  		return r.Views().BadRequest(err)
   107  	}
   108  	user.GivenName, err = web.FormValue[string](r, "GivenName")
   109  	if err != nil {
   110  		return r.Views().BadRequest(err)
   111  	}
   112  	user.FamilyName, err = web.FormValue[string](r, "FamilyName")
   113  	if err != nil {
   114  		return r.Views().BadRequest(err)
   115  	}
   116  	user.PictureURL, err = web.FormValue[string](r, "PictureURL")
   117  	if err != nil {
   118  		return r.Views().BadRequest(err)
   119  	}
   120  	user.Locale, err = web.FormValue[string](r, "Locale")
   121  	if err != nil {
   122  		return r.Views().BadRequest(err)
   123  	}
   124  	user.ID = uuid.V4()
   125  	user.LastLoginUTC = time.Now().UTC()
   126  	user.LastSeenUTC = time.Now().UTC()
   127  	if err = s.Model.Invoke(r).Create(&user); err != nil {
   128  		return r.Views().InternalError(err)
   129  	}
   130  	if user.ID.IsZero() {
   131  		panic("userID unset!")
   132  	}
   133  	if err = s.Model.EnsureUserInfoOnCreate(r, &user); err != nil {
   134  		return r.Views().InternalError(err)
   135  	}
   136  
   137  	// log the existing session out ...
   138  	if err = r.App().Logout(r); err != nil {
   139  		return r.Views().InternalError(err)
   140  	}
   141  	_, err = r.App().Login(user.ID.String(), r)
   142  	if err != nil {
   143  		return r.Views().InternalError(err)
   144  	}
   145  	return web.RedirectWithMethod(http.MethodGet, "/home")
   146  }