Alpenwerk HR — HR-Stammdatenverwaltung Mitarbeiter:innen, Organisationsstruktur, Planstellen, Eintritt, Austritt, Versetzung, Beförderung, Karenz, On- und Offboarding, Berichte, Exporte, Massenimport und ein lückenloser Nachweis jeder Änderung. Next.js 16, React 19 und TypeScript; PostgreSQL über Kysely; Anmeldung ausschliesslich über Microsoft Entra ID. Die Fachlogik liegt in der Datenbank, die Zugriffsschranke ist Row Level Security. Betrieb auf Windows Server: siehe deploy/windows/README.md. Sicherheitskonzept: docs/sicherheitskonzept.md.
168 lines
6.4 KiB
PowerShell
168 lines
6.4 KiB
PowerShell
# Gemeinsame Bausteine der Betriebsskripte. Wird von den anderen Skripten
|
|
# per Dot-Sourcing geladen, nicht direkt aufgerufen.
|
|
#
|
|
# Bewusst ohne Umlaute: Windows PowerShell 5.1 liest Skripte ohne BOM als
|
|
# ANSI, und aus "Pruefung" wuerde sonst Zeichensalat.
|
|
#
|
|
# Bewusst ohne PowerShell-7-Syntax (kein ??, kein ?:, kein &&): auf einem
|
|
# Windows Server ist 5.1 das, was da ist.
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$DienstName = 'AlpenwerkHR'
|
|
|
|
# Wohlbekannte SIDs statt Namen: "Administratoren" heisst auf einem deutschen
|
|
# Server anders als auf einem englischen, die SID ueberall gleich.
|
|
$SidAdministratoren = '*S-1-5-32-544'
|
|
$SidSystem = '*S-1-5-18'
|
|
$SidLocalService = '*S-1-5-19'
|
|
$SidIisIusrs = '*S-1-5-32-568'
|
|
|
|
function Read-EnvDatei {
|
|
param([Parameter(Mandatory = $true)][string]$Pfad)
|
|
$werte = @{}
|
|
if (-not (Test-Path -LiteralPath $Pfad)) { return $werte }
|
|
foreach ($zeile in Get-Content -LiteralPath $Pfad -Encoding UTF8) {
|
|
$t = $zeile.Trim()
|
|
if ($t -eq '' -or $t.StartsWith('#')) { continue }
|
|
$i = $t.IndexOf('=')
|
|
if ($i -lt 1) { continue }
|
|
$schluessel = $t.Substring(0, $i).Trim()
|
|
$wert = $t.Substring($i + 1).Trim()
|
|
if ($wert.Length -ge 2 -and (($wert.StartsWith('"') -and $wert.EndsWith('"')) -or ($wert.StartsWith("'") -and $wert.EndsWith("'")))) {
|
|
$wert = $wert.Substring(1, $wert.Length - 2)
|
|
}
|
|
$werte[$schluessel] = $wert
|
|
}
|
|
return $werte
|
|
}
|
|
|
|
# Liefert die Namen der Pflichtwerte, die fehlen oder noch einen Platzhalter
|
|
# <...> / {{...}} tragen.
|
|
function Get-FehlendeWerte {
|
|
param([hashtable]$Werte, [string[]]$Pflicht)
|
|
$fehlend = @()
|
|
foreach ($name in $Pflicht) {
|
|
$w = $Werte[$name]
|
|
if (-not $w -or $w -match '<[^>]*>' -or $w -match '\{\{') { $fehlend += $name }
|
|
}
|
|
return $fehlend
|
|
}
|
|
|
|
$PflichtApp = @(
|
|
'PORT', 'DATABASE_URL', 'AUTH_SECRET', 'AUTH_URL',
|
|
'AUTH_MICROSOFT_ENTRA_ID_ID', 'AUTH_MICROSOFT_ENTRA_ID_SECRET', 'AUTH_MICROSOFT_ENTRA_ID_ISSUER',
|
|
'CRON_SECRET'
|
|
)
|
|
$PflichtVerwaltung = @('MIGRATE_DATABASE_URL')
|
|
|
|
function New-Geheimnis {
|
|
param([int]$Bytes = 32)
|
|
$puffer = New-Object byte[] $Bytes
|
|
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
|
$rng.GetBytes($puffer)
|
|
$rng.Dispose()
|
|
# Hex statt Base64: kommt ohne + / = aus und muss deshalb in einer
|
|
# Verbindungs-URL nicht maskiert werden.
|
|
return -join ($puffer | ForEach-Object { $_.ToString('x2') })
|
|
}
|
|
|
|
function Get-NodePfad {
|
|
$befehl = Get-Command node.exe -ErrorAction SilentlyContinue
|
|
if (-not $befehl) { throw 'node.exe nicht gefunden. Node.js installieren (https://nodejs.org) und eine neue Konsole oeffnen.' }
|
|
return $befehl.Source
|
|
}
|
|
|
|
function Assert-NodeVersion {
|
|
$node = Get-NodePfad
|
|
$version = (& $node --version).Trim().TrimStart('v')
|
|
$haupt = [int]($version.Split('.')[0])
|
|
if ($haupt -lt 22) { throw "Node.js $version ist zu alt; gebraucht wird mindestens 22." }
|
|
Write-Host "Node.js $version ($node)"
|
|
}
|
|
|
|
# Neueste Installation unter "C:\Program Files\PostgreSQL\<version>\bin",
|
|
# sofern nicht ausdruecklich angegeben.
|
|
function Get-PgBin {
|
|
param([string]$Vorgabe)
|
|
if ($Vorgabe) {
|
|
if (-not (Test-Path -LiteralPath (Join-Path $Vorgabe 'pg_dump.exe'))) { throw "In $Vorgabe liegt kein pg_dump.exe." }
|
|
return $Vorgabe
|
|
}
|
|
$basis = Join-Path $env:ProgramFiles 'PostgreSQL'
|
|
$kandidat = Get-ChildItem -LiteralPath $basis -Directory -ErrorAction SilentlyContinue |
|
|
Where-Object { Test-Path -LiteralPath (Join-Path $_.FullName 'bin\pg_dump.exe') } |
|
|
Sort-Object { [int]($_.Name -replace '\D', '') } -Descending |
|
|
Select-Object -First 1
|
|
if (-not $kandidat) { throw "PostgreSQL-Programme nicht gefunden unter $basis. Pfad mit -PgBin angeben." }
|
|
return (Join-Path $kandidat.FullName 'bin')
|
|
}
|
|
|
|
# Fuehrt ein Node-Skript im Anwendungsverzeichnis aus, mit beiden
|
|
# Konfigurationsdateien. Bricht ab, wenn es scheitert.
|
|
function Invoke-NodeSkript {
|
|
param(
|
|
[Parameter(Mandatory = $true)][string]$AppVerzeichnis,
|
|
[Parameter(Mandatory = $true)][string]$Ziel,
|
|
[Parameter(Mandatory = $true)][string]$Skript
|
|
)
|
|
$node = Get-NodePfad
|
|
Push-Location -LiteralPath $AppVerzeichnis
|
|
try {
|
|
& $node "--env-file=$Ziel\app.env" "--env-file=$Ziel\verwaltung.env" $Skript
|
|
if ($LASTEXITCODE -ne 0) { throw "$Skript ist fehlgeschlagen (Exit-Code $LASTEXITCODE)." }
|
|
} finally {
|
|
Pop-Location
|
|
}
|
|
}
|
|
|
|
function Invoke-Icacls {
|
|
param([string[]]$Argumente)
|
|
# 2>&1 auf ein natives Programm erzeugt in PowerShell 5.1 Fehlerobjekte, und
|
|
# mit 'Stop' bricht schon die erste Zeile auf stderr alles ab -- deshalb hier
|
|
# lokal 'Continue' und die Entscheidung allein am Exit-Code.
|
|
$ErrorActionPreference = 'Continue'
|
|
$ausgabe = & icacls.exe @Argumente 2>&1
|
|
if ($LASTEXITCODE -ne 0) { throw "icacls $($Argumente -join ' ') ist fehlgeschlagen: $ausgabe" }
|
|
}
|
|
|
|
# Wartet, bis die Anwendung auf /login mit 200 antwortet.
|
|
function Wait-Gesund {
|
|
param([int]$Port, [int]$Sekunden = 90)
|
|
$bis = (Get-Date).AddSeconds($Sekunden)
|
|
while ((Get-Date) -lt $bis) {
|
|
try {
|
|
$antwort = Invoke-WebRequest -Uri "http://127.0.0.1:$Port/login" -UseBasicParsing -TimeoutSec 5 -MaximumRedirection 0
|
|
if ($antwort.StatusCode -eq 200) { return $true }
|
|
} catch {
|
|
# Fehler sind hier der Normalfall: der Dienst startet noch.
|
|
}
|
|
# Die Pause steht ausserhalb des catch. Stand sie nur darin, drehte eine
|
|
# Antwort, die zwar durchkommt aber nicht 200 ist, neunzig Sekunden lang
|
|
# eine Schleife ohne Pause -- gegen einen Dienst, der gerade hochfaehrt.
|
|
Start-Sleep -Seconds 3
|
|
}
|
|
return $false
|
|
}
|
|
|
|
function Copy-Verzeichnis {
|
|
param([string]$Quelle, [string]$Ziel, [switch]$Spiegeln)
|
|
# robocopy statt Copy-Item: kommt mit langen Pfaden in node_modules zurecht.
|
|
# /MIR loescht im Ziel, was in der Quelle fehlt; ohne nur hinzufuegen.
|
|
$modus = '/E'
|
|
if ($Spiegeln) { $modus = '/MIR' }
|
|
& robocopy.exe $Quelle $Ziel $modus /NFL /NDL /NJH /NJS /NP /R:2 /W:2 | Out-Null
|
|
# robocopy: 0-7 heisst Erfolg (mit oder ohne kopierte Dateien), ab 8 Fehler.
|
|
if ($LASTEXITCODE -ge 8) { throw "Kopieren von $Quelle nach $Ziel ist fehlgeschlagen (robocopy $LASTEXITCODE)." }
|
|
$global:LASTEXITCODE = 0
|
|
}
|
|
|
|
# Next.js schreibt zur Laufzeit in .next\cache. Der Dienst laeuft als
|
|
# LocalService und darf sonst im Anwendungsverzeichnis nur lesen.
|
|
function Set-CacheRechte {
|
|
param([string]$AppVerzeichnis)
|
|
$cache = Join-Path $AppVerzeichnis '.next\cache'
|
|
New-Item -ItemType Directory -Force -Path $cache | Out-Null
|
|
Invoke-Icacls @($cache, '/grant', "${SidLocalService}:(OI)(CI)M")
|
|
}
|