Files
register/marko_unpacked/marko/portal/services/IdServer.php
T
Admin Nasledstvo ac168868ee Initial import
2026-05-01 20:52:04 +03:00

145 lines
4.8 KiB
PHP

<?php
namespace app\services;
use app\models\RegisterObjectFiles;
use app\services\base\IdServerBase;
class IdServer extends IdServerBase
{
public function __construct()
{
$this->auth();
}
public function customGetRequest($req)
{
return $this->secureGetRequest($req);
}
public function customPostRequest($req, $data = [])
{
return $this->securePostRequest($req, $data);
}
public function updatePassword($userId, $password)
{
$url = \Yii::$app->params['id_server'] . '/admin/realms/' . \Yii::$app->params['realm'] . '/users/' . $userId . '/reset-password';
$data = [
'type' => 'password',
'temporary' => false,
'value' => $password,
];
$options = [
'http' => [
'header' => "Authorization: Bearer $this->accessToken\r\n" .
"Content-type: application/json\r\n",
'method' => 'PUT',
'content' => json_encode($data),
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
//get the http status code
$status = $http_response_header[0];
$status = explode(' ', $status);
$status = $status[1];
//$result = json_decode($result, true);
//check if $status is a success http status code - store the result in var is_success
$is_success = $status >= 200 && $status < 300;
return ['code' => $status, 'result' => $result, 'is_success' => $is_success];
}
//get the user's credentials
public function getCredentials($accessToken, $userId)
{
$url = \Yii::$app->params['id_server'] . '/admin/realms/' . \Yii::$app->params['realm'] . '/users/' . $userId . '/credentials';
$options = [
'http' => [
'header' => "Authorization: Bearer $accessToken\r\n" .
"Content-type: application/json\r\n",
'method' => 'GET',
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$result = json_decode($result, true);
return $result;
}
//remove a credential
public function removeCredential($accessToken, $userId, $credentialId)
{
$url = \Yii::$app->params['id_server'] . '/admin/realms/' . \Yii::$app->params['realm'] . '/users/' . $userId . '/credentials/' . $credentialId;
$options = [
'http' => [
'header' => "Authorization: Bearer $accessToken\r\n" .
"Content-type: application/json\r\n",
'method' => 'DELETE',
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
//get the http status code
$status = $http_response_header[0];
$status = explode(' ', $status);
$status = $status[1];
//$result = json_decode($result, true);
//check if $status is a success http status code - store the result in var is_success
$is_success = $status >= 200 && $status < 300;
return ['code' => $status, 'result' => $result, 'is_success' => $is_success];
}
public function turnTFA($userId)
{
$credentials = $this->getCredentials($this->accessToken, $userId);
//now loop over $credentials, looking for 'otp' type
foreach ($credentials as $credential) {
if ($credential['type'] == 'otp') {
$result = $this->removeCredential($this->accessToken, $userId, $credential['id']);
}
}
//now get the credentials again to confirm it's been deleted
$credentials = $this->getCredentials($this->accessToken, $userId);
}
public static function url($url)
{
$idServer = new IdServer();
$opts = array(
'http' =>
array(
'method' => 'GET',
'header' => 'Authorization: Bearer ' . $idServer->accessToken,
)
);
$context = stream_context_create($opts);
$hdrs = get_headers($url, true, $context);
header('Content-Type: ' . $hdrs['Content-Type']);
readfile($url, false, $context);
}
public static function getImg($id)
{
return \Yii::$app->params['portal'] . '/file-system/delivery/' . $id . '/';
}
public static function getVideoThumb($id)
{
return \Yii::$app->params['portal'] . '/file-system/video-thumb/' . $id . '/';
}
public static function getStreaming($id)
{
return \Yii::$app->params['portal'] . '/file-system/stream/' . $id . '/';
}
public static function getImgToResize($id) {
return \Yii::$app->params['portal'] . '/file-system/delivery-img/'.$id.'/';
}
}