-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add command for user create some code refactor
- Loading branch information
zoranbogoevski
committed
Mar 15, 2024
1 parent
6bf3a2c
commit 1adee70
Showing
30 changed files
with
388 additions
and
404 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Modules\Core\Traits; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
|
||
trait AutoRegistersCommands | ||
{ | ||
/** | ||
* Automatically registers console commands for a module. | ||
* | ||
* @param string $moduleName The name of the module. | ||
*/ | ||
protected function autoRegisterCommands(string $moduleName): void | ||
{ | ||
$commandsDirectory = module_path($moduleName, 'Console/Commands'); | ||
$namespace = 'Modules\\'.$moduleName.'\\Console\\Commands\\'; | ||
|
||
foreach (glob($commandsDirectory.'/*.php') as $commandFile) { | ||
$commandClassName = basename($commandFile, '.php'); | ||
$commandClass = $namespace.$commandClassName; | ||
|
||
if (is_subclass_of($commandClass, Command::class)) { | ||
$this->commands([$commandClass]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Modules\User\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Validator; | ||
use Modules\Role\Models\Role; | ||
use Modules\User\Models\User; | ||
|
||
class CreateUserCommand extends Command | ||
{ | ||
protected $signature = 'user:create'; | ||
protected $description = 'Create a new user and assign a role'; | ||
|
||
public function handle(): void | ||
{ | ||
$name = $this->ask('What is the user\'s name?'); | ||
$email = $this->ask('What is the user\'s email?'); | ||
$password = $this->secret('What is the user\'s password?'); | ||
|
||
// Validate input | ||
$validator = Validator::make(compact('name', 'email', 'password'), [ | ||
'name' => ['required', 'string', 'max:255'], | ||
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], | ||
'password' => ['required', 'string', 'min:8'], | ||
]); | ||
|
||
if ($validator->fails()) { | ||
$this->error('User not created. See error messages below:'); | ||
foreach ($validator->errors()->all() as $error) { | ||
$this->error($error); | ||
} | ||
return; | ||
} | ||
|
||
// Create user | ||
$user = User::create([ | ||
'name' => $name, | ||
'email' => $email, | ||
'password' => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", | ||
]); | ||
|
||
// Get available roles | ||
$roles = Role::pluck('name'); | ||
$roleName = $this->choice('Which role do you want to assign to the user?', $roles->toArray()); | ||
|
||
// Assign role | ||
$user->assignRole($roleName); | ||
|
||
$this->info("User {$user->name} created successfully and assigned the role of {$roleName}."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
namespace Modules\User\Database\Seeders; | ||
|
||
use Illuminate\Database\Seeder; | ||
use Illuminate\Support\Facades\Hash; | ||
use Modules\User\Models\User; | ||
use Spatie\Permission\Models\Permission; | ||
use Spatie\Permission\Models\Role; | ||
use Spatie\Permission\PermissionRegistrar; | ||
|
@@ -76,11 +74,6 @@ public function run(): void | |
// Super-admin gets all permissions | ||
$role3 = Role::create(['name' => 'super-admin']); | ||
$role3->givePermissionTo(Permission::all()); | ||
|
||
// Create demo users and assign roles | ||
$this->createUserWithRole('Example User', '[email protected]', 'manager'); | ||
$this->createUserWithRole('Example client User', '[email protected]', 'client'); | ||
$this->createUserWithRole('Example Super-Admin User', '[email protected]', 'super-admin'); | ||
} | ||
|
||
/** | ||
|
@@ -97,13 +90,4 @@ private function createRoleWithPermissions(string $roleName, array $resources, a | |
} | ||
} | ||
|
||
private function createUserWithRole(string $name, string $email, string $roleName): void | ||
{ | ||
$user = User::factory()->create([ | ||
'name' => $name, | ||
'email' => $email, | ||
'password' => Hash::make('test') | ||
]); | ||
$user->assignRole($roleName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,6 @@ | |
|
||
class UserObserver | ||
{ | ||
public function creating(User $user): void | ||
{ | ||
if ($user->email != '[email protected]') { | ||
$user->assignRole('client'); | ||
} | ||
|
||
if (!is_null($user->password)) { | ||
$user->password = Hash::make($user->password); | ||
} | ||
} | ||
|
||
public function updating(User $user): void | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Foundation\Auth\AuthenticatesUsers; | ||
use App\Http\Controllers\Controller; | ||
use Illuminate\Foundation\Auth\AuthenticatesUsers; | ||
|
||
class LoginController extends Controller | ||
{ | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Login Controller | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This controller handles authenticating users for the application and | ||
| redirecting them to your home screen. The controller uses a trait | ||
| to conveniently provide its functionality to your applications. | ||
| | ||
*/ | ||
use AuthenticatesUsers; | ||
class LoginController extends Controller | ||
{ | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Login Controller | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This controller handles authenticating users for the application and | ||
| redirecting them to your home screen. The controller uses a trait | ||
| to conveniently provide its functionality to your applications. | ||
| | ||
*/ | ||
|
||
use AuthenticatesUsers; | ||
|
||
/** | ||
* Where to redirect users after login. | ||
* | ||
* @var string | ||
*/ | ||
protected string $redirectTo = 'admin'; | ||
/** | ||
* Where to redirect users after login. | ||
* | ||
* @var string | ||
*/ | ||
protected $redirectTo = '/admin'; | ||
|
||
/** | ||
* Create a new controller instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->middleware('guest', ['except' => 'logout']); | ||
} | ||
/** | ||
* Create a new controller instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->middleware('guest')->except('logout'); | ||
} | ||
} |
Oops, something went wrong.