Files
2026-07-23 16:15:57 +09:00

196 lines
6.0 KiB
PHP

<?php
// resign_and_update.php
header('Content-Type: application/json');
// 1. Authorization 헤더 검사
$authHeader = '';
if (function_exists('getallheaders')) {
$hdrs = getallheaders();
if (!empty($hdrs['authorization'])) {
$authHeader = $hdrs['authorization'];
} elseif (!empty($hdrs['Authorization'])) {
$authHeader = $hdrs['Authorization'];
}
} elseif (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
$authHeader = $_SERVER['HTTP_AUTHORIZATION'];
}
$expectedKey = 'abcd1234efgh5678';
if ($authHeader !== $expectedKey) {
echo json_encode([
'status' => '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']
);
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;
}
$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;