Initial Commit
This commit is contained in:
0
app/Http/Controllers/.gitkeep
Normal file
0
app/Http/Controllers/.gitkeep
Normal file
69
app/Http/Controllers/PermissionsController.php
Normal file
69
app/Http/Controllers/PermissionsController.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Usermanagement\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class PermissionsController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\View
|
||||
* @return \Illuminate\Contracts\View\Factory
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('usermanagement::index');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* This function is responsible for displaying the form to create a new resource.
|
||||
* It returns the view for creating a new resource, which is located at 'usermanagement::create'.
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
||||
* @return \Illuminate\Contracts\View\View
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('usermanagement::create');
|
||||
}
|
||||
|
||||
|
||||
public function store(Request $request)
|
||||
: RedirectResponse
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
return view('usermanagement::show');
|
||||
}
|
||||
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
return view('usermanagement::edit');
|
||||
}
|
||||
|
||||
|
||||
public function update(Request $request, $id)
|
||||
: RedirectResponse
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
291
app/Http/Controllers/RolesController.php
Normal file
291
app/Http/Controllers/RolesController.php
Normal file
@@ -0,0 +1,291 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Usermanagement\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Usermanagement\Exports\RolesExport;
|
||||
use Modules\Usermanagement\Http\Requests\RoleRequest;
|
||||
use Modules\Usermanagement\Models\Role;
|
||||
|
||||
/**
|
||||
* Class RolesController
|
||||
*
|
||||
* This controller is responsible for managing user roles within the application.
|
||||
*
|
||||
* @package Modules\Usermanagement\Http\Controllers
|
||||
*/
|
||||
class RolesController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Auth\Authenticatable|null
|
||||
*/
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* UsersController constructor.
|
||||
*
|
||||
* Initializes the user property with the authenticated user.
|
||||
*
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware(function ($request, $next) {
|
||||
$this->user = Auth::guard('web')->user();
|
||||
return $next($request);
|
||||
});
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// Check if the authenticated user has the required permission to view roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.view')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view roles.');
|
||||
}
|
||||
|
||||
// Fetch all roles from the database
|
||||
$roles = Role::all();
|
||||
|
||||
// Return the view for displaying the roles
|
||||
return view('usermanagement::roles.index', compact('roles'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function store(RoleRequest $request)
|
||||
{
|
||||
// Check if the authenticated user has the required permission to store roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.store')) {
|
||||
//abort(403, 'Sorry! You are not allowed to store roles.');
|
||||
}
|
||||
|
||||
// Create a new role using the provided request data
|
||||
Role::create($request->all());
|
||||
|
||||
// Redirect back to the roles index with a success message
|
||||
return redirect()->route('users.roles.index')->with('success', 'Role created successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
// Check if the authenticated user has the required permission to create roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.create')) {
|
||||
//abort(403, 'Sorry! You are not allowed to create roles.');
|
||||
}
|
||||
|
||||
// Return the view for creating a new role
|
||||
return view('usermanagement::roles.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
// Check if the authenticated user has the required permission to view roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.view')) {
|
||||
abort(403, 'Sorry! You are not allowed to view roles.');
|
||||
}
|
||||
|
||||
// Fetch the specified role from the database
|
||||
$role = Role::find($id);
|
||||
|
||||
// Return the view for displaying the role
|
||||
return view('usermanagement::roles.show', compact('role'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
// Check if the authenticated user has the required permission to edit roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.edit')) {
|
||||
//abort(403, 'Sorry! You are not allowed to edit roles.');
|
||||
}
|
||||
|
||||
// Fetch the specified role from the database
|
||||
$role = Role::find($id);
|
||||
|
||||
// Return the view for editing the role
|
||||
return view('usermanagement::roles.create', compact('role'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update the specified role in storage.
|
||||
*
|
||||
* @param \Modules\Usermanagement\Http\Requests\RoleRequest $request The request object containing the role data.
|
||||
* @param int $id The unique identifier of the role to be updated.
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse Redirects back to the roles index with a success message upon successful update.
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException If the authenticated user does not have the required permission to update roles.
|
||||
*/
|
||||
public function update(RoleRequest $request, $id)
|
||||
{
|
||||
// Check if the authenticated user has the required permission to update roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.update')) {
|
||||
//abort(403, 'Sorry! You are not allowed to update roles.');
|
||||
}
|
||||
|
||||
// Fetch the specified role from the database
|
||||
$role = Role::find($id);
|
||||
|
||||
// Update the role using the provided request data
|
||||
$role->update($request->all());
|
||||
|
||||
// Redirect back to the roles index with a success message
|
||||
return redirect()->route('users.roles.index')->with('success', 'Role updated successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
// Check if the authenticated user has the required permission to delete roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.delete')) {
|
||||
//abort(403, 'Sorry! You are not allowed to delete roles.');
|
||||
}
|
||||
|
||||
// Fetch the specified role from the database
|
||||
$role = Role::find($id);
|
||||
|
||||
// Delete the role
|
||||
$role->delete();
|
||||
|
||||
// Redirect back to the roles index with a success message
|
||||
echo json_encode(['message' => 'User deleted successfully.', 'success' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore a deleted role.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function restore($id)
|
||||
{
|
||||
// Check if the authenticated user has the required permission to restore roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.restore')) {
|
||||
abort(403, 'Sorry! You are not allowed to restore roles.');
|
||||
}
|
||||
|
||||
// Fetch the specified role from the database
|
||||
$role = Role::withTrashed()->find($id);
|
||||
|
||||
// Restore the role
|
||||
$role->restore();
|
||||
|
||||
// Redirect back to the roles index with a success message
|
||||
return redirect()->route('users.roles.index')->with('success', 'Role restored successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Process support datatables ajax request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function dataForDatatables(Request $request)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('roles.view')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view users.');
|
||||
}
|
||||
|
||||
// Retrieve data from the database
|
||||
$query = Role::query();
|
||||
|
||||
// Apply search filter if provided
|
||||
if ($request->has('search') && !empty($request->get('search'))) {
|
||||
$search = $request->get('search');
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('name', 'LIKE', "%$search%");
|
||||
});
|
||||
}
|
||||
|
||||
// Apply sorting if provided
|
||||
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
||||
$order = $request->get('sortOrder');
|
||||
$column = $request->get('sortField');
|
||||
$query->orderBy($column, $order);
|
||||
}
|
||||
|
||||
// Get the total count of records
|
||||
$totalRecords = $query->count();
|
||||
|
||||
// Apply pagination if provided
|
||||
if ($request->has('start') && $request->has('length')) {
|
||||
$start = $request->get('start');
|
||||
$length = $request->get('length');
|
||||
$query->skip($start)->take($length);
|
||||
}
|
||||
|
||||
// Get the filtered count of records
|
||||
$filteredRecords = $query->count();
|
||||
|
||||
// Get the data for the current page
|
||||
$roles = $query->get();
|
||||
|
||||
// Calculate the page count
|
||||
$pageCount = ceil($totalRecords);
|
||||
|
||||
// Calculate the current page number
|
||||
$currentPage = 0 + 1;
|
||||
|
||||
// Return the response data as a JSON object
|
||||
return response()->json([
|
||||
'draw' => $request->get('draw'),
|
||||
'recordsTotal' => $totalRecords,
|
||||
'recordsFiltered' => $filteredRecords,
|
||||
'pageCount' => $pageCount,
|
||||
'page' => $currentPage,
|
||||
'totalCount' => $totalRecords,
|
||||
'data' => $roles,
|
||||
]);
|
||||
}
|
||||
|
||||
public function export()
|
||||
{
|
||||
return Excel::download(new RolesExport, 'roles.xlsx');
|
||||
}
|
||||
}
|
||||
249
app/Http/Controllers/UsersController.php
Normal file
249
app/Http/Controllers/UsersController.php
Normal file
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Usermanagement\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Usermanagement\Exports\UsersExport;
|
||||
use Modules\Usermanagement\Http\Requests\User as UserRequest;
|
||||
use Modules\Usermanagement\Models\Role;
|
||||
use Modules\Usermanagement\Models\User;
|
||||
|
||||
/**
|
||||
* Class UsersController
|
||||
*
|
||||
* This controller is responsible for managing user within the application.
|
||||
*
|
||||
* @package Modules\Usermanagement\Http\Controllers
|
||||
*/
|
||||
class UsersController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Auth\Authenticatable|null
|
||||
*/
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* UsersController constructor.
|
||||
*
|
||||
* Initializes the user property with the authenticated user.
|
||||
*/
|
||||
// public function __construct()
|
||||
// {
|
||||
// $this->middleware(function ($request, $next) {
|
||||
// $this->user = Auth::guard('web')->user();
|
||||
// return $next($request);
|
||||
// });
|
||||
// }
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('users.view')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view users.');
|
||||
}
|
||||
|
||||
return view('usermanagement::users.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Process support datatables ajax request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function dataForDatatables(Request $request)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('users.view')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view users.');
|
||||
}
|
||||
|
||||
// Retrieve data from the database
|
||||
$query = User::query();
|
||||
|
||||
// Apply search filter if provided
|
||||
if ($request->has('search') && !empty($request->get('search'))) {
|
||||
$search = $request->get('search');
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('name', 'LIKE', "%$search%")
|
||||
->orWhere('email', 'LIKE', "%$search%");
|
||||
});
|
||||
}
|
||||
|
||||
// Apply sorting if provided
|
||||
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
||||
$order = $request->get('sortOrder');
|
||||
$column = $request->get('sortField');
|
||||
$query->orderBy($column, $order);
|
||||
}
|
||||
|
||||
// Get the total count of records
|
||||
$totalRecords = $query->count();
|
||||
|
||||
// Apply pagination if provided
|
||||
if ($request->has('start') && $request->has('length')) {
|
||||
$start = $request->get('start');
|
||||
$length = $request->get('length');
|
||||
$query->skip($start)->take($length);
|
||||
}
|
||||
|
||||
// Get the filtered count of records
|
||||
$filteredRecords = $query->count();
|
||||
|
||||
// Get the data for the current page
|
||||
$users = $query->get();
|
||||
|
||||
// Calculate the page count
|
||||
$pageCount = ceil($totalRecords);
|
||||
|
||||
// Calculate the current page number
|
||||
$currentPage = 0 + 1;
|
||||
|
||||
// Return the response data as a JSON object
|
||||
return response()->json([
|
||||
'draw' => $request->get('draw'),
|
||||
'recordsTotal' => $totalRecords,
|
||||
'recordsFiltered' => $filteredRecords,
|
||||
'pageCount' => $pageCount,
|
||||
'page' => $currentPage,
|
||||
'totalCount' => $totalRecords,
|
||||
'data' => $users,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('users.edit')) {
|
||||
//abort(403, 'Sorry! You are not allowed to edit users.');
|
||||
}
|
||||
|
||||
$user = User::find($id);
|
||||
$roles = Role::all();
|
||||
return view('usermanagement::users.create', compact('user', 'roles'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Modules\Usermanagement\Http\Requests\User $request
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function update(UserRequest $request, $id)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('users.update')) {
|
||||
//abort(403, 'Sorry! You are not allowed to update users.');
|
||||
}
|
||||
|
||||
$user = User::find($id);
|
||||
$user->update($request->all());
|
||||
|
||||
return redirect()->route('users.index')->with('success', 'User updated successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('users.delete')) {
|
||||
//abort(403, 'Sorry! You are not allowed to delete users.');
|
||||
}
|
||||
|
||||
$user = User::find($id);
|
||||
$user->delete();
|
||||
|
||||
echo json_encode(['message' => 'User deleted successfully.', 'success' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function restore($id)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('users.restore')) {
|
||||
abort(403, 'Sorry! You are not allowed to restore users.');
|
||||
}
|
||||
|
||||
$user = User::withTrashed()->find($id);
|
||||
$user->restore();
|
||||
|
||||
return redirect()->route('users.index')->with('success', 'User restored successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* This function handles the creation of a new user in the application. It validates the incoming request data,
|
||||
* creates a new user record in the database, and redirects the user to the users index page with a success message.
|
||||
*
|
||||
* @param \Modules\Usermanagement\Http\Requests\User $request The incoming request containing the user data.
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse Redirects to the users index page with a success message upon successful creation.
|
||||
* @return \Illuminate\Http\RedirectResponse Redirects to the users create page upon validation failure.
|
||||
*/
|
||||
public function store(UserRequest $request)
|
||||
{
|
||||
$validated = $request->validated();
|
||||
|
||||
if ($validated) {
|
||||
$user = User::create($validated);
|
||||
|
||||
if ($user) {
|
||||
return redirect()->route('users.index')->with('success', 'User created successfully.');
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('users.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('users.create')) {
|
||||
//abort(403, 'Sorry! You are not allowed to create a user.');
|
||||
}
|
||||
|
||||
$roles = Role::all();
|
||||
return view('usermanagement::users.create', compact('roles'));
|
||||
}
|
||||
|
||||
public function export()
|
||||
{
|
||||
return Excel::download(new UsersExport, 'users.xlsx');
|
||||
}
|
||||
|
||||
}
|
||||
23
app/Http/Requests/ChangePassword.php
Normal file
23
app/Http/Requests/ChangePassword.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Usermanagement\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ChangePassword extends FormRequest
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns an array of validation rules for the password and current password fields.
|
||||
*
|
||||
* @return array The validation rules for the password and current password fields.
|
||||
*/
|
||||
public function rules()
|
||||
: array
|
||||
{
|
||||
return [
|
||||
'password' => 'required|string|min:8|confirmed',
|
||||
'current_password' => 'required|string|min:8'
|
||||
];
|
||||
}
|
||||
}
|
||||
22
app/Http/Requests/Login.php
Normal file
22
app/Http/Requests/Login.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Usermanagement\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class Login extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Returns an array of validation rules for the login form.
|
||||
*
|
||||
* @return array The validation rules.
|
||||
*/
|
||||
public function rules()
|
||||
: array
|
||||
{
|
||||
return [
|
||||
'email' => 'required|email',
|
||||
'password' => 'required'
|
||||
];
|
||||
}
|
||||
}
|
||||
46
app/Http/Requests/RoleRequest.php
Normal file
46
app/Http/Requests/RoleRequest.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Usermanagement\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class RoleRequest extends FormRequest
|
||||
{
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of validation rules for the registration form.
|
||||
*
|
||||
* @return array The validation rules.
|
||||
*/
|
||||
public function rules()
|
||||
: array
|
||||
{
|
||||
|
||||
$rules = [
|
||||
'guard_names' => 'required|string|in:web,api',
|
||||
];
|
||||
|
||||
if ($this->method() === 'PUT') {
|
||||
$rules['name'] = 'required|string|max:255|unique:roles,name,' . $this->id;
|
||||
} else {
|
||||
$rules['name'] = 'required|string|max:255';
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
public function prepareForValidation()
|
||||
{
|
||||
$this->merge([
|
||||
'guard_names' => 'web',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
49
app/Http/Requests/User.php
Normal file
49
app/Http/Requests/User.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Usermanagement\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class User extends FormRequest
|
||||
{
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of validation rules for the registration form.
|
||||
*
|
||||
* @return array The validation rules.
|
||||
*/
|
||||
public function rules()
|
||||
: array
|
||||
{
|
||||
|
||||
$rules = [
|
||||
'name' => 'required|string|max:255',
|
||||
];
|
||||
|
||||
if ($this->password || $this->method() === 'POST') {
|
||||
$rules['email'] = 'required|email|unique:users,email';
|
||||
$rules['password'] = 'required|string|min:8|confirmed';
|
||||
}
|
||||
|
||||
if ($this->method() === 'PUT') {
|
||||
$rules['email'] = 'required|email|unique:users,email,' . $this->id;
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
public function passedValidation()
|
||||
{
|
||||
$this->merge([
|
||||
'password' => Hash::make($this->password)
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user