@@ -68,7 +68,6 @@ protected override DatabaseThreatDetectionPolicyModel ApplyUserInputToModel(Data
68
68
base . ApplyUserInputToModel ( model ) ;
69
69
70
70
model . ThreatDetectionState = ThreatDetectionStateType . Enabled ;
71
- model . UseServerDefault = UseServerDefaultOptions . Disabled ;
72
71
73
72
if ( NotificationRecipientsEmails != null )
74
73
{
@@ -85,9 +84,50 @@ protected override DatabaseThreatDetectionPolicyModel ApplyUserInputToModel(Data
85
84
model . ExcludedDetectionTypes = ExcludedDetectionType . Select ( s => SecurityConstants . ExcludedDetectionToExcludedDetectionTypes [ s ] ) . ToArray ( ) ;
86
85
}
87
86
88
- model . ValidateInput ( ) ;
87
+ ValidateInput ( model ) ;
89
88
90
89
return model ;
91
90
}
91
+
92
+ /// <summary>
93
+ /// Preforms validity checks
94
+ /// </summary>
95
+ /// <param name="model">The model</param>
96
+ private void ValidateInput ( DatabaseThreatDetectionPolicyModel model )
97
+ {
98
+ // Validity checks:
99
+ // 1. Check that EmailAddresses are in correct format
100
+ bool areEmailAddressesInCorrectFormat = AreEmailAddressesInCorrectFormat ( model . NotificationRecipientsEmails ) ;
101
+ if ( ! areEmailAddressesInCorrectFormat )
102
+ {
103
+ throw new Exception ( Properties . Resources . EmailsAreNotValid ) ;
104
+ }
105
+
106
+ // 2. check that EmailAdmins is not False and NotificationRecipientsEmails is not empty
107
+ if ( ! model . EmailAdmins && string . IsNullOrEmpty ( model . NotificationRecipientsEmails ) )
108
+ {
109
+ throw new Exception ( Properties . Resources . NeedToProvideEmail ) ;
110
+ }
111
+ }
112
+
113
+ /// <summary>
114
+ /// Checks if email addresses are in a correct format
115
+ /// </summary>
116
+ /// <param name="emailAddresses">The email addresses</param>
117
+ /// <returns>Returns whether the email addresses are in a correct format</returns>
118
+ private bool AreEmailAddressesInCorrectFormat ( string emailAddresses )
119
+ {
120
+ if ( string . IsNullOrEmpty ( emailAddresses ) )
121
+ {
122
+ return true ;
123
+ }
124
+
125
+ string [ ] emailAddressesArray = emailAddresses . Split ( ';' ) . Where ( s => ! string . IsNullOrEmpty ( s ) ) . ToArray ( ) ;
126
+ var emailRegex =
127
+ new Regex ( string . Format ( "{0}{1}" ,
128
+ @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" ,
129
+ @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$" ) ) ;
130
+ return ! emailAddressesArray . Any ( e => ! emailRegex . IsMatch ( e ) ) ;
131
+ }
92
132
}
93
133
}
0 commit comments