diff --git a/app/Console/Commands/DumpFromGit.php b/app/Console/Commands/DumpFromGit.php index a52b5cb..4113418 100644 --- a/app/Console/Commands/DumpFromGit.php +++ b/app/Console/Commands/DumpFromGit.php @@ -4,6 +4,7 @@ use App\Facades\EntityFacade; +use App\Models\Membership; use App\Models\User; use App\Traits\DumpFromGit\CreateCategoriesAndGroupsTrait; use App\Traits\DumpFromGit\CreateEntitiesTrait; @@ -11,17 +12,16 @@ use App\Traits\DumpFromGit\EntitiesHelp\FixEntityTrait; use App\Traits\DumpFromGit\EntitiesHelp\UpdateEntity; use App\Traits\EdugainTrait; -use App\Traits\EntityFolderTrait; use App\Traits\FederationTrait; use App\Traits\GitTrait; +use Exception; use Illuminate\Console\Command; use App\Traits\ValidatorTrait; -use Illuminate\Support\Facades\Artisan; class DumpFromGit extends Command { - use GitTrait, ValidatorTrait,EntityFolderTrait; + use GitTrait, ValidatorTrait; use CreateFederationTrait,CreateEntitiesTrait,CreateCategoriesAndGroupsTrait; use UpdateEntity,FederationTrait,FixEntityTrait; use EdugainTrait; @@ -38,18 +38,27 @@ class DumpFromGit extends Command * * @var string */ - protected $description = 'Command description'; + protected $description = 'Dump all old information from git'; + + private function createMetadataFiles(): void + { + $this->updateFederationFolders(); + $membership = Membership::select('entity_id','federation_id')->whereApproved(1)->get(); + foreach ($membership as $member) { + EntityFacade::saveMetadataToFederationFolder($member->entity_id, $member->federation_id); + } + } /** * Execute the console command. - * @throws \Exception no amin + * @throws Exception no amin */ public function handle() { $firstAdminId = User::where('admin', 1)->first()->id; if(empty($firstAdminId)) - throw new \Exception('firstAdminId is null'); + throw new Exception('firstAdminId is null'); $this->initializeGit(); @@ -60,7 +69,7 @@ public function handle() $this->updateEntitiesXml(); $this->updateFederationFolders(); $this->fixEntities(); - $this->createAllMetadataFiles(); + $this->createMetadataFiles(); $this->makeEdu2Edugain(); diff --git a/app/Events/FederationApprove.php b/app/Events/FederationApprove.php new file mode 100644 index 0000000..ed58ab2 --- /dev/null +++ b/app/Events/FederationApprove.php @@ -0,0 +1,28 @@ +federation = $federation; + } + +} diff --git a/app/Http/Controllers/EntityController.php b/app/Http/Controllers/EntityController.php index 84e1f90..1a08c1d 100644 --- a/app/Http/Controllers/EntityController.php +++ b/app/Http/Controllers/EntityController.php @@ -37,6 +37,8 @@ use App\Notifications\FederationMemberChanged; use App\Notifications\IdpCategoryChanged; use App\Notifications\YourEntityRightsChanged; +use App\Traits\DumpFromGit\EntitiesHelp\DeleteFromEntity; +use App\Traits\DumpFromGit\EntitiesHelp\UpdateEntity; use App\Traits\GitTrait; use App\Traits\ValidatorTrait; use Illuminate\Http\Request; @@ -50,6 +52,7 @@ class EntityController extends Controller { use ValidatorTrait, GitTrait; + use DeleteFromEntity,UpdateEntity; public function __construct() { @@ -127,6 +130,8 @@ public function store(StoreEntity $request) if ($new_entity['type'] === 'idp') { $new_entity = array_merge($new_entity, ['hfd' => true]); } + $new_entity= array_merge($new_entity, ['xml_file' => $this->deleteTags($new_entity['metadata']) ]); + $entity = Entity::create($new_entity); $entity->operators()->attach(Auth::id()); $entity->federations()->attach($federation, [ @@ -134,6 +139,7 @@ public function store(StoreEntity $request) 'requested_by' => Auth::id(), ]); + $this->updateEntityXml(Entity::where('id', $entity['id'])->first()); return $entity; }); diff --git a/app/Http/Controllers/FederationController.php b/app/Http/Controllers/FederationController.php index a824912..38e3221 100644 --- a/app/Http/Controllers/FederationController.php +++ b/app/Http/Controllers/FederationController.php @@ -4,11 +4,8 @@ use App\Http\Requests\StoreFederation; use App\Http\Requests\UpdateFederation; -use App\Jobs\GitAddFederation; use App\Jobs\GitAddMembers; -use App\Jobs\GitDeleteFederation; use App\Jobs\GitDeleteMembers; -use App\Jobs\GitUpdateFederation; use App\Models\Entity; use App\Models\Federation; use App\Models\User; @@ -157,7 +154,7 @@ public function update(UpdateFederation $request, Federation $federation) $federation->approved = true; $federation->update(); - GitAddFederation::dispatch($federation, 'approve', Auth::user()); + //GitAddFederation::dispatch($federation, 'approve', Auth::user()); Notification::send($federation->operators, new FederationApproved($federation)); Notification::send(User::activeAdmins()->select('id', 'email')->get(), new FederationApproved($federation)); @@ -194,7 +191,7 @@ public function update(UpdateFederation $request, Federation $federation) ->route('federations.show', $federation); } - GitUpdateFederation::dispatch($federation, Auth::user()); + // GitUpdateFederation::dispatch($federation, Auth::user()); Notification::send($federation->operators, new FederationUpdated($federation)); Notification::send(User::activeAdmins()->select('id', 'email')->get(), new FederationUpdated($federation)); @@ -212,11 +209,13 @@ public function update(UpdateFederation $request, Federation $federation) $state = $federation->trashed() ? 'deleted' : 'restored'; $color = $federation->trashed() ? 'red' : 'green'; - if ($federation->trashed()) { + +/* if ($federation->trashed()) { GitDeleteFederation::dispatch($federation, Auth::user()); } else { + GitAddFederation::dispatch($federation, 'state', Auth::user()); - } + }*/ Notification::send($federation->operators, new FederationStateChanged($federation)); Notification::send(User::activeAdmins()->select('id', 'email')->get(), new FederationStateChanged($federation)); diff --git a/app/Jobs/GitAddFederation.php b/app/Jobs/Old_GitAddFederation.php similarity index 97% rename from app/Jobs/GitAddFederation.php rename to app/Jobs/Old_GitAddFederation.php index 1a3e4bb..8d593cb 100644 --- a/app/Jobs/GitAddFederation.php +++ b/app/Jobs/Old_GitAddFederation.php @@ -16,7 +16,7 @@ use Illuminate\Support\Facades\Storage; use Throwable; -class GitAddFederation implements ShouldQueue +class Old_GitAddFederation implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, GitTrait; diff --git a/app/Jobs/GitDeleteFederation.php b/app/Jobs/Old_GitDeleteFederation.php similarity index 97% rename from app/Jobs/GitDeleteFederation.php rename to app/Jobs/Old_GitDeleteFederation.php index 6fcdc9e..67bf2ed 100644 --- a/app/Jobs/GitDeleteFederation.php +++ b/app/Jobs/Old_GitDeleteFederation.php @@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Mail; use Throwable; -class GitDeleteFederation implements ShouldQueue +class Old_GitDeleteFederation implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, GitTrait; diff --git a/app/Jobs/GitUpdateFederation.php b/app/Jobs/Old_GitUpdateFederation.php similarity index 97% rename from app/Jobs/GitUpdateFederation.php rename to app/Jobs/Old_GitUpdateFederation.php index 361bfc7..84ad3df 100644 --- a/app/Jobs/GitUpdateFederation.php +++ b/app/Jobs/Old_GitUpdateFederation.php @@ -16,7 +16,7 @@ use Illuminate\Support\Facades\Storage; use Throwable; -class GitUpdateFederation implements ShouldQueue +class Old_GitUpdateFederation implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, GitTrait; diff --git a/app/Jobs/SaveMetadataToFolders.php b/app/Jobs/SaveMetadataToFolders.php deleted file mode 100644 index f61599c..0000000 --- a/app/Jobs/SaveMetadataToFolders.php +++ /dev/null @@ -1,42 +0,0 @@ -entity_id = $entity_id; - $this->federation_id = $federation_id; - } - - /** - * Execute the job. - */ - public function handle(): void - { - EntityFacade::SaveEntityMetadataToFile($this->entity_id,$this->federation_id); - - } -} diff --git a/app/Listeners/CreateFederationFolder.php b/app/Listeners/CreateFederationFolder.php new file mode 100644 index 0000000..deaf14b --- /dev/null +++ b/app/Listeners/CreateFederationFolder.php @@ -0,0 +1,38 @@ +federation; + if($federation->approved) + { + if(!Storage::disk('metadata')->exists($federation->name)){ + $this->createFederationFolder($federation->name); + } + } + + } +} diff --git a/app/Models/Federation.php b/app/Models/Federation.php index a00e6a9..0a5ab66 100644 --- a/app/Models/Federation.php +++ b/app/Models/Federation.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\FederationApprove; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; @@ -60,4 +61,9 @@ public function scopeSearch($query, string $search = null) ->orWhere('xml_id', 'like', "%$search%") ->orWhere('xml_name', 'like', "%$search%"); } + + protected $dispatchesEvents = [ + 'updated' => FederationApprove::class, + ]; + } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index a9f10a6..c9661ca 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use App\Events\FederationApprove; +use App\Listeners\CreateFederationFolder; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -18,6 +20,10 @@ class EventServiceProvider extends ServiceProvider Registered::class => [ SendEmailVerificationNotification::class, ], + FederationApprove::class => + [ + CreateFederationFolder::class, + ] ]; /** diff --git a/app/Services/EntityService.php b/app/Services/EntityService.php index be6c082..a58ed2e 100644 --- a/app/Services/EntityService.php +++ b/app/Services/EntityService.php @@ -2,26 +2,36 @@ namespace App\Services; use App\Models\Entity; use App\Models\Federation; +use Exception; use Illuminate\Support\Facades\Storage; class EntityService { - public function saveMetadataToFederationFolder($entity_id,$federation_id) + + /** + * Save to federation Folder using federation-id + * @throws Exception if federation doesnt exist in database + */ + + public function saveMetadataToFederationFolder($entity_id,$federation_id): void { $federation = Federation::find($federation_id); if(!$federation){ - throw new \Exception("Federation $federation_id not found"); + throw new Exception("Federation $federation_id not found"); } $this->saveEntityMetadataToFolder($entity_id,$federation->name); } - - public function saveEntityMetadataToFolder($entity_id,$folderName) + /** + * save entity if we know folder name + * @throws Exception if entity doesnt exist in database + */ + public function saveEntityMetadataToFolder($entity_id,$folderName): void { $entity = Entity::find($entity_id); if(!$entity){ - throw new \Exception("Entity not found with id $entity_id"); + throw new Exception("Entity not found with id $entity_id"); } $fileName = $entity->file; if(!Storage::disk('metadata')->exists($folderName)) diff --git a/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php b/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php index 8314d9c..76e0aa1 100644 --- a/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php +++ b/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php @@ -180,8 +180,37 @@ private function updateRegistrationInfo(string $xml_document, string $entityId,a return $dom->saveXML(); } + /** + * @param array $timestampDocumentArray for add registration time from git file + * @return void update entity in db and return + */ + public function updateEntityXml($entity,array $timestampDocumentArray = []): void + { + if(empty($entity->xml_file)) + return; + + $xml_document = $entity->xml_file; + $isIdp = false; + if($entity->type == "idp") + $isIdp = true; + if($entity->rs) + { + $xml_document = $this->updateResearchAndScholarship($xml_document,$isIdp); + } + if(!empty($entity->category_id)) + { + $xml_document = $this->updateXmlCategories($xml_document,$entity->category_id); + } + + $xml_document = $this->updateRegistrationInfo($xml_document,$entity->entityid,$timestampDocumentArray); + + + Entity::whereId($entity->id)->update(['xml_file' => $xml_document]); + + } + public function updateEntitiesXml() : void { @@ -196,28 +225,7 @@ public function updateEntitiesXml() : void foreach (Entity::select()->get() as $entity) { - if(empty($entity->xml_file)) - continue; - - $xml_document = $entity->xml_file; - $isIdp = false; - if($entity->type == "idp") - $isIdp = true; - - - if($entity->rs) - { - $xml_document = $this->updateResearchAndScholarship($xml_document,$isIdp); - } - if(!empty($entity->category_id)) - { - $xml_document = $this->updateXmlCategories($xml_document,$entity->category_id); - } - - $xml_document = $this->updateRegistrationInfo($xml_document,$entity->entityid,$timestampDocumentArray); - - - Entity::whereId($entity->id)->update(['xml_file' => $xml_document]); + $this->updateEntityXml($entity,$timestampDocumentArray); } } diff --git a/app/Traits/EntityFolderTrait.php b/app/Traits/EntityFolderTrait.php deleted file mode 100644 index ebf8453..0000000 --- a/app/Traits/EntityFolderTrait.php +++ /dev/null @@ -1,26 +0,0 @@ -updateFederationFolders(); - $membership = Membership::select('entity_id','federation_id')->whereApproved(1)->get(); - foreach ($membership as $member) { - EntityFacade::saveMetadataToFederationFolder($member->entity_id, $member->federation_id); - } - - - } - - - -} diff --git a/tests/Feature/Http/Controllers/FederationControllerTest.php b/tests/Feature/Http/Controllers/FederationControllerTest.php index cde99b2..bec4c36 100644 --- a/tests/Feature/Http/Controllers/FederationControllerTest.php +++ b/tests/Feature/Http/Controllers/FederationControllerTest.php @@ -2,9 +2,9 @@ namespace Tests\Feature\Http\Controllers; -use App\Jobs\GitAddFederation; +use App\Jobs\Old_GitAddFederation; use App\Jobs\GitAddMembers; -use App\Jobs\GitDeleteFederation; +use App\Jobs\Old_GitDeleteFederation; use App\Jobs\GitDeleteMembers; use App\Jobs\GitUpdateFederation; use App\Models\Entity; @@ -392,9 +392,9 @@ public function a_user_with_operator_permission_can_edit_an_existing_federation( $federation->refresh(); $this->assertEquals($federationName, $federation->name); - Bus::assertDispatched(GitUpdateFederation::class, function ($job) use ($federation) { +/* Bus::assertDispatched(GitUpdateFederation::class, function ($job) use ($federation) { return $job->federation->is($federation); - }); + });*/ } /** @test */ @@ -421,9 +421,9 @@ public function a_user_with_operator_permission_can_change_an_existing_federatio $this->assertTrue($federation->trashed()); $this->assertEquals(route('federations.show', $federation), url()->current()); - Bus::assertDispatched(GitDeleteFederation::class, function ($job) use ($federation) { +/* Bus::assertDispatched(GitDeleteFederation::class, function ($job) use ($federation) { return $job->federation->is($federation); - }); + });*/ $this ->followingRedirects() @@ -437,9 +437,11 @@ public function a_user_with_operator_permission_can_change_an_existing_federatio $this->assertFalse($federation->trashed()); $this->assertEquals(route('federations.show', $federation), url()->current()); - Bus::assertDispatched(GitAddFederation::class, function ($job) use ($federation) { + // TODO ask about this because no job no test + +/* Bus::assertDispatched(Old_GitAddFederation::class, function ($job) use ($federation) { return $job->federation->is($federation); - }); + });*/ } /** @test */ @@ -604,7 +606,7 @@ public function a_user_without_operator_permission_cannot_edit_an_existing_feder $this->assertEquals(route('federations.show', $federation), url()->current()); - Bus::assertNotDispatched(GitUpdateFederation::class); +// Bus::assertNotDispatched(GitUpdateFederation::class); } /** @test */ @@ -958,9 +960,9 @@ public function an_admin_can_change_an_existing_federations_state() $this->assertTrue($federation->trashed()); $this->assertEquals(route('federations.show', $federation), url()->current()); - Bus::assertDispatched(GitDeleteFederation::class, function ($job) use ($federation) { +/* Bus::assertDispatched(GitDeleteFederation::class, function ($job) use ($federation) { return $job->federation->is($federation); - }); + });*/ $this ->followingRedirects() @@ -974,9 +976,9 @@ public function an_admin_can_change_an_existing_federations_state() $this->assertFalse($federation->trashed()); $this->assertEquals(route('federations.show', $federation), url()->current()); - Bus::assertDispatched(GitAddFederation::class, function ($job) use ($federation) { +/* Bus::assertDispatched(Old_GitAddFederation::class, function ($job) use ($federation) { return $job->federation->is($federation); - }); + });*/ } /** @test */ @@ -1208,9 +1210,9 @@ public function an_admin_can_approve_a_new_federation_request() $this->assertEquals(route('federations.show', $federation), url()->current()); - Bus::assertDispatched(GitAddFederation::class, function ($job) use ($federation) { +/* Bus::assertDispatched(Old_GitAddFederation::class, function ($job) use ($federation) { return $job->federation->is($federation); - }); + });*/ } /** @test */