diff --git a/app/Console/Commands/AddUser.php b/app/Console/Commands/AddUser.php new file mode 100644 index 0000000..418cb65 --- /dev/null +++ b/app/Console/Commands/AddUser.php @@ -0,0 +1,39 @@ +ask('Enter the user\'s first name'); + $lastName = $this->ask('Enter the user\'s last name'); + $email = $this->ask('Enter the user\'s email'); + + // Fetch available roles + $roles = Role::all()->pluck('label')->toArray(); + $role = $this->choice('Select a role for the user', $roles); + + // Create the user + $user = User::create([ + 'first' => $firstName, + 'last' => $lastName, + 'fullname' => "{$firstName} {$lastName}", + 'email' => $email, + 'username' => $email, + ]); + + // Assign the role to the user + $user->assignRole($role); // Ensure you have a method to assign roles + + $this->info("User {$firstName} {$lastName} with role {$role} has been created successfully."); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 69914e9..68dcef2 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -13,7 +13,7 @@ class Kernel extends ConsoleKernel * @var array */ protected $commands = [ - // + \App\Console\Commands\AddUser::class, ]; /** diff --git a/app/Models/User.php b/app/Models/User.php index 40a5460..1aa8c72 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -71,6 +71,18 @@ public function roles(): BelongsToMany return $this->belongsToMany(Role::class, 'appuser_role_link', 'uid', 'rid'); } + /** + * Assign a role to the user. + * + * @param string $roleName + * @return void + */ + public function assignRole(string $roleName) + { + $role = Role::where('label', $roleName)->firstOrFail(); + $this->roles()->attach($role); + } + /** * The permissions that belong to the user. */