thebadspace/app/Http/Controllers/MemberController.php
ro 098cd72186 form validation for member editing
added some form validation in the member controller to make sure all
necessary info is provided before editing/creating member
2024-09-29 16:11:50 -06:00

155 lines
4.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Repositories\MemberRepository;
class MemberController extends Controller
{
protected $member;
public function __construct(
MemberRepository $memberRepo
) {
$this->member = $memberRepo;
}
public function index(Request $request)
{
$member = Auth::user();
return view('back.member', [
'handle' => $member->handle,
'members' => $this->member->getAll(),
'mode' => 'index',
'title' => "Manage Members"]);
}
public function profile(Request $request)
{
$member = Auth::user();
$avi = '';
if ($member->avatar == 'default-member-avatar') {
$avi = '/assets/images/global/default-avi.png';
} else {
$avi = $member->avatar;
}
return view('back.profile', [
'title' => "Hey, it's you!",
'handle' => $member->handle,
'email' => $member->email,
'avatar' => $avi,
'pronouns' => $member->pronoun,
'uuid' => $member->uuid,
'role' => $member->role
]);
}
public function editMember(Request $request, $uuid = 0)
{
$member = $this->member->get($uuid);
$avi = '';
if ($member->avatar == 'default-member-avatar') {
$avi = '/assets/images/global/default-avi.png';
} else {
$avi = $member->avatar;
}
return view('back.member', [
'member' => $member,
'avatar' => $avi,
'mode' => 'member-edit',
'title' => "Edit Member Info"]);
}
public function createMember(Request $Request)
{
return view('back.member', [
'mode' => 'member-create',
'title' => "Make a new friend"]);
}
//actions
public function profileEdit(Request $request)
{
$token = csrf_token();
//check if logged in member id matches profile request id
$member = Auth::user();
if ($member->uuid == $request->id) {
//validate required fields
$valid = $request->validate([
'handle' => ['required'],
'email' => ['required'],
]);
if ($valid) {
$response = $this->member->editProfile($request);
if ($response['status'] == true) {
return back()->with('message', $response['message']);
} else {
return back()->withErrors([$response['message']]);
}
} else {
return back()->withErrors(['Misssing some required info, homie.']);
}
} else {
return back()->withErrors(['This is not your profile to edit.']);
}
}
public function memberEdit(Request $request)
{
$token = csrf_token();
//role check
$member = Auth::user();
if ($member->role == 0) {
$valid = $request->validate([
'handle' => ['required'],
'email' => ['required'],
'role' => ['required']
]);
if ($valid) {
$response = $this->member->edit($request);
if ($response['status'] == true) {
return back()->with('message', $response['message']);
} else {
return back()->withErrors([$response['message']]);
}
} else {
return back()->withErrors(['Misssing some required info, homie.']);
}
} else {
return back()->withErrors(['Nah, you can\'t do this. Wrong permissions.']);
}
}
public function memberCreate(Request $request)
{
$token = csrf_token();
$member = Auth::user();
if ($member->role == 0) {
$valid = $request->validate([
'handle' => ['required'],
'email' => ['required'],
'role' => ['required'],
'pronouns' => ['required'],
'fresh_pass' => ['required'],
'fresh_pass_confirm' => ['required'],
]);
if ($valid) {
$response = $this->member->add($request);
if ($response['status'] == true) {
return redirect('/den/member')->with('message', $response['message']);
} else {
return back()->withErrors([$response['message']]);
}
} else {
return back()->withErrors(['Misssing some required info, homie.']);
}
} else {
return back()->withErrors(['Nah, you can\'t do this. Wrong permissions.']);
}
}
}