1
1
<?php
2
2
3
3
/**
4
- * Implement API credential management actions such as listing, sending warning emails and
5
- * deleting credentials.
4
+ * Implement API credential management actions such as listing,
5
+ * sending warning emails and deleting credentials.
6
6
*/
7
7
8
8
namespace org \gocdb \scripts ;
9
9
10
- require_once dirname (__FILE__ ) . "/../../lib/Gocdb_Services/APIAuthenticationService.php " ;
11
- require_once dirname (__FILE__ ) . "/../../lib/Gocdb_Services/Factory.php " ;
10
+ require_once dirname (__FILE__ )
11
+ . "/../../lib/Gocdb_Services/APIAuthenticationService.php " ;
12
+ require_once dirname (__FILE__ )
13
+ . "/../../lib/Gocdb_Services/Factory.php " ;
12
14
13
15
use APIAuthentication ;
14
16
use DateInterval ;
@@ -22,32 +24,38 @@ class ManageAPICredentialsActions
22
24
private $ entityManager ;
23
25
private $ baseTime ;
24
26
27
+ /**
28
+ * @param \Doctrine\Orm\EntityManager $entitymanager A valid Doctrine
29
+ * Entity Manager
30
+ * @param bool $dryRun If true no action is taken and
31
+ * a report is generated instead
32
+ * @param \DateTime $baseTime Time from which interval
33
+ * of no-use is measured
34
+ */
25
35
public function __construct ($ dryRun , $ entityManager , $ baseTime )
26
36
{
27
- /**
28
- * @param \Doctrine\Orm\EntityManager $entitymanager A valid Doctrine Entity Manager
29
- * @param bool $dryRun If true no action is taken and a report is generated instead
30
- * @param \DateTime $baseTime Time from which interval of no-use is measured
31
- **/
32
37
$ this ->dryRun = $ dryRun ;
33
38
$ this ->entityManager = $ entityManager ;
34
39
$ this ->baseTime = $ baseTime ;
35
40
}
41
+
36
42
/**
37
43
* Find API credentials unused for a number of months.
38
44
*
39
- * Find API credentials which have not been used for a number of months prior to a given base time based
40
- * on the credential property lastUseTime.
45
+ * Find API credentials which have not been used for a number of months
46
+ * prior to a given base time based on the credential property lastUseTime.
41
47
*
42
- * @param int $threshold The number of months of no-use prior to $baseTime to use for selection
48
+ * @param int $threshold The number of months of no-use
49
+ * prior to $baseTime to use for selection
43
50
*/
44
51
public function getCreds ($ threshold , $ propertyName )
45
52
{
46
53
$ qbl = $ this ->entityManager ->createQueryBuilder ();
47
54
55
+ // `$qbl->expr()->isNotNull("cred.user")` => cope with legacy entities
48
56
$ qbl ->select ('cred ' )
49
57
->from ('APIAuthentication ' , 'cred ' )
50
- ->where ($ qbl ->expr ()->isNotNull ("cred.user " )) // cope with legacy entities
58
+ ->where ($ qbl ->expr ()->isNotNull ("cred.user " ))
51
59
->andWhere ('cred. ' . $ propertyName . '< :threshold ' );
52
60
53
61
$ timeThresh = clone $ this ->baseTime ;
@@ -62,18 +70,23 @@ public function getCreds($threshold, $propertyName)
62
70
63
71
return $ creds ;
64
72
}
73
+
65
74
/**
66
75
* Select API credentials for deletion.
67
76
*
68
- * Find API credentials which have not been used for a given number of months
69
- * and delete them or, if dry-run option is true, generate a summary report
70
- * of the credentils found.
77
+ * Find API credentials which have not been used for a given
78
+ * number of months and delete them or, if dry-run option is true,
79
+ * generate a summary report of the credentils found.
80
+ *
81
+ * @param array $creds Array of credentials to process.
82
+ * @param \Doctrine\Orm\EntityManager $entitymanager A valid Doctrine
83
+ * Entity Manager
84
+ * @param \DateTime $baseTime Time from which interval of
85
+ * no-use is measured
86
+ * @param int $deleteThreshold The number of months of no-use
87
+ * which will trigger deletion
71
88
*
72
- * @param array $creds Array of credentials to process.
73
- * @param \Doctrine\Orm\EntityManager $entitymanager A valid Doctrine Entity Manager
74
- * @param \DateTime $baseTime Time from which interval of no-use is measured
75
- * @param int $deleteThreshold The number of months of no-use which will trigger deletion
76
- * @return array Credentials which were not deleted.
89
+ * @return array Credentials which were not deleted.
77
90
*/
78
91
public function deleteCreds ($ creds , $ deleteThreshold )
79
92
{
@@ -82,36 +95,60 @@ public function deleteCreds($creds, $deleteThreshold)
82
95
$ serv = new APIAuthenticationService ();
83
96
$ serv ->setEntityManager ($ this ->entityManager );
84
97
85
- /* @var $apiCred APIAuthentication */
86
98
foreach ($ creds as $ apiCred ) {
87
- if ($ this ->isOverThreshold ($ apiCred , $ this ->baseTime , $ deleteThreshold )) {
99
+ if (
100
+ $ this ->isOverThreshold (
101
+ $ apiCred ,
102
+ $ this ->baseTime ,
103
+ $ deleteThreshold
104
+ )
105
+ ) {
88
106
$ deletedCreds [] = $ apiCred ;
107
+
89
108
if (!$ this ->dryRun ) {
90
109
$ serv ->deleteAPIAuthentication ($ apiCred );
91
110
}
92
111
}
93
112
}
113
+
94
114
if ($ this ->dryRun ) {
95
115
$ this ->reportDryRun ($ deletedCreds , "deleting " );
96
116
}
97
117
98
- return array_udiff ($ creds , $ deletedCreds , array ($ this , 'compareCredIds ' ));
118
+ return array_udiff (
119
+ $ creds ,
120
+ $ deletedCreds ,
121
+ array (
122
+ $ this ,
123
+ 'compareCredIds '
124
+ )
125
+ );
99
126
}
127
+
100
128
/**
101
- * Send of warning emails where credentials have not been used for a given number of months
129
+ * Send of warning emails where credentials have not
130
+ * been used for a given number of months
131
+ *
132
+ * Find API credentials from the input array which have not been used for
133
+ * a given number of months and send emails to the owners and site address,
134
+ * taken from the credential object, warning of impending deletion if the
135
+ * period of no-use reaches a given threshold.
136
+ * If dry-run option is true, generate a summary report of the
137
+ * credentials found instead of sending emails.
102
138
*
103
- * Find API credentials from the input array which have not been used for a given number of months
104
- * and send emails to the owners and site address, taken from the credential object,
105
- * warning of impending deletion if the period of no-use reaches a given threshold.
106
- * If dry-run option is true, generate a summary report of the credentials found
107
- * instead of sending emails.
139
+ * @param array $creds Array of credentials
140
+ * to process.
141
+ * @param int $warningThreshold The number of months of no-use
142
+ * which triggers warning emails
143
+ * @param int $deleteThreshold The number of months of no-use
144
+ * which will trigger deletion
145
+ * @param string $fromEmail Email address to use
146
+ * as sender's (From:) address
147
+ * @param string $replyToEmail Email address for
148
+ * replies (Reply-To:)
108
149
*
109
- * @param array $creds Array of credentials to process.
110
- * @param int $warningThreshold The number of months of no-use which triggers warning emails
111
- * @param int $deleteThreshold The number of months of no-use which will trigger deletion
112
- * @param string $fromEmail Email address to use as sender's (From:) address
113
- * @param string $replyToEmail Email address for replies (Reply-To:)
114
- * @return array Array of credentials identifed for sending warning emails
150
+ * @return array Array of credentials identifed
151
+ * for sending warning emails
115
152
*/
116
153
public function warnUsers (
117
154
$ creds ,
@@ -122,13 +159,21 @@ public function warnUsers(
122
159
) {
123
160
$ warnedCreds = [];
124
161
125
- /* @var $api APIAuthentication */
126
162
foreach ($ creds as $ apiCred ) {
127
- // The credentials list is pre-selected based on the given threshold in the query
128
- // so this check is probably redundant.
129
- if ($ this ->isOverThreshold ($ apiCred , $ this ->baseTime , $ warningThreshold )) {
163
+ /**
164
+ * The credentials list is pre-selected based on the given
165
+ * threshold in the query so this check is probably redundant.
166
+ */
167
+ if (
168
+ $ this ->isOverThreshold (
169
+ $ apiCred ,
170
+ $ this ->baseTime ,
171
+ $ warningThreshold
172
+ )
173
+ ) {
130
174
$ lastUsed = $ apiCred ->getLastUseTime ();
131
- $ lastUseMonths = $ this ->baseTime ->diff ($ lastUsed )->format ('%m ' );
175
+ $ lastUseMonths = $ this ->baseTime ->diff ($ lastUsed )
176
+ ->format ('%m ' );
132
177
133
178
if (!$ this ->dryRun ) {
134
179
$ this ->sendWarningEmail (
@@ -148,28 +193,43 @@ public function warnUsers(
148
193
$ this ->reportDryRun ($ warnedCreds , "sending warning emails " );
149
194
}
150
195
151
- return array_udiff ($ creds , $ warnedCreds , array ($ this , 'compareCredIds ' ));
196
+ return array_udiff (
197
+ $ creds ,
198
+ $ warnedCreds ,
199
+ array (
200
+ $ this ,
201
+ 'compareCredIds '
202
+ )
203
+ );
152
204
}
153
- /**
154
- * @return boolean true if the credential has not been used within $threshold months, else false
155
- */
156
- private function isOverThreshold (APIAuthentication $ cred , DateTime $ baseTime , $ threshold )
157
- {
205
+
206
+ /**
207
+ * @return boolean true if the credential has not been
208
+ * used within $threshold months, else false
209
+ */
210
+ private function isOverThreshold (
211
+ APIAuthentication $ cred ,
212
+ DateTime $ baseTime ,
213
+ $ threshold
214
+ ) {
158
215
$ lastUsed = $ cred ->getLastUseTime ();
159
216
160
217
$ diffTime = $ baseTime ->diff ($ lastUsed );
161
218
$ lastUseMonths = ($ diffTime ->y * 12 ) + $ diffTime ->m ;
162
219
163
220
return $ lastUseMonths >= $ threshold ;
164
221
}
165
- /**
166
- * Helper function to check if two API credentials have the same id.
167
- *
168
- * @return integer zero if equal, -1 if id1 < id2, 1 if id1 > id2
169
- *
170
- */
171
- private function compareCredIds (APIAuthentication $ cred1 , APIAuthentication $ cred2 )
172
- {
222
+
223
+ /**
224
+ * Helper function to check if two API credentials have the same id.
225
+ *
226
+ * @return integer zero if equal, -1 if id1 < id2, 1 if id1 > id2
227
+ *
228
+ */
229
+ private function compareCredIds (
230
+ APIAuthentication $ cred1 ,
231
+ APIAuthentication $ cred2
232
+ ) {
173
233
$ id1 = $ cred1 ->getId ();
174
234
$ id2 = $ cred2 ->getId ();
175
235
@@ -180,19 +240,26 @@ private function compareCredIds(APIAuthentication $cred1, APIAuthentication $cre
180
240
return $ id1 > $ id2 ? 1 : -1 ;
181
241
}
182
242
183
- /**
184
- * Format and send warning emails.
185
- *
186
- * Send emails to API credential owner and the registered site address warning of impending credential deletion
187
- * if the credential remains unused until a given threshold of months.
188
- *
189
- * @param string $fromEmail Email address to use as sender's (From:) address
190
- * @param string $replyToEmail Email address for replies (Reply-To:)
191
- * @param \APIAuthentication $api Credential to warn about
192
- * @param int $elapsedMonths The number of months of non-use so far.
193
- * @param int $deleteionThreshold The number of months of no-use which will trigger deletion if reached.
194
- * @return void
195
- */
243
+ /**
244
+ * Format and send warning emails.
245
+ *
246
+ * Send emails to API credential owner and the registered site address
247
+ * warning of impending credential deletion if the credential remains
248
+ * unused until a given threshold of months.
249
+ *
250
+ * @param string $fromEmail Email address to use as sender's
251
+ * (From:) address
252
+ * @param string $replyToEmail Email address for replies
253
+ * (Reply-To:)
254
+ * @param \APIAuthentication $api Credential to warn about
255
+ * @param int $elapsedMonths The number of months of
256
+ * non-use so far.
257
+ * @param int $deleteionThreshold The number of months of no-use
258
+ * which will trigger deletion
259
+ * if reached.
260
+ *
261
+ * @return void
262
+ */
196
263
private function sendWarningEmail (
197
264
$ fromEmail ,
198
265
$ replyToEmail ,
@@ -207,9 +274,11 @@ private function sendWarningEmail(
207
274
208
275
$ headersArray = array ("From: $ fromEmail " ,
209
276
"Cc: $ siteEmail " );
277
+
210
278
if (strlen ($ replyToEmail ) > 0 && $ fromEmail !== $ replyToEmail ) {
211
279
$ headersArray [] = "Reply-To: $ replyToEmail " ;
212
280
}
281
+
213
282
$ headers = join ("\r\n" , $ headersArray );
214
283
215
284
$ subject = "GOCDB: Site API credential deletion notice " ;
@@ -230,21 +299,27 @@ private function sendWarningEmail(
230
299
// Send the email (or not, according to local configuration)
231
300
Factory::getEmailService ()->send ($ userEmail , $ subject , $ body , $ headers );
232
301
}
233
- /**
234
- * Generate a summary report.
235
- *
236
- * Generate a report to stdout summarising information about each credential in an array when
237
- * a dry-run operation is in progress.
238
- *
239
- * @param array $creds Array of API credential objects to be summarised.
240
- * @param string $text Brief description of the operation which would have been
241
- * performed without dry-run to be included in the report.
242
- * @return void
243
- */
302
+
303
+ /**
304
+ * Generate a summary report.
305
+ *
306
+ * Generate a report to stdout summarising information about each
307
+ * credential in an array when a dry-run operation is in progress.
308
+ *
309
+ * @param array $creds Array of API credential objects
310
+ * to be summarised.
311
+ * @param string $text Brief description of the operation
312
+ * which would have been
313
+ * performed without dry-run to be
314
+ * included in the report.
315
+ *
316
+ * @return void
317
+ */
244
318
private function reportDryRun (array $ creds , $ text )
245
319
{
246
320
if (count ($ creds ) == 0 ) {
247
321
print ("Dry run: No matching credentials found for $ text. \n" );
322
+
248
323
return ;
249
324
}
250
325
0 commit comments