@@ -384,32 +384,6 @@ public function user(string|int|null $user = null): ?Objects\User
384384 return is_null ($ user ) ? $ this ->user : new Objects \User ($ user );
385385 }
386386
387- /**
388- * Check if the module is installed
389- *
390- * @return bool
391- */
392- public function isInstalled (): bool
393- {
394- // Check if the database has been initialized
395- if (is_null ($ this ->Database )) {
396- return false ;
397- }
398-
399- // Check if the database is currently installed
400- if (!$ this ->Database ->isInstalled ()) {
401- return false ;
402- }
403-
404- // Check if the database is currently connected
405- if (!$ this ->Database ->isConnected ()) {
406- return false ;
407- }
408-
409- // Check if the module is currently installed
410- return $ this ->Config ->reload ('auth ' )->get ('auth ' , 'installed ' ) === true ;
411- }
412-
413387 /**
414388 * Install the module
415389 *
@@ -430,127 +404,121 @@ public function install(array $config): array
430404 // Check if the database has been initialized
431405 if (!is_null ($ this ->Database )) {
432406
433- // Check if the database is currently installed
434- if ($ this ->Database ->isInstalled ()) {
435-
436- // Connect to the database
437- $ this ->Database ->connect ();
438-
439- // Check if the database is currently connected
440- if ($ this ->Database ->isConnected ()) {
441-
442- // Create the organization
443- $ Query = $ this ->Database ->query ()
444- ->table ('organizations ' )
445- ->insert ([
446- 'owner ' => $ config ['username ' ]
447- ]);
448- $ affected = $ Query ->execute ();
449- $ organizationId = $ Query ->lastId ();
450-
451- // Create the organization vCard
452- $ Query = $ this ->Database ->query ()
453- ->table ('vcards ' )
454- ->insert ([
455- 'owner ' => $ config ['username ' ],
456- 'category ' => 'Organization ' ,
457- 'name ' => $ config ['organization ' ],
458- 'organization ' => $ organizationId
459- ]);
460- $ affected += $ Query ->execute ();
461- $ organizationVcardId = $ Query ->lastId ();
462-
463- // Create the user vCard
464- $ Query = $ this ->Database ->query ()
465- ->table ('vcards ' )
466- ->insert ([
467- 'owner ' => $ config ['username ' ],
468- 'category ' => 'User ' ,
469- 'email ' => $ config ['username ' ],
470- 'organization ' => $ organizationId
471- ]);
472- $ affected += $ Query ->execute ();
473- $ userVcardId = $ Query ->lastId ();
474-
475- // Create the user backend
476- $ Query = $ this ->Database ->query ()
477- ->table ('backends ' )
478- ->insert ([
479- 'owner ' => $ config ['username ' ],
480- 'type ' => 'local ' ,
481- 'password ' => password_hash ($ config ['password ' ], PASSWORD_DEFAULT ),
482- 'organization ' => $ organizationId
483- ]);
484- $ affected += $ Query ->execute ();
485- $ userBackendId = $ Query ->lastId ();
486-
487- // Create the user api token
488- $ Query = $ this ->Database ->query ()
489- ->table ('tokens ' )
490- ->insert ([
491- 'owner ' => $ config ['username ' ],
492- 'token ' => $ UUID ->toString ($ config ['username ' ])
493- ]);
494- $ affected += $ Query ->execute ();
495- $ userTokenId = $ Query ->lastId ();
496-
497- // Create the user
498- $ Query = $ this ->Database ->query ()
499- ->table ('users ' )
500- ->insert ([
501- 'owner ' => $ config ['username ' ],
502- 'username ' => $ config ['username ' ],
503- 'backend ' => $ userBackendId ,
504- 'vcard ' => $ userVcardId ,
505- 'organization ' => $ organizationId ,
506- 'token ' => $ userTokenId ,
507- 'isVerified ' => 1
508- ]);
509- $ affected += $ Query ->execute ();
510- $ userId = $ Query ->lastId ();
511-
512- // Update the organization
513- $ Query = $ this ->Database ->query ()
514- ->table ('organizations ' )
515- ->update ([
516- 'users ' => json_encode ([$ userId ], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ),
517- 'vcard ' => $ organizationVcardId ,
518- 'isActive ' => 1
519- ])
520- ->where ('id ' , $ organizationId );
521- $ affected += $ Query ->execute ();
522-
523- // Update the user token
524- $ Query = $ this ->Database ->query ()
525- ->table ('tokens ' )
526- ->update ([
527- 'user ' => $ userId
528- ])
529- ->where ('id ' , $ userTokenId );
530- $ affected += $ Query ->execute ();
531-
532- // Update the group membership
533- $ Query = $ this ->Database ->query ()
534- ->table ('groups ' )
535- ->update ([
536- 'users ' => json_encode ([$ userId ], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT )
537- ])
538- ->where ('name ' , 'Administrator ' );
539- $ affected += $ Query ->execute ();
540-
541- // Check if the database records were created
542- if ($ affected >= 9 ){
543-
544- // Add a true status
545- $ status [] = true ;
546- } else {
547- $ status [] = "Failed to install " ;
548- }
407+ // Connect to the database
408+ $ this ->Database ->connect ();
409+
410+ // Check if the database is currently connected
411+ if ($ this ->Database ->isConnected ()) {
412+
413+ // Create the organization
414+ $ Query = $ this ->Database ->query ()
415+ ->table ('organizations ' )
416+ ->insert ([
417+ 'owner ' => $ config ['username ' ]
418+ ]);
419+ $ affected = $ Query ->execute ();
420+ $ organizationId = $ Query ->lastId ();
421+
422+ // Create the organization vCard
423+ $ Query = $ this ->Database ->query ()
424+ ->table ('vcards ' )
425+ ->insert ([
426+ 'owner ' => $ config ['username ' ],
427+ 'category ' => 'Organization ' ,
428+ 'name ' => $ config ['organization ' ],
429+ 'organization ' => $ organizationId
430+ ]);
431+ $ affected += $ Query ->execute ();
432+ $ organizationVcardId = $ Query ->lastId ();
433+
434+ // Create the user vCard
435+ $ Query = $ this ->Database ->query ()
436+ ->table ('vcards ' )
437+ ->insert ([
438+ 'owner ' => $ config ['username ' ],
439+ 'category ' => 'User ' ,
440+ 'email ' => $ config ['username ' ],
441+ 'organization ' => $ organizationId
442+ ]);
443+ $ affected += $ Query ->execute ();
444+ $ userVcardId = $ Query ->lastId ();
445+
446+ // Create the user backend
447+ $ Query = $ this ->Database ->query ()
448+ ->table ('backends ' )
449+ ->insert ([
450+ 'owner ' => $ config ['username ' ],
451+ 'type ' => 'local ' ,
452+ 'password ' => password_hash ($ config ['password ' ], PASSWORD_DEFAULT ),
453+ 'organization ' => $ organizationId
454+ ]);
455+ $ affected += $ Query ->execute ();
456+ $ userBackendId = $ Query ->lastId ();
457+
458+ // Create the user api token
459+ $ Query = $ this ->Database ->query ()
460+ ->table ('tokens ' )
461+ ->insert ([
462+ 'owner ' => $ config ['username ' ],
463+ 'token ' => $ UUID ->toString ($ config ['username ' ])
464+ ]);
465+ $ affected += $ Query ->execute ();
466+ $ userTokenId = $ Query ->lastId ();
467+
468+ // Create the user
469+ $ Query = $ this ->Database ->query ()
470+ ->table ('users ' )
471+ ->insert ([
472+ 'owner ' => $ config ['username ' ],
473+ 'username ' => $ config ['username ' ],
474+ 'backend ' => $ userBackendId ,
475+ 'vcard ' => $ userVcardId ,
476+ 'organization ' => $ organizationId ,
477+ 'token ' => $ userTokenId ,
478+ 'isVerified ' => 1
479+ ]);
480+ $ affected += $ Query ->execute ();
481+ $ userId = $ Query ->lastId ();
482+
483+ // Update the organization
484+ $ Query = $ this ->Database ->query ()
485+ ->table ('organizations ' )
486+ ->update ([
487+ 'users ' => json_encode ([$ userId ], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ),
488+ 'vcard ' => $ organizationVcardId ,
489+ 'isActive ' => 1
490+ ])
491+ ->where ('id ' , $ organizationId );
492+ $ affected += $ Query ->execute ();
493+
494+ // Update the user token
495+ $ Query = $ this ->Database ->query ()
496+ ->table ('tokens ' )
497+ ->update ([
498+ 'user ' => $ userId
499+ ])
500+ ->where ('id ' , $ userTokenId );
501+ $ affected += $ Query ->execute ();
502+
503+ // Update the group membership
504+ $ Query = $ this ->Database ->query ()
505+ ->table ('groups ' )
506+ ->update ([
507+ 'users ' => json_encode ([$ userId ], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT )
508+ ])
509+ ->where ('name ' , 'Administrator ' );
510+ $ affected += $ Query ->execute ();
511+
512+ // Check if the database records were created
513+ if ($ affected >= 9 ){
514+
515+ // Add a true status
516+ $ status [] = true ;
549517 } else {
550- $ status [] = "Database not connected " ;
518+ $ status [] = "Failed to install " ;
551519 }
552520 } else {
553- $ status [] = "Database not installed " ;
521+ $ status [] = "Database not connected " ;
554522 }
555523 } else {
556524 $ status [] = "Database not initialized " ;
0 commit comments