@@ -158,32 +158,33 @@ public function defineTabs($options = []) {
158
158
* @return array|string
159
159
*/
160
160
function getTabNameForItem (CommonGLPI $ item , $ withtemplate = 0 ) {
161
- if (static ::canView ()) {
162
- switch ($ item ->getType ()) {
163
- case __CLASS__ :
164
- $ tab = [1 => __ ('Danger zone ! ' , 'flyvemdm ' )];
165
- return $ tab ;
166
- break ;
167
-
168
- case PluginFlyvemdmFleet::class:
169
- if (!$ withtemplate ) {
170
- $ nb = 0 ;
171
- $ fleetId = $ item ->getID ();
172
- $ pluralNumber = Session::getPluralNumber ();
173
- if ($ _SESSION ['glpishow_count_on_tabs ' ]) {
174
- $ DbUtil = new DbUtils ();
175
- $ nb = $ DbUtil ->countElementsInTable (static ::getTable (), ['plugin_flyvemdm_fleets_id ' => $ fleetId ]);
176
- }
177
- return self ::createTabEntry (self ::getTypeName ($ pluralNumber ), $ nb );
161
+ if (!static ::canView ()) {
162
+ return '' ;
163
+ }
164
+
165
+ switch ($ item ->getType ()) {
166
+ case __CLASS__ :
167
+ $ tab = [1 => __ ('Danger zone ! ' , 'flyvemdm ' )];
168
+ return $ tab ;
169
+ break ;
170
+
171
+ case PluginFlyvemdmFleet::class:
172
+ if (!$ withtemplate ) {
173
+ $ nb = 0 ;
174
+ $ fleetId = $ item ->getID ();
175
+ $ pluralNumber = Session::getPluralNumber ();
176
+ if ($ _SESSION ['glpishow_count_on_tabs ' ]) {
177
+ $ DbUtil = new DbUtils ();
178
+ $ nb = $ DbUtil ->countElementsInTable (static ::getTable (), ['plugin_flyvemdm_fleets_id ' => $ fleetId ]);
178
179
}
179
- break ;
180
+ return self ::createTabEntry (self ::getTypeName ($ pluralNumber ), $ nb );
181
+ }
182
+ break ;
180
183
181
- case Computer::class:
182
- return __ ('Flyve MDM Agent ' , 'flyvemdm ' );
183
- break ;
184
- }
184
+ case Computer::class:
185
+ return __ ('Flyve MDM Agent ' , 'flyvemdm ' );
186
+ break ;
185
187
}
186
-
187
188
return '' ;
188
189
}
189
190
@@ -308,7 +309,7 @@ public static function showDangerZone(PluginFlyvemdmAgent $item) {
308
309
*/
309
310
public static function showForFleet (PluginFlyvemdmFleet $ item ) {
310
311
if (!PluginFlyvemdmFleet::canView ()) {
311
- return false ;
312
+ return ;
312
313
}
313
314
314
315
$ start = isset ($ _GET ["start " ]) ? intval ($ _GET ["start " ]) : 0 ;
@@ -324,14 +325,17 @@ public static function showForFleet(PluginFlyvemdmFleet $item) {
324
325
$ number = count ($ rows );
325
326
326
327
// get the pager
327
- $ pager = Html::printAjaxPager (self ::getTypeName (1 ), $ start , $ number , '' , false );
328
+ $ pager_top = Html::printAjaxPager (self ::getTypeName (1 ), $ start , $ number , '' , false );
329
+ $ pager_bottom = Html::printAjaxPager (self ::getTypeName (1 ), $ start , $ number , '' , false );
328
330
329
331
$ data = [
330
- 'number ' => $ number ,
331
- 'pager ' => $ pager ,
332
- 'agents ' => $ rows ,
333
- 'start ' => $ start ,
334
- 'stop ' => $ start + $ _SESSION ['glpilist_limit ' ]
332
+ 'number ' => $ number ,
333
+ 'pager_top ' => $ pager_top ,
334
+ 'pager_bottom ' => $ pager_bottom ,
335
+ 'agents ' => $ rows ,
336
+ 'agentUrl ' => Toolbox::getItemTypeFormURL (self ::class),
337
+ 'start ' => $ start ,
338
+ 'stop ' => $ start + $ _SESSION ['glpilist_limit ' ],
335
339
];
336
340
337
341
$ twig = plugin_flyvemdm_getTemplateEngine ();
@@ -1724,89 +1728,93 @@ protected function setupMqttAccess() {
1724
1728
return ;
1725
1729
}
1726
1730
1727
- if ($ user = $ this ->getOwner ()) {
1728
- $ config = Config::getConfigurationValues ('flyvemdm ' , [
1729
- 'guest_profiles_id ' ,
1730
- 'android_bugcollecctor_url ' ,
1731
- 'android_bugcollector_login ' ,
1732
- 'android_bugcollector_passwd ' ,
1733
- 'mqtt_broker_address ' ,
1734
- 'mqtt_broker_port ' ,
1735
- 'mqtt_broker_tls_port ' ,
1736
- 'mqtt_tls_for_clients ' ,
1737
- ]);
1738
- $ guestProfileId = $ config ['guest_profiles_id ' ];
1739
- if ($ user ->getID () == $ _SESSION ['glpiID ' ] && $ _SESSION ['glpiactiveprofile ' ]['id ' ] == $ guestProfileId ) {
1740
- $ mqttClearPassword = '' ;
1741
-
1742
- // Create, or re-eanble the mqtt user for the device
1743
- $ computer = new Computer ();
1744
- if (!$ computer ->getFromDB ($ this ->fields ['computers_id ' ])) {
1745
- // TODO : failed to find the computer
1746
- return ;
1747
- }
1748
- $ serial = $ computer ->getField ('serial ' );
1749
- if (!empty ($ serial )) {
1750
- $ acls = [
1751
- [
1752
- 'topic ' => $ this ->getTopic () . '/Status/# ' ,
1753
- 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_WRITE
1754
- ],
1755
- [
1756
- 'topic ' => $ this ->getTopic () . '/Command/# ' ,
1757
- 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_READ
1758
- ],
1759
- [
1760
- 'topic ' => $ this ->getTopic () . '/Policy/# ' ,
1761
- 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_READ
1762
- ],
1763
- [
1764
- 'topic ' => $ this ->getTopic () . '/FlyvemdmManifest/# ' ,
1765
- 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_WRITE
1766
- ],
1767
- [
1768
- 'topic ' => '/FlyvemdmManifest/# ' ,
1769
- 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_READ
1770
- ],
1771
- ];
1772
-
1773
- $ mqttUser = new PluginFlyvemdmMqttuser ();
1774
- $ mqttClearPassword = PluginFlyvemdmMqttuser::getRandomPassword ();
1775
- // TODO: try make the enrollment fails at this point if getRandomPassword throw exception.
1776
- if (!$ mqttUser ->getByUser ($ serial )) {
1777
- // The user does not exists
1778
- $ mqttUser ->add ([
1779
- 'user ' => $ serial ,
1780
- 'enabled ' => '1 ' ,
1781
- 'password ' => $ mqttClearPassword ,
1782
- '_acl ' => $ acls ,
1783
- '_reset_acl ' => true ,
1784
- ]);
1785
- } else {
1786
- // The user exists
1787
- $ mqttUser ->update ([
1788
- 'id ' => $ mqttUser ->getID (),
1789
- 'enabled ' => '1 ' ,
1790
- 'password ' => $ mqttClearPassword ,
1791
- '_acl ' => $ acls ,
1792
- '_reset_acl ' => true ,
1793
- ]);
1794
- }
1795
- }
1731
+ $ user = $ this ->getOwner ();
1732
+ if (!$ user ) {
1733
+ return ;
1734
+ }
1735
+
1736
+ $ config = Config::getConfigurationValues ('flyvemdm ' , [
1737
+ 'guest_profiles_id ' ,
1738
+ 'android_bugcollecctor_url ' ,
1739
+ 'android_bugcollector_login ' ,
1740
+ 'android_bugcollector_passwd ' ,
1741
+ 'mqtt_broker_address ' ,
1742
+ 'mqtt_broker_port ' ,
1743
+ 'mqtt_broker_tls_port ' ,
1744
+ 'mqtt_tls_for_clients ' ,
1745
+ ]);
1746
+ $ guestProfileId = $ config ['guest_profiles_id ' ];
1747
+ if ($ user ->getID () != $ _SESSION ['glpiID ' ] || $ _SESSION ['glpiactiveprofile ' ]['id ' ] != $ guestProfileId ) {
1748
+ return ;
1749
+ }
1750
+ $ mqttClearPassword = '' ;
1751
+
1752
+ // Create, or re-eanble the mqtt user for the device
1753
+ $ computer = new Computer ();
1754
+ if (!$ computer ->getFromDB ($ this ->fields ['computers_id ' ])) {
1755
+ // TODO : failed to find the computer
1756
+ return ;
1757
+ }
1758
+ $ serial = $ computer ->getField ('serial ' );
1759
+ if (!empty ($ serial )) {
1760
+ $ acls = [
1761
+ [
1762
+ 'topic ' => $ this ->getTopic () . '/Status/# ' ,
1763
+ 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_WRITE
1764
+ ],
1765
+ [
1766
+ 'topic ' => $ this ->getTopic () . '/Command/# ' ,
1767
+ 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_READ
1768
+ ],
1769
+ [
1770
+ 'topic ' => $ this ->getTopic () . '/Policy/# ' ,
1771
+ 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_READ
1772
+ ],
1773
+ [
1774
+ 'topic ' => $ this ->getTopic () . '/FlyvemdmManifest/# ' ,
1775
+ 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_WRITE
1776
+ ],
1777
+ [
1778
+ 'topic ' => '/FlyvemdmManifest/# ' ,
1779
+ 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_READ
1780
+ ],
1781
+ ];
1796
1782
1797
- // The request comes from the owner of the device or the device itself, mandated by the user
1798
- $ this ->fields ['topic ' ] = $ this ->getTopic ();
1799
- $ this ->fields ['mqttpasswd ' ] = $ mqttClearPassword ;
1800
- $ this ->fields ['broker ' ] = $ config ['mqtt_broker_address ' ];
1801
- $ this ->fields ['port ' ] = $ config ['mqtt_tls_for_clients ' ] !== '0 '
1802
- ? $ config ['mqtt_broker_tls_port ' ]
1803
- : $ config ['mqtt_broker_port ' ];
1804
- $ this ->fields ['tls ' ] = $ config ['mqtt_tls_for_clients ' ];
1805
- $ this ->fields ['android_bugcollecctor_url ' ] = $ config ['android_bugcollecctor_url ' ];
1806
- $ this ->fields ['android_bugcollector_login ' ] = $ config ['android_bugcollector_login ' ];
1807
- $ this ->fields ['android_bugcollector_passwd ' ] = $ config ['android_bugcollector_passwd ' ];
1783
+ $ mqttUser = new PluginFlyvemdmMqttuser ();
1784
+ $ mqttClearPassword = PluginFlyvemdmMqttuser::getRandomPassword ();
1785
+ // TODO: try make the enrollment fails at this point if getRandomPassword throw exception.
1786
+ if (!$ mqttUser ->getByUser ($ serial )) {
1787
+ // The user does not exists
1788
+ $ mqttUser ->add ([
1789
+ 'user ' => $ serial ,
1790
+ 'enabled ' => '1 ' ,
1791
+ 'password ' => $ mqttClearPassword ,
1792
+ '_acl ' => $ acls ,
1793
+ '_reset_acl ' => true ,
1794
+ ]);
1795
+ } else {
1796
+ // The user exists
1797
+ $ mqttUser ->update ([
1798
+ 'id ' => $ mqttUser ->getID (),
1799
+ 'enabled ' => '1 ' ,
1800
+ 'password ' => $ mqttClearPassword ,
1801
+ '_acl ' => $ acls ,
1802
+ '_reset_acl ' => true ,
1803
+ ]);
1808
1804
}
1809
1805
}
1806
+
1807
+ // The request comes from the owner of the device or the device itself, mandated by the user
1808
+ $ this ->fields ['topic ' ] = $ this ->getTopic ();
1809
+ $ this ->fields ['mqttpasswd ' ] = $ mqttClearPassword ;
1810
+ $ this ->fields ['broker ' ] = $ config ['mqtt_broker_address ' ];
1811
+ $ this ->fields ['port ' ] = $ config ['mqtt_tls_for_clients ' ] !== '0 '
1812
+ ? $ config ['mqtt_broker_tls_port ' ]
1813
+ : $ config ['mqtt_broker_port ' ];
1814
+ $ this ->fields ['tls ' ] = $ config ['mqtt_tls_for_clients ' ];
1815
+ $ this ->fields ['android_bugcollecctor_url ' ] = $ config ['android_bugcollecctor_url ' ];
1816
+ $ this ->fields ['android_bugcollector_login ' ] = $ config ['android_bugcollector_login ' ];
1817
+ $ this ->fields ['android_bugcollector_passwd ' ] = $ config ['android_bugcollector_passwd ' ];
1810
1818
}
1811
1819
1812
1820
/**
@@ -1858,32 +1866,34 @@ protected function changeMqttAcl(PluginFlyvemdmFleet $old, PluginFlyvemdmFleet $
1858
1866
} else {
1859
1867
$ success = $ mqttUser ->getFromDBByQuery ("LEFT JOIN `glpi_computers` `c` ON (`c`.`serial`=`user`) WHERE `c`.`id`=' $ computerId' " );
1860
1868
}
1861
- if ($ success ) {
1862
- $ mqttAcl = new PluginFlyvemdmMqttacl ();
1863
- if ($ old ->getField ('is_default ' ) == '0 ' ) {
1864
- $ mqttAcl ->getFromDBByCrit ([
1865
- 'AND ' => [
1866
- 'topic ' => $ old ->getTopic () . '/# ' ,
1867
- PluginFlyvemdmMqttuser::getForeignKeyField () => $ mqttUser ->getID ()
1868
- ]
1869
- ]);
1870
- if ($ new ->getField ('is_default ' ) != '0 ' ) {
1871
- $ mqttAcl ->delete (['id ' => $ mqttAcl ->getID ()]);
1872
-
1873
- } else {
1874
- $ mqttAcl ->update ([
1875
- 'id ' => $ mqttAcl ->getID (),
1876
- 'topic ' => $ new ->getTopic () . '/# ' ,
1877
- 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_READ
1878
- ]);
1879
- }
1869
+
1870
+ if (!$ success ) {
1871
+ return ;
1872
+ }
1873
+
1874
+ $ mqttAcl = new PluginFlyvemdmMqttacl ();
1875
+ if ($ old ->getField ('is_default ' ) == '0 ' ) {
1876
+ $ mqttAcl ->getFromDBByCrit ([
1877
+ 'AND ' => [
1878
+ 'topic ' => $ old ->getTopic () . '/# ' ,
1879
+ PluginFlyvemdmMqttuser::getForeignKeyField () => $ mqttUser ->getID ()
1880
+ ]
1881
+ ]);
1882
+ if ($ new ->getField ('is_default ' ) != '0 ' ) {
1883
+ $ mqttAcl ->delete (['id ' => $ mqttAcl ->getID ()]);
1880
1884
} else {
1881
- $ mqttAcl ->add ([
1882
- ' plugin_flyvemdm_mqttusers_id ' => $ mqttUser ->getID (),
1883
- 'topic ' => $ new ->getTopic () . '/# ' ,
1884
- 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_READ
1885
+ $ mqttAcl ->update ([
1886
+ ' id ' => $ mqttAcl ->getID (),
1887
+ 'topic ' => $ new ->getTopic () . '/# ' ,
1888
+ 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_READ
1885
1889
]);
1886
1890
}
1891
+ } else {
1892
+ $ mqttAcl ->add ([
1893
+ 'plugin_flyvemdm_mqttusers_id ' => $ mqttUser ->getID (),
1894
+ 'topic ' => $ new ->getTopic () . '/# ' ,
1895
+ 'access_level ' => PluginFlyvemdmMqttacl::MQTTACL_READ
1896
+ ]);
1887
1897
}
1888
1898
}
1889
1899
0 commit comments