'error', 'message' => '인증 실패']); // exit; // } // // 필수 파라미터 확인 // $required = ['emp_no', 'name', 'email', 'type', 'date']; // foreach ($required as $key) { // if (empty($data[$key])) { // http_response_code(400); // echo json_encode(['status' => 'error', 'message' => "$key 값이 누락되었습니다."]); // exit; // } // } // // DB 연결 // $conn = new mysqli('localhost', 'egbim', 'baron3840!!', 'egbim'); // if ($conn->connect_error) { // http_response_code(500); // echo json_encode(['status' => 'error', 'message' => 'DB 연결 실패']); // exit; // } // mysqli_set_charset($conn, 'utf8mb4'); // // 중복 체크 // $chk_stmt = $conn->prepare("SELECT COUNT(*) FROM resign_info WHERE emp_no = ? AND date = ?"); // $chk_stmt->bind_param("ss", $data['emp_no'], $data['date']); // $chk_stmt->execute(); // $chk_stmt->bind_result($exists); // $chk_stmt->fetch(); // $chk_stmt->close(); // if ($exists > 0) { // http_response_code(409); // echo json_encode(['status' => 'error', 'message' => '이미 등록된 퇴사자 정보입니다.']); // exit; // } // // INSERT 실행 // $stmt = $conn->prepare("INSERT INTO resign_info (emp_no, name, email, type, date) VALUES (?, ?, ?, ?, ?)"); // $stmt->bind_param("sssss", $data['emp_no'], $data['name'], $data['email'], $data['type'], $data['date']); // if ($stmt->execute()) { // echo json_encode(['status' => 'success', 'message' => '퇴사자 정보가 등록되었습니다.']); // } else { // http_response_code(500); // echo json_encode(['status' => 'error', 'message' => 'DB insert 실패: ' . $stmt->error]); // } // $stmt->close(); // $conn->close(); ?> "error", "message" => "인증 실패"]); // exit; // } // // 2. POST된 JSON 데이터 수신 // $input = file_get_contents('php://input'); // $data = json_decode($input, true); // // 3. 필수 파라미터 확인 // $required = ['emp_no', 'name', 'email', 'type', 'date']; // foreach ($required as $key) { // if (empty($data[$key])) { // echo json_encode(["status" => "error", "message" => "누락된 필드: $key"]); // exit; // } // } // // 4. DB 연결 // $mysqli = new mysqli('localhost', 'egbim', 'baron3840!!', 'egbim'); // if ($mysqli->connect_error) { // echo json_encode(["status" => "error", "message" => "DB 연결 실패"]); // exit; // } // // 5. INSERT 실행 // $stmt = $mysqli->prepare(" // INSERT INTO resign_info (emp_no, name, email, type, date) // VALUES (?, ?, ?, ?, ?) // "); // if (!$stmt) { // echo json_encode(["status" => "error", "message" => "쿼리 준비 실패"]); // exit; // } // $stmt->bind_param("sssss", $data['emp_no'], $data['name'], $data['email'], $data['type'], $data['date']); // if ($stmt->execute()) { // echo json_encode(["status" => "success", "message" => "퇴사자 정보가 등록되었습니다"]); // } else { // echo json_encode(["status" => "error", "message" => "DB insert 실패"]); // } // $stmt->close(); // $mysqli->close(); // // 6. Descope API로 **삭제** 호출 // $projectId = 'P2wON5fy1K6kyia269VpeIzYP8oP'; // $managementKey = 'K2ycqpjeh1voPBdxXxzB3ScZOQ6v9aiLmU2cIj70X1H8Kcoz0KWfCWmofUwAsAkJroXA8QC'; // $loginId = $data['email']; // // 7. Descope 관리 API: 사용자 삭제 (delete user) // $url = 'https://api.descope.com/v1/mgmt/user/delete'; // $headers = [ // 'Content-Type: application/json', // "Authorization: Bearer {$projectId}:{$managementKey}" // ]; // $body = json_encode([ // 'loginId' => $loginId // ]); // $ch = curl_init($url); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_setopt($ch, CURLOPT_POST, true); // curl_setopt($ch, CURLOPT_POSTFIELDS, $body); // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // $response = curl_exec($ch); // $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // $curlErr = curl_error($ch); // curl_close($ch); // // 8. 사내 emp_status 업데이트 API // $internalUrl = 'http://172.16.10.191:20000/user/emp_status'; // $payload2 = [ // 'email' => $data['email'], // 'status' => 'inactive', // 'active' or 'inactive' // ]; // $ch2 = curl_init($internalUrl); // curl_setopt_array($ch2, [ // CURLOPT_RETURNTRANSFER => true, // CURLOPT_POST => true, // CURLOPT_POSTFIELDS => json_encode($payload2), // CURLOPT_HTTPHEADER => ['Content-Type: application/json'], // ]); // $res2 = curl_exec($ch2); // $code2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE); // $err2 = curl_error($ch2); // curl_close($ch2); // // 결과 리턴 // if ($curlErr) { // echo json_encode([ // 'status'=>'partial_success', // 'message'=>'DB 등록은 성공했으나 Descope 삭제 중 오류: '.$curlErr // ]); // } else { // $resData = json_decode($response, true); // if ($httpCode >= 200 && $httpCode < 300) { // echo json_encode([ // 'status' => 'success', // 'message' => '퇴사자 DB 등록 및 Descope 사용자 삭제 완료', // 'descopeCode'=> $httpCode, // 'descopeRes' => $resData // ]); // } else { // echo json_encode([ // 'status' => 'partial_success', // 'message' => 'DB 등록은 성공했으나 Descope API 에러', // 'descopeCode'=> $httpCode, // 'descopeRes' => $resData // ]); // } // } // exit; ?> 'error', 'step' => 'auth', 'message' => '인증에 실패했습니다. 올바른 인증키를 사용했는지 확인해 주세요.' ], JSON_UNESCAPED_UNICODE); exit; } // 2. POST된 JSON 데이터 수신 $input = file_get_contents('php://input'); $data = json_decode($input, true); if (!is_array($data)) { echo json_encode([ 'status' => 'error', 'step' => 'parse', 'message' => '전달된 JSON이 올바르지 않습니다.' ], JSON_UNESCAPED_UNICODE); exit; } // 3. 필수 파라미터 확인 $required = ['emp_no','name','email','type','date']; foreach ($required as $key) { if (empty($data[$key])) { $friendly = [ 'emp_no' => '사번', 'name' => '이름', 'email' => '이메일', 'type' => '퇴사 유형', 'date' => '퇴사 일자' ][$key] ?? $key; echo json_encode([ 'status' => 'error', 'step' => 'validation', 'message' => "필수 항목이 누락되었습니다: {$friendly} 누락." ], JSON_UNESCAPED_UNICODE); exit; } } // 4. DB 연결 및 INSERT $db = new mysqli('localhost','egbim','baron3840!!','egbim'); if ($db->connect_error) { echo json_encode([ 'status' => 'error', 'step' => 'db_connect', 'message' => '데이터베이스 연결에 실패했습니다.' ], JSON_UNESCAPED_UNICODE); exit; } $stmt = $db->prepare(" INSERT INTO resign_info (emp_no,name,email,type,date) VALUES (?,?,?,?,?) "); $stmt->bind_param('sssss', $data['emp_no'], $data['name'], $data['email'], $data['type'], $data['date'] ); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); try { $stmt->execute(); if ($stmt->affected_rows === 0) { echo "중복된 데이터로 저장되지 않았습니다."; } else { echo "정상 저장되었습니다."; } } catch (mysqli_sql_exception $e) { if ($e->getCode() === 1062) { echo "중복된 키가 존재합니다: " . $e->getMessage(); } else { echo "데이터베이스 오류 발생: " . $e->getMessage(); } } exit; if (!$stmt->execute()) { // 중복 키(Primary Key/email) 에러 if ($stmt->errno === 1062) { echo json_encode([ 'status' => 'error', 'step' => 'db_insert', 'message' => '이미 동일한 이메일의 퇴사자 정보가 등록되어 있습니다.' ], JSON_UNESCAPED_UNICODE); } else { echo json_encode([ 'status' => 'error', 'step' => 'db_insert', 'message' => '퇴사자 정보를 저장하는 데 실패했습니다: ' . $stmt->error ], JSON_UNESCAPED_UNICODE); } $stmt->close(); $db->close(); exit; } else{ // execute()는 성공했지만 변경된 행이 없는 경우 if ($stmt->affected_rows === 0) { echo json_encode([ 'status' => 'error', 'step' => 'db_insert', 'message' => '이미 동일한 이메일의 퇴사자 정보가 존재하여 저장되지 않았습니다.' ], JSON_UNESCAPED_UNICODE); $stmt->close(); $db->close(); exit; } } $stmt->close(); $db->close(); // 초기 응답 객체 $result = [ 'status' => 'success', 'db' => 'ok', 'descope' => null, 'emp_status' => null, 'message' => '퇴사자 정보가 DB에 정상적으로 등록되었습니다.' ]; // DB 실패 시 바로 리턴 if (! $dbResult) { $result['status'] = 'error'; echo json_encode($result, JSON_UNESCAPED_UNICODE); exit; } // 이메일이 비어 있으면 나머지 호출 생략 if (empty($data['email'])) { $result['status'] = 'partial_success'; $result['message'] = '(이메일 정보가 없어 Descope 삭제 및 사내 상태 업데이트는 생략되었습니다.)'; echo json_encode($result, JSON_UNESCAPED_UNICODE); exit; } // 5. Descope 사용자 삭제 $projectId = 'P2wON5fy1K6kyia269VpeIzYP8oP'; $managementKey = 'K2ycqpjeh1voPBdxXxzB3ScZOQ6v9aiLmU2cIj70X1H8Kcoz0KWfCWmofUwAsAkJroXA8QC'; $ch1 = curl_init('https://api.descope.com/v1/mgmt/user/delete'); curl_setopt_array($ch1, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode(['loginId'=>$data['email']]), CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', "Authorization: Bearer {$projectId}:{$managementKey}" ], ]); $res1 = curl_exec($ch1); $code1 = curl_getinfo($ch1, CURLINFO_HTTP_CODE); $err1 = curl_error($ch1); curl_close($ch1); if ($err1) { $result['descope'] = [ 'status' => 'error', 'message' => 'Descope 사용자 삭제 중 오류가 발생했습니다: ' . $err1 ]; } elseif ($code1 >= 200 && $code1 < 300) { $result['descope'] = ['status'=>'ok','message'=>'Descope 사용자 삭제에 성공했습니다.']; } else { $result['descope'] = [ 'status' => 'error', 'message' => "Descope API 에러 (HTTP {$code1})", 'response' => json_decode($res1, true) ]; } // 6. 사내 emp_status 업데이트 $ch2 = curl_init('http://172.16.10.191:20000/user/emp_status'); curl_setopt_array($ch2, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode([ 'email' => $data['email'], 'status' => 'inactive' ]), CURLOPT_HTTPHEADER => ['Content-Type: application/json'], ]); $res2 = curl_exec($ch2); $code2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE); $err2 = curl_error($ch2); curl_close($ch2); if ($err2) { $result['emp_status'] = [ 'status' => 'error', 'message' => 'BEPs에 전송 오류가 발생했습니다: ' . $err2 ]; } elseif ($code2 >= 200 && $code2 < 300) { $result['emp_status'] = ['status'=>'ok','message'=>'BEPs에 전송 성공했습니다.']; } else { $result['emp_status'] = [ 'status' => 'error', 'message' => "BEPS API 에러 (HTTP {$code2})", 'response' => json_decode($res2, true) ]; } echo json_encode($result, JSON_UNESCAPED_UNICODE); exit;