adminService = $adminService; } public function store(Request $request) { try { $userData = [ 'email' => $request->email, 'authority' => $request->authority, 'tenantId' => $request->tenant_id, 'customerId' => $request->customer_id, 'firstName' => $request->first_name, 'lastName' => $request->last_name, 'phone' => $request->phone, 'name' => $request->name, 'description' => $request->description, 'defaultDashboardId' => $request->default_dashboard_id, 'defaultDashboardFullscreen' => $request->default_dashboard_fullscreen, 'homeDashboardId' => $request->home_dashboard_id, 'homeDashboardHideToolbar' => $request->home_dashboard_hide_toolbar, 'userCredentialsEnabled' => $request->user_credentials_enabled, 'failedLoginAttempts' => $request->failed_login_attempts, 'lastLoginTs' => $request->last_login_ts, 'version' => $request->version, ]; $response = $this->adminService->createUser($userData); $user = new User(); $user->id = Str::uuid(); $user->created_time = now()->timestamp; $user->tenant_id = $request->tenant_id; $user->customer_id = $request->customer_id; $user->email = $request->email; $user->authority = $request->authority; $user->first_name = $request->first_name; $user->last_name = $request->last_name; $user->phone = $request->phone; $user->version = $request->version; $user->name = $request->name; $user->description = $request->description; $user->default_dashboard_id = $request->default_dashboard_id; $user->default_dashboard_fullscreen = $request->default_dashboard_fullscreen; $user->home_dashboard_id = $request->home_dashboard_id; $user->home_dashboard_hide_toolbar = $request->home_dashboard_hide_toolbar; $user->user_credentials_enabled = $request->user_credentials_enabled; $user->failed_login_attempts = $request->failed_login_attempts; $user->last_login_ts = $request->last_login_ts; $user->save(); $randomToken = Str::random(64); $activationLink = url("/apia/activate/{$user->id}?token={$randomToken}"); $mail = Mail::to($user->email)->send(new UserCreatedMail($user, $activationLink)); return response()->json([ 'message' => __('auth.data_fetched_successfully'), 'user_id' => $user->id, 'activation_link' => $activationLink, 'token' => $randomToken, 'data' => $response ], 200); } catch (QueryException $e) { Log::error('Error in creating User ' . $e->getMessage()); return jsonResponseWithErrorMessageApi(__('auth.something went wrong'), 401); } } public function list() { try { $users = $this->adminService->listUsers(); if (!empty($users['data'])) { return response()->json([ 'message' => 'Users fetched successfully', 'users' => $users['data'] ], 200); } return response()->json(['message' => 'No users found'], 404); } catch (Exception $e) { return response()->json(['error' => 'Failed to fetch users', 'details' => $e->getMessage()], 500); } } public function delete($userId) { try { $response = $this->adminService->deleteUser($userId); if (isset($response['status']) && $response['status'] === 404) { return response()->json([ 'error' => "User with ID $userId not found in ThingsBoard", 'response' => $response ], 404); } $user = User::find($userId); if ($user) { $user->delete(); } else { return response()->json([ 'error' => "User with ID $userId not found in local database" ], 404); } return response()->json([ 'message' => 'User deleted successfully from ThingsBoard and local database', 'response' => $response ], 200); } catch (Exception $e) { Log::error('Error in deleting User ' . $e->getMessage()); return response()->json([ 'error' => 'Failed to delete user', 'details' => $e->getMessage() ], 500); } } // public function activate(Request $request, $id) // { // Log::info('Full Request URL: ' . $request->fullUrl()); // try { // $user = User::find($id); // if (!$user) { // Log::error("User not found for ID: {$id}"); // return response()->json([ // 'status' => false, // 'message' => 'User not found.' // ], 404); // } // $token = $request->query('token'); // if (!$token) { // Log::error("Token missing for User ID: {$id}"); // return response()->json([ // 'status' => false, // 'message' => 'Invalid activation link.' // ], 401); // } // // Validate password input // $validated = $request->validate([ // 'password' => 'required|min:6|confirmed' // ]); // // Update the user's password locally // $user->password = Hash::make($validated['password']); // $user->save(); // // Use the activateUser function to activate in ThingsBoard // $this->adminService->activateUser($user, $validated['password'], $token); // Log::info("User ID: {$id} activated successfully."); // return response()->json([ // 'status' => true, // 'message' => 'User activated and password set successfully in Laravel and ThingsBoard!', // 'user_id' => $user->id // ], 200); // } catch (ValidationException $e) { // Log::error("Validation error for User ID: {$id}. Exception: " . $e->getMessage()); // return response()->json([ // 'status' => false, // 'message' => 'Validation error.', // 'errors' => $e->errors() // ], 422); // } catch (\Exception $e) { // Log::error("Error activating user ID: {$id}. Exception: " . $e->getMessage()); // return response()->json([ // 'status' => false, // 'message' => 'An error occurred. Please try again later.', // 'error' => $e->getMessage() // ], 500); // } // } public function activate(Request $request, $id) { Log::info('Full Request URL: ' . $request->fullUrl()); try { $user = User::find($id); if (!$user) { Log::error("User not found for ID: {$id}"); return response()->json([ 'status' => false, 'message' => 'User not found.' ], 404); } $token = $request->query('token'); if (!$token) { Log::error("Token missing for User ID: {$id}"); return response()->json([ 'status' => false, 'message' => 'Invalid activation link.' ], 401); } // ✅ Validate password input $validated = $request->validate([ 'password' => 'required|min:6|confirmed' ]); // ✅ Update the user's password locally $user->password = Hash::make($validated['password']); $user->save(); Log::info("User ID: {$id} activated successfully in Laravel."); return response()->json([ 'status' => true, 'message' => 'User activated and password set successfully in Laravel!', 'user_id' => $user->id ], 200); } catch (ValidationException $e) { Log::error("Validation error for User ID: {$id}. Exception: " . $e->getMessage()); return response()->json([ 'status' => false, 'message' => 'Validation error.', 'errors' => $e->errors() ], 422); } catch (\Exception $e) { Log::error("Error activating user ID: {$id}. Exception: " . $e->getMessage()); return response()->json([ 'status' => false, 'message' => 'An error occurred. Please try again later.', 'error' => $e->getMessage() ], 500); } } // public function autoLogin(Request $request) // { // $request->validate([ // 'email' => 'required|email' // ]); // $email = $request->email; // $user = User::where('email', $email)->first(); // if (!$user) { // return response()->json([ // 'status' => false, // 'message' => 'User not found in Laravel. Please register or verify your email.' // ], 404); // } // Auth::login($user); // $thingsboardUser = $this->adminService->getUserByEmail($email); // if ($thingsboardUser) { // $tbUserId = $thingsboardUser['id']['id']; // $thingsboardDashboardUrl = "http://your-thingsboard-domain.com/dashboard/{$tbUserId}"; // } else { // $thingsboardDashboardUrl = null; // } // return response()->json([ // 'status' => true, // 'message' => 'User found, redirecting to dashboards...', // 'laravel_dashboard_url' => url("/dashboard/{$user->id}"), // 'thingsboard_dashboard_url' => $thingsboardDashboardUrl // ], 200); // } // public function loginUser(Request $request) // { // $email = $request->input('email'); // if (!$email) { // return response()->json([ // 'status' => false, // 'message' => 'Email is required.' // ], 400); // } // $localResponse = null; // $thingsboardResponse = null; // // ✅ Check in local database // $user = User::where('email', $email)->first(); // if ($user) { // $localResponse = [ // 'status' => true, // 'message' => 'Login successful (Local). Redirecting to Local dashboard...', // 'user_id' => $user->id, // 'email' => $email, // 'dashboard_url' => url('/dashboard') // Local dashboard URL // ]; // } else { // $localResponse = [ // 'status' => false, // 'message' => 'User not found in Local database.' // ]; // } // // ✅ Check in ThingsBoard // $thingsboardResponse = $this->adminService->getUserByIdThingsBoard($email); // if ($thingsboardResponse['status']) { // $thingsboardUser = $thingsboardResponse['user']; // $thingsboardResponse = [ // 'status' => true, // 'message' => 'Login successful (ThingsBoard). Redirecting to ThingsBoard dashboard...', // 'user_id' => $thingsboardUser['id']['id'], // 'email' => $email, // 'dashboard_url' => $thingsboardResponse['dashboard_url'] // ]; // } else { // $thingsboardResponse = [ // 'status' => false, // 'message' => 'User not found in ThingsBoard.' // ]; // } // // ✅ Return both responses // return response()->json([ // 'local' => $localResponse, // 'thingsboard' => $thingsboardResponse // ], 200); // } public function loginUser(Request $request) { $email = $request->input('email'); if (!$email) { return response()->json([ 'status' => false, 'message' => 'Email is required.' ], 400); } $localResponse = null; $thingsboardResponse = null; // ✅ Check in Local database $user = User::where('email', $email)->first(); if ($user) { $localResponse = [ 'status' => true, 'message' => 'Login successful (Local). Redirecting to Local dashboard...', 'user_id' => $user->id, 'email' => $email, 'dashboard_url' => url('/dashboard') ]; } else { $localResponse = [ 'status' => false, 'message' => 'User not found in Local database.' ]; } // ✅ Fetch ThingsBoard user by email, then by ID $thingsboardResponse = $this->adminService->getUserByEmailThingsBoard($email); // ✅ Return both responses return response()->json([ 'local' => $localResponse, 'thingsboard' => $thingsboardResponse ], 200); } }