66 */
77namespace OCA \WorkflowEngine \Check ;
88
9+ use OCP \Cache \CappedMemoryCache ;
910use OCP \IL10N ;
1011use OCP \IRequest ;
1112use OCP \WorkflowEngine \ICheck ;
1213
1314class RequestRemoteAddress implements ICheck {
15+ protected CappedMemoryCache $ checked ;
1416
1517 /**
1618 * @param IL10N $l
@@ -20,6 +22,7 @@ public function __construct(
2022 protected IL10N $ l ,
2123 protected IRequest $ request ,
2224 ) {
25+ $ this ->checked = new CappedMemoryCache ();
2326 }
2427
2528 /**
@@ -29,18 +32,24 @@ public function __construct(
2932 */
3033 #[\Override]
3134 public function executeCheck ($ operator , $ value ) {
35+ $ cacheKey = sha1 ($ operator . $ value );
36+
37+ if (isset ($ this ->checked [$ cacheKey ])) {
38+ return $ this ->checked [$ cacheKey ];
39+ }
40+
3241 $ actualValue = $ this ->request ->getRemoteAddress ();
3342 $ decodedValue = explode ('/ ' , $ value );
3443
35- if ( $ operator === ' matchesIPv4 ' ) {
36- return $ this ->matchIPv4 ($ actualValue , $ decodedValue [0 ], (int )$ decodedValue [1 ]);
37- } elseif ( $ operator === ' !matchesIPv4 ' ) {
38- return ! $ this ->matchIPv4 ($ actualValue , $ decodedValue [0 ], (int )$ decodedValue [1 ]);
39- } elseif ( $ operator === ' matchesIPv6 ' ) {
40- return $ this -> matchIPv6 ( $ actualValue , $ decodedValue [ 0 ], ( int ) $ decodedValue [ 1 ]) ;
41- } else {
42- return ! $ this ->matchIPv6 ( $ actualValue , $ decodedValue [ 0 ], ( int ) $ decodedValue [ 1 ]) ;
43- }
44+ $ result = match ( $ operator ) {
45+ ' matchesIPv4 ' => $ this ->matchIPv4 ($ actualValue , $ decodedValue [0 ], (int )$ decodedValue [1 ]),
46+ ' !matchesIPv4 ' => ! $ this -> matchIPv4 ( $ actualValue , $ decodedValue [ 0 ], ( int ) $ decodedValue [ 1 ]),
47+ ' matchesIPv6 ' => $ this ->matchIPv6 ($ actualValue , $ decodedValue [0 ], (int )$ decodedValue [1 ]),
48+ default => ! $ this -> matchIPv6 ( $ actualValue , $ decodedValue [ 0 ], ( int ) $ decodedValue [ 1 ]),
49+ } ;
50+
51+ $ this ->checked [ $ cacheKey ] = $ result ;
52+ return $ result ;
4453 }
4554
4655 /**
0 commit comments