Skip to content

Commit

Permalink
Merge branch 'development' into 'master'
Browse files Browse the repository at this point in the history
prepare realease v.3.0.1

Closes #146

See merge request dadangnh/iam!276
  • Loading branch information
azhar2202 committed Feb 27, 2024
2 parents 155f27e + 67766be commit 8e2e2f6
Show file tree
Hide file tree
Showing 10 changed files with 590 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ DATABASE_URL="postgresql://db_user:db_pass@database:5432/db_name?serverVersion=1
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PASSPHRASE=0fbc4c14c12774d90d4da80f93d7e336
JWT_TTL=3600
JWT_REFRESH_TTL=27000
###< lexik/jwt-authentication-bundle ###

REDIS_URL=redis://redis:6379
Expand Down
2 changes: 1 addition & 1 deletion config/packages/gesdinet_jwt_refresh_token.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gesdinet_jwt_refresh_token:
refresh_token_class: App\Entity\RefreshToken
ttl: 2592000
ttl: '%env(int:JWT_REFRESH_TTL)%'
return_expiration: true
single_use: true
2 changes: 1 addition & 1 deletion config/packages/lexik_jwt_authentication.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ lexik_jwt_authentication:
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
token_ttl: 3600
token_ttl: '%env(int:JWT_TTL)%'
40 changes: 40 additions & 0 deletions migrations/Version20240219025757.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240219025757 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE role_aplikasi (role_id UUID NOT NULL, aplikasi_id UUID NOT NULL, PRIMARY KEY(role_id, aplikasi_id))');
$this->addSql('CREATE INDEX IDX_C182B197D60322AC ON role_aplikasi (role_id)');
$this->addSql('CREATE INDEX IDX_C182B19752224EF8 ON role_aplikasi (aplikasi_id)');
$this->addSql('COMMENT ON COLUMN role_aplikasi.role_id IS \'(DC2Type:uuid)\'');
$this->addSql('COMMENT ON COLUMN role_aplikasi.aplikasi_id IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE role_aplikasi ADD CONSTRAINT FK_C182B197D60322AC FOREIGN KEY (role_id) REFERENCES role (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE role_aplikasi ADD CONSTRAINT FK_C182B19752224EF8 FOREIGN KEY (aplikasi_id) REFERENCES aplikasi (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE role_aplikasi DROP CONSTRAINT FK_C182B197D60322AC');
$this->addSql('ALTER TABLE role_aplikasi DROP CONSTRAINT FK_C182B19752224EF8');
$this->addSql('DROP TABLE role_aplikasi');
}
}
147 changes: 147 additions & 0 deletions src/Controller/CommonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use App\Entity\Core\Role;
use App\Entity\Pegawai\JabatanPegawai;
use App\Entity\Pegawai\JabatanPegawaiLuar;
use App\Entity\Pegawai\Pegawai;
use App\Helper\AplikasiHelper;
use App\Helper\RoleHelper;
use DateTimeImmutable;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
Expand Down Expand Up @@ -662,4 +664,149 @@ public function showRolesByJabatanPegawaiLuars(string $id,

return $this->findRoleFromIdJabatanPegawaiLuar($id, $iriConverter);
}

/**
* @param string $roleName
* @param IriConverterInterface $iriConverter
* @return JsonResponse
*/
#[Route('/api/roles/{roleName}/role_aplikasi', methods: ['GET'])]
#[IsGranted('ROLE_USER')]
public function aplikasisFromRoleName(string $roleName,
IriConverterInterface $iriConverter): JsonResponse
{
$this->ensureUserLoggedIn();

$role = $this->doctrine
->getRepository(Role::class)
->findOneBy(['nama' => $roleName]);

if (null === $role) {
return $this->json([
'code' => 404,
'error' => 'No roles associated with this name'
], 204);
}

$listAplikasi = [];
foreach (RoleHelper::getAplikasiByRole1($role) as $aplikasi) {
$listAplikasi[] = $aplikasi;
}

return $this->json([
'aplikasi' => $listAplikasi,
]);
}

/**
* @param IriConverterInterface $iriConverter
* @return JsonResponse
*/
#[Route('/api/token/all_aplikasi_by_token', methods: ['POST'])]
#[IsGranted('ROLE_USER')]
public function showAllAplikasisByToken(IriConverterInterface $iriConverter): JsonResponse
{
return $this->findAllAplikasiByToken($iriConverter);
}

/**
* @param IriConverterInterface $iriConverter
* @return JsonResponse
*/
private function findAllAplikasiByToken(IriConverterInterface $iriConverter): JsonResponse
{
$listAplikasi = [];
$listRoles = $this->findRolesFromCurrentUser();

foreach ($listRoles as $roles) {
foreach ($this->getAllAplikasiByRole($roles) as $aplikasis){
$listAplikasi[] = $aplikasis;
}
}

$user = $this->getUser();

if($user->getPegawai() !== null){
$obPegawai= $this->doctrine
->getRepository(Pegawai::class)
->findOneBy(['id' => $user->getPegawai()['pegawaiId']]);

if (null !== $obPegawai && null !== $obPegawai->getJabatanPegawais()) {
/** @var JabatanPegawai $jabatanPegawai */
foreach ($obPegawai->getJabatanPegawais() as $jabatanPegawai) {
// Only add jabatan pegawai that is active and not expired
if ($jabatanPegawai->getTanggalMulai() <= new DateTimeImmutable('now')
&& ($jabatanPegawai->getTanggalSelesai() >= new DateTimeImmutable('now')
|| null === $jabatanPegawai->getTanggalSelesai())
) {
foreach(array_values(RoleHelper::getAplikasiFromJabatanPegawai($this->doctrine, $jabatanPegawai)) as $aplikasi)
{
$listAplikasi[] = $aplikasi;
}
}
}
}
}
$obPegawaiLuar= $this->doctrine
->getRepository(Pegawai::class)
->findOneBy(['id' => $user->getPegawaiLuar()['pegawaiId']]);
if (null !== $obPegawaiLuar && null !== $obPegawaiLuar->getJabatanPegawais()) {
foreach ($obPegawaiLuar->getJabatanPegawais() as $jabatanPegawaiLuar) {
// Only process active jabatans
if ($jabatanPegawaiLuar->getTanggalMulai() <= new DateTimeImmutable('now')
&& ($jabatanPegawaiLuar->getTanggalSelesai() >= new DateTimeImmutable('now')
|| null === $jabatanPegawaiLuar->getTanggalSelesai())
) {
foreach(array_values(RoleHelper::getAplikasiFromJabatanPegawaiLuar($this->doctrine, $jabatanPegawaiLuar)) as $aplikasi)
{
$listAplikasi[] = $aplikasi;
}
}
}

}

return $this->json([
'aplikasi_count' => count($listAplikasi),
'aplikasi' => array_unique($listAplikasi),
]);
}

/**
* @param Role $role
* @return array
*/
public static function getAllAplikasiByRole(Role $role): array
{
$listAplikasi = [];
$aplikasis = $role?->getAplikasis();
if (null !== $aplikasis) {
foreach ($aplikasis as $aplikasi) {
$listAplikasi[] = $aplikasi->getNama();
}
}

return $listAplikasi;
}

/**
* @return array
*/
private function findRolesFromJabatanPegawai(): array
{
$this->ensureUserLoggedIn();

$listOfPlainRoles = $this->getUser()?->getRoles();
$listRoles = [];
foreach ($listOfPlainRoles as $plainRole) {
$role = $this->doctrine
->getRepository(Role::class)
->findOneBy(['nama' => $plainRole]);
if (null !== $role) {
$listRoles[] = $role;
}
}

return $listRoles;
}
}
39 changes: 39 additions & 0 deletions src/Entity/Core/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use App\Entity\Aplikasi\Aplikasi;
use App\Entity\Organisasi\Eselon;
use App\Entity\Organisasi\Jabatan;
use App\Entity\Organisasi\JabatanLuar;
Expand Down Expand Up @@ -598,6 +599,15 @@ class Role
)]
private ?bool $Operator = false;

#[ORM\ManyToMany(targetEntity: Aplikasi::class)]
#[Groups(
groups: [
'role:read',
'role:write'
]
)]
private Collection $Aplikasis;

public function __construct()
{
$this->id = Uuid::v4();
Expand All @@ -613,6 +623,7 @@ public function __construct()
$this->jabatanLuars = new ArrayCollection();
$this->kantorLuars = new ArrayCollection();
$this->unitLuars = new ArrayCollection();
$this->Aplikasis = new ArrayCollection();
}

public function __toString(): string
Expand Down Expand Up @@ -1082,4 +1093,32 @@ public function setOperator(?bool $Operator): self

return $this;
}

/**
* @return Collection|Aplikasi[]
*/
public function getAplikasis(): Collection|array
{
return $this->Aplikasis;
}

public function addAplikasi(Aplikasi $aplikasi): self
{
if (!$this->Aplikasis->contains($aplikasi)) {
$this->Aplikasis[] = $aplikasi;
// $aplikasi->addRole($this);
}

return $this;
}

public function removeAplikasi(Aplikasi $aplikasi): self
{
if ($this->Aplikasis->contains($aplikasi)) {
$this->Aplikasis->removeElement($aplikasi);
// $aplikasi->removeRole($this);
}

return $this;
}
}
Loading

0 comments on commit 8e2e2f6

Please sign in to comment.