44
55namespace Icinga \Module \X509 \Controllers ;
66
7- use Icinga \Data \Filter \FilterExpression ;
87use Icinga \Exception \ConfigurationError ;
98use Icinga \Module \X509 \CertificatesTable ;
109use Icinga \Module \X509 \Controller ;
11- use Icinga \Module \X509 \FilterAdapter ;
12- use Icinga \Module \X509 \SortAdapter ;
13- use Icinga \Module \X509 \SqlFilter ;
14- use ipl \Web \Control \PaginationControl ;
15- use ipl \Sql ;
16- use ipl \Web \Url ;
10+ use Icinga \Module \X509 \Model \X509Certificate ;
11+ use Icinga \Module \X509 \Web \Control \SearchBar \ObjectSuggestions ;
12+ use ipl \Orm \Query ;
13+ use ipl \Web \Control \LimitControl ;
14+ use ipl \Web \Control \SortControl ;
1715
1816class CertificatesController extends Controller
1917{
2018 public function indexAction ()
2119 {
22- $ this
23- ->initTabs ()
24- ->setTitle ($ this ->translate ('Certificates ' ));
20+ $ this ->initTabs ();
21+ $ this ->addTitleTab ($ this ->translate ('Certificates ' ));
2522
2623 try {
2724 $ conn = $ this ->getDb ();
@@ -30,100 +27,99 @@ public function indexAction()
3027 return ;
3128 }
3229
33- $ select = (new Sql \Select ())
34- ->from ('x509_certificate c ' )
35- ->columns ([
36- 'c.id ' , 'c.subject ' , 'c.issuer ' , 'c.version ' , 'c.self_signed ' , 'c.ca ' , 'c.trusted ' ,
37- 'c.pubkey_algo ' , 'c.pubkey_bits ' , 'c.signature_algo ' , 'c.signature_hash_algo ' ,
38- 'c.valid_from ' , 'c.valid_to ' ,
39- ]);
40-
41- $ this ->view ->paginator = new PaginationControl (new Sql \Cursor ($ conn , $ select ), Url::fromRequest ());
42- $ this ->view ->paginator ->apply ();
43-
44- $ sortAndFilterColumns = [
45- 'subject ' => $ this ->translate ('Certificate ' ),
46- 'issuer ' => $ this ->translate ('Issuer ' ),
47- 'version ' => $ this ->translate ('Version ' ),
48- 'self_signed ' => $ this ->translate ('Is Self-Signed ' ),
49- 'ca ' => $ this ->translate ('Is Certificate Authority ' ),
50- 'trusted ' => $ this ->translate ('Is Trusted ' ),
51- 'pubkey_algo ' => $ this ->translate ('Public Key Algorithm ' ),
52- 'pubkey_bits ' => $ this ->translate ('Public Key Strength ' ),
53- 'signature_algo ' => $ this ->translate ('Signature Algorithm ' ),
30+ $ certificates = X509Certificate::on ($ conn );
31+
32+ $ sortColumns = [
33+ 'subject ' => $ this ->translate ('Certificate ' ),
34+ 'issuer ' => $ this ->translate ('Issuer ' ),
35+ 'version ' => $ this ->translate ('Version ' ),
36+ 'self_signed ' => $ this ->translate ('Is Self-Signed ' ),
37+ 'ca ' => $ this ->translate ('Is Certificate Authority ' ),
38+ 'trusted ' => $ this ->translate ('Is Trusted ' ),
39+ 'pubkey_algo ' => $ this ->translate ('Public Key Algorithm ' ),
40+ 'pubkey_bits ' => $ this ->translate ('Public Key Strength ' ),
41+ 'signature_algo ' => $ this ->translate ('Signature Algorithm ' ),
5442 'signature_hash_algo ' => $ this ->translate ('Signature Hash Algorithm ' ),
55- 'valid_from ' => $ this ->translate ('Valid From ' ),
56- 'valid_to ' => $ this ->translate ('Valid To ' ),
57- 'duration ' => $ this ->translate ('Duration ' ),
58- 'expires ' => $ this ->translate ('Expiration ' )
43+ 'valid_from ' => $ this ->translate ('Valid From ' ),
44+ 'valid_to ' => $ this ->translate ('Valid To ' ),
45+ 'duration ' => $ this ->translate ('Duration ' ),
46+ 'expires ' => $ this ->translate ('Expiration ' )
5947 ];
6048
61- $ this ->setupSortControl (
62- $ sortAndFilterColumns ,
63- new SortAdapter ($ select , function ($ field ) {
64- if ($ field === 'duration ' ) {
65- return '(valid_to - valid_from) ' ;
66- } elseif ($ field === 'expires ' ) {
67- return 'CASE WHEN UNIX_TIMESTAMP() > valid_to '
68- . ' THEN 0 ELSE (valid_to - UNIX_TIMESTAMP()) / 86400 END ' ;
69- }
70- })
71- );
72-
73- $ this ->setupLimitControl ();
74-
75- $ filterAdapter = new FilterAdapter ();
76- $ this ->setupFilterControl (
77- $ filterAdapter ,
78- $ sortAndFilterColumns ,
79- ['subject ' , 'issuer ' ],
80- ['format ' ]
81- );
82-
83- (new SqlFilter ($ conn ))->apply ($ select , $ filterAdapter ->getFilter (), function (FilterExpression $ filter ) {
84- switch ($ filter ->getColumn ()) {
85- case 'issuer_hash ' :
86- $ value = $ filter ->getExpression ();
87-
88- if (is_array ($ value )) {
89- $ value = array_map ('hex2bin ' , $ value );
90- } else {
91- $ value = hex2bin ($ value );
92- }
93-
94- return $ filter ->setExpression ($ value );
95- case 'duration ' :
96- return $ filter ->setColumn ('(valid_to - valid_from) ' );
97- case 'expires ' :
98- return $ filter ->setColumn (
99- 'CASE WHEN UNIX_TIMESTAMP() > valid_to THEN 0 ELSE (valid_to - UNIX_TIMESTAMP()) / 86400 END '
100- );
101- case 'valid_from ' :
102- case 'valid_to ' :
103- $ expr = $ filter ->getExpression ();
104- if (! is_numeric ($ expr )) {
105- return $ filter ->setExpression (strtotime ($ expr ));
106- }
107-
108- // expression doesn't need changing
109- default :
110- return false ;
49+ $ limitControl = $ this ->createLimitControl ();
50+ $ paginator = $ this ->createPaginationControl ($ certificates );
51+ $ sortControl = $ this ->createSortControl ($ certificates , $ sortColumns );
52+
53+ $ searchBar = $ this ->createSearchBar ($ certificates , [
54+ $ limitControl ->getLimitParam (),
55+ $ sortControl ->getSortParam ()
56+ ]);
57+
58+ if ($ searchBar ->hasBeenSent () && ! $ searchBar ->isValid ()) {
59+ if ($ searchBar ->hasBeenSubmitted ()) {
60+ $ filter = $ this ->getFilter ();
61+ } else {
62+ $ this ->addControl ($ searchBar );
63+ $ this ->sendMultipartUpdate ();
64+ return ;
11165 }
112- });
66+ } else {
67+ $ filter = $ searchBar ->getFilter ();
68+ }
69+
70+ $ certificates ->peekAhead ($ this ->view ->compact );
71+
72+ $ certificates ->filter ($ filter );
11373
114- $ this ->handleFormatRequest ($ conn , $ select , function (\PDOStatement $ stmt ) {
115- foreach ($ stmt as $ cert ) {
74+ $ this ->addControl ($ paginator );
75+ $ this ->addControl ($ sortControl );
76+ $ this ->addControl ($ limitControl );
77+ $ this ->addControl ($ searchBar );
78+
79+ // List of allowed columns to be exported
80+ $ exportable = array_flip ([
81+ 'id ' , 'subject ' , 'issuer ' , 'version ' , 'self_signed ' , 'ca ' , 'trusted ' ,
82+ 'pubkey_algo ' , 'pubkey_bits ' , 'signature_algo ' , 'signature_hash_algo ' ,
83+ 'valid_from ' , 'valid_to '
84+ ]);
85+
86+ $ this ->handleFormatRequest ($ certificates , function (Query $ certificates ) use ($ exportable ) {
87+ /** @var X509Certificate $cert */
88+ foreach ($ certificates as $ cert ) {
11689 $ cert ['valid_from ' ] = (new \DateTime ())
11790 ->setTimestamp ($ cert ['valid_from ' ])
11891 ->format ('l F jS, Y H:i:s e ' );
11992 $ cert ['valid_to ' ] = (new \DateTime ())
12093 ->setTimestamp ($ cert ['valid_to ' ])
12194 ->format ('l F jS, Y H:i:s e ' );
12295
123- yield $ cert ;
96+ yield array_intersect_key ( iterator_to_array ( $ cert-> getIterator ()), $ exportable ) ;
12497 }
12598 });
12699
127- $ this ->view ->certificatesTable = (new CertificatesTable ())->setData ($ conn ->select ($ select ));
100+ $ this ->addContent ((new CertificatesTable ())->setData ($ certificates ));
101+
102+ if (! $ searchBar ->hasBeenSubmitted () && $ searchBar ->hasBeenSent ()) {
103+ $ this ->sendMultipartUpdate (); // Updates the browser search bar
104+ }
105+ }
106+
107+ public function completeAction ()
108+ {
109+ $ suggestions = new ObjectSuggestions ();
110+ $ suggestions ->setModel (X509Certificate::class);
111+ $ suggestions ->forRequest ($ this ->getServerRequest ());
112+ $ this ->getDocument ()->add ($ suggestions );
113+ }
114+
115+ public function searchEditorAction ()
116+ {
117+ $ editor = $ this ->createSearchEditor (X509Certificate::on ($ this ->getDb ()), [
118+ LimitControl::DEFAULT_LIMIT_PARAM ,
119+ SortControl::DEFAULT_SORT_PARAM
120+ ]);
121+
122+ $ this ->getDocument ()->add ($ editor );
123+ $ this ->setTitle (t ('Adjust Filter ' ));
128124 }
129125}
0 commit comments