Skip to content
Open
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ Designed to be easy to instal, customise and expand upon. Using a modular approa
* Dashboard.php
* Home.php
* Superadmin.php
* Database
* Migrations
* 20210708160915_create_auth_logins_table.php
* 20210708160915_create_auth_tokens_table.php
* 20210708160915_create_user_roles_table.php
* 20210708160915_create_users_table.php
* Seeds
* UserSeeder.php
* Filters
* Auth.php
* Language
Expand Down Expand Up @@ -82,7 +90,11 @@ Dowload the package and extract into your project route. There are no files that

Create your database and import
```
db.sql
php spark migrate --all
```
Create demo users
```
php spark db:seed UserSeeder
```

## Configuration
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateAuthLoginsTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true,
],
'user_id' => [
'type' => 'INT',
'constraint' => 50,
],
'firstname' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
'lastname' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
'role' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
'ip_address' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
'date' => [
'type' => 'DATETIME',
],
'successfull' => [
'type' => 'INT',
'constraint' => 2,
],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('auth_logins');
}

public function down()
{
$this->forge->dropTable('auth_logins');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateAuthTokensTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true,
],
'user_id' => [
'type' => 'INT',
'constraint' => 11,
],
'selector' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
'hashedvalidator' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
'expires' => [
'type' => 'DATETIME',
],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('auth_tokens');
}

public function down()
{
$this->forge->dropTable('auth_tokens');
}
}
31 changes: 31 additions & 0 deletions app/Database/Migrations/20210708160915_create_user_roles_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateUserRolesTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true,
],
'role_name' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('user_roles');
}

public function down()
{
$this->forge->dropTable('user_roles');
}
}
86 changes: 86 additions & 0 deletions app/Database/Migrations/20210708160915_create_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateUsersTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true,
],
'firstname' => [
'type' => 'VARCHAR',
'constraint' => '50',
],
'lastname' => [
'type' => 'VARCHAR',
'constraint' => '50',
],
'email' => [
'type' => 'VARCHAR',
'constraint' => '50',
],
'password' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
'reset_token' => [
'type' => 'VARCHAR',
'constraint' => '250',
],
'reset_expire' => [
'type' => 'DATETIME',
'null' => true,
],
'activated' => [
'type' => 'TINYINT',
'constraint' => 1,
'default' => 0,
],
'activate_token' => [
'type' => 'VARCHAR',
'constraint' => '250',
'null' => true,
],
'activate_expire' => [
'type' => 'VARCHAR',
'constraint' => '250',
'null' => true,
],
'role' => [
'type' => 'INT',
'constraint' => 11,
],
'created_at datetime NOT NULL DEFAULT current_timestamp()',
'updated_at datetime NOT NULL DEFAULT current_timestamp()',
// 'created_at' => [
// 'type' => 'DATETIME',
// 'null' => false,
// 'default' => current_timestamp(),
// ],
// 'updated_at' => [
// 'type' => 'DATETIME',
// 'null' => false,
// 'default' => current_timestamp(),
// ],
'deleted_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('users');
}

public function down()
{
$this->forge->dropTable('users');
}
}
Empty file added app/Database/Seeds/.gitkeep
Empty file.
81 changes: 81 additions & 0 deletions app/Database/Seeds/UserSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Database\Seeds;

use App\Models\AuthModel;
use CodeIgniter\Database\Seeder;

/**
* Class UserSeeder
*
* A seeder to insert some sample users with roles into the database.
*/
class UserSeeder extends Seeder
{
/**
* @var AuthModel
*/
protected $authModel;

/**
* Runs the seeder.
*/
public function run()
{
// Initialize the model
$this->authModel = new AuthModel();

// Get the available roles from the config file
$assignRoles = config('Auth')->assignRoles;

// Define an array of sample users without roles
$users = [
[
'firstname' => 'Alice',
'lastname' => 'Smith',
'email' => 'super-admin@example.com',
'password' => 'secret',
'activated' => '1',
],
[
'firstname' => 'Bob',
'lastname' => 'Jones',
'email' => 'tenant@example.com',
'password' => 'secret',
'activated' => '1',
],
[
'firstname' => 'Charlie',
'lastname' => 'Brown',
'email' => 'customer@example.com',
'password' => 'secret',
'activated' => '1',
],
];

// Define an array of roles for each user
$roles = [
$assignRoles['Super Admin'],
$assignRoles['Tenant'],
$assignRoles['Customer'],
];

// Loop through the users and roles arrays and assign the roles to the users
$i = 0;
foreach ($assignRoles as $assignRoleValue) {
$users[$i]['role'] = $assignRoleValue;
$i++;
}

// Insert each user using the model
foreach ($users as $user) {
$this->authModel->save($user);

// Get the role key from the assign roles array using the role value
$roleKey = array_search($user['role'], $assignRoles);

// Display a message with the email and password of the user
echo "User created with email: {$user['email']}, password: secret and role: {$roleKey}\n";
}
}
}