1use crate::{Result, EventualiError};
2use serde::{Deserialize, Serialize};
3use std::collections::{HashMap, BTreeMap};
4use chrono::{DateTime, Utc, Duration};
5use uuid::Uuid;
6use sha2::{Sha256, Digest};
7
8pub struct GdprManager {
10 data_subjects: BTreeMap<String, DataSubject>,
11 processing_activities: Vec<ProcessingActivity>,
12 consent_records: BTreeMap<String, ConsentRecord>,
13 #[allow(dead_code)] lawful_basis_registry: BTreeMap<String, LawfulBasis>,
15 retention_policies: BTreeMap<String, RetentionPolicy>,
16 breach_notifications: Vec<BreachNotification>,
17 data_protection_impact_assessments: BTreeMap<String, DataProtectionImpactAssessment>,
18 privacy_by_design_controls: Vec<PrivacyControl>,
19 data_exports: Vec<DataExportRecord>,
20 deletion_log: Vec<DeletionRecord>,
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct DataSubject {
26 pub subject_id: String,
27 pub external_id: Option<String>,
28 pub email: Option<String>,
29 pub name: Option<String>,
30 pub created_at: DateTime<Utc>,
31 pub last_updated: DateTime<Utc>,
32 pub data_locations: Vec<DataLocation>,
33 pub consent_status: HashMap<String, ConsentStatus>,
34 pub lawful_basis: HashMap<String, LawfulBasisType>,
35 pub retention_periods: HashMap<String, RetentionPeriod>,
36 pub subject_rights_requests: Vec<SubjectRightsRequest>,
37 pub opt_out_status: HashMap<String, bool>,
38 pub data_minimization_applied: bool,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct DataLocation {
44 pub database_name: String,
45 pub table_name: String,
46 pub column_name: String,
47 pub data_type: PersonalDataType,
48 pub data_classification: DataClassification,
49 pub encrypted: bool,
50 pub pseudonymized: bool,
51 pub retention_period: Option<Duration>,
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
56pub enum PersonalDataType {
57 BasicPersonalData, SpecialCategoryData, BiometricData, LocationData, BehavioralData, CommunicationData, FinancialData, IdentificationData, TechnicalData, ProfessionalData, }
68
69#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
71pub enum DataClassification {
72 Public,
73 Internal,
74 Confidential,
75 SpecialCategory, ChildrensData, }
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct ProcessingActivity {
82 pub activity_id: String,
83 pub name: String,
84 pub description: String,
85 pub controller: DataController,
86 pub data_protection_officer_contact: Option<String>,
87 pub purposes: Vec<ProcessingPurpose>,
88 pub categories_of_data_subjects: Vec<String>,
89 pub categories_of_personal_data: Vec<PersonalDataType>,
90 pub categories_of_recipients: Vec<String>,
91 pub transfers_to_third_countries: Vec<InternationalTransfer>,
92 pub retention_periods: HashMap<PersonalDataType, Duration>,
93 pub technical_and_organizational_measures: Vec<SecurityMeasure>,
94 pub lawful_basis: LawfulBasisType,
95 pub created_at: DateTime<Utc>,
96 pub last_reviewed: DateTime<Utc>,
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct DataController {
102 pub name: String,
103 pub contact_details: String,
104 pub representative: Option<String>,
105 pub dpo_contact: Option<String>,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct ProcessingPurpose {
111 pub purpose: String,
112 pub description: String,
113 pub lawful_basis: LawfulBasisType,
114 pub legitimate_interest_assessment: Option<String>,
115 pub data_minimization_applied: bool,
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct InternationalTransfer {
121 pub destination_country: String,
122 pub transfer_mechanism: TransferMechanism,
123 pub adequacy_decision: bool,
124 pub safeguards: Vec<String>,
125 pub derogation: Option<String>,
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize)]
130pub enum TransferMechanism {
131 AdequacyDecision,
132 StandardContractualClauses,
133 BindingCorporateRules,
134 CertificationMechanism,
135 CodeOfConduct,
136 Derogation(String),
137}
138
139#[derive(Debug, Clone, Serialize, Deserialize)]
141pub struct SecurityMeasure {
142 pub measure_type: SecurityMeasureType,
143 pub description: String,
144 pub implemented: bool,
145 pub implementation_date: Option<DateTime<Utc>>,
146 pub responsible_party: String,
147}
148
149#[derive(Debug, Clone, Serialize, Deserialize)]
151pub enum SecurityMeasureType {
152 Encryption,
153 Pseudonymization,
154 AccessControl,
155 IntegrityMeasure,
156 AvailabilityMeasure,
157 ResilienceMeasure,
158 RegularTesting,
159 DataBackup,
160 IncidentResponse,
161 PrivacyByDesign,
162}
163
164#[derive(Debug, Clone, Serialize, Deserialize)]
166pub struct ConsentRecord {
167 pub consent_id: String,
168 pub data_subject_id: String,
169 pub purpose: String,
170 pub consent_text: String,
171 pub consent_given_at: DateTime<Utc>,
172 pub consent_method: ConsentMethod,
173 pub consent_status: ConsentStatus,
174 pub withdrawn_at: Option<DateTime<Utc>>,
175 pub withdrawal_method: Option<String>,
176 pub granular_consents: HashMap<String, bool>,
177 pub evidence_of_consent: ConsentEvidence,
178 pub parental_consent_required: bool,
179 pub parental_consent_obtained: Option<DateTime<Utc>>,
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize)]
184pub enum ConsentMethod {
185 WebForm,
186 Email,
187 PhysicalDocument,
188 VerbalConsent,
189 ImpliedConsent,
190 APICall,
191}
192
193#[derive(Debug, Clone, Serialize, Deserialize)]
195pub enum ConsentStatus {
196 Given,
197 Withdrawn,
198 Expired,
199 Refused,
200 Pending,
201}
202
203#[derive(Debug, Clone, Serialize, Deserialize)]
205pub struct ConsentEvidence {
206 pub timestamp: DateTime<Utc>,
207 pub ip_address: Option<String>,
208 pub user_agent: Option<String>,
209 pub form_version: Option<String>,
210 pub witness: Option<String>,
211 pub digital_signature: Option<String>,
212 pub audit_trail: Vec<String>,
213}
214
215#[derive(Debug, Clone, Serialize, Deserialize)]
217pub struct LawfulBasis {
218 pub basis_id: String,
219 pub data_subject_id: String,
220 pub processing_purpose: String,
221 pub basis_type: LawfulBasisType,
222 pub basis_details: String,
223 pub documented_at: DateTime<Utc>,
224 pub reviewed_at: Option<DateTime<Utc>>,
225 pub balancing_test_conducted: Option<DateTime<Utc>>,
226 pub balancing_test_result: Option<String>,
227}
228
229#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
231pub enum LawfulBasisType {
232 Consent, Contract, LegalObligation, VitalInterests, PublicTask, LegitimateInterests, }
239
240#[derive(Debug, Clone, Serialize, Deserialize)]
242pub struct RetentionPolicy {
243 pub policy_id: String,
244 pub data_category: PersonalDataType,
245 pub retention_period: Duration,
246 pub retention_criteria: String,
247 pub disposal_method: DisposalMethod,
248 pub review_frequency: Duration,
249 pub last_reviewed: DateTime<Utc>,
250 pub automatic_deletion: bool,
251 pub archival_period: Option<Duration>,
252}
253
254#[derive(Debug, Clone, Serialize, Deserialize)]
256pub struct RetentionPeriod {
257 pub start_date: DateTime<Utc>,
258 pub end_date: DateTime<Utc>,
259 pub basis_for_retention: String,
260 pub automatic_deletion_scheduled: bool,
261}
262
263#[derive(Debug, Clone, Serialize, Deserialize)]
265pub enum DisposalMethod {
266 SecureDeletion,
267 Anonymization,
268 Pseudonymization,
269 Archival,
270 PhysicalDestruction,
271}
272
273#[derive(Debug, Clone, Serialize, Deserialize)]
275pub struct BreachNotification {
276 pub breach_id: String,
277 pub detected_at: DateTime<Utc>,
278 pub reported_to_authority_at: Option<DateTime<Utc>>,
279 pub notified_subjects_at: Option<DateTime<Utc>>,
280 pub breach_type: BreachType,
281 pub affected_data_subjects: usize,
282 pub categories_of_data_affected: Vec<PersonalDataType>,
283 pub likely_consequences: String,
284 pub measures_taken: Vec<String>,
285 pub measures_to_mitigate: Vec<String>,
286 pub risk_assessment: RiskLevel,
287 pub authority_reference: Option<String>,
288 pub requires_subject_notification: bool,
289 pub notification_delay_reason: Option<String>,
290}
291
292#[derive(Debug, Clone, Serialize, Deserialize)]
294pub enum BreachType {
295 ConfidentialityBreach, IntegrityBreach, AvailabilityBreach, Combined(Vec<BreachType>), }
300
301#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
303pub enum RiskLevel {
304 Low,
305 Medium,
306 High,
307 Critical,
308}
309
310#[derive(Debug, Clone, Serialize, Deserialize)]
312pub struct DataProtectionImpactAssessment {
313 pub dpia_id: String,
314 pub processing_operation: String,
315 pub description: String,
316 pub necessity_assessment: String,
317 pub proportionality_assessment: String,
318 pub risks_to_data_subjects: Vec<PrivacyRisk>,
319 pub measures_to_address_risks: Vec<RiskMitigation>,
320 pub residual_risk_level: RiskLevel,
321 pub consultation_with_dpo: bool,
322 pub consultation_with_authority: Option<DateTime<Utc>>,
323 pub created_at: DateTime<Utc>,
324 pub reviewed_at: Option<DateTime<Utc>>,
325 pub approved_by: String,
326}
327
328#[derive(Debug, Clone, Serialize, Deserialize)]
330pub struct PrivacyRisk {
331 pub risk_id: String,
332 pub description: String,
333 pub likelihood: RiskLevel,
334 pub impact: RiskLevel,
335 pub overall_risk: RiskLevel,
336 pub affected_rights: Vec<DataSubjectRight>,
337}
338
339#[derive(Debug, Clone, Serialize, Deserialize)]
341pub struct RiskMitigation {
342 pub mitigation_id: String,
343 pub risk_addressed: String,
344 pub measure: String,
345 pub implementation_status: ImplementationStatus,
346 pub responsible_party: String,
347 pub target_date: DateTime<Utc>,
348}
349
350#[derive(Debug, Clone, Serialize, Deserialize)]
352pub enum ImplementationStatus {
353 Planned,
354 InProgress,
355 Implemented,
356 Verified,
357 Deferred,
358}
359
360#[derive(Debug, Clone, Serialize, Deserialize)]
362pub struct PrivacyControl {
363 pub control_id: String,
364 pub control_type: PrivacyControlType,
365 pub description: String,
366 pub implementation_level: ImplementationLevel,
367 pub effectiveness: EffectivenessLevel,
368 pub last_tested: Option<DateTime<Utc>>,
369 pub responsible_team: String,
370}
371
372#[derive(Debug, Clone, Serialize, Deserialize)]
374pub enum PrivacyControlType {
375 DataMinimization,
376 PurposeLimitation,
377 StorageLimitation,
378 AccuracyControl,
379 IntegrityControl,
380 ConfidentialityControl,
381 AccountabilityControl,
382 TransparencyControl,
383}
384
385#[derive(Debug, Clone, Serialize, Deserialize)]
387pub enum ImplementationLevel {
388 NotImplemented,
389 PartiallyImplemented,
390 FullyImplemented,
391 OptimallyImplemented,
392}
393
394#[derive(Debug, Clone, Serialize, Deserialize)]
396pub enum EffectivenessLevel {
397 Ineffective,
398 LimitedEffectiveness,
399 ModeratelyEffective,
400 HighlyEffective,
401}
402
403#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
405pub enum DataSubjectRight {
406 RightToInformation, RightOfAccess, RightToRectification, RightToErasure, RightToRestrictProcessing, RightToDataPortability, RightToObject, RightsRelatedToAutomatedDecisionMaking, }
415
416#[derive(Debug, Clone, Serialize, Deserialize)]
418pub struct SubjectRightsRequest {
419 pub request_id: String,
420 pub data_subject_id: String,
421 pub request_type: DataSubjectRight,
422 pub request_details: String,
423 pub requested_at: DateTime<Utc>,
424 pub identity_verified_at: Option<DateTime<Utc>>,
425 pub identity_verification_method: Option<String>,
426 pub processed_at: Option<DateTime<Utc>>,
427 pub response_sent_at: Option<DateTime<Utc>>,
428 pub request_status: RequestStatus,
429 pub response_method: Option<ResponseMethod>,
430 pub complexity_assessment: ComplexityLevel,
431 pub extension_granted: bool,
432 pub extension_reason: Option<String>,
433 pub third_party_requests: Vec<String>,
434 pub processing_fee: Option<f64>,
435 pub rejection_reason: Option<String>,
436}
437
438#[derive(Debug, Clone, Serialize, Deserialize)]
440pub enum RequestStatus {
441 Received,
442 IdentityVerificationRequired,
443 InProgress,
444 Completed,
445 Rejected,
446 PartiallyFulfilled,
447 Extended,
448}
449
450#[derive(Debug, Clone, Serialize, Deserialize)]
452pub enum ResponseMethod {
453 Email,
454 Post,
455 SecurePortal,
456 InPerson,
457 StructuredFormat,
458}
459
460#[derive(Debug, Clone, Serialize, Deserialize)]
462pub enum ComplexityLevel {
463 Simple, Complex, ManifestlyUnfounded,
466 Excessive,
467}
468
469#[derive(Debug, Clone, Serialize, Deserialize)]
471pub struct DataExportRecord {
472 pub export_id: String,
473 pub data_subject_id: String,
474 pub export_requested_at: DateTime<Utc>,
475 pub export_completed_at: Option<DateTime<Utc>>,
476 pub export_format: ExportFormat,
477 pub data_categories_exported: Vec<PersonalDataType>,
478 pub file_size_bytes: Option<u64>,
479 pub download_expires_at: DateTime<Utc>,
480 pub downloaded_at: Option<DateTime<Utc>>,
481 pub encryption_applied: bool,
482 pub secure_delivery_method: String,
483}
484
485#[derive(Debug, Clone, Serialize, Deserialize)]
487pub enum ExportFormat {
488 Json,
489 Xml,
490 Csv,
491 Pdf,
492 StructuredFormat(String),
493}
494
495#[derive(Debug, Clone, Serialize, Deserialize)]
497pub struct DeletionRecord {
498 pub deletion_id: String,
499 pub data_subject_id: String,
500 pub deletion_requested_at: DateTime<Utc>,
501 pub deletion_completed_at: DateTime<Utc>,
502 pub deletion_method: DisposalMethod,
503 pub data_categories_deleted: Vec<PersonalDataType>,
504 pub locations_deleted: Vec<DataLocation>,
505 pub verification_hash: String,
506 pub certified_by: String,
507 pub retention_exceptions: Vec<String>,
508 pub legal_hold_applied: bool,
509}
510
511impl GdprManager {
512 pub fn new() -> Self {
514 Self {
515 data_subjects: BTreeMap::new(),
516 processing_activities: Vec::new(),
517 consent_records: BTreeMap::new(),
518 lawful_basis_registry: BTreeMap::new(),
519 retention_policies: BTreeMap::new(),
520 breach_notifications: Vec::new(),
521 data_protection_impact_assessments: BTreeMap::new(),
522 privacy_by_design_controls: Vec::new(),
523 data_exports: Vec::new(),
524 deletion_log: Vec::new(),
525 }
526 }
527
528 pub fn with_eu_configuration() -> Self {
530 let mut manager = Self::new();
531 manager.initialize_standard_policies();
532 manager.initialize_privacy_controls();
533 manager
534 }
535
536 pub fn register_data_subject(&mut self, external_id: String, email: Option<String>, name: Option<String>) -> Result<String> {
538 let subject_id = Uuid::new_v4().to_string();
539 let now = Utc::now();
540
541 let data_subject = DataSubject {
542 subject_id: subject_id.clone(),
543 external_id: Some(external_id),
544 email,
545 name,
546 created_at: now,
547 last_updated: now,
548 data_locations: Vec::new(),
549 consent_status: HashMap::new(),
550 lawful_basis: HashMap::new(),
551 retention_periods: HashMap::new(),
552 subject_rights_requests: Vec::new(),
553 opt_out_status: HashMap::new(),
554 data_minimization_applied: false,
555 };
556
557 self.data_subjects.insert(subject_id.clone(), data_subject);
558 Ok(subject_id)
559 }
560
561 pub fn record_consent(
563 &mut self,
564 data_subject_id: String,
565 purpose: String,
566 consent_text: String,
567 consent_method: ConsentMethod,
568 evidence: ConsentEvidence,
569 ) -> Result<String> {
570 let consent_id = Uuid::new_v4().to_string();
571 let now = Utc::now();
572
573 let consent_record = ConsentRecord {
574 consent_id: consent_id.clone(),
575 data_subject_id: data_subject_id.clone(),
576 purpose: purpose.clone(),
577 consent_text,
578 consent_given_at: now,
579 consent_method,
580 consent_status: ConsentStatus::Given,
581 withdrawn_at: None,
582 withdrawal_method: None,
583 granular_consents: HashMap::new(),
584 evidence_of_consent: evidence,
585 parental_consent_required: false,
586 parental_consent_obtained: None,
587 };
588
589 self.consent_records.insert(consent_id.clone(), consent_record);
590
591 if let Some(data_subject) = self.data_subjects.get_mut(&data_subject_id) {
593 data_subject.consent_status.insert(purpose, ConsentStatus::Given);
594 }
595
596 Ok(consent_id)
597 }
598
599 pub fn withdraw_consent(&mut self, consent_id: String, withdrawal_method: String) -> Result<()> {
601 if let Some(consent) = self.consent_records.get_mut(&consent_id) {
602 consent.consent_status = ConsentStatus::Withdrawn;
603 consent.withdrawn_at = Some(Utc::now());
604 consent.withdrawal_method = Some(withdrawal_method);
605
606 if let Some(data_subject) = self.data_subjects.get_mut(&consent.data_subject_id) {
608 data_subject.consent_status.insert(consent.purpose.clone(), ConsentStatus::Withdrawn);
609 }
610
611 Ok(())
612 } else {
613 Err(EventualiError::Validation("Consent record not found".to_string()))
614 }
615 }
616
617 pub fn process_access_request(&mut self, data_subject_id: String, request_details: String) -> Result<SubjectRightsRequest> {
619 let request_id = Uuid::new_v4().to_string();
620 let now = Utc::now();
621
622 let request = SubjectRightsRequest {
623 request_id: request_id.clone(),
624 data_subject_id: data_subject_id.clone(),
625 request_type: DataSubjectRight::RightOfAccess,
626 request_details,
627 requested_at: now,
628 identity_verified_at: None,
629 identity_verification_method: None,
630 processed_at: None,
631 response_sent_at: None,
632 request_status: RequestStatus::Received,
633 response_method: None,
634 complexity_assessment: ComplexityLevel::Simple,
635 extension_granted: false,
636 extension_reason: None,
637 third_party_requests: Vec::new(),
638 processing_fee: None,
639 rejection_reason: None,
640 };
641
642 if let Some(data_subject) = self.data_subjects.get_mut(&data_subject_id) {
644 data_subject.subject_rights_requests.push(request.clone());
645 }
646
647 Ok(request)
648 }
649
650 pub fn process_rectification_request(&mut self, data_subject_id: String, corrections: HashMap<String, String>) -> Result<SubjectRightsRequest> {
652 let request_id = Uuid::new_v4().to_string();
653 let now = Utc::now();
654
655 let request_details = format!("Rectification requested for: {:?}", corrections.keys().collect::<Vec<_>>());
656
657 let request = SubjectRightsRequest {
658 request_id: request_id.clone(),
659 data_subject_id: data_subject_id.clone(),
660 request_type: DataSubjectRight::RightToRectification,
661 request_details,
662 requested_at: now,
663 identity_verified_at: None,
664 identity_verification_method: None,
665 processed_at: None,
666 response_sent_at: None,
667 request_status: RequestStatus::Received,
668 response_method: None,
669 complexity_assessment: ComplexityLevel::Simple,
670 extension_granted: false,
671 extension_reason: None,
672 third_party_requests: Vec::new(),
673 processing_fee: None,
674 rejection_reason: None,
675 };
676
677 if let Some(data_subject) = self.data_subjects.get_mut(&data_subject_id) {
679 data_subject.subject_rights_requests.push(request.clone());
680 }
681
682 Ok(request)
683 }
684
685 pub fn process_erasure_request(&mut self, data_subject_id: String, erasure_grounds: String) -> Result<SubjectRightsRequest> {
687 let request_id = Uuid::new_v4().to_string();
688 let now = Utc::now();
689
690 let request = SubjectRightsRequest {
691 request_id: request_id.clone(),
692 data_subject_id: data_subject_id.clone(),
693 request_type: DataSubjectRight::RightToErasure,
694 request_details: erasure_grounds,
695 requested_at: now,
696 identity_verified_at: None,
697 identity_verification_method: None,
698 processed_at: None,
699 response_sent_at: None,
700 request_status: RequestStatus::Received,
701 response_method: None,
702 complexity_assessment: ComplexityLevel::Complex, extension_granted: false,
704 extension_reason: None,
705 third_party_requests: Vec::new(),
706 processing_fee: None,
707 rejection_reason: None,
708 };
709
710 if let Some(data_subject) = self.data_subjects.get_mut(&data_subject_id) {
712 data_subject.subject_rights_requests.push(request.clone());
713 }
714
715 Ok(request)
716 }
717
718 pub fn process_portability_request(&mut self, data_subject_id: String, export_format: ExportFormat) -> Result<SubjectRightsRequest> {
720 let request_id = Uuid::new_v4().to_string();
721 let now = Utc::now();
722
723 let request_details = format!("Data portability request in format: {export_format:?}");
724
725 let request = SubjectRightsRequest {
726 request_id: request_id.clone(),
727 data_subject_id: data_subject_id.clone(),
728 request_type: DataSubjectRight::RightToDataPortability,
729 request_details,
730 requested_at: now,
731 identity_verified_at: None,
732 identity_verification_method: None,
733 processed_at: None,
734 response_sent_at: None,
735 request_status: RequestStatus::Received,
736 response_method: None,
737 complexity_assessment: ComplexityLevel::Complex,
738 extension_granted: false,
739 extension_reason: None,
740 third_party_requests: Vec::new(),
741 processing_fee: None,
742 rejection_reason: None,
743 };
744
745 let export_record = DataExportRecord {
747 export_id: Uuid::new_v4().to_string(),
748 data_subject_id: data_subject_id.clone(),
749 export_requested_at: now,
750 export_completed_at: None,
751 export_format,
752 data_categories_exported: Vec::new(),
753 file_size_bytes: None,
754 download_expires_at: now + Duration::days(30), downloaded_at: None,
756 encryption_applied: true,
757 secure_delivery_method: "secure_download_link".to_string(),
758 };
759
760 self.data_exports.push(export_record);
761
762 if let Some(data_subject) = self.data_subjects.get_mut(&data_subject_id) {
764 data_subject.subject_rights_requests.push(request.clone());
765 }
766
767 Ok(request)
768 }
769
770 pub fn execute_data_deletion(&mut self, data_subject_id: String, deletion_method: DisposalMethod, locations: Vec<DataLocation>) -> Result<String> {
772 let deletion_id = Uuid::new_v4().to_string();
773 let now = Utc::now();
774
775 let mut hasher = Sha256::new();
777 hasher.update(deletion_id.as_bytes());
778 hasher.update(data_subject_id.as_bytes());
779 hasher.update(now.to_rfc3339().as_bytes());
780 let verification_hash = format!("{:x}", hasher.finalize());
781
782 let deletion_record = DeletionRecord {
783 deletion_id: deletion_id.clone(),
784 data_subject_id: data_subject_id.clone(),
785 deletion_requested_at: now,
786 deletion_completed_at: now,
787 deletion_method,
788 data_categories_deleted: locations.iter().map(|l| l.data_type.clone()).collect(),
789 locations_deleted: locations,
790 verification_hash,
791 certified_by: "GDPR_System".to_string(),
792 retention_exceptions: Vec::new(),
793 legal_hold_applied: false,
794 };
795
796 self.deletion_log.push(deletion_record);
797 Ok(deletion_id)
798 }
799
800 pub fn report_data_breach(
802 &mut self,
803 breach_type: BreachType,
804 affected_subjects: usize,
805 affected_data_categories: Vec<PersonalDataType>,
806 consequences: String,
807 measures_taken: Vec<String>,
808 ) -> Result<String> {
809 let breach_id = Uuid::new_v4().to_string();
810 let now = Utc::now();
811
812 let risk_level = self.assess_breach_risk(&breach_type, affected_subjects, &affected_data_categories);
814 let requires_subject_notification = risk_level >= RiskLevel::High;
815
816 let breach_notification = BreachNotification {
817 breach_id: breach_id.clone(),
818 detected_at: now,
819 reported_to_authority_at: None,
820 notified_subjects_at: None,
821 breach_type,
822 affected_data_subjects: affected_subjects,
823 categories_of_data_affected: affected_data_categories,
824 likely_consequences: consequences,
825 measures_taken,
826 measures_to_mitigate: Vec::new(),
827 risk_assessment: risk_level,
828 authority_reference: None,
829 requires_subject_notification,
830 notification_delay_reason: None,
831 };
832
833 self.breach_notifications.push(breach_notification);
834 Ok(breach_id)
835 }
836
837 pub fn create_dpia(&mut self, processing_operation: String, description: String) -> Result<String> {
839 let dpia_id = Uuid::new_v4().to_string();
840 let now = Utc::now();
841
842 let dpia = DataProtectionImpactAssessment {
843 dpia_id: dpia_id.clone(),
844 processing_operation,
845 description,
846 necessity_assessment: "Assessment pending".to_string(),
847 proportionality_assessment: "Assessment pending".to_string(),
848 risks_to_data_subjects: Vec::new(),
849 measures_to_address_risks: Vec::new(),
850 residual_risk_level: RiskLevel::Medium,
851 consultation_with_dpo: false,
852 consultation_with_authority: None,
853 created_at: now,
854 reviewed_at: None,
855 approved_by: "Pending".to_string(),
856 };
857
858 self.data_protection_impact_assessments.insert(dpia_id.clone(), dpia);
859 Ok(dpia_id)
860 }
861
862 pub fn get_compliance_status(&self) -> GdprComplianceStatus {
864 let total_subjects = self.data_subjects.len();
865 let active_consents = self.consent_records.values()
866 .filter(|c| matches!(c.consent_status, ConsentStatus::Given))
867 .count();
868 let pending_requests = self.data_subjects.values()
869 .flat_map(|ds| &ds.subject_rights_requests)
870 .filter(|r| matches!(r.request_status, RequestStatus::Received | RequestStatus::InProgress))
871 .count();
872 let unresolved_breaches = self.breach_notifications.iter()
873 .filter(|b| b.reported_to_authority_at.is_none())
874 .count();
875
876 GdprComplianceStatus {
877 total_data_subjects: total_subjects,
878 active_consents,
879 withdrawn_consents: self.consent_records.values()
880 .filter(|c| matches!(c.consent_status, ConsentStatus::Withdrawn))
881 .count(),
882 pending_subject_requests: pending_requests,
883 completed_subject_requests: self.data_subjects.values()
884 .flat_map(|ds| &ds.subject_rights_requests)
885 .filter(|r| matches!(r.request_status, RequestStatus::Completed))
886 .count(),
887 total_processing_activities: self.processing_activities.len(),
888 active_dpias: self.data_protection_impact_assessments.len(),
889 total_data_breaches: self.breach_notifications.len(),
890 unresolved_breaches,
891 data_exports_completed: self.data_exports.iter()
892 .filter(|e| e.export_completed_at.is_some())
893 .count(),
894 deletion_records: self.deletion_log.len(),
895 }
896 }
897
898 pub fn generate_gdpr_compliance_report(&self, start_date: DateTime<Utc>, end_date: DateTime<Utc>) -> GdprComplianceReport {
900 let report_id = Uuid::new_v4().to_string();
901 let generated_at = Utc::now();
902
903 let period_consents = self.consent_records.values()
905 .filter(|c| c.consent_given_at >= start_date && c.consent_given_at <= end_date)
906 .count();
907
908 let period_requests = self.data_subjects.values()
909 .flat_map(|ds| &ds.subject_rights_requests)
910 .filter(|r| r.requested_at >= start_date && r.requested_at <= end_date)
911 .collect::<Vec<_>>();
912
913 let period_breaches = self.breach_notifications.iter()
914 .filter(|b| b.detected_at >= start_date && b.detected_at <= end_date)
915 .collect::<Vec<_>>();
916
917 let mut requests_by_type = HashMap::new();
919 for request in &period_requests {
920 *requests_by_type.entry(request.request_type.clone()).or_insert(0) += 1;
921 }
922
923 let compliance_score = self.calculate_compliance_score();
925 let recommendations = self.generate_compliance_recommendations();
926
927 GdprComplianceReport {
928 report_id,
929 generated_at,
930 period_start: start_date,
931 period_end: end_date,
932 total_data_subjects: self.data_subjects.len(),
933 new_consents_given: period_consents,
934 consents_withdrawn: self.consent_records.values()
935 .filter(|c| matches!(c.consent_status, ConsentStatus::Withdrawn) &&
936 c.withdrawn_at.is_some_and(|w| w >= start_date && w <= end_date))
937 .count(),
938 subject_requests_received: period_requests.len(),
939 subject_requests_by_type: requests_by_type,
940 subject_requests_completed_on_time: period_requests.iter()
941 .filter(|r| matches!(r.request_status, RequestStatus::Completed) &&
942 !r.extension_granted)
943 .count(),
944 data_breaches_reported: period_breaches.len(),
945 breaches_reported_within_72h: period_breaches.iter()
946 .filter(|b| b.reported_to_authority_at.is_some_and(|r| r <= b.detected_at + Duration::hours(72)))
947 .count(),
948 processing_activities_documented: self.processing_activities.len(),
949 dpias_completed: self.data_protection_impact_assessments.len(),
950 privacy_controls_implemented: self.privacy_by_design_controls.iter()
951 .filter(|c| matches!(c.implementation_level, ImplementationLevel::FullyImplemented |
952 ImplementationLevel::OptimallyImplemented))
953 .count(),
954 data_exports_fulfilled: self.data_exports.iter()
955 .filter(|e| e.export_completed_at.is_some_and(|c| c >= start_date && c <= end_date))
956 .count(),
957 deletions_executed: self.deletion_log.iter()
958 .filter(|d| d.deletion_completed_at >= start_date && d.deletion_completed_at <= end_date)
959 .count(),
960 compliance_score,
961 key_risks_identified: self.identify_key_risks(),
962 recommendations,
963 }
964 }
965
966 fn initialize_standard_policies(&mut self) {
969 let policies = vec![
971 (PersonalDataType::BasicPersonalData, Duration::days(1095)), (PersonalDataType::SpecialCategoryData, Duration::days(2555)), (PersonalDataType::FinancialData, Duration::days(2555)), (PersonalDataType::CommunicationData, Duration::days(365)), (PersonalDataType::LocationData, Duration::days(90)), (PersonalDataType::BehavioralData, Duration::days(730)), ];
978
979 for (data_type, retention_period) in policies {
980 let policy_id = Uuid::new_v4().to_string();
981 let policy = RetentionPolicy {
982 policy_id: policy_id.clone(),
983 data_category: data_type,
984 retention_period,
985 retention_criteria: "Standard GDPR retention policy".to_string(),
986 disposal_method: DisposalMethod::SecureDeletion,
987 review_frequency: Duration::days(365),
988 last_reviewed: Utc::now(),
989 automatic_deletion: true,
990 archival_period: Some(Duration::days(30)),
991 };
992 self.retention_policies.insert(policy_id, policy);
993 }
994 }
995
996 fn initialize_privacy_controls(&mut self) {
997 let controls = vec![
998 (PrivacyControlType::DataMinimization, "Collect only necessary personal data"),
999 (PrivacyControlType::PurposeLimitation, "Process data only for specified purposes"),
1000 (PrivacyControlType::StorageLimitation, "Delete data when no longer needed"),
1001 (PrivacyControlType::AccuracyControl, "Keep personal data accurate and up to date"),
1002 (PrivacyControlType::IntegrityControl, "Protect data from unauthorized alteration"),
1003 (PrivacyControlType::ConfidentialityControl, "Protect data from unauthorized access"),
1004 (PrivacyControlType::AccountabilityControl, "Demonstrate compliance with GDPR"),
1005 (PrivacyControlType::TransparencyControl, "Provide clear information about processing"),
1006 ];
1007
1008 for (control_type, description) in controls {
1009 let control = PrivacyControl {
1010 control_id: Uuid::new_v4().to_string(),
1011 control_type,
1012 description: description.to_string(),
1013 implementation_level: ImplementationLevel::PartiallyImplemented,
1014 effectiveness: EffectivenessLevel::ModeratelyEffective,
1015 last_tested: None,
1016 responsible_team: "Privacy Team".to_string(),
1017 };
1018 self.privacy_by_design_controls.push(control);
1019 }
1020 }
1021
1022 fn assess_breach_risk(&self, breach_type: &BreachType, affected_subjects: usize, data_categories: &[PersonalDataType]) -> RiskLevel {
1023 let mut risk_score = 0;
1024
1025 match breach_type {
1027 BreachType::ConfidentialityBreach => risk_score += 3,
1028 BreachType::IntegrityBreach => risk_score += 2,
1029 BreachType::AvailabilityBreach => risk_score += 1,
1030 BreachType::Combined(_) => risk_score += 4,
1031 }
1032
1033 risk_score += match affected_subjects {
1035 0..=10 => 0,
1036 11..=100 => 1,
1037 101..=1000 => 2,
1038 1001..=10000 => 3,
1039 _ => 4,
1040 };
1041
1042 for category in data_categories {
1044 risk_score += match category {
1045 PersonalDataType::SpecialCategoryData => 4,
1046 PersonalDataType::FinancialData => 3,
1047 PersonalDataType::BiometricData => 4,
1048 _ => 1,
1049 };
1050 }
1051
1052 match risk_score {
1053 0..=3 => RiskLevel::Low,
1054 4..=6 => RiskLevel::Medium,
1055 7..=10 => RiskLevel::High,
1056 _ => RiskLevel::Critical,
1057 }
1058 }
1059
1060 fn calculate_compliance_score(&self) -> f64 {
1061 let mut score = 100.0;
1062
1063 let overdue_requests = self.data_subjects.values()
1065 .flat_map(|ds| &ds.subject_rights_requests)
1066 .filter(|r| {
1067 matches!(r.request_status, RequestStatus::Received | RequestStatus::InProgress) &&
1068 r.requested_at < Utc::now() - Duration::days(30)
1069 })
1070 .count();
1071
1072 score -= (overdue_requests as f64) * 5.0; let unresolved_breaches = self.breach_notifications.iter()
1075 .filter(|b| b.reported_to_authority_at.is_none() &&
1076 b.detected_at < Utc::now() - Duration::hours(72))
1077 .count();
1078
1079 score -= (unresolved_breaches as f64) * 10.0; score.max(0.0)
1083 }
1084
1085 fn identify_key_risks(&self) -> Vec<String> {
1086 let mut risks = Vec::new();
1087
1088 let overdue_breaches = self.breach_notifications.iter()
1090 .filter(|b| b.reported_to_authority_at.is_none() &&
1091 b.detected_at < Utc::now() - Duration::hours(72))
1092 .count();
1093
1094 if overdue_breaches > 0 {
1095 risks.push(format!("{overdue_breaches} data breaches not reported within 72 hours"));
1096 }
1097
1098 let overdue_requests = self.data_subjects.values()
1100 .flat_map(|ds| &ds.subject_rights_requests)
1101 .filter(|r| {
1102 matches!(r.request_status, RequestStatus::Received | RequestStatus::InProgress) &&
1103 r.requested_at < Utc::now() - Duration::days(30)
1104 })
1105 .count();
1106
1107 if overdue_requests > 0 {
1108 risks.push(format!("{overdue_requests} subject rights requests overdue"));
1109 }
1110
1111 let expired_consents = self.consent_records.values()
1113 .filter(|c| matches!(c.consent_status, ConsentStatus::Expired))
1114 .count();
1115
1116 if expired_consents > 0 {
1117 risks.push(format!("{expired_consents} consent records have expired"));
1118 }
1119
1120 if risks.is_empty() {
1121 risks.push("No significant compliance risks identified".to_string());
1122 }
1123
1124 risks
1125 }
1126
1127 fn generate_compliance_recommendations(&self) -> Vec<String> {
1128 let mut recommendations = Vec::new();
1129
1130 let partial_controls = self.privacy_by_design_controls.iter()
1132 .filter(|c| matches!(c.implementation_level, ImplementationLevel::PartiallyImplemented))
1133 .count();
1134
1135 if partial_controls > 0 {
1136 recommendations.push("Complete implementation of privacy by design controls".to_string());
1137 }
1138
1139 let high_risk_activities = self.processing_activities.iter()
1141 .filter(|a| a.categories_of_personal_data.contains(&PersonalDataType::SpecialCategoryData))
1142 .count();
1143
1144 if high_risk_activities > self.data_protection_impact_assessments.len() {
1145 recommendations.push("Conduct DPIAs for high-risk processing activities".to_string());
1146 }
1147
1148 let consent_coverage = (self.consent_records.len() as f64) / (self.data_subjects.len() as f64) * 100.0;
1150 if consent_coverage < 80.0 {
1151 recommendations.push("Improve consent collection and management processes".to_string());
1152 }
1153
1154 if recommendations.is_empty() {
1155 recommendations.push("Maintain current high standards of GDPR compliance".to_string());
1156 }
1157
1158 recommendations
1159 }
1160}
1161
1162#[derive(Debug, Clone, Serialize, Deserialize)]
1164pub struct GdprComplianceStatus {
1165 pub total_data_subjects: usize,
1166 pub active_consents: usize,
1167 pub withdrawn_consents: usize,
1168 pub pending_subject_requests: usize,
1169 pub completed_subject_requests: usize,
1170 pub total_processing_activities: usize,
1171 pub active_dpias: usize,
1172 pub total_data_breaches: usize,
1173 pub unresolved_breaches: usize,
1174 pub data_exports_completed: usize,
1175 pub deletion_records: usize,
1176}
1177
1178#[derive(Debug, Clone, Serialize, Deserialize)]
1180pub struct GdprComplianceReport {
1181 pub report_id: String,
1182 pub generated_at: DateTime<Utc>,
1183 pub period_start: DateTime<Utc>,
1184 pub period_end: DateTime<Utc>,
1185 pub total_data_subjects: usize,
1186 pub new_consents_given: usize,
1187 pub consents_withdrawn: usize,
1188 pub subject_requests_received: usize,
1189 pub subject_requests_by_type: HashMap<DataSubjectRight, usize>,
1190 pub subject_requests_completed_on_time: usize,
1191 pub data_breaches_reported: usize,
1192 pub breaches_reported_within_72h: usize,
1193 pub processing_activities_documented: usize,
1194 pub dpias_completed: usize,
1195 pub privacy_controls_implemented: usize,
1196 pub data_exports_fulfilled: usize,
1197 pub deletions_executed: usize,
1198 pub compliance_score: f64,
1199 pub key_risks_identified: Vec<String>,
1200 pub recommendations: Vec<String>,
1201}
1202
1203impl Default for GdprManager {
1204 fn default() -> Self {
1205 Self::new()
1206 }
1207}
1208
1209#[cfg(test)]
1210mod tests {
1211 use super::*;
1212
1213 #[test]
1214 fn test_gdpr_manager_creation() {
1215 let manager = GdprManager::new();
1216 assert_eq!(manager.data_subjects.len(), 0);
1217 assert_eq!(manager.consent_records.len(), 0);
1218 }
1219
1220 #[test]
1221 fn test_data_subject_registration() {
1222 let mut manager = GdprManager::new();
1223 let subject_id = manager.register_data_subject(
1224 "user123".to_string(),
1225 Some("user@example.com".to_string()),
1226 Some("John Doe".to_string()),
1227 ).unwrap();
1228
1229 assert!(!subject_id.is_empty());
1230 assert_eq!(manager.data_subjects.len(), 1);
1231
1232 let data_subject = manager.data_subjects.get(&subject_id).unwrap();
1233 assert_eq!(data_subject.external_id, Some("user123".to_string()));
1234 assert_eq!(data_subject.email, Some("user@example.com".to_string()));
1235 }
1236
1237 #[test]
1238 fn test_consent_recording() {
1239 let mut manager = GdprManager::new();
1240 let subject_id = manager.register_data_subject(
1241 "user123".to_string(),
1242 Some("user@example.com".to_string()),
1243 None,
1244 ).unwrap();
1245
1246 let evidence = ConsentEvidence {
1247 timestamp: Utc::now(),
1248 ip_address: Some("192.168.1.1".to_string()),
1249 user_agent: Some("Mozilla/5.0".to_string()),
1250 form_version: Some("v1.0".to_string()),
1251 witness: None,
1252 digital_signature: None,
1253 audit_trail: vec!["Form submitted".to_string()],
1254 };
1255
1256 let consent_id = manager.record_consent(
1257 subject_id.clone(),
1258 "marketing".to_string(),
1259 "I agree to receive marketing emails".to_string(),
1260 ConsentMethod::WebForm,
1261 evidence,
1262 ).unwrap();
1263
1264 assert!(!consent_id.is_empty());
1265 assert_eq!(manager.consent_records.len(), 1);
1266
1267 let consent = manager.consent_records.get(&consent_id).unwrap();
1268 assert_eq!(consent.data_subject_id, subject_id);
1269 assert!(matches!(consent.consent_status, ConsentStatus::Given));
1270 }
1271
1272 #[test]
1273 fn test_consent_withdrawal() {
1274 let mut manager = GdprManager::new();
1275 let subject_id = manager.register_data_subject(
1276 "user123".to_string(),
1277 Some("user@example.com".to_string()),
1278 None,
1279 ).unwrap();
1280
1281 let evidence = ConsentEvidence {
1282 timestamp: Utc::now(),
1283 ip_address: Some("192.168.1.1".to_string()),
1284 user_agent: None,
1285 form_version: None,
1286 witness: None,
1287 digital_signature: None,
1288 audit_trail: Vec::new(),
1289 };
1290
1291 let consent_id = manager.record_consent(
1292 subject_id,
1293 "marketing".to_string(),
1294 "I agree to receive marketing emails".to_string(),
1295 ConsentMethod::WebForm,
1296 evidence,
1297 ).unwrap();
1298
1299 manager.withdraw_consent(consent_id.clone(), "Email unsubscribe".to_string()).unwrap();
1300
1301 let consent = manager.consent_records.get(&consent_id).unwrap();
1302 assert!(matches!(consent.consent_status, ConsentStatus::Withdrawn));
1303 assert!(consent.withdrawn_at.is_some());
1304 }
1305
1306 #[test]
1307 fn test_subject_rights_requests() {
1308 let mut manager = GdprManager::new();
1309 let subject_id = manager.register_data_subject(
1310 "user123".to_string(),
1311 Some("user@example.com".to_string()),
1312 Some("John Doe".to_string()),
1313 ).unwrap();
1314
1315 let access_request = manager.process_access_request(
1317 subject_id.clone(),
1318 "Please provide all my personal data".to_string(),
1319 ).unwrap();
1320
1321 assert_eq!(access_request.request_type, DataSubjectRight::RightOfAccess);
1322 assert!(matches!(access_request.request_status, RequestStatus::Received));
1323
1324 let erasure_request = manager.process_erasure_request(
1326 subject_id.clone(),
1327 "No longer need account".to_string(),
1328 ).unwrap();
1329
1330 assert_eq!(erasure_request.request_type, DataSubjectRight::RightToErasure);
1331 assert!(matches!(erasure_request.complexity_assessment, ComplexityLevel::Complex));
1332
1333 let portability_request = manager.process_portability_request(
1335 subject_id,
1336 ExportFormat::Json,
1337 ).unwrap();
1338
1339 assert_eq!(portability_request.request_type, DataSubjectRight::RightToDataPortability);
1340 assert_eq!(manager.data_exports.len(), 1);
1341 }
1342
1343 #[test]
1344 fn test_data_breach_reporting() {
1345 let mut manager = GdprManager::new();
1346
1347 let breach_id = manager.report_data_breach(
1348 BreachType::ConfidentialityBreach,
1349 1000,
1350 vec![PersonalDataType::BasicPersonalData, PersonalDataType::FinancialData],
1351 "Unauthorized access to customer database".to_string(),
1352 vec!["Database access revoked".to_string(), "Passwords reset".to_string()],
1353 ).unwrap();
1354
1355 assert!(!breach_id.is_empty());
1356 assert_eq!(manager.breach_notifications.len(), 1);
1357
1358 let breach = &manager.breach_notifications[0];
1359 assert_eq!(breach.affected_data_subjects, 1000);
1360 assert!(breach.requires_subject_notification);
1361 assert!(matches!(breach.risk_assessment, RiskLevel::High | RiskLevel::Critical));
1362 }
1363
1364 #[test]
1365 fn test_compliance_status() {
1366 let manager = GdprManager::with_eu_configuration();
1367 let status = manager.get_compliance_status();
1368
1369 assert_eq!(status.total_data_subjects, 0);
1370 assert_eq!(status.active_consents, 0);
1371 assert!(status.total_processing_activities >= 0);
1372 }
1373
1374 #[test]
1375 fn test_gdpr_compliance_report() {
1376 let manager = GdprManager::with_eu_configuration();
1377 let start_date = Utc::now() - Duration::days(30);
1378 let end_date = Utc::now();
1379
1380 let report = manager.generate_gdpr_compliance_report(start_date, end_date);
1381
1382 assert!(!report.report_id.is_empty());
1383 assert!(report.compliance_score >= 0.0);
1384 assert!(report.compliance_score <= 100.0);
1385 assert!(!report.recommendations.is_empty());
1386 }
1387}