Files
egbim_homepage/eng/skin/member/basic/descope_update.php
T
2026-07-23 16:15:57 +09:00

178 lines
6.0 KiB
PHP

<?php
session_start();
// -- JSON 응답 보장 --
header('Content-Type: application/json; charset=UTF-8');
// -- 실서비스 전에는 환경변수 사용 권장 --
$projectId = 'P2wON5fy1K6kyia269VpeIzYP8oP';
$managementKey = 'K32l5ORmzy32OvaaPvpdZsMY3JmKQb7a3vvrl10PgjlJUGk3K7EssMH3uW5VGQSbrgtEdPj';
// -- POST 입력값 (여기서 name 값 등 반드시 필수체크 필요!) --
$email = trim($_POST['email'] ?? '');
$name = trim($_POST['name'] ?? '');
$password = trim($_POST['password'] ?? '');
$family = trim($_POST['familyCompany'] ?? ''); // Machine Name: familycompany
$familyUniqueKey = trim($_POST['familyUniqueKey'] ?? ''); // Machine Name: familyUniqueKey
$team = trim($_POST['team'] ?? ''); // Machine Name: team
$position = trim($_POST['position'] ?? ''); // Machine Name: position
$hp = trim($_POST['phone'] ?? ''); // phone
// $completeForm = ($_POST['completeForm'] ?? '') ? true : false; // Boolean 처리
$completeForm = filter_var($_POST['completeForm'] ?? false, FILTER_VALIDATE_BOOLEAN);
$refreshJwt = trim($_POST['refreshJwt']);
// $refreshJwt = $_SESSION['refreshJwt'] ?? '';
$company = trim($_POST['company'] ?? ''); // Machine Name: company 외부사용자 회사명
// if (!$refreshJwt) {
// // 필수 값 누락시 에러 반환
// echo json_encode(["error" => "refreshJWT is missing"]);
// exit;
// }
if (!$refreshJwt) {
echo json_encode([
"status" => "fail",
"message" => "세션이 만료되었습니다. 다시 로그인해 주세요."
]);
exit;
}
$internalDomains = [
"hanmaceng.co.kr",
"samaneng.com",
"jangheon.co.kr",
"hallasanup.com",
"pre-cast.co.kr",
"baroncs.co.kr"
];
$domain = explode('@', $email)[1] ?? '';
// if (in_array($domain, $internalDomains)) {
// $tenantId = "T2wQcWCBhUfJgUWHWgNwLg4iUDVY"; //내부
// } else {
// $tenantId = "T2x4TDzxasp7auPCPcN8uOrxXchh"; //외부
// }
if (in_array($domain, $internalDomains)) {
// 내부 사용자 → 내부 tenant 하나만
$userTenants = [
[
"tenantId" => "T2wQcWCBhUfJgUWHWgNwLg4iUDVY" // 내부 tenantId
]
];
} else {
// 외부 사용자 → customer + egbim 두 개 tenant 자동 등록
$userTenants = [
[
"tenantId" => "T2x4TDzxasp7auPCPcN8uOrxXchh" // customer tenantId
],
[
"tenantId" => "T2yGFrGSnFX601G22JOMojJX7OMd" // egbim tenantId
]
];
}
//라이센스 만료일 계산
$now = new DateTime('now', new DateTimeZone('Asia/Seoul'));
$expiry = (clone $now)->modify('+90 days')->format(DateTime::ATOM);
// -- Custom Attribute 실제 Machine Name에 맞춰서 할당 (Descope 콘솔 기준) --
$custom = [
"familyCompany" => $family, // 소속 가족사
"familyUniqueKey" => $familyUniqueKey, // 사번
"team" => $team, // 소속팀
"position" => $position, // 직위
"completeForm" => $completeForm, // 정보입력완료 (Boolean)
// "egBimLExpiryDate" => $expiry //egbim 라이센스 만료일
// 필요하면 추가로 다른 Machine Name도 사용 가능
];
if (! in_array($domain, $internalDomains, true)) {
$custom["egBimLExpiryDate"] = $expiry;
}
if ($company) {
$custom["company"] = $company; // 외부사용자 회사명 추가
}
// -- 1. 사용자 정보 업데이트 --
$update_body = [
"loginId" => $email,
"email" => $email,
"name" => $name,
"phone" => $hp,
"verifiedPhone" => false,
"customAttributes" => $custom,
// "roleNames" => [ "Default User" ],
"userTenants" => $userTenants
];
$ch = curl_init('https://api.descope.com/v1/mgmt/user/update');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"Authorization: Bearer {$projectId}:{$managementKey}"
],
CURLOPT_POSTFIELDS => json_encode($update_body),
]);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$res_data = json_decode($response, true);
// -- 1-1. 사용자 정보 업데이트 실패시 --
if ($http_code !== 200) {
$msg = $res_data['errorDescription'] ?? 'Descope 정보 업데이트 실패';
echo json_encode([
'status' => 'fail',
'step' => 'info',
'message' => $msg,
'raw' => $response
]);
exit;
}
// -- 2. 비밀번호 변경 (입력값이 있을 경우) --
if ($password) {
$pw_body = [
"loginId" => $email,
"newPassword" => $password
];
error_log("Descope password update payload: " . json_encode($pw_body));
error_log("Using refreshJwt for password update: " . $refreshJwt);
$ch2 = curl_init('https://api.descope.com/v1/auth/password/update');
curl_setopt_array($ch2, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"Authorization: Bearer {$projectId}:{$refreshJwt}"
],
CURLOPT_POSTFIELDS => json_encode($pw_body),
]);
$response2 = curl_exec($ch2);
$http_code2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE);
curl_close($ch2);
$res_data2 = json_decode($response2, true);
if ($http_code2 !== 200) {
$msg2 = $res_data2['errorDescription'] ?? 'Descope 비밀번호 변경 실패';
echo json_encode([
'status' => 'fail',
'step' => 'password',
'message' => $msg2,
'raw' => $response2
]);
exit;
}
}
// -- 3. 모두 성공 --
echo json_encode(['status' => 'ok']);
?>