Skip to content

Commit

Permalink
fix user create error with password
Browse files Browse the repository at this point in the history
add command for user create some code refactor
  • Loading branch information
zoranbogoevski committed Mar 15, 2024
1 parent 6bf3a2c commit 1adee70
Show file tree
Hide file tree
Showing 30 changed files with 388 additions and 404 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function up()
$table->enum('status', ['active', 'inactive'])->default('active');
$table->unsignedBigInteger('parent_id')->nullable()->index();
$table->foreign('parent_id')->references('id')->on('categories');
$table->integer('_lft')->nullable();
$table->integer('_rgt')->nullable();
$table->timestamps();
});
}
Expand Down
18 changes: 9 additions & 9 deletions Modules/Category/Service/CategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@

class CategoryService
{
public CategoryRepository $category_repository;
public CategoryRepository $categoryRepository;

public function __construct(CategoryRepository $category_repository)
public function __construct(CategoryRepository $categoryRepository)
{
$this->category_repository = $category_repository;
$this->categoryRepository = $categoryRepository;
}

public function getAll()
{
return $this->category_repository->findAll();
return $this->categoryRepository->findAll();
}

public function store(array $data)
{
return $this->category_repository->create($data);
return $this->categoryRepository->create($data);
}

public function show(int $id)
{
return $this->category_repository->findById($id);
return $this->categoryRepository->findById($id);
}

public function edit(int $id)
{
return $this->category_repository->findById($id);
return $this->categoryRepository->findById($id);
}

public function update(int $id, array $data)
{
return $this->category_repository->update($id, $data);
return $this->categoryRepository->update($id, $data);
}

public function destroy(int $id): void
{
$this->category_repository->delete($id);
$this->categoryRepository->delete($id);
}
}
28 changes: 28 additions & 0 deletions Modules/Core/Traits/AutoRegistersCommands.php
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]);
}
}
}
}
52 changes: 52 additions & 0 deletions Modules/User/Console/Commands/CreateUserCommand.php
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}.");
}
}
3 changes: 0 additions & 3 deletions Modules/User/Database/Factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public function definition(): array
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => Carbon::now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'photo' => $this->faker->word,
'provider' => $this->faker->word,
'provider_id' => $this->faker->word,
'remember_token' => Str::random(10),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
Expand Down
16 changes: 0 additions & 16 deletions Modules/User/Database/Seeders/PermissionTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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);
}
}
10 changes: 0 additions & 10 deletions Modules/User/Models/Observers/UserObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 1 addition & 4 deletions Modules/User/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class User extends Authenticatable
use Impersonate;

protected $table = 'users';

/**
* @var string[]
*/
Expand All @@ -117,9 +117,6 @@ class User extends Authenticatable
'email',
'email_verified_at',
'password',
'photo',
'provider',
'provider_id',
'status',
'remember_token',
];
Expand Down
12 changes: 8 additions & 4 deletions Modules/User/Providers/UserServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,45 @@

use Config;
use Illuminate\Support\ServiceProvider;
use Modules\Core\Traits\AutoRegistersCommands;
use Modules\User\Models\Observers\UserObserver;
use Modules\User\Models\User;

class UserServiceProvider extends ServiceProvider
{
use AutoRegistersCommands;

/**
* @var string $moduleName
*/
protected $moduleName = 'User';
protected string $moduleName = 'User';

/**
* @var string $moduleNameLower
*/
protected $moduleNameLower = 'user';
protected string $moduleNameLower = 'user';

/**
* Boot the application events.
*
* @return void
*/
public function boot()
public function boot(): void
{
User::observe(UserObserver::class);
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->loadMigrationsFrom(module_path($this->moduleName, 'Database/Migrations'));
$this->autoRegisterCommands($this->moduleName);
}

/**
* Register translations.
*
* @return void
*/
public function registerTranslations()
public function registerTranslations(): void
{
$langPath = resource_path('lang/modules/'.$this->moduleNameLower);

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Auth/ConfirmPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\ConfirmsPasswords;

class ConfirmPasswordController extends Controller
Expand All @@ -18,14 +17,15 @@ class ConfirmPasswordController extends Controller
| this trait and override any functions that require customization.
|
*/

use ConfirmsPasswords;

/**
* Where to redirect users when the intended url fails.
*
* @var string
*/
protected string $redirectTo = RouteServiceProvider::HOME;
protected $redirectTo = '/home';

/**
* Create a new controller instance.
Expand Down
11 changes: 1 addition & 10 deletions app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ class ForgotPasswordController extends Controller
| your application to your users. Feel free to explore this trait.
|
*/
use SendsPasswordResetEmails;

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
use SendsPasswordResetEmails;
}
63 changes: 32 additions & 31 deletions app/Http/Controllers/Auth/LoginController.php
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');
}
}
Loading

0 comments on commit 1adee70

Please sign in to comment.