Create the first superadmin from DOCWELL_ADMIN_EMAIL on start
When DOCWELL_ADMIN_EMAIL and DOCWELL_ADMIN_PASSWORD are set (and optionally DOCWELL_ADMIN_NAME), docwell-server creates that account as superadmin on start while the instance has no accounts. Once any account exists it changes nothing, so restarts never reset the password or a role changed in the admin panel. Without them, the first sign-up still wins. Pass the variables through every compose file, document them in the .env examples and the configuration, access and quick-start guides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b9be8be156
commit
3c7403ddf9
27 files changed
+239
-3
No files matched your search
@@ -331,6 +331,43 @@ func (s *Service) Signup(ctx context.Context, in SignupInput) (Session, error) {
|
||||
return s.Login(ctx, in.Email, in.Password, "")
|
||||
}
|
||||
|
||||
// EnsureSuperadmin creates the instance's first account as superadmin when the
|
||||
// instance has no accounts yet (DOCWELL_ADMIN_EMAIL, DOCWELL_ADMIN_PASSWORD).
|
||||
// Once anyone exists it changes nothing, so restarts never reset a password
|
||||
// or a role changed in the admin panel. created reports whether it made one.
|
||||
func (s *Service) EnsureSuperadmin(ctx context.Context, email, password, name string) (created bool, err error) {
|
||||
email = strings.TrimSpace(email)
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" {
|
||||
name = "Admin"
|
||||
}
|
||||
if len(password) < 8 {
|
||||
return false, &ValidationError{Fields: map[string]string{"password": "Password must be at least 8 characters."}}
|
||||
}
|
||||
existing, err := pocketbase.List[UserRecord](ctx, s.pb, "users", pocketbase.ListOptions{PerPage: 1})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if existing.TotalItems > 0 {
|
||||
return false, nil
|
||||
}
|
||||
_, err = pocketbase.Create[UserRecord](ctx, s.pb, "users", map[string]any{
|
||||
"email": email,
|
||||
"password": password,
|
||||
"passwordConfirm": password,
|
||||
"name": name,
|
||||
"status": "active",
|
||||
"role": access.RoleSuperadmin,
|
||||
"emailVisibility": false,
|
||||
// The operator chose this address; there is no one to confirm it.
|
||||
"verified": true,
|
||||
})
|
||||
if err != nil {
|
||||
return false, mapError(err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *Service) signupRules(ctx context.Context) (bool, []string, error) {
|
||||
if s.opts.Rules != nil {
|
||||
return s.opts.Rules(ctx)
|
||||
|
||||
@@ -171,6 +171,45 @@ func TestFirstSignupBecomesSuperadminThenSignupIsClosed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureSuperadminOnlyOnEmptyInstance(t *testing.T) {
|
||||
f, pb := newFakePB(t)
|
||||
s := NewService(pb, Options{SignupEnabled: false})
|
||||
|
||||
var verr *ValidationError
|
||||
if _, err := s.EnsureSuperadmin(ctx, "root@docwell.test", "short", ""); !errors.As(err, &verr) {
|
||||
t.Errorf("short password err = %v", err)
|
||||
}
|
||||
|
||||
created, err := s.EnsureSuperadmin(ctx, " root@docwell.test ", "password123", "")
|
||||
if err != nil || !created {
|
||||
t.Fatalf("first run = %v, %v", created, err)
|
||||
}
|
||||
if rec := f.users["root@docwell.test"]; rec["role"] != "superadmin" || rec["name"] != "Admin" {
|
||||
t.Errorf("created = %+v", rec)
|
||||
}
|
||||
sess, err := s.Login(ctx, "root@docwell.test", "password123", "")
|
||||
if err != nil || sess.User.Role != "superadmin" {
|
||||
t.Errorf("login = %+v, %v", sess.User, err)
|
||||
}
|
||||
|
||||
// A restart with another password changes nothing.
|
||||
created, err = s.EnsureSuperadmin(ctx, "root@docwell.test", "different123", "Root")
|
||||
if err != nil || created || f.passwords["root@docwell.test"] != "password123" {
|
||||
t.Errorf("second run = %v, %v, password %q", created, err, f.passwords["root@docwell.test"])
|
||||
}
|
||||
// Nor does a different address once someone exists.
|
||||
if created, err := s.EnsureSuperadmin(ctx, "other@docwell.test", "password123", ""); err != nil || created {
|
||||
t.Errorf("other address = %v, %v", created, err)
|
||||
}
|
||||
if _, ok := f.users["other@docwell.test"]; ok {
|
||||
t.Error("created a second superadmin")
|
||||
}
|
||||
// And the next signup is an ordinary one (closed here).
|
||||
if _, err := s.Signup(ctx, SignupInput{Email: "bob@docwell.test", Password: "password123", Name: "Bob"}); !errors.Is(err, ErrSignupDisabled) {
|
||||
t.Errorf("signup after bootstrap err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignupDomainsAndValidation(t *testing.T) {
|
||||
_, pb := newFakePB(t)
|
||||
s := NewService(pb, Options{SignupEnabled: true, SignupDomains: []string{"docwell.test"}})
|
||||
|
||||
Reference in new issue
Block a user