Files
scadaproj/docs/deploy/2026-11-19-vd03-nam-ldap/sb-role-mappings.ps1
T

21 lines
1.4 KiB
PowerShell

# Insert the DEV UG_WW_* -> role rows (plan §4.4 SQL + §0.2 DEV interim HIST_ADMIN_DEV->Deployer). Idempotent; refuses conflicts.
$ErrorActionPreference = 'Stop'
$e = @((Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\ScadaBridge-Central').Environment)
$cs = ($e | Where-Object { $_ -like 'ScadaBridge__Database__ConfigurationDb=*' }) -replace '^[^=]*=',''
$sb = New-Object System.Data.SqlClient.SqlConnectionStringBuilder $cs
$c = New-Object System.Data.SqlClient.SqlConnection $sb.ConnectionString; $c.Open(); $cmd = $c.CreateCommand()
$cmd.CommandText = @"
SET XACT_ABORT ON;
BEGIN TRAN;
DECLARE @t TABLE (LdapGroupName nvarchar(500) NOT NULL, Role nvarchar(100) NOT NULL);
INSERT @t VALUES (N'UG_WW_APP_ADMIN_DEV', N'Administrator'), (N'UG_WW_APP_IDE_DEV', N'Designer'), (N'UG_WW_APP_IDE_RO_DEV', N'Viewer'), (N'UG_WW_HIST_ADMIN_DEV', N'Deployer');
IF EXISTS (SELECT 1 FROM dbo.LdapGroupMappings m JOIN @t t ON t.LdapGroupName = m.LdapGroupName WHERE m.Role <> t.Role)
THROW 50001, 'LdapGroupMappings maps one of these groups to a different role; resolve by hand.', 1;
INSERT dbo.LdapGroupMappings (LdapGroupName, Role)
SELECT t.LdapGroupName, t.Role FROM @t t
WHERE NOT EXISTS (SELECT 1 FROM dbo.LdapGroupMappings m WHERE m.LdapGroupName = t.LdapGroupName);
COMMIT;
SELECT Id, LdapGroupName, Role FROM dbo.LdapGroupMappings ORDER BY Id;
"@
$r = $cmd.ExecuteReader(); while ($r.Read()) { "{0} {1} -> {2}" -f $r[0],$r[1],$r[2] }; $c.Close()