Files

108 lines
4.4 KiB
PHP
Raw Permalink Normal View History

2026-03-06 19:26:11 +03:00
<?php
namespace Conmed\Authserver;
use Bitrix\Main\Config\Option;
use Bitrix\Main\Context;
use Bitrix\Main\Type\DateTime;
use Bitrix\Highloadblock\HighloadBlockTable;
use Bitrix\Main\Loader;
trait ProfileTrait {
public static function userAction() {
header('Content-Type: application/json');
$req = Context::getCurrent()->getRequest();
$auth = $req->getHeader('Authorization');
$token = (preg_match('/Bearer\s+(.*)$/i', $auth, $m)) ? trim($m[1]) : $req->get("access_token");
$uid = self::getUidByToken($token);
if($uid) {
$u = \CUser::GetByID($uid)->Fetch();
$gn = []; $gc = []; $rs = \Bitrix\Main\GroupTable::getList(['filter'=>['ID'=>\CUser::GetUserGroup($u['ID']),'=ACTIVE'=>'Y'],'select'=>['NAME','STRING_ID','C_SORT']]);
while($g = $rs->fetch()) {
if($g['C_SORT']==555) $gn[]=$g['NAME'];
if($g['STRING_ID']) $gc[]=$g['STRING_ID'];
}
echo json_encode([
'id'=>$u['ID'], 'login'=>$u['LOGIN'], 'email'=>$u['EMAIL'], 'name'=>$u['NAME'],
'last_name'=>$u['LAST_NAME'], 'second_name'=>$u['SECOND_NAME'],
'specialties'=>$gn,
'city'=>$u['PERSONAL_CITY'],
//'phone'=>$u['PERSONAL_PHONE'],
'phone'=>$u['PERSONAL_MOBILE'],
'country'=>$u['PERSONAL_COUNTRY'], // ДОБАВЛЕНО
'specialties_code'=>$gc, 'groups_code'=>$gc
]);
} else { header('HTTP/1.0 401 Unauthorized'); }
}
public static function updateAction() {
header('Content-Type: application/json'); $req = Context::getCurrent()->getRequest();
if(!self::checkClient($req->getPost("client_id"), $req->getPost("client_secret"))) die(json_encode(['error'=>'forbidden']));
$uid = self::getUidByToken($req->getPost("access_token"));
if(!$uid) die(json_encode(['error'=>'invalid_token']));
$fields = [
"NAME" => $req->getPost("name"),
"LAST_NAME" => $req->getPost("last_name"),
"SECOND_NAME" => $req->getPost("second_name"),
//"PERSONAL_PHONE" => $req->getPost("phone"),
"PERSONAL_MOBILE" => $req->getPost("phone"),
"PERSONAL_CITY" => $req->getPost("city"),
"PERSONAL_COUNTRY" => $req->getPost("country") // ДОБАВЛЕНО
];
$newSpecs = $req->getPost("specialties");
$newNames = [];
$newCodes = [];
if(is_array($newSpecs)) {
$curG = \CUser::GetUserGroup($uid);
$finalG = [];
$allSpecG = [];
$rs = \Bitrix\Main\GroupTable::getList(['filter'=>['=C_SORT'=>555],'select'=>['ID']]);
while($g = $rs->fetch()) $allSpecG[] = $g['ID'];
foreach($curG as $gid) if(!in_array($gid, $allSpecG)) $finalG[] = $gid;
$rs = \Bitrix\Main\GroupTable::getList(['filter'=>['=STRING_ID'=>$newSpecs, '=C_SORT'=>555]]);
while($g = $rs->fetch()) {
$finalG[] = $g['ID'];
$newNames[] = $g['NAME'];
$newCodes[] = $g['STRING_ID'];
}
$fields["GROUP_ID"] = $finalG;
}
if((new \CUser)->Update($uid, $fields)) {
self::audit("PROFILE_UPDATED", $req->getPost("client_id"), $uid);
echo json_encode([
'status' => 'success',
'new_specialties' => $newNames,
'new_specialties_code' => $newCodes
]);
} else {
echo json_encode(['status'=>'error', 'message'=>'Update failed']);
}
}
public static function passwordAction() {
header('Content-Type: application/json'); $req = Context::getCurrent()->getRequest();
if(!self::checkClient($req->getPost("client_id"), $req->getPost("client_secret"))) die(json_encode(['error'=>'forbidden']));
$uid = self::getUidByToken($req->getPost("access_token"));
if(!$uid) die(json_encode(['error'=>'invalid_token']));
$np = $req->getPost("new_password");
$v = self::validatePassword($np);
if($v !== true) die(json_encode(['status'=>'error', 'message'=>$v]));
if((new \CUser)->Update($uid, ["PASSWORD"=>$np, "CONFIRM_PASSWORD"=>$np])) {
self::audit("PASS_CHANGED", $req->getPost("client_id"), $uid);
echo json_encode(['status'=>'success']);
}
}
}