-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-admin-user.ps1
More file actions
286 lines (249 loc) · 10.7 KB
/
setup-admin-user.ps1
File metadata and controls
286 lines (249 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# ============================================================================
# Admin User Setup Script for Library Booking System
# ============================================================================
# This script handles all admin user operations:
# - Create new admin user via API (ensures correct password hashing)
# - Approve existing admin user
# - Fix/recreate admin user if there are issues
#
# Usage:
# .\setup-admin-user.ps1 # Full setup (delete, create, approve)
# .\setup-admin-user.ps1 -ApproveOnly # Just approve existing user
# .\setup-admin-user.ps1 -Recreate # Delete and recreate (same as default)
#
# Admin Credentials:
# Username: admin1
# Password: 12345678a
# Email: admin@gmail.com
# Role: ADMIN
# ============================================================================
param(
[switch]$ApproveOnly, # Only approve existing user (don't create)
[switch]$Recreate # Delete and recreate (default behavior)
)
$ErrorActionPreference = "Stop"
# Admin user configurations
$admins = @(
@{
Username = "admin1"
Email = "admin@gmail.com"
Password = "12345678a"
Role = "ADMIN"
},
@{
Username = "admin2"
Email = "admin2@gmail.com"
Password = "12345678a"
Role = "ADMIN"
}
)
# Legacy single admin support (for backward compatibility)
$adminUsername = $admins[0].Username
$adminEmail = $admins[0].Email
$adminPassword = $admins[0].Password
$adminRole = $admins[0].Role
$apiBaseUrl = "http://localhost:8080"
$registerUrl = "$apiBaseUrl/api/auth/register"
Write-Host "=== Admin User Setup ===" -ForegroundColor Green
Write-Host ""
# Check if services are running
Write-Host "Checking if services are available..." -ForegroundColor Yellow
try {
$healthCheck = Invoke-WebRequest -Uri "$apiBaseUrl/api/auth/health" -UseBasicParsing -TimeoutSec 5 -ErrorAction Stop
Write-Host " [OK] Services are running" -ForegroundColor Green
} catch {
Write-Host " [ERROR] Services are not available. Please start services first:" -ForegroundColor Red
Write-Host " docker compose up -d" -ForegroundColor Gray
exit 1
}
# Function to check if user exists
function Test-AdminUserExists {
$result = docker exec library-postgres psql -U postgres -d user_db -t -A -c "SELECT username FROM users WHERE username = '$adminUsername';" 2>&1
return ($result -and $result.Trim() -eq $adminUsername)
}
# Function to approve user
function Approve-AdminUser {
Write-Host "Approving admin user..." -ForegroundColor Yellow
$updateResult = docker exec library-postgres psql -U postgres -d user_db -c @"
UPDATE users
SET pending_approval = false,
rejected = false,
restricted = false,
updated_at = NOW()
WHERE username = '$adminUsername';
"@
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK] User approved" -ForegroundColor Green
return $true
} else {
Write-Host " [ERROR] Failed to approve user" -ForegroundColor Red
return $false
}
}
# Function to delete user
function Remove-AdminUser {
Write-Host "Removing existing admin user..." -ForegroundColor Yellow
docker exec library-postgres psql -U postgres -d user_db -c "DELETE FROM users WHERE username = '$adminUsername' OR email = '$adminEmail';" | Out-Null
Start-Sleep -Seconds 1
Write-Host " [OK] Done" -ForegroundColor Green
}
# Function to create user via API
function New-AdminUser {
Write-Host "Registering admin user via API..." -ForegroundColor Yellow
$userData = @{
username = $adminUsername
email = $adminEmail
password = $adminPassword
role = $adminRole
} | ConvertTo-Json
try {
$response = Invoke-RestMethod -Uri $registerUrl -Method Post -Body $userData -ContentType "application/json" -ErrorAction Stop
Write-Host " [OK] User registered successfully" -ForegroundColor Green
Start-Sleep -Seconds 2 # Wait for database update
return $true
} catch {
$statusCode = $_.Exception.Response.StatusCode.value__
$errorMessage = $_.Exception.Message
if ($statusCode -eq 409) {
Write-Host " [WARN] User already exists" -ForegroundColor Yellow
return $true
} else {
Write-Host " [ERROR] Registration failed: $errorMessage" -ForegroundColor Red
Write-Host " Status Code: $statusCode" -ForegroundColor Yellow
return $false
}
}
}
# Function to verify user
function Test-AdminUser {
param($username)
if (-not $username) { $username = $adminUsername }
Write-Host "Verifying admin user: $username..." -ForegroundColor Yellow
$userInfo = docker exec library-postgres psql -U postgres -d user_db -t -A -F "|" -c "SELECT username, email, role, pending_approval, restricted, rejected FROM users WHERE username = '$username';"
if ($userInfo) {
$fields = $userInfo -split '\|'
Write-Host " [OK] User found:" -ForegroundColor Green
Write-Host " Username: $($fields[0])" -ForegroundColor Cyan
Write-Host " Email: $($fields[1])" -ForegroundColor Cyan
Write-Host " Role: $($fields[2])" -ForegroundColor Cyan
Write-Host " Pending Approval: $($fields[3])" -ForegroundColor $(if ($fields[3] -eq 'f') { 'Green' } else { 'Red' })
Write-Host " Restricted: $($fields[4])" -ForegroundColor $(if ($fields[4] -eq 'f') { 'Green' } else { 'Red' })
Write-Host " Rejected: $($fields[5])" -ForegroundColor $(if ($fields[5] -eq 'f') { 'Green' } else { 'Red' })
return $true
} else {
Write-Host " [ERROR] User not found!" -ForegroundColor Red
return $false
}
}
# Function to process all admin users
function Process-AllAdmins {
$successCount = 0
$failCount = 0
foreach ($admin in $admins) {
Write-Host "Processing admin: $($admin.Username)..." -ForegroundColor Yellow
# Check if user exists
$exists = docker exec library-postgres psql -U postgres -d user_db -t -A -c "SELECT username FROM users WHERE username = '$($admin.Username)';" 2>&1
$exists = ($exists -and $exists.Trim() -eq $admin.Username)
if ($exists) {
Write-Host " User exists, checking status..." -ForegroundColor Gray
$userInfo = docker exec library-postgres psql -U postgres -d user_db -t -A -F "|" -c "SELECT pending_approval, restricted, rejected FROM users WHERE username = '$($admin.Username)';"
if ($userInfo) {
$fields = $userInfo -split '\|'
$needsApproval = $fields[0] -eq 't' -or $fields[1] -eq 't' -or $fields[2] -eq 't'
if ($needsApproval) {
$updateResult = docker exec library-postgres psql -U postgres -d user_db -c @"
UPDATE users
SET pending_approval = false,
rejected = false,
restricted = false,
updated_at = NOW()
WHERE username = '$($admin.Username)';
"@ | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK] User approved" -ForegroundColor Green
$successCount++
} else {
Write-Host " [ERROR] Failed to approve user" -ForegroundColor Red
$failCount++
}
} else {
Write-Host " [OK] User already approved" -ForegroundColor Green
$successCount++
}
}
} else {
# Create new admin user
Write-Host " Creating new admin user..." -ForegroundColor Gray
$userData = @{
username = $admin.Username
email = $admin.Email
password = $admin.Password
role = $admin.Role
} | ConvertTo-Json
try {
$response = Invoke-RestMethod -Uri $registerUrl -Method Post -Body $userData -ContentType "application/json" -ErrorAction Stop
Write-Host " [OK] Registered successfully" -ForegroundColor Green
Start-Sleep -Seconds 2
# Approve the user
$updateResult = docker exec library-postgres psql -U postgres -d user_db -c @"
UPDATE users
SET pending_approval = false,
rejected = false,
restricted = false,
updated_at = NOW()
WHERE username = '$($admin.Username)';
"@ | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK] User created and approved" -ForegroundColor Green
$successCount++
} else {
Write-Host " [WARN] User created but approval failed" -ForegroundColor Yellow
$successCount++
}
} catch {
$statusCode = $_.Exception.Response.StatusCode.value__
if ($statusCode -eq 409) {
Write-Host " [WARN] User already exists" -ForegroundColor Yellow
$successCount++
} else {
Write-Host " [ERROR] Registration failed: $($_.Exception.Message)" -ForegroundColor Red
$failCount++
}
}
}
Write-Host ""
}
return @{ Success = $successCount; Failed = $failCount }
}
# Main execution logic
# Process all admin users
Write-Host "Mode: Processing all admin users" -ForegroundColor Cyan
Write-Host ""
$result = Process-AllAdmins
Write-Host "=== Summary ===" -ForegroundColor Green
Write-Host "Successfully processed: $($result.Success)" -ForegroundColor $(if ($result.Success -gt 0) { 'Green' } else { 'Red' })
Write-Host "Failed: $($result.Failed)" -ForegroundColor $(if ($result.Failed -gt 0) { 'Red' } else { 'Green' })
Write-Host ""
# Verify all admins
Write-Host "=== Verification ===" -ForegroundColor Green
foreach ($admin in $admins) {
Test-AdminUser -username $admin.Username
Write-Host ""
}
# Display credentials
Write-Host "=== Admin Credentials ===" -ForegroundColor Green
foreach ($admin in $admins) {
Write-Host "Admin ($($admin.Username)):" -ForegroundColor Cyan
Write-Host " Username: $($admin.Username)" -ForegroundColor Gray
Write-Host " Password: $($admin.Password)" -ForegroundColor Gray
Write-Host " Email: $($admin.Email)" -ForegroundColor Gray
Write-Host " Role: $($admin.Role)" -ForegroundColor Gray
Write-Host ""
}
if ($result.Failed -eq 0) {
Write-Host "All admin users created successfully!" -ForegroundColor Green
exit 0
} else {
Write-Host "Some admin users failed to create. Please check the errors above." -ForegroundColor Red
exit 1
}