first change
This commit is contained in:
0
Modules/User/app/Http/Controllers/.gitkeep
Normal file
0
Modules/User/app/Http/Controllers/.gitkeep
Normal file
65
Modules/User/app/Http/Controllers/ActivityLogController.php
Normal file
65
Modules/User/app/Http/Controllers/ActivityLogController.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ActivityLogController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('user::index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('user::create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return view('user::show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
return view('user::edit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
90
Modules/User/app/Http/Controllers/PermissionController.php
Normal file
90
Modules/User/app/Http/Controllers/PermissionController.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Modules\User\Services\PermissionService;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class PermissionController extends Controller
|
||||
{
|
||||
|
||||
private $permissionService;
|
||||
public function __construct(PermissionService $permissionService)
|
||||
{
|
||||
$this->permissionService = $permissionService;
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (request()->ajax()) {
|
||||
$model = Permission::query()->latest();
|
||||
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->addColumn('action', 'user::permission.datatable.action')
|
||||
->rawColumns(['action'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
return view('user::permission.index', [
|
||||
'title' => 'Permission Lists',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Permission $permission)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Permission $permission)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$this->permissionService->deletePermission($id);
|
||||
flash()->success("Permission has been deleted.");
|
||||
return response()->json(['status' => 200, 'message' => 'Permission has been deleted!'], 200);
|
||||
}
|
||||
|
||||
}
|
118
Modules/User/app/Http/Controllers/RoleController.php
Normal file
118
Modules/User/app/Http/Controllers/RoleController.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Modules\User\Services\RoleService;
|
||||
use Modules\User\Services\PermissionService;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class RoleController extends Controller
|
||||
{
|
||||
private $roleService;
|
||||
private $permissionService;
|
||||
public function __construct(RoleService $roleService, PermissionService $permissionService)
|
||||
{
|
||||
$this->roleService = $roleService;
|
||||
$this->permissionService = $permissionService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (request()->ajax()) {
|
||||
$model = Role::query()->latest();
|
||||
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->addColumn('action', 'user::role.datatable.action')
|
||||
->rawColumns(['action'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
return view('user::role.index', [
|
||||
'title' => 'Role Lists',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data['title'] = "Create Role";
|
||||
$data['editable'] = false;
|
||||
$data['permissionLists'] = $this->permissionService->getPermissionListsArrangedByPrefix();
|
||||
return view('user::role.create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required',
|
||||
'guard_name' => 'string',
|
||||
]);
|
||||
|
||||
$role = $this->roleService->storeRole($validated, $request->permissions);
|
||||
|
||||
flash()->success("Role for {$role->name} has been created.");
|
||||
|
||||
return redirect()->route('role.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$data['title'] = "Edit Role";
|
||||
$data['editable'] = true;
|
||||
$data['role'] = $role = $this->roleService->getRoleById($id);
|
||||
$data['permissionIDsArray'] = $role?->permissions?->pluck('id')->toArray();
|
||||
$data['permissionLists'] = $this->permissionService->getPermissionListsArrangedByPrefix();
|
||||
return view('user::role.edit', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required',
|
||||
'guard_name' => 'string',
|
||||
]);
|
||||
|
||||
$role = $this->roleService->updateRole($id, $validated, $request->permissions);
|
||||
flash()->success("Role for {$role->name} has been updated.");
|
||||
return redirect()->route('role.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id): Response
|
||||
{
|
||||
$this->roleService->deleteRole($id);
|
||||
flash()->success("Role has been deleted.");
|
||||
return response()->json(['status' => 200, 'message' => 'Role has been deleted!'], 200);
|
||||
}
|
||||
}
|
224
Modules/User/app/Http/Controllers/UserController.php
Normal file
224
Modules/User/app/Http/Controllers/UserController.php
Normal file
@@ -0,0 +1,224 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\Rules;
|
||||
use Modules\User\Services\RoleService;
|
||||
use Modules\User\Services\UserService;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
protected $userService;
|
||||
protected $roleService;
|
||||
|
||||
public function __construct(UserService $userService, RoleService $roleService)
|
||||
{
|
||||
$this->userService = $userService;
|
||||
$this->roleService = $roleService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(?int $id = null)
|
||||
{
|
||||
$isEditing = !is_null($id);
|
||||
$user = $isEditing ? $this->userService->getUserById($id) : null;
|
||||
|
||||
if (request()->ajax()) {
|
||||
$model = user::query()->orderBy('order');
|
||||
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->addColumn('role', function (User $user) {
|
||||
if ($user->roles->isEmpty()) {
|
||||
return 'Not Assigned';
|
||||
}
|
||||
$html = $user->roles->map(function ($role) {
|
||||
return "<span class='badge bg-primary p-1'>{$role->name}</span>";
|
||||
})->implode(' ');
|
||||
|
||||
return $html;
|
||||
})
|
||||
->editColumn('can_login', function (User $user) {
|
||||
return $user->can_login ? 'Yes' : 'No';
|
||||
})
|
||||
->editColumn('employee', function (User $user) {
|
||||
return $user->employee?->full_name ?? "-";
|
||||
})
|
||||
->addColumn('action', 'user::user.datatable.action')
|
||||
->rawColumns(['action', 'role', 'employee'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
return view('user::user.index', [
|
||||
'user' => $user,
|
||||
'title' => $isEditing ? 'Edit User' : 'Add User',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$isEditing = $request->has('id');
|
||||
|
||||
if ($isEditing) {
|
||||
$validated = $request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => [
|
||||
'required',
|
||||
'string',
|
||||
'lowercase',
|
||||
'email',
|
||||
'max:255',
|
||||
Rule::unique(User::class)->ignore($request->id),
|
||||
],
|
||||
'password' => ['nullable', 'confirmed', Rules\Password::defaults()],
|
||||
'is_admin' => ['nullable'],
|
||||
]);
|
||||
|
||||
$user = $this->userService->updateUser($request->id, $validated);
|
||||
flash()->success("User for {$user->name} has been updated.");
|
||||
return to_route('user.index');
|
||||
}
|
||||
|
||||
$maxOrder = User::max('order');
|
||||
$order = $maxOrder ? ++$maxOrder : 1;
|
||||
|
||||
$request->mergeIfMissing([
|
||||
'order' => $order,
|
||||
]);
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:' . User::class],
|
||||
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
||||
'is_admin' => ['nullable'],
|
||||
'order' => ['integer'],
|
||||
]);
|
||||
|
||||
$user = $this->userService->storeUser($validated);
|
||||
flash()->success("User for {$user->name} has been created.");
|
||||
return to_route('user.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$user = $this->userService->getUserById($id);
|
||||
return view('user::user.edit', [
|
||||
'user' => $user,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$user = $this->userService->deleteUser($id);
|
||||
return response()->json(['status' => 200, 'message' => "User has been deleted."], 200);
|
||||
}
|
||||
|
||||
public function reorder(Request $request)
|
||||
{
|
||||
$users = $this->userService->getAllUsers();
|
||||
|
||||
foreach ($users as $user) {
|
||||
foreach ($request->order as $order) {
|
||||
if ($order['id'] == $user->id) {
|
||||
$user->update(['order' => $order['position']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return response(['status' => true, 'message' => 'Reordered successfully'], 200);
|
||||
}
|
||||
|
||||
public function showAssignRoleForm($id)
|
||||
{
|
||||
$user = $this->userService->getUserById($id);
|
||||
|
||||
$roleOptions = $this->roleService->pluck();
|
||||
$userRoleIds = $user->roles->pluck('id');
|
||||
|
||||
return view('user::user.partials._form', [
|
||||
'editable' => true,
|
||||
'user' => $user,
|
||||
'roleOptions' => $roleOptions,
|
||||
'userRoleIds' => $userRoleIds,
|
||||
]);
|
||||
}
|
||||
|
||||
public function assignRole(Request $request, $id)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'role' => ['nullable', 'array'],
|
||||
]);
|
||||
|
||||
$user = $this->userService->getUserById($id);
|
||||
|
||||
if (!isEmptyArray($request->role)) {
|
||||
$user->roles()->sync($validated['role']);
|
||||
$message = "Role has been assigned";
|
||||
} else {
|
||||
$user->roles()->detach();
|
||||
$message = "Role has been removed";
|
||||
}
|
||||
|
||||
if ($request->ajax()) {
|
||||
return response([
|
||||
'status' => 200,
|
||||
'message' => $message,
|
||||
], 200);
|
||||
}
|
||||
|
||||
flash()->success($message);
|
||||
return to_route('user.index');
|
||||
}
|
||||
|
||||
public function toggleActivation(Request $request, $id)
|
||||
{
|
||||
$user = $this->userService->getUserById($id);
|
||||
$message = $user->can_login ? 'User has been deactivated' : 'User has been activated';
|
||||
$user->update(['can_login' => !$user->can_login]);
|
||||
|
||||
return response([
|
||||
'status' => 200,
|
||||
'message' => $message,
|
||||
], 200);
|
||||
}
|
||||
}
|
43
Modules/User/app/Models/ActivityLog.php
Normal file
43
Modules/User/app/Models/ActivityLog.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Models;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Traits\CreatedUpdatedBy;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
// use Modules\User\Database\Factories\ActivityLogFactory;
|
||||
|
||||
class ActivityLog extends Model
|
||||
{
|
||||
use HasFactory, CreatedUpdatedBy;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'data',
|
||||
'loggable_type',
|
||||
'loggable_id',
|
||||
'createdby',
|
||||
'updatedby',
|
||||
];
|
||||
|
||||
|
||||
public function loggable(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function createdBy()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'createdby');
|
||||
}
|
||||
|
||||
// protected static function newFactory(): ActivityLogFactory
|
||||
// {
|
||||
// // return ActivityLogFactory::new();
|
||||
// }
|
||||
}
|
0
Modules/User/app/Models/Scopes/.gitkeep
Normal file
0
Modules/User/app/Models/Scopes/.gitkeep
Normal file
0
Modules/User/app/Providers/.gitkeep
Normal file
0
Modules/User/app/Providers/.gitkeep
Normal file
30
Modules/User/app/Providers/EventServiceProvider.php
Normal file
30
Modules/User/app/Providers/EventServiceProvider.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Providers;
|
||||
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* The event handler mappings for the application.
|
||||
*
|
||||
* @var array<string, array<int, string>>
|
||||
*/
|
||||
protected $listen = [];
|
||||
|
||||
/**
|
||||
* Indicates if events should be discovered.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected static $shouldDiscoverEvents = true;
|
||||
|
||||
/**
|
||||
* Configure the proper event listeners for email verification.
|
||||
*/
|
||||
protected function configureEmailVerification(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
50
Modules/User/app/Providers/RouteServiceProvider.php
Normal file
50
Modules/User/app/Providers/RouteServiceProvider.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $name = 'User';
|
||||
|
||||
/**
|
||||
* Called before routes are registered.
|
||||
*
|
||||
* Register any model bindings or pattern based filters.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the routes for the application.
|
||||
*/
|
||||
public function map(): void
|
||||
{
|
||||
$this->mapApiRoutes();
|
||||
$this->mapWebRoutes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "web" routes for the application.
|
||||
*
|
||||
* These routes all receive session state, CSRF protection, etc.
|
||||
*/
|
||||
protected function mapWebRoutes(): void
|
||||
{
|
||||
Route::middleware('web')->group(module_path($this->name, '/routes/web.php'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "api" routes for the application.
|
||||
*
|
||||
* These routes are typically stateless.
|
||||
*/
|
||||
protected function mapApiRoutes(): void
|
||||
{
|
||||
Route::middleware('api')->prefix('api')->name('api.')->group(module_path($this->name, '/routes/api.php'));
|
||||
}
|
||||
}
|
118
Modules/User/app/Providers/UserServiceProvider.php
Normal file
118
Modules/User/app/Providers/UserServiceProvider.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Nwidart\Modules\Traits\PathNamespace;
|
||||
|
||||
class UserServiceProvider extends ServiceProvider
|
||||
{
|
||||
use PathNamespace;
|
||||
|
||||
protected string $name = 'User';
|
||||
|
||||
protected string $nameLower = 'user';
|
||||
|
||||
/**
|
||||
* Boot the application events.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerCommands();
|
||||
$this->registerCommandSchedules();
|
||||
$this->registerTranslations();
|
||||
$this->registerConfig();
|
||||
$this->registerViews();
|
||||
$this->loadMigrationsFrom(module_path($this->name, 'database/migrations'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->register(EventServiceProvider::class);
|
||||
$this->app->register(RouteServiceProvider::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register commands in the format of Command::class
|
||||
*/
|
||||
protected function registerCommands(): void
|
||||
{
|
||||
// $this->commands([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register command Schedules.
|
||||
*/
|
||||
protected function registerCommandSchedules(): void
|
||||
{
|
||||
// $this->app->booted(function () {
|
||||
// $schedule = $this->app->make(Schedule::class);
|
||||
// $schedule->command('inspire')->hourly();
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* Register translations.
|
||||
*/
|
||||
public function registerTranslations(): void
|
||||
{
|
||||
$langPath = resource_path('lang/modules/'.$this->nameLower);
|
||||
|
||||
if (is_dir($langPath)) {
|
||||
$this->loadTranslationsFrom($langPath, $this->nameLower);
|
||||
$this->loadJsonTranslationsFrom($langPath);
|
||||
} else {
|
||||
$this->loadTranslationsFrom(module_path($this->name, 'lang'), $this->nameLower);
|
||||
$this->loadJsonTranslationsFrom(module_path($this->name, 'lang'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register config.
|
||||
*/
|
||||
protected function registerConfig(): void
|
||||
{
|
||||
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
|
||||
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register views.
|
||||
*/
|
||||
public function registerViews(): void
|
||||
{
|
||||
$viewPath = resource_path('views/modules/'.$this->nameLower);
|
||||
$sourcePath = module_path($this->name, 'resources/views');
|
||||
|
||||
$this->publishes([$sourcePath => $viewPath], ['views', $this->nameLower.'-module-views']);
|
||||
|
||||
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->nameLower);
|
||||
|
||||
$componentNamespace = $this->module_namespace($this->name, $this->app_path(config('modules.paths.generator.component-class.path')));
|
||||
Blade::componentNamespace($componentNamespace, $this->nameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*/
|
||||
public function provides(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
private function getPublishableViewPaths(): array
|
||||
{
|
||||
$paths = [];
|
||||
foreach (config('view.paths') as $path) {
|
||||
if (is_dir($path.'/modules/'.$this->nameLower)) {
|
||||
$paths[] = $path.'/modules/'.$this->nameLower;
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
}
|
||||
}
|
0
Modules/User/app/Services/.gitkeep
Normal file
0
Modules/User/app/Services/.gitkeep
Normal file
98
Modules/User/app/Services/PermissionService.php
Normal file
98
Modules/User/app/Services/PermissionService.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Services;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class PermissionService
|
||||
{
|
||||
|
||||
public function getPermissionListsArrangedByPrefix()
|
||||
{
|
||||
$permissions = self::getPermissions();
|
||||
|
||||
$routeNameArr = [];
|
||||
foreach ($permissions as $permission) {
|
||||
if (!is_null($permission->name)) {
|
||||
$routeName = explode('.', $permission->name);
|
||||
if (is_array($routeName) && !empty($routeName[0])) {
|
||||
$routeNameArr[$routeName[0]][$permission->id] = array_key_exists(1, $routeName) ? $routeName[1] : $routeName[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ksort($routeNameArr);
|
||||
|
||||
return $routeNameArr;
|
||||
}
|
||||
|
||||
public static function generatePermissionFromRoutes()
|
||||
{
|
||||
$routes = Route::getRoutes();
|
||||
foreach ($routes as $route) {
|
||||
|
||||
$routeName = $names[] = $route->getName();
|
||||
|
||||
$ignoreRoutes = [
|
||||
'debugbar',
|
||||
'unisharp',
|
||||
'livewire',
|
||||
'login',
|
||||
'register',
|
||||
'logout',
|
||||
'post',
|
||||
'sanctum',
|
||||
'ignition',
|
||||
'welcome',
|
||||
'home',
|
||||
'api',
|
||||
];
|
||||
|
||||
$routePrefix = explode('.', $routeName);
|
||||
if (is_array($routePrefix) && !empty($routePrefix[0])) {
|
||||
if (!in_array($routePrefix[0], $ignoreRoutes) && !Permission::where('name', $routeName)->exists()) {
|
||||
Permission::updateOrCreate(['name' => $routeName], ['name' => $routeName, 'guard_name' => 'web']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$permissions = Permission::pluck('name', 'id');
|
||||
$diffRoutes = array_diff($permissions->toArray(), $names);
|
||||
if ($diffRoutes) {
|
||||
Permission::whereIn('id', array_keys($diffRoutes))->delete();
|
||||
}
|
||||
|
||||
$roles = Role::all();
|
||||
foreach ($roles as $role) {
|
||||
|
||||
if ($role->name == 'admin' || $role->name == 'super-admin') {
|
||||
$role->givePermissionTo(Permission::all());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getPermissions()
|
||||
{
|
||||
$query = Permission::query();
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
public function getPermissionById($id)
|
||||
{
|
||||
return Permission::findOrFail($id);
|
||||
}
|
||||
|
||||
public function deletePermission($id)
|
||||
{
|
||||
return DB::transaction(function () use ($id) {
|
||||
$permission = self::getPermissionById($id);
|
||||
$permission->delete();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
60
Modules/User/app/Services/RoleService.php
Normal file
60
Modules/User/app/Services/RoleService.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Services;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class RoleService
|
||||
{
|
||||
|
||||
public function getRoles(Request $request)
|
||||
{
|
||||
$query = Role::query();
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
public function storeRole(array $roleData, array $permissionIds): Role
|
||||
{
|
||||
return DB::transaction(function () use ($roleData, $permissionIds) {
|
||||
$role = Role::create($roleData);
|
||||
$role->permissions()->attach($permissionIds);
|
||||
return $role;
|
||||
});
|
||||
}
|
||||
|
||||
public function getRoleById(int $id)
|
||||
{
|
||||
return Role::findOrFail($id);
|
||||
}
|
||||
|
||||
public function updateRole(int $id, array $roleData, array $permissionIds)
|
||||
{
|
||||
$role = self::getRoleById($id);
|
||||
|
||||
return DB::transaction(function () use ($role, $roleData, $permissionIds) {
|
||||
$role->update($roleData);
|
||||
$role->permissions()->sync($permissionIds);
|
||||
return $role;
|
||||
});
|
||||
}
|
||||
|
||||
public function deleteRole(int $id)
|
||||
{
|
||||
return DB::transaction(function () use ($id) {
|
||||
$role = self::getRoleById($id);
|
||||
$role->permissions()->detach();
|
||||
$role->delete();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public static function pluck(callable $query = null)
|
||||
{
|
||||
$baseQuery = Role::query();
|
||||
if (is_callable($query)) {
|
||||
$query($baseQuery);
|
||||
}
|
||||
return $baseQuery->pluck('name', 'id');
|
||||
}
|
||||
}
|
80
Modules/User/app/Services/UserService.php
Normal file
80
Modules/User/app/Services/UserService.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class UserService
|
||||
{
|
||||
|
||||
public function getAllUsers(Request $request)
|
||||
{
|
||||
$query = User::query();
|
||||
|
||||
$query->when($request->filled('name'), function ($query) use ($request) {
|
||||
$query->where('name', 'like', '%' . $request->name . '%');
|
||||
})->when($request->filled('email'), function ($query) use ($request) {
|
||||
$query->where('email', 'like', '%' . $request->email . '%');
|
||||
});
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
public static function storeUser(array $userData): User
|
||||
{
|
||||
return DB::transaction(function () use ($userData) {
|
||||
$user = User::create([
|
||||
'name' => $userData['name'],
|
||||
'email' => $userData['email'],
|
||||
'password' => Hash::make($userData['password'] ?? "password"),
|
||||
'can_login' => $userData['can_login'] ?? false,
|
||||
'order' => $userData['order'],
|
||||
]);
|
||||
|
||||
return $user;
|
||||
});
|
||||
}
|
||||
|
||||
public static function getUserById(int $id)
|
||||
{
|
||||
return User::findOrFail($id);
|
||||
}
|
||||
|
||||
public function updateUser(int $id, array $userData)
|
||||
{
|
||||
$user = $this->getUserById($id);
|
||||
|
||||
return DB::transaction(function () use ($user, $userData) {
|
||||
$user->name = $userData['name'];
|
||||
$user->email = $userData['email'];
|
||||
$user->is_admin = !empty($userData['is_admin']);
|
||||
|
||||
if (isset($userData['password'])) {
|
||||
$user->password = Hash::make($userData['password']);
|
||||
}
|
||||
|
||||
$user->save();
|
||||
return $user;
|
||||
});
|
||||
}
|
||||
|
||||
public function deleteUser(int $id)
|
||||
{
|
||||
return DB::transaction(function () use ($id) {
|
||||
$user = $this->getUserById($id);
|
||||
$user->delete();
|
||||
return $user;
|
||||
});
|
||||
}
|
||||
|
||||
public function pluck(callable $query = null)
|
||||
{
|
||||
$baseQuery = User::query();
|
||||
if (is_callable($query)) {
|
||||
$query($baseQuery);
|
||||
}
|
||||
return $baseQuery->pluck('name', 'id');
|
||||
}
|
||||
}
|
35
Modules/User/app/Traits/HasActivityLogs.php
Normal file
35
Modules/User/app/Traits/HasActivityLogs.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Modules\User\Models\ActivityLog;
|
||||
|
||||
trait HasActivityLogs
|
||||
{
|
||||
/**
|
||||
* Create a new activity log.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function createLog(array $data)
|
||||
{
|
||||
return $this->activityLogs()->create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a polymorphic one-to-many relationship for activity logs.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
||||
*/
|
||||
public function activityLogs(): MorphMany
|
||||
{
|
||||
return $this->morphMany(ActivityLog::class, 'loggable')->latest();
|
||||
}
|
||||
|
||||
// $estimate->customer->createLog([
|
||||
// 'title' => 'Estimate Created',
|
||||
// 'data' => "Estimate <span class='text-success'>{$estimate->estimate_code}</span> was created <span class='text-primary'>by</span> <span class='text-success'>" . auth()->user()->name . "</span>.",
|
||||
// ]);
|
||||
}
|
Reference in New Issue
Block a user