Skip to content

Commit

Permalink
Merge pull request #1 from ntidev/dev-refacting
Browse files Browse the repository at this point in the history
Dev refacting
  • Loading branch information
yansellrivasdiaz authored Sep 27, 2021
2 parents a7020e2 + 3955ad1 commit 937eb47
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 128 deletions.
9 changes: 9 additions & 0 deletions Service/KeycloakAdminGroupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public function updateGroup($id, $data) {
return $response;
}

public function getRoles($id) {
$url = $this->basePath.self::GET_ROLES_URL;
$url = str_replace("{id}", $id, $url);
$url = str_replace("{clientId}", $this->container->getParameter(self::KEYCLOAK_CLIENT_ID_CODE), $url);
$result = $this->restGet($url);
$response = json_decode($result, true);
return $response;
}

public function deleteGroup($id) {
$url = $this->basePath.self::UPDATE_BY_ID_URL;
$url = str_replace("{id}", $id, $url);
Expand Down
65 changes: 60 additions & 5 deletions Service/KeycloakAdminRoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@
class KeycloakAdminRoleService extends KeycloakSecurityService {

protected $basePath = "/auth/admin/realms/{realm}/clients/{clientIdCode}/roles";

protected $basePathRealm = "/auth/admin/realms/{realm}/roles-by-id";

const GET_ALL_URL = "";
const GET_BY_ID_URL = "/{roleId}";
const UPDATE_BY_ID_URL = "/{roleId}";
const DELETE_BY_ID_URL = "/{roleId}";
const GET_BY_NAME_URL = "/{name}";
const UPDATE_BY_NAME_URL = "/{name}";
const DELETE_BY_NAME_URL = "/{name}";
const GET_BY_NAME_COMPOSITES_URL = "/{name}/composites";
const UPDATE_BY_NAME_COMPOSITES_URL = "/{name}/composites";
const GET_BY_ID_COMPOSITES_URL = "/{roleId}/composites";
const UPDATE_BY_ID_COMPOSITES_URL = "/{roleId}/composites";

public function __construct(ContainerInterface $container) {
parent::__construct($container);
$this->basePath = str_replace("{realm}", $this->container->getParameter(self::KEYCLOAK_REALM), $this->basePath);
$this->basePathRealm = str_replace("{realm}", $this->container->getParameter(self::KEYCLOAK_REALM), $this->basePathRealm);
$this->basePath = str_replace("{clientIdCode}", $this->container->getParameter(self::KEYCLOAK_CLIENT_ID_CODE), $this->basePath);
}

Expand All @@ -28,7 +36,7 @@ public function getAll($options = array()) {
$response = json_decode($result, true);
return $response;
}

public function getByName($name, $options = array()) {
$url = $this->basePath.self::GET_BY_NAME_URL;
$url = str_replace("{name}", $name, $url);
Expand All @@ -38,6 +46,22 @@ public function getByName($name, $options = array()) {
return $response;
}

public function getById($roleId) {
$url = $this->basePathRealm.self::GET_BY_ID_URL;
$url = str_replace("{roleId}", $roleId, $url);
$result = $this->restGet($url);
$response = json_decode($result, true);
return $response;
}

public function updateRoleById($roleId, $data) {
$url = $this->basePathRealm.self::UPDATE_BY_ID_URL;
$url = str_replace("{roleId}", $roleId, $url);
$result = $this->restPut($url, $data);
$response = json_decode($result, true);
return $response;
}

public function saveNewRole($data) {
$url = $this->basePath;
$result = $this->restPost($url, $data);
Expand All @@ -54,11 +78,19 @@ public function updateRole($role, $data) {
}

public function deleteRole($role) {
$url = $this->basePath.self::UPDATE_BY_NAME_URL;
$url = $this->basePath.self::DELETE_BY_NAME_URL;
$url = str_replace("{name}", $role, $url);
$result = $this->restDelete($url);
$response = json_decode($result, true);
return $response;
}

public function deleteRoleById($roleId) {
$url = $this->basePathRealm.self::DELETE_BY_ID_URL;
$url = str_replace("{roleId}", $roleId, $url);
$result = $this->restDelete($url);
$response = json_decode($result, true);
return $response;
}

public function getCompositesByName($name, $options = array()) {
Expand All @@ -69,15 +101,31 @@ public function getCompositesByName($name, $options = array()) {
$response = json_decode($result, true);
return $response;
}


public function getCompositesById($roleId) {
$url = $this->basePathRealm.self::GET_BY_ID_COMPOSITES_URL;
$url = str_replace("{roleId}", $roleId, $url);
$result = $this->restGet($url);
$response = json_decode($result, true);
return $response;
}

public function updateCompositesByName($name, $data) {
$url = $this->basePath.self::UPDATE_BY_NAME_COMPOSITES_URL;
$url = str_replace("{name}", $name, $url);
$result = $this->restPost($url, $data);
$response = json_decode($result, true);
return $response;
}


public function updateCompositesById($roleId, $data) {
$url = $this->basePathRealm.self::UPDATE_BY_ID_COMPOSITES_URL;
$url = str_replace("{roleId}", $roleId, $url);
$result = $this->restPost($url, $data);
$response = json_decode($result, true);
return $response;
}

public function deleteCompositesByName($name, $data) {
$url = $this->basePath.self::UPDATE_BY_NAME_COMPOSITES_URL;
$url = str_replace("{name}", $name, $url);
Expand All @@ -86,4 +134,11 @@ public function deleteCompositesByName($name, $data) {
return $response;
}

public function deleteCompositesById($roleId, $data) {
$url = $this->basePathRealm.self::UPDATE_BY_ID_COMPOSITES_URL;
$url = str_replace("{roleId}", $roleId, $url);
$result = $this->restDelete($url, $data);
$response = json_decode($result, true);
return $response;
}
}
40 changes: 31 additions & 9 deletions Service/KeycloakAdminUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class KeycloakAdminUserService extends KeycloakSecurityService {
const UPDATE_ROLES_URL = "/{id}/role-mappings/clients/{clientId}";
const RESET_PASSWORD_URL = "/{id}/execute-actions-email";
const UPDATE_GROUP_USER = "/{id}/groups/{groupId}";
const DENIED_ROLES = "denied_roles";

public function __construct(ContainerInterface $container) {
parent::__construct($container);
Expand All @@ -37,7 +38,7 @@ public function get($id) {
$url = $this->basePath.self::GET_URL;
$url = str_replace("{id}", $id, $url);
$result = $this->restGet($url);
$response = json_decode($result, true);
$response = $this->attributesDecode(json_decode($result, true));
return $response;
}

Expand All @@ -50,13 +51,12 @@ public function getFromEmail($email, $roles = false) {

if(!isset($res[0])) return null;

$userData = $res[0];
$userData = $this->attributesDecode($res[0]);

if($roles == true){
// Parse denied roles
$deniedRoles = [];
if(isset($userData['attributes']) && isset($userData['attributes']['denied_roles'])){
$deniedRoles = json_decode($userData['attributes']['denied_roles'][0], true);
$deniedRoles = $deniedRoles[$clientId];
}

Expand All @@ -70,12 +70,6 @@ public function getFromEmail($email, $roles = false) {
$userData['roles'] = array_keys($rolesTmp);
}

if(isset($userData['attributes'])){
foreach ($userData['attributes'] as $attribute => $value) {
$userData['attributes'][$attribute] = is_array($value) ? ($value[0] == 'true' ? true : ($value[0] == 'false' ? false : $value[0])) : $value;
}
}

return $userData;
}catch (\Exception $ex){
return null;
Expand Down Expand Up @@ -167,4 +161,32 @@ public function updateUserGroup($id, $groupId, $data) {
return $response;
}

private function attributesDecode($data = null)
{
if(array_key_exists("attributes",$data) && $data["attributes"]){
foreach ($data['attributes'] as $attribute => $value) {
if(strpos($attribute,self::DENIED_ROLES) !== false && is_array($value)){
$data['attributes'][$attribute] = $this->isJSON($value[0]) ? json_decode($value[0],true) : [];
}else if(is_array($value)){
$data['attributes'][$attribute] = $this->isBoolean($value[0]) ? filter_var($value[0], FILTER_VALIDATE_BOOLEAN, false) : ($this->isJSON($value[0]) ? json_decode($value[0],true) : ($this->isNullOrEmptyString($value[0]) ? null : $value[0]));
}else{
$data['attributes'][$attribute] = $this->isBoolean($value) ? filter_var($value, FILTER_VALIDATE_BOOLEAN, false) : ($this->isJSON($value) ? json_decode($value,true) : ($this->isNullOrEmptyString($value) ? null : $value));
}
}
}
return $data;
}

private function isJSON($string){
return is_string($string) && is_array(json_decode($string, true)) && (json_last_error() == JSON_ERROR_NONE) ? true : false;
}

private function isBoolean($string){
if(!$string) return false;
return null !== filter_var($string, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}

private function isNullOrEmptyString($string){
return (!isset($string) || trim($string) === "" || trim($string) == "null");
}
}
Loading

0 comments on commit 937eb47

Please sign in to comment.