Full web : fichiers restants
This commit is contained in:
1 parent
ec3b7c9a01
commit
4826fa5b82
26 files changed
+2537
-793
No files matched your search
@@ -180,3 +180,33 @@ func (s *Store) keepAnAdmin(ctx context.Context, name, newRole string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ErrSetupDone: the first administrator already exists.
|
||||
var ErrSetupDone = errors.New("the first administrator already exists")
|
||||
|
||||
// CreateFirstAdmin creates the first administrator from the web page shown on an
|
||||
// empty database. The insert only happens while the table is empty: two
|
||||
// concurrent requests cannot both succeed.
|
||||
func (s *Store) CreateFirstAdmin(ctx context.Context, name, pass string) error {
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" || strings.ContainsAny(name, " \t\r\n") {
|
||||
return errors.New("invalid user name")
|
||||
}
|
||||
if len(pass) < 12 {
|
||||
return errors.New("password: 12 characters minimum")
|
||||
}
|
||||
hash, err := HashPassword(pass)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
now := time.Now()
|
||||
res, err := s.db.ExecContext(ctx, `INSERT INTO users (name, role, password, epoch, created_at)
|
||||
SELECT ?, 'admin', ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM users)`, name, hash, now.UnixNano(), now.Unix())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return ErrSetupDone
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user