|
| 1 | +<?php namespace Database\Seeders; |
| 2 | + |
| 3 | +use Illuminate\Database\Seeder; |
| 4 | +use Illuminate\Support\Facades\DB; |
| 5 | +use Illuminate\Support\Facades\Schema; |
| 6 | + |
| 7 | +class AdminUserTableSeeder extends Seeder |
| 8 | +{ |
| 9 | + public function run(): void |
| 10 | + { |
| 11 | + if (!Schema::hasTable('users') || !Schema::hasTable('user_attributes')) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + $username = (string) env('EVO_ADMIN_USERNAME', 'admin'); |
| 16 | + $email = (string) env('EVO_ADMIN_EMAIL', 'admin@example.com'); |
| 17 | + $password = (string) env('EVO_ADMIN_PASSWORD', 'admin'); |
| 18 | + |
| 19 | + $roleId = 1; |
| 20 | + if (Schema::hasTable('user_roles')) { |
| 21 | + $adminRoleId = DB::table('user_roles')->where('name', 'Administrator')->value('id'); |
| 22 | + if ($adminRoleId !== null) { |
| 23 | + $roleId = (int) $adminRoleId; |
| 24 | + } else { |
| 25 | + $minRoleId = DB::table('user_roles')->min('id'); |
| 26 | + if ($minRoleId !== null) { |
| 27 | + $roleId = (int) $minRoleId; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + $now = time(); |
| 33 | + |
| 34 | + DB::transaction(function () use ($username, $email, $password, $roleId, $now): void { |
| 35 | + $existingId = DB::table('users')->where('username', $username)->value('id'); |
| 36 | + |
| 37 | + $usersData = [ |
| 38 | + 'username' => $username, |
| 39 | + 'password' => md5($password), |
| 40 | + 'cachepwd' => '', |
| 41 | + 'refresh_token' => null, |
| 42 | + 'access_token' => null, |
| 43 | + 'valid_to' => null, |
| 44 | + 'verified_key' => null, |
| 45 | + ]; |
| 46 | + |
| 47 | + $userId = null; |
| 48 | + if ($existingId !== null) { |
| 49 | + $userId = (int) $existingId; |
| 50 | + } else { |
| 51 | + $usersColumns = Schema::getColumnListing('users'); |
| 52 | + $usersInsert = array_intersect_key($usersData, array_flip($usersColumns)); |
| 53 | + $userId = (int) DB::table('users')->insertGetId($usersInsert); |
| 54 | + } |
| 55 | + |
| 56 | + if ($userId <= 0) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + $attributesData = [ |
| 61 | + 'internalKey' => $userId, |
| 62 | + 'fullname' => $username, |
| 63 | + 'first_name' => null, |
| 64 | + 'last_name' => null, |
| 65 | + 'middle_name' => null, |
| 66 | + 'role' => $roleId, |
| 67 | + 'email' => $email, |
| 68 | + 'phone' => '', |
| 69 | + 'mobilephone' => '', |
| 70 | + 'blocked' => 0, |
| 71 | + 'blockeduntil' => 0, |
| 72 | + 'blockedafter' => 0, |
| 73 | + 'logincount' => 0, |
| 74 | + 'lastlogin' => 0, |
| 75 | + 'thislogin' => 0, |
| 76 | + 'failedlogincount' => 0, |
| 77 | + 'sessionid' => '', |
| 78 | + 'dob' => null, |
| 79 | + 'gender' => 0, |
| 80 | + 'country' => '', |
| 81 | + 'street' => '', |
| 82 | + 'city' => '', |
| 83 | + 'state' => '', |
| 84 | + 'zip' => '', |
| 85 | + 'fax' => '', |
| 86 | + 'photo' => '', |
| 87 | + 'comment' => null, |
| 88 | + 'createdon' => $now, |
| 89 | + 'editedon' => $now, |
| 90 | + 'verified' => 1, |
| 91 | + ]; |
| 92 | + |
| 93 | + $attributesColumns = Schema::getColumnListing('user_attributes'); |
| 94 | + $attributesInsert = array_intersect_key($attributesData, array_flip($attributesColumns)); |
| 95 | + |
| 96 | + $attributesExists = DB::table('user_attributes')->where('internalKey', $userId)->exists(); |
| 97 | + if (!$attributesExists) { |
| 98 | + DB::table('user_attributes')->insert($attributesInsert); |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | +} |
0 commit comments