1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: make very first user have admin privileges

This commit is contained in:
Ferdinand Mütsch 2021-02-12 18:49:47 +01:00
parent 5b3e88247e
commit 8191a52ce1
9 changed files with 45 additions and 10 deletions

View File

@ -24,8 +24,13 @@ func (m *UserServiceMock) GetAll() ([]*models.User, error) {
return args.Get(0).([]*models.User), args.Error(1) return args.Get(0).([]*models.User), args.Error(1)
} }
func (m *UserServiceMock) CreateOrGet(signup *models.Signup) (*models.User, bool, error) { func (m *UserServiceMock) Count() (int64, error) {
args := m.Called(signup) args := m.Called()
return int64(args.Int(0)), args.Error(1)
}
func (m *UserServiceMock) CreateOrGet(signup *models.Signup, isAdmin bool) (*models.User, bool, error) {
args := m.Called(signup, isAdmin)
return args.Get(0).(*models.User), args.Bool(1), args.Error(2) return args.Get(0).(*models.User), args.Bool(1), args.Error(2)
} }

View File

@ -1,8 +1,9 @@
package view package view
type LoginViewModel struct { type LoginViewModel struct {
Success string Success string
Error string Error string
TotalUsers int
} }
func (s *LoginViewModel) WithSuccess(m string) *LoginViewModel { func (s *LoginViewModel) WithSuccess(m string) *LoginViewModel {

View File

@ -48,6 +48,7 @@ type IUserRepository interface {
GetById(string) (*models.User, error) GetById(string) (*models.User, error)
GetByApiKey(string) (*models.User, error) GetByApiKey(string) (*models.User, error)
GetAll() ([]*models.User, error) GetAll() ([]*models.User, error)
Count() (int64, error)
InsertOrGet(*models.User) (*models.User, bool, error) InsertOrGet(*models.User) (*models.User, bool, error)
Update(*models.User) (*models.User, error) Update(*models.User) (*models.User, error)
UpdateField(*models.User, string, interface{}) (*models.User, error) UpdateField(*models.User, string, interface{}) (*models.User, error)

View File

@ -40,6 +40,16 @@ func (r *UserRepository) GetAll() ([]*models.User, error) {
return users, nil return users, nil
} }
func (r *UserRepository) Count() (int64, error) {
var count int64
if err := r.db.
Model(&models.User{}).
Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
func (r *UserRepository) InsertOrGet(user *models.User) (*models.User, bool, error) { func (r *UserRepository) InsertOrGet(user *models.User) (*models.User, bool, error) {
result := r.db.FirstOrCreate(user, &models.User{ID: user.ID}) result := r.db.FirstOrCreate(user, &models.User{ID: user.ID})
if err := result.Error; err != nil { if err := result.Error; err != nil {

View File

@ -150,7 +150,9 @@ func (h *LoginHandler) PostSignup(w http.ResponseWriter, r *http.Request) {
return return
} }
_, created, err := h.userSrvc.CreateOrGet(&signup) numUsers, _ := h.userSrvc.Count()
_, created, err := h.userSrvc.CreateOrGet(&signup, numUsers == 0)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
templates[conf.SignupTemplate].Execute(w, h.buildViewModel(r).WithError("failed to create new user")) templates[conf.SignupTemplate].Execute(w, h.buildViewModel(r).WithError("failed to create new user"))
@ -166,8 +168,11 @@ func (h *LoginHandler) PostSignup(w http.ResponseWriter, r *http.Request) {
} }
func (h *LoginHandler) buildViewModel(r *http.Request) *view.LoginViewModel { func (h *LoginHandler) buildViewModel(r *http.Request) *view.LoginViewModel {
numUsers, _ := h.userSrvc.Count()
return &view.LoginViewModel{ return &view.LoginViewModel{
Success: r.URL.Query().Get("success"), Success: r.URL.Query().Get("success"),
Error: r.URL.Query().Get("error"), Error: r.URL.Query().Get("error"),
TotalUsers: int(numUsers),
} }
} }

View File

@ -63,7 +63,8 @@ type IUserService interface {
GetUserById(string) (*models.User, error) GetUserById(string) (*models.User, error)
GetUserByKey(string) (*models.User, error) GetUserByKey(string) (*models.User, error)
GetAll() ([]*models.User, error) GetAll() ([]*models.User, error)
CreateOrGet(*models.Signup) (*models.User, bool, error) Count() (int64, error)
CreateOrGet(*models.Signup, bool) (*models.User, bool, error)
Update(*models.User) (*models.User, error) Update(*models.User) (*models.User, error)
Delete(*models.User) error Delete(*models.User) error
ResetApiKey(*models.User) (*models.User, error) ResetApiKey(*models.User) (*models.User, error)

View File

@ -56,11 +56,16 @@ func (srv *UserService) GetAll() ([]*models.User, error) {
return srv.repository.GetAll() return srv.repository.GetAll()
} }
func (srv *UserService) CreateOrGet(signup *models.Signup) (*models.User, bool, error) { func (srv *UserService) Count() (int64, error) {
return srv.repository.Count()
}
func (srv *UserService) CreateOrGet(signup *models.Signup, isAdmin bool) (*models.User, bool, error) {
u := &models.User{ u := &models.User{
ID: signup.Username, ID: signup.Username,
ApiKey: uuid.NewV4().String(), ApiKey: uuid.NewV4().String(),
Password: signup.Password, Password: signup.Password,
IsAdmin: isAdmin,
} }
if hash, err := utils.HashBcrypt(u.Password, srv.Config.Security.PasswordSalt); err != nil { if hash, err := utils.HashBcrypt(u.Password, srv.Config.Security.PasswordSalt); err != nil {

View File

@ -1 +1 @@
1.23.6 1.23.7

View File

@ -49,6 +49,13 @@
type="password" id="password_repeat" type="password" id="password_repeat"
name="password_repeat" placeholder="Repeat your password" minlength="6" required> name="password_repeat" placeholder="Repeat your password" minlength="6" required>
</div> </div>
{{ if eq .TotalUsers 0 }}
<p class="text-sm text-gray-300 mt-4 mb-8">
⚠️ <strong>Please note: </strong> Since there are no users registered in the system, yet, the first user will have administrative privileges, while additional users won't.
</p>
{{ end }}
<div class="flex justify-between float-right"> <div class="flex justify-between float-right">
<button type="submit" class="py-1 px-3 rounded bg-green-700 hover:bg-green-800 text-white text-sm"> <button type="submit" class="py-1 px-3 rounded bg-green-700 hover:bg-green-800 text-white text-sm">
Create Account Create Account