first(); } catch (\Throwable $th) { return false; } if (!$User) { return false; } return $User; } public static function getDetailsbyHashkey($hashkey): bool|array { $User = self::getUserbyHashkey($hashkey); if (!$User) { return false; } // $UserDetail = $User->map(function ($detail) { // return [ // 'hashkey' => $detail->hashkey, // 'mobile_number' => $detail->mobile_number, // 'total_balance' => $detail->total_balance, // 'active' => $detail->active, // 'parent' => $detail->parent ? $detail->parent->name : null, // 'nickname' => $detail->nickname, // 'fullname' => $detail->fullname, // 'name' => $detail->name, // 'username' => $detail->username, // 'acct_type' => $detail->acct_type, // 'modified' => $detail->updated_at, // 'total_credit' => $detail->total_credit, // 'created' => $detail->created_at, // // 'children' => $detail->getAllDescendants, // ]; // }); $UserDetail = [ 'hashkey' => $User->hashkey, 'mobile_number' => $User->mobile_number, 'total_balance' => $User->total_balance, 'active' => $User->active, 'parent' => $User->parent ? $User->parent->name : null, 'parent_hashkey' => $User->parent ? $User->parent->hashkey : null, 'nickname' => $User->nickname, 'fullname' => $User->fullname, 'name' => $User->name, 'username' => $User->username, 'acct_type' => $User->acct_type, 'modified' => $User->updated_at, 'total_credit' => $User->total_credit, 'created' => $User->created_at, // 'children' => $User->getAllDescendants, // if you need this later 'store_hashkey' => Store::where('owner_id', $User->id)->orWhere('manager_id', $User->id)->value('hashkey'), 'stores' => Store::where('owner_id', $User->id)->orWhere('manager_id', $User->id)->get()->map(fn($s) => [ 'hashkey' => $s->hashkey, 'name' => $s->name, 'role' => $s->owner_id === $User->id ? 'owner' : 'manager' ]) ]; $CurrentUserType = Auth::user()->acct_type; try { $UserDetail = $UserDetail->toArray(); } catch (\Throwable $th) { } if ($CurrentUserType !== UserTypes::ULTIMATE) { unset($UserDetail['modified']); unset($UserDetail['total_credit']); unset($UserDetail['fullname']); unset($UserDetail['username']); } return $UserDetail; } public static function getDirectChildrenofTargetUser($hashkey) { $User = self::getUserbyHashkey($hashkey); if (!$User) { return false; } $children = $User->children->map(function ($child) { return [ 'hashkey' => $child->hashkey, 'name' => $child->name, 'fullname' => $child->fullname, 'username' => $child->username, 'mobile_number' => $child->mobile_number, 'total_balance' => $child->total_balance, 'acct_type' => $child->acct_type, 'active' => (bool) $child->active, ]; }); return $children; } public function Response_directChildrenofTargetUser(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } $children = self::getDirectChildrenofTargetUser($target_user); return PageController::PageResponse($children); } public static function getchildrenofTargetUser($hashkey) { $User = self::getUserbyHashkey($hashkey); if (!$User) { return false; } $children = $User; $children = $User->getAllDescendants()->map(function ($child) { $parentname = $child->parent->name . '(' . $child->parent->username . ')' . '---' . $child->parent->mobile_number; return [ 'mobile_number' => $child->mobile_number, 'total_balance' => $child->total_balance, 'acct_type' => $child->acct_type, 'active' => $child->active, 'parent' => $parentname, 'hashkey' => $child->hashkey, ]; }); return $children; } private static function isPermittedthenFindUserorFail(string $hashkey, bool|UserActions $permissionORUserAction) { if (!$hashkey || !is_string($hashkey)) { throw new \Exception('Invalid target user'); } if (is_bool($permissionORUserAction)) { if ($permissionORUserAction === true) { return true; } if ($permissionORUserAction === false) { throw new \Exception('Permission Denied'); } } if (!UserPermissions::isActionPermitted($hashkey, $permissionORUserAction)) { throw new \Exception('Permission Denied'); } $user = User::where('hashkey', $hashkey)->first(); if (!$user) { throw new \Exception('User not found'); } return $user; } public static function ToggleUserActive(bool $active, $hashkey, bool $autologout = true) { if (!$hashkey || !is_string($hashkey)) { return false; } if ($active) { if (!UserPermissions::isUserSetActiveAllowed($hashkey)) { return Response::json(['error' => 'Not Allowed'], 403); } } else { if (!UserPermissions::isUserSetInactiveAllowed($hashkey)) { return Response::json(['error' => 'Not Allowed'], 403); } } try { $targetUser = User::where('hashkey', $hashkey)->first(); if (!$targetUser) { return Response::json(['error' => 'User not found'], 404); } // $targetUser->active = false; $targetUser->active = $active; $targetUser->save(); if (!$active && $autologout) { self::LogoutUser($hashkey); } return Response::json(['success' => true], 200); } catch (\Throwable $th) { return Response::json(['error' => $th->getMessage()], 500); } } public static function ViewNotes($hashkey) { try { $target_user = User::where('hashkey', $hashkey)->first(); if (!$target_user) { throw new \Exception('User not found'); } if (!UserPermissions::isUserNotesViewingAllowed($hashkey)) { throw new \Exception('Permission Denied'); } return $target_user->notes; } catch (\Throwable $th) { throw new \Exception('Error retrieving notes: ' . $th->getMessage()); } } public static function ReplaceNotes(string $hashkey, string $note) { try { if (!UserPermissions::isUserNotesUpdateAllowed($hashkey)) { throw new \Exception('Permission Denied'); } $target_user = User::where('hashkey', $hashkey)->first(); if (!$target_user) { throw new \Exception('User not found'); } $target_user->notes = $note; $target_user->save(); return true; } catch (\Throwable $th) { throw new \Exception('Error updating notes: ' . $th->getMessage()); } } public static function DeleteNotes(string $hashkey) { if (!UserPermissions::isUserNotesDeletionAllowed($hashkey)) { throw new \Exception('Permission Denied'); } return self::ReplaceNotes($hashkey, ''); } public static function ViewExec($hashkey) { try { $target_user = User::where('hashkey', $hashkey)->first(); if (!$target_user) { throw new \Exception('User not found'); } if (!UserPermissions::isUserExecViewingAllowed($hashkey)) { throw new \Exception('Permission Denied'); } return $target_user->exec_command; } catch (\Throwable $th) { throw new \Exception($th->getMessage()); } } public static function DeleteExec(string $hashkey) { if (!UserPermissions::isUserExecDeletionAllowed($hashkey)) { throw new \Exception('Permission Denied'); } return self::ReplaceExec($hashkey, ''); } public static function ReplaceExec(string $hashkey, string $exec) { try { $target_user = User::where('hashkey', $hashkey)->first(); if (!$target_user) { throw new \Exception('User not found'); } if (!UserPermissions::isUserExecChangeAllowed($hashkey)) { throw new \Exception('Permission Denied'); } $target_user->exec_command = $exec; $target_user->save(); return true; } catch (\Throwable $th) { throw new \Exception($th->getMessage()); } } public static function UpdateUserDetails(string $hashkey, $details) { try { $target_user = User::where('hashkey', $hashkey)->first(); if (!$target_user) { throw new \Exception('User not found'); } if (!UserPermissions::isUserModificationAllowed($hashkey)) { throw new \Exception('Permission Denied'); } if (isset($details['mobile_number'])) { $target_user->mobile_number = LoginController::normalizePhMobile((string) $details['mobile_number']); } if (isset($details['nickname'])) { $target_user->nickname = $details['nickname']; } if (isset($details['name'])) { $target_user->name = $details['name']; } if (isset($details['username'])) { $target_user->username = $details['username']; } if (isset($details['fullname'])) { $target_user->fullname = $details['fullname']; } if (isset($details['type']) && $details['type'] !== '') { $target_user->acct_type = $details['type']; } if (isset($details['parent']) && $details['parent'] !== '') { $parentModel = User::where('hashkey', $details['parent'])->first(); if ($parentModel) { $target_user->parentuid = $parentModel->id; } } $target_user->save(); return true; } catch (\Throwable $th) { throw new \Exception($th->getMessage()); } } public static function ResetUserPassword(string $hashkey, string $newPassword) { try { $target_user = User::where('hashkey', $hashkey)->first(); if (!$target_user) { throw new \Exception('User not found'); } if (!UserPermissions::isUserPasswordChangeAllowed($hashkey)) { throw new \Exception('Permission Denied'); } $target_user->password = Hash::make($newPassword); $target_user->save(); return true; } catch (\Throwable $th) { throw new \Exception($th->getMessage()); } } public static function TransferMyCredit(string $hashkey, float $amount) { $currentuser = Auth::user(); if ($amount <= 0) { throw new \Exception('Invalid amount'); } try { $target_user = User::where('hashkey', $hashkey)->first(); $currentUserBalance = $currentuser->total_balance; if ($currentuser->acct_type !== UserTypes::ULTIMATE && $currentUserBalance < $amount) { throw new \Exception('Insufficient balance'); } if (!$target_user) { throw new \Exception('User not found'); } if (!UserPermissions::isDirectCreditTransfertoUserAllowed($hashkey)) { throw new \Exception('Permission Denied'); } $target_user->total_credit += $amount; $target_user->save(); return true; } catch (\Throwable $th) { throw new \Exception($th->getMessage()); } } public static function LogoutUser(string $hashkey) { $user = self::isPermittedthenFindUserorFail($hashkey, UserActions::ForceLogoutUser); if (!$user) { throw new \Exception('Error Fetching User'); } try { return \App\Http\Controllers\RemoteLogoutController::remoteLogout($hashkey); } catch (\Throwable $th) { throw new \Exception($th->getMessage()); } } public static function ShowUserRoles(string $hashkey) { $user = self::isPermittedthenFindUserorFail($hashkey, UserActions::UserAllowedtoViewOtherUserRoles); if (!$user) { throw new \Exception('Error Fetching User'); } $roles = UserPermissions::getUserRoles($user->id); $formatted = []; foreach ($roles as $role) { // $role is already an instance of UserActions if ($role instanceof UserActions) { $formatted[] = [ 'value' => $role->value, 'name' => $role->name ]; } else { $formatted[] = [ 'value' => (string) $role, 'name' => 'UNKNOWN' ]; } } return $formatted; } public static function ShowAllRoles() { $allowed = UserPermissions::isActionPermitted(Auth::id(), UserActions::UserAllowedtoViewAllRoles); if (!$allowed) { throw new \Exception('Not Allowed'); } return array_map(function (UserActions $role) { return [ 'value' => $role->value, 'name' => $role->name ]; }, UserActions::cases()); } public static function ChangeUserRoles(string $target_user, array $new_roles) { // 1) Permission + load $user = self::isPermittedthenFindUserorFail( $target_user, permissionORUserAction: UserActions::UserAllowedtoChangeAnotherUserRoles ); // 2) Normalize all incoming roles to enums (accept enum, backing value, or case name) $new_roles = array_map(fn($r) => UserPermissions::normalizeRole($r), $new_roles); // 3) Defaults for this user type (fix: acct_type, not type) $default_roles_map = UserPermissions::roles(); $acctType = $user->acct_type; // cast to enum in your model $default_roles = $default_roles_map[$acctType->value] ?? []; // array of UserActions // 4) Remove any newly granted roles from denied_roles $denied_roles = $user->denied_roles ?? []; // expect UserActions[] via your cast $denied_roles = array_values(array_filter( $denied_roles, fn(UserActions $r) => !in_array($r, $new_roles, true) )); $user->denied_roles = $denied_roles; // 5) Clean additional_roles to only those present in new_roles $additional_roles = $user->additional_roles ?? []; // expect UserActions[] via your cast // use strict comparison instead of array_intersect (which is non-strict) $additional_roles = array_values(array_filter( $additional_roles, fn(UserActions $r) => in_array($r, $new_roles, true) )); // 6) Add roles that are in new_roles but NOT in defaults into additional_roles foreach ($new_roles as $role) { if (!in_array($role, $default_roles, true) && !in_array($role, $additional_roles, true)) { $additional_roles[] = $role; } } // 7) Save $user->additional_roles = $additional_roles; // your cast will persist backing values $user->save(); return $user; } public function ChangeUserParent(string $targetUser, string|int $parent) { $targetUserModel = self::isPermittedthenFindUserorFail( $targetUser, permissionORUserAction: UserActions::ChangeAnotherUsersParent ); try { // Resolve parent user if (is_string($parent)) { $parentModel = User::where('hashkey', $parent)->first(); if (empty($parentModel)) { throw new \Exception("Parent user not found by hashkey."); } } else { $parentModel = User::findOrFail($parent); } $targetUserModel->parentuid = $parentModel->id; $targetUserModel->save(); return true; } catch (\Throwable $th) { throw new \Exception($th->getMessage(), 1); } } public static function DeleteUser(string $hashkey) { try { if (!UserPermissions::isUserDeletionAllowed($hashkey)) { throw new \Exception('Permission Denied'); } $target_user = User::where('hashkey', $hashkey)->first(); if (!$target_user) { throw new \Exception('User not found'); } // Optional: Logout the user before deleting self::LogoutUser($hashkey); $target_user->delete(); return true; } catch (\Throwable $th) { throw new \Exception('Error deleting user: ' . $th->getMessage()); } } public static function DetachStoreFromUser(string $userHash, string $storeHash) { try { $user = User::where('hashkey', $userHash)->first(); $store = Store::where('hashkey', $storeHash)->first(); if (!$user || !$store) { throw new \Exception('User or Store not found'); } if (!UserPermissions::isUserModificationAllowed($userHash)) { throw new \Exception('Permission Denied'); } if ($store->owner_id === $user->id) { $store->owner_id = null; } if ($store->manager_id === $user->id) { $store->manager_id = null; } $store->save(); return true; } catch (\Throwable $th) { throw new \Exception('Error detaching store: ' . $th->getMessage()); } } public static function ExtendUserSessions(string $hashkey) { $user = self::isPermittedthenFindUserorFail($hashkey, UserActions::UserAllowedtoChangeAnotherUserRoles); // Reuse permission for now or add a new one $sessions = Redis::smembers("user_sessions:{$hashkey}"); if (empty($sessions)) { return false; } $results = []; foreach ($sessions as $sessionId) { $results[$sessionId] = \App\Http\Controllers\LoginController::setSessiontoKeepAlive($sessionId); } return $results; } } trait PageResponses_UserModify { public function Response_UserDetails(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } $user_details = self::getDetailsbyHashkey($target_user); return PageController::PageResponse($user_details); } public function Response_childrenofTargetUser(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } $user_details = self::getchildrenofTargetUser($target_user); return PageController::PageResponse($user_details); } public function Response_EnableUser(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } return self::ToggleUserActive(true, $target_user); } public function Response_DisableUser(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } return self::ToggleUserActive(false, $target_user); } public function Response_ViewNotes(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } try { $notes = self::ViewNotes($target_user); } catch (\Throwable $th) { //throw $th; return response()->json($th->getMessage(), 500); } // if ($notes === false) { // return response()->json(data: 'User not found', 404); // } return response()->raw($notes); } public function Response_ReplaceNotes(Request $request) { $target_user = $request->input('target_user'); $newnotecontent = $request->input('newnotecontent'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } try { $success = self::ReplaceNotes($target_user, $newnotecontent); } catch (\Throwable $th) { //throw $th; return response()->json($th->getMessage(), 500); } // if (!$success) { // return response()->json('User not found or update failed', 400); // } return response()->json(true, 200); } public function Response_DeleteNotes(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } try { $notes = self::DeleteNotes($target_user); } catch (\Throwable $th) { return response()->json($th->getMessage(), 500); } if ($notes === false) { return response()->json('Error', 404); } return response()->json(true, 200); } public function Response_ViewExec(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } try { $exec = self::ViewExec($target_user); } catch (\Throwable $th) { return response()->json($th->getMessage(), 500); } if ($exec === false) { return response()->json('User not found', 404); } return response()->raw($exec); } public function Response_ReplaceExec(Request $request) { $target_user = $request->input('target_user'); $newexeccontent = $request->input('newexeccontent'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } try { $success = self::ReplaceExec($target_user, $newexeccontent); } catch (\Throwable $th) { return response()->json($th->getMessage(), 500); } if (!$success) { return response()->json('User not found or update failed', 400); } return response()->json(true, 200); } public function Response_DeleteExec(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } try { $notes = self::DeleteExec($target_user); } catch (\Throwable $th) { return response()->json($th->getMessage(), 500); } return response()->json(true, 200); } public function Response_DetachStore(Request $request) { $target_user = $request->input('target_user'); $store_hash = $request->input('store_hash'); if (!$target_user || !$store_hash) { return Response::json(['success' => false, 'message' => 'Missing parameters'], 400); } try { $success = self::DetachStoreFromUser($target_user, $store_hash); return Response::json(['success' => $success], 200); } catch (\Throwable $th) { return Response::json(['success' => false, 'message' => $th->getMessage()], 500); } } public function Response_UpdateUserDetails(Request $request) { $target_user = $request->input('target_user'); $details = $request->input('details'); if (!$target_user || !is_string($target_user) || !is_array($details)) { return Response::json(['success' => false], 404); } try { $success = self::UpdateUserDetails($target_user, $details); } catch (\Throwable $th) { return response()->json($th->getMessage(), 500); } if (!$success) { return response()->json('User not found or update failed', 400); } return response()->json(true, 200); } public function Response_ResetUserPassword(Request $request) { $target_user = $request->input('target_user'); $newPassword = $request->input('user_new_password'); if (!$target_user || !is_string($target_user) || !$newPassword || !is_string($newPassword)) { return Response::json(['success' => false], 404); } try { $success = self::ResetUserPassword($target_user, $newPassword); } catch (\Throwable $th) { return response()->json($th->getMessage(), 500); } if (!$success) { return response()->json('User not found or update failed', 400); } return response()->json(true, 200); } public function Response_LogoutUser(Request $request) { $target_user = $request->input('target_user'); try { $user = self::LogoutUser($target_user); if (!$user) { return response()->json('Unable to Logout User!', 400); } return response()->json(true, 200); } catch (\Throwable $th) { return response()->json('Unable to Logout User ' . $th->getMessage(), 400); } } public function Response_UserRoles(Request $request) { $target_user = $request->input('target_user'); try { $roles = self::ShowUserRoles($target_user); return response()->json($roles, 200); } catch (\Throwable $th) { return response()->json('Unable fetch User Roles' . $th->getMessage(), 400); } } public function Response_AllRoles() { try { $roles = self::ShowAllRoles(); return response()->json($roles, 200); } catch (\Throwable $th) { return response()->json('Error' . $th->getMessage(), 400); } } public function Response_ChangeUserRoles(Request $request) { $target_user = $request->input('target_user'); $new_roles = $request->input('roles'); try { $success = self::ChangeUserRoles($target_user, $new_roles); if (!$success) { return response()->json('Error', 500); } return response()->json(true, 200); } catch (\Throwable $th) { return response()->json($th->getMessage(), 500); } } public function Response_ChangeUserParent(Request $request) { $target_user = $request->input('target_user'); $parent = $request->input('parent'); try { $success = self::ChangeUserParent($target_user, $parent); if (!$success) { return response()->json('Error', 500); } return response()->json(true, 200); } catch (\Throwable $th) { return response()->json($th->getMessage(), 500); } } // public function Response_TransferMyCredit(Request $request) // { // $target_user = $request->input('target_user'); // $amount = $request->input('amount'); // if (!$target_user || !is_string($target_user) || !$amount || !is_numeric($amount)) { // return Response::json(false, 404); // } // try { // $success = self::TransferMyCredit($target_user, (float)$amount); // } catch (\Throwable $th) { // return response()->json($th->getMessage(), 500); // } // if (!$success) { // return response()->json('User not found or transfer failed', 400); // } // return response()->json(true, 200); // } public function Response_DeleteUser(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(false, 404); } try { $success = self::DeleteUser($target_user); return response()->json($success, 200); } catch (\Throwable $th) { return response()->json($th->getMessage(), 500); } } public function Response_ExtendUserSessions(Request $request) { $target_user = $request->input('target_user'); if (!$target_user || !is_string($target_user)) { return Response::json(['success' => false], 404); } try { $results = self::ExtendUserSessions($target_user); if ($results === false) { return Response::json(['success' => false, 'message' => 'No active sessions found for this user.'], 404); } return Response::json(['success' => true, 'data' => $results], 200); } catch (\Throwable $th) { return Response::json(['success' => false, 'message' => $th->getMessage()], 500); } } } //Add This Controls Later At User Modify Blade // redirecttofile('/UserdetailsControls', 'pages/slvl/adminpages/disableuser', 'GET', $IsUserUltimate); // redirecttofile('/UserdetailsControls', 'pages/slvl/adminpages/open_place_bet', 'GET', $IsUserUltimate); // redirecttofile('/UserdetailsControls', 'pages/slvl/adminpages/open_set_notes', 'GET', $IsUserUltimate); // redirecttofile('/UserdetailsControls', 'pages/slvl/adminpages/open_set_exec', 'GET', $IsUserUltimate); // redirecttofile('/UserdetailsControls', 'pages/slvl/adminpages/open_last30DaysAmountPrizes_Report', 'GET', $IsUserUltimate); // redirecttofile('/UserdetailsControls', 'pages/slvl/adminpages/open_request_credit', 'GET', $IsUserUltimate); // redirecttofile('/UserdetailsControls', 'pages/slvl/adminpages/updateuserdetails', 'GET', $IsUserUltimate); // redirecttofile('/UserdetailsControls', 'pages/slvl/adminpages/resetuserpassword', 'GET', $IsUserUltimate); // redirecttofile('/UserdetailsControls', 'pages/slvl/adminpages/open_logout_user', 'GET', $IsUserUltimate);