From 91b8a27b3fef3bf5522ca1bc3b764850c31b07e1 Mon Sep 17 00:00:00 2001 From: Oleg Kosarev Date: Sun, 9 Jul 2023 22:54:34 -0500 Subject: [PATCH] Make Migrations and Seeds Convert sql file to Migration files. Make seed for loop make all demo user for all roles in default config. --- README.md | 14 ++- app/Database/Migrations/.gitkeep | 0 ...0210708160915_create_auth_logins_table.php | 54 ++++++++++++ ...0210708160915_create_auth_tokens_table.php | 42 +++++++++ ...20210708160915_create_user_roles_table.php | 31 +++++++ .../20210708160915_create_users_table.php | 86 +++++++++++++++++++ app/Database/Seeds/.gitkeep | 0 app/Database/Seeds/UserSeeder.php | 81 +++++++++++++++++ 8 files changed, 307 insertions(+), 1 deletion(-) create mode 100644 app/Database/Migrations/.gitkeep create mode 100644 app/Database/Migrations/20210708160915_create_auth_logins_table.php create mode 100644 app/Database/Migrations/20210708160915_create_auth_tokens_table.php create mode 100644 app/Database/Migrations/20210708160915_create_user_roles_table.php create mode 100644 app/Database/Migrations/20210708160915_create_users_table.php create mode 100644 app/Database/Seeds/.gitkeep create mode 100644 app/Database/Seeds/UserSeeder.php diff --git a/README.md b/README.md index 3894769..ea0322e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/app/Database/Migrations/.gitkeep b/app/Database/Migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/app/Database/Migrations/20210708160915_create_auth_logins_table.php b/app/Database/Migrations/20210708160915_create_auth_logins_table.php new file mode 100644 index 0000000..dc57441 --- /dev/null +++ b/app/Database/Migrations/20210708160915_create_auth_logins_table.php @@ -0,0 +1,54 @@ +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'); + } +} \ No newline at end of file diff --git a/app/Database/Migrations/20210708160915_create_auth_tokens_table.php b/app/Database/Migrations/20210708160915_create_auth_tokens_table.php new file mode 100644 index 0000000..7892fe4 --- /dev/null +++ b/app/Database/Migrations/20210708160915_create_auth_tokens_table.php @@ -0,0 +1,42 @@ +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'); + } +} \ No newline at end of file diff --git a/app/Database/Migrations/20210708160915_create_user_roles_table.php b/app/Database/Migrations/20210708160915_create_user_roles_table.php new file mode 100644 index 0000000..3be605a --- /dev/null +++ b/app/Database/Migrations/20210708160915_create_user_roles_table.php @@ -0,0 +1,31 @@ +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'); + } +} \ No newline at end of file diff --git a/app/Database/Migrations/20210708160915_create_users_table.php b/app/Database/Migrations/20210708160915_create_users_table.php new file mode 100644 index 0000000..5cb97e5 --- /dev/null +++ b/app/Database/Migrations/20210708160915_create_users_table.php @@ -0,0 +1,86 @@ +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'); + } +} \ No newline at end of file diff --git a/app/Database/Seeds/.gitkeep b/app/Database/Seeds/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/app/Database/Seeds/UserSeeder.php b/app/Database/Seeds/UserSeeder.php new file mode 100644 index 0000000..66c980a --- /dev/null +++ b/app/Database/Seeds/UserSeeder.php @@ -0,0 +1,81 @@ +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"; + } + } +} \ No newline at end of file