firstcommit
This commit is contained in:
0
Modules/User/app/Http/Controllers/.gitkeep
Normal file
0
Modules/User/app/Http/Controllers/.gitkeep
Normal file
92
Modules/User/app/Http/Controllers/PermissionController.php
Normal file
92
Modules/User/app/Http/Controllers/PermissionController.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Modules\User\Repositories\PermissionInterface;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
class PermissionController extends Controller
|
||||
{
|
||||
|
||||
private $permissionRepository;
|
||||
public function __construct(PermissionInterface $permissionRepository)
|
||||
{
|
||||
$this->permissionRepository = $permissionRepository;
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$data['permissionLists'] = $this->permissionRepository->findAll();
|
||||
$data['title'] = 'Permission Lists';
|
||||
|
||||
return view('user::permissions.index', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(Permission $permission)
|
||||
{
|
||||
$permission->delete();
|
||||
toastr()->success('Permission has been deleted!');
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
public function generatePermissionFromRoutes()
|
||||
{
|
||||
try {
|
||||
$this->permissionRepository->generatePermissionFromRoutes();
|
||||
toastr()->success('Permission generated successfully!');
|
||||
} catch (\Throwable $th) {
|
||||
toastr()->error($th->getMessage());
|
||||
|
||||
}
|
||||
|
||||
return to_route('permission.index');
|
||||
|
||||
}
|
||||
}
|
117
Modules/User/app/Http/Controllers/RoleController.php
Normal file
117
Modules/User/app/Http/Controllers/RoleController.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?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\Repositories\PermissionInterface;
|
||||
use Modules\User\Repositories\RoleInterface;
|
||||
|
||||
class RoleController extends Controller
|
||||
{
|
||||
private $roleRepository;
|
||||
private $permissionRepository;
|
||||
public function __construct(RoleInterface $roleRepository, PermissionInterface $permissionRepository)
|
||||
{
|
||||
$this->roleRepository = $roleRepository;
|
||||
$this->permissionRepository = $permissionRepository;
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$data['title'] = "Role Lists";
|
||||
$data['roles'] = $this->roleRepository->findAll();
|
||||
return view('user::role.index', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data['title'] = "Create Role";
|
||||
$data['editable'] = false;
|
||||
$data['permissionLists'] = $this->permissionRepository->getPermissionListsArrangedByPrefix();
|
||||
return view('user::role.create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
try {
|
||||
|
||||
$validatedData = $request->validate([
|
||||
'name' => 'required',
|
||||
'guard_name' => 'string',
|
||||
]);
|
||||
|
||||
$role = $this->roleRepository->create($validatedData);
|
||||
|
||||
$role->permissions()->attach($request->permissions);
|
||||
|
||||
toastr()->success('New role has been created!');
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
toastr()->success($th->getMessage());
|
||||
}
|
||||
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'] = $this->roleRepository->getRoleById($id);
|
||||
$data['permissionIDsArray'] = $data['role']?->permissions?->pluck('id')->toArray();
|
||||
$data['permissionLists'] = $this->permissionRepository->getPermissionListsArrangedByPrefix();
|
||||
return view('user::role.edit', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id): RedirectResponse
|
||||
{
|
||||
try {
|
||||
$validatedData = $request->validate([
|
||||
'name' => 'required',
|
||||
'guard_name' => 'string',
|
||||
]);
|
||||
|
||||
$role = $this->roleRepository->update($id, $validatedData);
|
||||
$role->permissions()->sync($request->permissions);
|
||||
|
||||
toastr()->success('Role has been updated!');
|
||||
} catch (\Throwable $th) {
|
||||
toastr()->error($th->getMessage());
|
||||
}
|
||||
return redirect()->route('role.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id): Response
|
||||
{
|
||||
$this->roleRepository->delete($id);
|
||||
toastr()->success('Role has been deleted!');
|
||||
return response()->json(['status' => true, 'message' => 'Role has been deleted!']);
|
||||
}
|
||||
}
|
148
Modules/User/app/Http/Controllers/UserController.php
Normal file
148
Modules/User/app/Http/Controllers/UserController.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Modules\Employee\Repositories\EmployeeInterface;
|
||||
use Modules\User\Repositories\RoleInterface;
|
||||
use Modules\User\Repositories\UserInterface;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
|
||||
protected $userRepository;
|
||||
protected $employeeRepository;
|
||||
protected $roleRepository;
|
||||
|
||||
public function __construct(UserInterface $userRepository, EmployeeInterface $employeeRepository, RoleInterface $roleRepository)
|
||||
{
|
||||
$this->userRepository = $userRepository;
|
||||
$this->employeeRepository = $employeeRepository;
|
||||
$this->roleRepository = $roleRepository;
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$data['users'] = $this->userRepository->findAll();
|
||||
$data['editable'] = false;
|
||||
$data['roleLists'] = $this->roleRepository->pluck();
|
||||
$data['employeeLists'] = $this->employeeRepository->pluck();
|
||||
return view('user::user.index', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data['title'] = "Create User";
|
||||
$data['editable'] = false;
|
||||
$data['roleLists'] = $this->roleRepository->pluck();
|
||||
$data['employeeLists'] = $this->employeeRepository->pluck();
|
||||
return view('user::user.create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
try {
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|min:5',
|
||||
'email' => 'required',
|
||||
'password' => 'required',
|
||||
]);
|
||||
|
||||
$validated['password'] = bcrypt($validated['password']);
|
||||
|
||||
$this->userRepository->create($validated, $request->role);
|
||||
|
||||
toastr()->success('User has been created!');
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
|
||||
echo $th->getMessage();
|
||||
|
||||
toastr()->error($th->getMessage());
|
||||
|
||||
}
|
||||
|
||||
return redirect()->route('user.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$data['user'] = $this->userRepository->getUserById($id);
|
||||
return view('user::user.show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$data['title'] = "Edit User";
|
||||
$data['editable'] = true;
|
||||
$data['roleLists'] = $this->roleRepository->pluck();
|
||||
$data['employeeLists'] = $this->employeeRepository->pluck();
|
||||
$data['user'] = $this->userRepository->getUserById($id);
|
||||
return view('user::user.edit', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id): RedirectResponse
|
||||
{
|
||||
try {
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|min:5',
|
||||
'email' => 'required',
|
||||
'password' => 'required',
|
||||
]);
|
||||
|
||||
$validated['password'] = bcrypt($validated['password']);
|
||||
|
||||
$this->userRepository->update($id, $validated, $request->role);
|
||||
|
||||
toastr()->success('User has been updated!');
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
|
||||
toastr()->error($th->getMessage());
|
||||
|
||||
}
|
||||
|
||||
return redirect()->route('user.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
|
||||
$this->userRepository->delete($id);
|
||||
|
||||
toastr()->success('User has been deleted!');
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
|
||||
toastr()->error($th->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
0
Modules/User/app/Http/Requests/.gitkeep
Normal file
0
Modules/User/app/Http/Requests/.gitkeep
Normal file
0
Modules/User/app/Models/.gitkeep
Normal file
0
Modules/User/app/Models/.gitkeep
Normal file
0
Modules/User/app/Providers/.gitkeep
Normal file
0
Modules/User/app/Providers/.gitkeep
Normal file
49
Modules/User/app/Providers/RouteServiceProvider.php
Normal file
49
Modules/User/app/Providers/RouteServiceProvider.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* 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('User', '/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('User', '/routes/api.php'));
|
||||
}
|
||||
}
|
123
Modules/User/app/Providers/UserServiceProvider.php
Normal file
123
Modules/User/app/Providers/UserServiceProvider.php
Normal file
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Modules\User\Repositories\PermissionInterface;
|
||||
use Modules\User\Repositories\PermissionRepository;
|
||||
use Modules\User\Repositories\RoleInterface;
|
||||
use Modules\User\Repositories\RoleRepository;
|
||||
use Modules\User\Repositories\UserInterface;
|
||||
use Modules\User\Repositories\UserRepository;
|
||||
|
||||
class UserServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $moduleName = 'User';
|
||||
|
||||
protected string $moduleNameLower = 'user';
|
||||
|
||||
/**
|
||||
* Boot the application events.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerCommands();
|
||||
$this->registerCommandSchedules();
|
||||
$this->registerTranslations();
|
||||
$this->registerConfig();
|
||||
$this->registerViews();
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->bind(UserInterface::class, UserRepository::class);
|
||||
$this->app->bind(RoleInterface::class, RoleRepository::class);
|
||||
$this->app->bind(PermissionInterface::class, PermissionRepository::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->moduleNameLower);
|
||||
|
||||
if (is_dir($langPath)) {
|
||||
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom($langPath);
|
||||
} else {
|
||||
$this->loadTranslationsFrom(module_path($this->moduleName, 'lang'), $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom(module_path($this->moduleName, 'lang'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register config.
|
||||
*/
|
||||
protected function registerConfig(): void
|
||||
{
|
||||
$this->publishes([module_path($this->moduleName, 'config/config.php') => config_path($this->moduleNameLower.'.php')], 'config');
|
||||
$this->mergeConfigFrom(module_path($this->moduleName, 'config/config.php'), $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register views.
|
||||
*/
|
||||
public function registerViews(): void
|
||||
{
|
||||
$viewPath = resource_path('views/modules/'.$this->moduleNameLower);
|
||||
$sourcePath = module_path($this->moduleName, 'resources/views');
|
||||
|
||||
$this->publishes([$sourcePath => $viewPath], ['views', $this->moduleNameLower.'-module-views']);
|
||||
|
||||
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
|
||||
|
||||
$componentNamespace = str_replace('/', '\\', config('modules.namespace').'\\'.$this->moduleName.'\\'.ltrim(config('modules.paths.generator.component-class.path'), config('modules.paths.app_folder','')));
|
||||
Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->moduleNameLower)) {
|
||||
$paths[] = $path.'/modules/'.$this->moduleNameLower;
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
}
|
||||
}
|
0
Modules/User/app/Repositories/.gitkeep
Normal file
0
Modules/User/app/Repositories/.gitkeep
Normal file
16
Modules/User/app/Repositories/PermissionInterface.php
Normal file
16
Modules/User/app/Repositories/PermissionInterface.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Repositories;
|
||||
|
||||
interface PermissionInterface
|
||||
{
|
||||
public function findAll();
|
||||
public function getPermissionById($permissionId);
|
||||
public function delete($permissionId);
|
||||
public function create(array $permissionDetails);
|
||||
public function update($permissionId, array $newDetails);
|
||||
|
||||
public function getPermissionListsArrangedByPrefix();
|
||||
|
||||
public static function generatePermissionFromRoutes();
|
||||
}
|
91
Modules/User/app/Repositories/PermissionRepository.php
Normal file
91
Modules/User/app/Repositories/PermissionRepository.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Repositories;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class PermissionRepository implements PermissionInterface
|
||||
{
|
||||
public function findAll()
|
||||
{
|
||||
return Permission::get();
|
||||
}
|
||||
|
||||
public function getPermissionById($permissionId)
|
||||
{
|
||||
return Permission::findOrFail($permissionId);
|
||||
}
|
||||
|
||||
public function delete($permissionId)
|
||||
{
|
||||
Permission::destroy($permissionId);
|
||||
}
|
||||
|
||||
public function create(array $permissionDetails)
|
||||
{
|
||||
return Permission::create($permissionDetails);
|
||||
}
|
||||
|
||||
public function update($permissionId, array $newDetails)
|
||||
{
|
||||
return Permission::where('id', $permissionId)->update($newDetails);
|
||||
}
|
||||
|
||||
public function getPermissionListsArrangedByPrefix()
|
||||
{
|
||||
$permissions = self::findAll();
|
||||
|
||||
$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];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $routeNameArr;
|
||||
}
|
||||
|
||||
public static function generatePermissionFromRoutes()
|
||||
{
|
||||
$routes = Route::getRoutes();
|
||||
|
||||
foreach ($routes as $route) {
|
||||
|
||||
$routeName = $route->getName();
|
||||
|
||||
$ignoreRoutes = [
|
||||
'debugbar',
|
||||
'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::create(['name' => $routeName, 'guard_name' => 'web']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$roles = Role::all();
|
||||
foreach ($roles as $role) {
|
||||
|
||||
if ($role->name == 'admin' || $role->name == 'super-admin') {
|
||||
$role->givePermissionTo(Permission::all());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
13
Modules/User/app/Repositories/RoleInterface.php
Normal file
13
Modules/User/app/Repositories/RoleInterface.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Repositories;
|
||||
|
||||
interface RoleInterface
|
||||
{
|
||||
public function pluck();
|
||||
public function findAll();
|
||||
public function getRoleById($roleId);
|
||||
public function delete($roleId);
|
||||
public function create(array $RoleDetails);
|
||||
public function update($roleId, array $newDetails);
|
||||
}
|
43
Modules/User/app/Repositories/RoleRepository.php
Normal file
43
Modules/User/app/Repositories/RoleRepository.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Repositories;
|
||||
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class RoleRepository implements RoleInterface
|
||||
{
|
||||
public function pluck()
|
||||
{
|
||||
return Role::pluck('name', 'id');
|
||||
}
|
||||
|
||||
public function findAll()
|
||||
{
|
||||
return Role::with('permissions')->get();
|
||||
}
|
||||
|
||||
public function getRoleById($roleId)
|
||||
{
|
||||
return Role::with('permissions')->findOrFail($roleId);
|
||||
}
|
||||
|
||||
public function delete($roleId)
|
||||
{
|
||||
$role = self::getRoleById($roleId);
|
||||
$role->permissions()->detach();
|
||||
return $role->delete();
|
||||
}
|
||||
|
||||
public function create(array $roleDetails)
|
||||
{
|
||||
return Role::create($roleDetails);
|
||||
}
|
||||
|
||||
public function update($roleId, array $newDetails)
|
||||
{
|
||||
$role = Role::find($roleId);
|
||||
$role->update($newDetails);
|
||||
return $role;
|
||||
}
|
||||
|
||||
}
|
12
Modules/User/app/Repositories/UserInterface.php
Normal file
12
Modules/User/app/Repositories/UserInterface.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Repositories;
|
||||
|
||||
interface UserInterface
|
||||
{
|
||||
public function findAll();
|
||||
public function getUserById($userId);
|
||||
public function delete($userId);
|
||||
public function create(array $UserDetails, array $role);
|
||||
public function update($userId, array $newDetails, array $role);
|
||||
}
|
40
Modules/User/app/Repositories/UserRepository.php
Normal file
40
Modules/User/app/Repositories/UserRepository.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Repositories;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class UserRepository implements UserInterface
|
||||
{
|
||||
public function findAll()
|
||||
{
|
||||
return User::with(['employee.department'])->get();
|
||||
}
|
||||
|
||||
public function getUserById($userId)
|
||||
{
|
||||
return User::findOrFail($userId);
|
||||
}
|
||||
|
||||
public function create(array $userDetails, array $role)
|
||||
{
|
||||
$user = User::create($userDetails);
|
||||
$user->roles()->attach($role);
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function update($userId, array $newDetails, array $role)
|
||||
{
|
||||
$user = User::whereId($userId)->update($newDetails);
|
||||
$user->roles()->sync($role);
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function delete($userId)
|
||||
{
|
||||
$user = User::whereId($userId)->first();
|
||||
$user->roles()->detach();
|
||||
return $user->destroy();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user