diff --git a/app/Http/Controllers/Apis/Protected/Main/OAuth2LegalDocumentsApiController.php b/app/Http/Controllers/Apis/Protected/Main/OAuth2LegalDocumentsApiController.php new file mode 100644 index 00000000..40eaa150 --- /dev/null +++ b/app/Http/Controllers/Apis/Protected/Main/OAuth2LegalDocumentsApiController.php @@ -0,0 +1,74 @@ +repository = $repository; + } + + /** + * @param $id + * @return \Illuminate\Http\JsonResponse|mixed + */ + public function getById($id){ + try{ + + $document = $this->repository->getBySlug(trim($id)); + if(is_null($document)) + $document = $this->repository->getById(intval($id)); + + if(is_null($document)) return $this->error404(); + + return $this->ok(SerializerRegistry::getInstance()->getSerializer + ( + $document + )->serialize()); + } + catch (ValidationException $ex1) { + Log::warning($ex1); + return $this->error412(array($ex1->getMessage())); + } + catch(EntityNotFoundException $ex2) + { + Log::warning($ex2); + return $this->error404(array('message'=> $ex2->getMessage())); + } + catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } +} \ No newline at end of file diff --git a/app/Http/Routes/public.php b/app/Http/Routes/public.php index e3ad9c9a..720270d1 100644 --- a/app/Http/Routes/public.php +++ b/app/Http/Routes/public.php @@ -44,6 +44,11 @@ Route::group([ Route::get('', 'OAuth2MembersApiController@getAll'); }); + // members + Route::group(['prefix' => 'legal-documents'], function () { + Route::get('{id}', 'OAuth2LegalDocumentsApiController@getById'); + }); + // speakers Route::group(['prefix' => 'speakers'], function () { Route::group(['prefix' => '{speaker_id}'], function () { diff --git a/app/ModelSerializers/LegalAgreementSerializer.php b/app/ModelSerializers/LegalAgreementSerializer.php new file mode 100644 index 00000000..cae332b2 --- /dev/null +++ b/app/ModelSerializers/LegalAgreementSerializer.php @@ -0,0 +1,49 @@ + 'owner_id:json_int', + 'DocumentId' => 'document_id:json_int', + ]; + + public function serialize($expand = null, array $fields = array(), array $relations = array(), array $params = array()) + { + $legal_agreement = $this->object; + if (!$legal_agreement instanceof LegalAgreement) return []; + $values = parent::serialize($expand, $fields, $relations, $params); + if (!empty($expand)) { + $exp_expand = explode(',', $expand); + foreach ($exp_expand as $relation) { + switch (trim($relation)) { + case 'document': + { + $document = App::make(ILegalDocumentRepository::class)->getById($values['document_id']); + unset($values['document_id']); + $values['document'] = SerializerRegistry::getInstance()->getSerializer($document)->serialize($expand, [], ['none']); + } + break; + } + } + } + return $values; + } +} \ No newline at end of file diff --git a/app/ModelSerializers/LegalDocumentSerializer.php b/app/ModelSerializers/LegalDocumentSerializer.php new file mode 100644 index 00000000..07c66648 --- /dev/null +++ b/app/ModelSerializers/LegalDocumentSerializer.php @@ -0,0 +1,28 @@ + 'id:json_int', + 'Title' => 'title:json_string', + 'Slug' => 'slug:json_string', + 'Content' => 'content:json_string', + ]; +} \ No newline at end of file diff --git a/app/ModelSerializers/OwnMemberSerializer.php b/app/ModelSerializers/OwnMemberSerializer.php index 66aef8d1..98410830 100644 --- a/app/ModelSerializers/OwnMemberSerializer.php +++ b/app/ModelSerializers/OwnMemberSerializer.php @@ -45,6 +45,7 @@ final class OwnMemberSerializer extends AbstractMemberSerializer 'summit_tickets', 'rsvp', 'sponsor_memberships', + 'legal_agreements', ]; private static $expand_group_events = [ @@ -138,6 +139,13 @@ final class OwnMemberSerializer extends AbstractMemberSerializer $values['summit_tickets'] = $res; } + if(in_array('legal_agreements', $relations)){ + $res = []; + foreach ($member->getLegalAgreements() as $agreement) + $res[] = intval($agreement->getId()); + $values['legal_agreements'] = $res; + } + if (!empty($expand)) { foreach (explode(',', $expand) as $relation) { $relation = trim($relation); @@ -223,6 +231,18 @@ final class OwnMemberSerializer extends AbstractMemberSerializer $values['rsvp'] = $rsvps; } break; + case 'legal_agreements':{ + if(!in_array('legal_agreements', $relations)) break; + if(is_null($summit)) break; + $res = []; + foreach ($member->getLegalAgreements() as $agreement){ + $rsvps[] = SerializerRegistry::getInstance() + ->getSerializer($agreement) + ->serialize(AbstractSerializer::filterExpandByPrefix($expand, $relation)); + } + $values['legal_agreements'] = $res; + } + break; } } } diff --git a/app/ModelSerializers/SerializerRegistry.php b/app/ModelSerializers/SerializerRegistry.php index 4a56b2f6..af4499d9 100644 --- a/app/ModelSerializers/SerializerRegistry.php +++ b/app/ModelSerializers/SerializerRegistry.php @@ -80,6 +80,7 @@ use App\ModelSerializers\Summit\SummitLocationBannerSerializer; use App\ModelSerializers\Summit\TrackTagGroups\TrackTagGroupAllowedTagSerializer; use App\ModelSerializers\Summit\TrackTagGroups\TrackTagGroupSerializer; use Libs\ModelSerializers\IModelSerializer; +use models\main\LegalDocument; use models\oauth2\IResourceServerContext; use ModelSerializers\ChatTeams\ChatTeamInvitationSerializer; use ModelSerializers\ChatTeams\ChatTeamMemberSerializer; @@ -353,6 +354,9 @@ final class SerializerRegistry self::SerializerType_Admin => AdminMemberSerializer::class ]; + $this->registry['LegalAgreement'] = LegalAgreementSerializer::class; + $this->registry['LegalDocument'] = LegalDocumentSerializer::class; + $this->registry['Group'] = GroupSerializer::class; $this->registry['Affiliation'] = AffiliationSerializer::class; $this->registry['Organization'] = OrganizationSerializer::class; diff --git a/app/Models/Foundation/Main/LegalAgreement.php b/app/Models/Foundation/Main/LegalAgreement.php index fa00bb3c..b7d2b857 100644 --- a/app/Models/Foundation/Main/LegalAgreement.php +++ b/app/Models/Foundation/Main/LegalAgreement.php @@ -67,11 +67,23 @@ class LegalAgreement extends SilverstripeBaseModel } /** - * @param int $document_id + * @return int */ - public function setDocumentId(int $document_id): void + public function getOwnerId(){ + try { + return $this->owner->getId(); + } + catch(\Exception $ex){ + return 0; + } + } + + /** + * @param LegalDocument $document + */ + public function setDocument(LegalDocument $document): void { - $this->document_id = $document_id; + $this->document_id = $document->getId(); } /** @@ -90,31 +102,12 @@ class LegalAgreement extends SilverstripeBaseModel $this->owner = $owner; } - /** - * this is for legacy reasons - * @param string $slug - * @return int|null - */ - public static function getLegalAgreementIDBySlug(string $slug):?int { - try { - $sql = <<execute([ - 'url_segment' => trim($slug), - 'class_name' => "LegalDocumentPage" - ]); - $res = $stmt->fetchAll(); - if(count($res) == 0 ) return null; - $id = intval($res[0]['ID']); - return $id; + public function getContent():?String{ + + } + + public function getTitle():?string{ - } catch (\Exception $ex) { - return null; - } - return null; } } \ No newline at end of file diff --git a/app/Models/Foundation/Main/LegalDocument.php b/app/Models/Foundation/Main/LegalDocument.php new file mode 100644 index 00000000..d13ab9ee --- /dev/null +++ b/app/Models/Foundation/Main/LegalDocument.php @@ -0,0 +1,84 @@ +id = $id; + $this->title = $title; + $this->slug = $slug; + $this->content = $content; + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @return string + */ + public function getTitle(): string + { + return $this->title; + } + + /** + * @return string + */ + public function getSlug(): string + { + return $this->slug; + } + + /** + * @return string + */ + public function getContent(): string + { + return $this->content; + } + +} \ No newline at end of file diff --git a/app/Models/Foundation/Main/Member.php b/app/Models/Foundation/Main/Member.php index aab851ae..494938a3 100644 --- a/app/Models/Foundation/Main/Member.php +++ b/app/Models/Foundation/Main/Member.php @@ -1841,16 +1841,13 @@ SQL; $this->resign_date = new \DateTime('now', new \DateTimeZone(self::DefaultTimeZone)); } - public function signFoundationMembership() + public function signFoundationMembership(LegalDocument $document) { if (!$this->isFoundationMember()) { // Set up member with legal agreement for becoming an OpenStack Foundation Member $legalAgreement = new LegalAgreement(); $legalAgreement->setOwner($this); - $documentId = LegalAgreement::getLegalAgreementIDBySlug(LegalAgreement::Slug); - if(is_null($documentId)) - throw new ValidationException(sprintf("LegalAgreement %s does not exists.", LegalAgreement::Slug)); - $legalAgreement->setDocumentId($documentId); + $legalAgreement->setDocument($document); $this->legal_agreements->add($legalAgreement); $this->membership_type = self::MembershipTypeFoundation; $this->resign_date = null; diff --git a/app/Models/Foundation/Main/Repositories/ILegalDocumentRepository.php b/app/Models/Foundation/Main/Repositories/ILegalDocumentRepository.php new file mode 100644 index 00000000..89abf26c --- /dev/null +++ b/app/Models/Foundation/Main/Repositories/ILegalDocumentRepository.php @@ -0,0 +1,32 @@ +getConnection()->prepare($sql); + $stmt->execute([ + 'url_segment' => trim($slug), + 'class_name' => "LegalDocumentPage" + ]); + $res = $stmt->fetchAll(); + if(count($res) == 0 ) return null; + new LegalDocument( + $res[0]['ID'], + trim($res[0]['Title']), + trim($res[0]['URLSegment']), + trim($res[0]['Content']) + ); + } catch (\Exception $ex) { + return null; + } + return null; + } + + /** + * @param int $id + * @return LegalDocument|null + */ + public function getById(int $id): ?LegalDocument + { + try { + $sql = <<getConnection()->prepare($sql); + $stmt->execute([ + 'id' => $id, + 'class_name' => "LegalDocumentPage" + ]); + $res = $stmt->fetchAll(); + if(count($res) == 0 ) return null; + new LegalDocument( + $res[0]['ID'], + trim($res[0]['Title']), + trim($res[0]['URLSegment']), + trim($res[0]['Content']) + ); + } catch (\Exception $ex) { + return null; + } + return null; + } +} \ No newline at end of file diff --git a/app/Repositories/RepositoriesProvider.php b/app/Repositories/RepositoriesProvider.php index 62f4ae07..383fbbdc 100644 --- a/app/Repositories/RepositoriesProvider.php +++ b/app/Repositories/RepositoriesProvider.php @@ -14,6 +14,7 @@ use App\Models\Foundation\Main\Language; use App\Models\Foundation\Main\Repositories\ILanguageRepository; +use App\Models\Foundation\Main\Repositories\ILegalDocumentRepository; use App\Models\Foundation\Main\Repositories\IProjectSponsorshipTypeRepository; use App\Models\Foundation\Main\Repositories\ISponsoredProjectRepository; use App\Models\Foundation\Main\Repositories\ISummitAdministratorPermissionGroupRepository; @@ -112,6 +113,7 @@ use models\summit\SummitRegistrationPromoCode; use models\summit\SummitRoomReservation; use models\summit\SummitTaxType; use models\summit\SummitTicketType; +use repositories\main\DoctrineLegalDocumentRepository; /** * Class RepositoriesProvider @@ -636,5 +638,10 @@ final class RepositoriesProvider extends ServiceProvider return EntityManager::getRepository(SupportingCompany::class); } ); + + App::singleton( + ILegalDocumentRepository::class, + DoctrineLegalDocumentRepository::class + ); } } \ No newline at end of file diff --git a/app/Services/Model/Imp/MemberService.php b/app/Services/Model/Imp/MemberService.php index 0af6b1c0..413fc96d 100644 --- a/app/Services/Model/Imp/MemberService.php +++ b/app/Services/Model/Imp/MemberService.php @@ -14,6 +14,7 @@ use App\Events\NewMember; use App\Models\Foundation\Main\IGroup; +use App\Models\Foundation\Main\Repositories\ILegalDocumentRepository; use App\Services\Apis\IExternalUserApi; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Log; @@ -26,6 +27,7 @@ use models\main\Group; use models\main\IGroupRepository; use models\main\IMemberRepository; use models\main\IOrganizationRepository; +use models\main\LegalAgreement; use models\main\Member; use DateTime; use models\main\Organization; @@ -77,6 +79,11 @@ final class MemberService */ private $speaker_registration_request_repository; + /** + * @var ILegalDocumentRepository + */ + private $legal_document_repository; + /** * MemberService constructor. * @param IMemberRepository $member_repository @@ -86,6 +93,7 @@ final class MemberService * @param ICacheService $cache_service * @param IExternalUserApi $external_user_api * @param ISpeakerRegistrationRequestRepository $speaker_registration_request_repository + * @param ILegalDocumentRepository $legal_document_repository * @param ITransactionService $tx_service */ public function __construct @@ -97,6 +105,7 @@ final class MemberService ICacheService $cache_service, IExternalUserApi $external_user_api, ISpeakerRegistrationRequestRepository $speaker_registration_request_repository, + ILegalDocumentRepository $legal_document_repository, ITransactionService $tx_service ) { @@ -108,6 +117,7 @@ final class MemberService $this->cache_service = $cache_service; $this->external_user_api = $external_user_api; $this->speaker_registration_request_repository = $speaker_registration_request_repository; + $this->legal_document_repository = $legal_document_repository; } /** @@ -507,7 +517,10 @@ final class MemberService throw new EntityNotFoundException(sprintf("Group %s not found", IGroup::FoundationMembers)); $member->add2Group($group); - $member->signFoundationMembership(); + $document = $this->legal_document_repository->getBySlug(LegalAgreement::Slug); + if(is_null($document)) + throw new EntityNotFoundException(sprintf("Legal Document %s not found.",LegalAgreement::Slug)); + $member->signFoundationMembership($document); return $member; });