@@ -167,6 +167,78 @@ export class DatabaseService {
167167 }
168168 }
169169
170+ /**
171+ * Resets the admin user account with a new random password.
172+ * If the admin user doesn't exist, it creates one.
173+ * Prints the new username and password to the console.
174+ * @returns {Promise<void> }
175+ */
176+ async resetAdminUser ( ) : Promise < void > {
177+ const prisma = new PrismaClient ( ) ;
178+ const adminUser = process . env . KUBERO_ADMIN_USERNAME || 'admin' ;
179+ const adminEmail = process . env . KUBERO_ADMIN_EMAIL || '[email protected] ' ; 180+ const role = process . env . KUBERO_SYSTEM_USER_ROLE || 'admin' ;
181+ const userGroups = [ 'everyone' , 'admin' ] ;
182+
183+ try {
184+ // Generate a random password
185+ const plainPassword = crypto
186+ . randomBytes ( 25 )
187+ . toString ( 'base64' )
188+ . slice ( 0 , 19 ) ;
189+ // Create bcrypt hash
190+ const passwordHash = await bcrypt . hash ( plainPassword , 10 ) ;
191+
192+ // Check if admin user exists
193+ const existingUser = await prisma . user . findUnique ( {
194+ where : { id : '2' } ,
195+ } ) ;
196+
197+ if ( existingUser ) {
198+ // Update existing admin user
199+ await prisma . user . update ( {
200+ where : { id : '2' } ,
201+ data : {
202+ password : passwordHash ,
203+ updatedAt : new Date ( ) ,
204+ } ,
205+ } ) ;
206+ console . log ( '\n\n\n' , 'Admin account has been reset' ) ;
207+ } else {
208+ // Create new admin user
209+ await prisma . user . create ( {
210+ data : {
211+ id : '2' ,
212+ username : adminUser ,
213+ email : adminEmail ,
214+ password : passwordHash ,
215+ isActive : true ,
216+ role : { connect : { name : role } } ,
217+ userGroups :
218+ userGroups && Array . isArray ( userGroups )
219+ ? {
220+ connect : userGroups . map ( ( g : any ) => ( { name : g } ) ) ,
221+ }
222+ : undefined ,
223+ createdAt : new Date ( ) ,
224+ updatedAt : new Date ( ) ,
225+ } ,
226+ } ) ;
227+ console . log ( '\n\n\n' , 'New admin account created' ) ;
228+ }
229+
230+ console . log ( ' username: ' , adminUser ) ;
231+ console . log ( ' password: ' , plainPassword ) ;
232+ console . log ( ' email: ' , adminEmail , '\n\n\n' ) ;
233+
234+ this . logger . log ( 'Admin user reset successfully.' ) ;
235+ return ;
236+ } catch ( error ) {
237+ this . logger . error ( 'Failed to reset admin user.' , error ) ;
238+ throw error ;
239+ }
240+ }
241+
170242 private async migrateLegeacyUsers ( ) {
171243 const prisma = new PrismaClient ( ) ;
172244
0 commit comments