Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.0.14
v0.0.15
256 changes: 112 additions & 144 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,32 +384,6 @@ public function user(string|int|null $user = null): ?Objects\User
return is_null($user) ? $this->user : new Objects\User($user);
}

/**
* Check if the module is installed
*
* @return bool
*/
public function isInstalled(): bool
{
// Check if the database has been initialized
if (is_null($this->Database)) {
return false;
}

// Check if the database is currently installed
if (!$this->Database->isInstalled()) {
return false;
}

// Check if the database is currently connected
if (!$this->Database->isConnected()) {
return false;
}

// Check if the module is currently installed
return $this->Config->reload('auth')->get('auth', 'installed') === true;
}

/**
* Install the module
*
Expand All @@ -430,127 +404,121 @@ public function install(array $config): array
// Check if the database has been initialized
if (!is_null($this->Database)) {

// Check if the database is currently installed
if ($this->Database->isInstalled()) {

// Connect to the database
$this->Database->connect();

// Check if the database is currently connected
if ($this->Database->isConnected()) {

// Create the organization
$Query = $this->Database->query()
->table('organizations')
->insert([
'owner' => $config['username']
]);
$affected = $Query->execute();
$organizationId = $Query->lastId();

// Create the organization vCard
$Query = $this->Database->query()
->table('vcards')
->insert([
'owner' => $config['username'],
'category' => 'Organization',
'name' => $config['organization'],
'organization' => $organizationId
]);
$affected += $Query->execute();
$organizationVcardId = $Query->lastId();

// Create the user vCard
$Query = $this->Database->query()
->table('vcards')
->insert([
'owner' => $config['username'],
'category' => 'User',
'email' => $config['username'],
'organization' => $organizationId
]);
$affected += $Query->execute();
$userVcardId = $Query->lastId();

// Create the user backend
$Query = $this->Database->query()
->table('backends')
->insert([
'owner' => $config['username'],
'type' => 'local',
'password' => password_hash($config['password'], PASSWORD_DEFAULT),
'organization' => $organizationId
]);
$affected += $Query->execute();
$userBackendId = $Query->lastId();

// Create the user api token
$Query = $this->Database->query()
->table('tokens')
->insert([
'owner' => $config['username'],
'token' => $UUID->toString($config['username'])
]);
$affected += $Query->execute();
$userTokenId = $Query->lastId();

// Create the user
$Query = $this->Database->query()
->table('users')
->insert([
'owner' => $config['username'],
'username' => $config['username'],
'backend' => $userBackendId,
'vcard' => $userVcardId,
'organization' => $organizationId,
'token' => $userTokenId,
'isVerified' => 1
]);
$affected += $Query->execute();
$userId = $Query->lastId();

// Update the organization
$Query = $this->Database->query()
->table('organizations')
->update([
'users' => json_encode([$userId], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT),
'vcard' => $organizationVcardId,
'isActive' => 1
])
->where('id', $organizationId);
$affected += $Query->execute();

// Update the user token
$Query = $this->Database->query()
->table('tokens')
->update([
'user' => $userId
])
->where('id', $userTokenId);
$affected += $Query->execute();

// Update the group membership
$Query = $this->Database->query()
->table('groups')
->update([
'users' => json_encode([$userId], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
])
->where('name', 'Administrator');
$affected += $Query->execute();

// Check if the database records were created
if($affected >= 9){

// Add a true status
$status[] = true;
} else {
$status[] = "Failed to install";
}
// Connect to the database
$this->Database->connect();

// Check if the database is currently connected
if ($this->Database->isConnected()) {

// Create the organization
$Query = $this->Database->query()
->table('organizations')
->insert([
'owner' => $config['username']
]);
$affected = $Query->execute();
$organizationId = $Query->lastId();

// Create the organization vCard
$Query = $this->Database->query()
->table('vcards')
->insert([
'owner' => $config['username'],
'category' => 'Organization',
'name' => $config['organization'],
'organization' => $organizationId
]);
$affected += $Query->execute();
$organizationVcardId = $Query->lastId();

// Create the user vCard
$Query = $this->Database->query()
->table('vcards')
->insert([
'owner' => $config['username'],
'category' => 'User',
'email' => $config['username'],
'organization' => $organizationId
]);
$affected += $Query->execute();
$userVcardId = $Query->lastId();

// Create the user backend
$Query = $this->Database->query()
->table('backends')
->insert([
'owner' => $config['username'],
'type' => 'local',
'password' => password_hash($config['password'], PASSWORD_DEFAULT),
'organization' => $organizationId
]);
$affected += $Query->execute();
$userBackendId = $Query->lastId();

// Create the user api token
$Query = $this->Database->query()
->table('tokens')
->insert([
'owner' => $config['username'],
'token' => $UUID->toString($config['username'])
]);
$affected += $Query->execute();
$userTokenId = $Query->lastId();

// Create the user
$Query = $this->Database->query()
->table('users')
->insert([
'owner' => $config['username'],
'username' => $config['username'],
'backend' => $userBackendId,
'vcard' => $userVcardId,
'organization' => $organizationId,
'token' => $userTokenId,
'isVerified' => 1
]);
$affected += $Query->execute();
$userId = $Query->lastId();

// Update the organization
$Query = $this->Database->query()
->table('organizations')
->update([
'users' => json_encode([$userId], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT),
'vcard' => $organizationVcardId,
'isActive' => 1
])
->where('id', $organizationId);
$affected += $Query->execute();

// Update the user token
$Query = $this->Database->query()
->table('tokens')
->update([
'user' => $userId
])
->where('id', $userTokenId);
$affected += $Query->execute();

// Update the group membership
$Query = $this->Database->query()
->table('groups')
->update([
'users' => json_encode([$userId], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
])
->where('name', 'Administrator');
$affected += $Query->execute();

// Check if the database records were created
if($affected >= 9){

// Add a true status
$status[] = true;
} else {
$status[] = "Database not connected";
$status[] = "Failed to install";
}
} else {
$status[] = "Database not installed";
$status[] = "Database not connected";
}
} else {
$status[] = "Database not initialized";
Expand Down
10 changes: 0 additions & 10 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,6 @@ public function schema(): Objects\Schema
return new Objects\Schema($this->connector);
}

/**
* Check if the module is installed
*
* @return bool
*/
public function isInstalled(): bool
{
return filter_var($this->Config->reload('database')->get('database', 'installed'), FILTER_VALIDATE_BOOLEAN);
}

/**
* Install the module
*
Expand Down