action.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /** ADSERVER / SERVEUR ACTIVE DIRECTORY **/
  3. //Récuperation d'une liste de serveur active directory
  4. Action::register('activedirectory_adserver_search',function(&$response){
  5. global $_;
  6. User::check_access('activedirectory','read');
  7. require_once(__DIR__.SLASH.'AdServer.class.php');
  8. // OPTIONS DE RECHERCHE, A ACTIVER POUR UNE RECHERCHE AVANCEE
  9. $query = 'SELECT main.* FROM '.AdServer::tableName().' main WHERE 1';
  10. $data = array();
  11. //Recherche simple
  12. if(!empty($_['filters']['keyword'])){
  13. $query .= ' AND main.label LIKE ?';
  14. $data[] = '%'.$_['filters']['keyword'].'%';
  15. }
  16. //Recherche avancée
  17. if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array('main.ip','main.port','main.sslPort','main.protocolVersion','main.domain','main.userRoot','main.groupRoot','main.readonlyLogin','main.readonlyPassword','main.adminLogin','main.adminPassword','main.mapping','main.authenticationMode'),$query,$data);
  18. //Tri des colonnes
  19. if(isset($_['sort'])) sort_secure_query($_['sort'],array('main.ip','main.port','main.sslPort','main.protocolVersion','main.domain','main.userRoot','main.groupRoot','main.readonlyLogin','main.readonlyPassword','main.adminLogin','main.adminPassword','main.mapping','main.authenticationMode'),$query,$data);
  20. //Pagination
  21. //Par défaut pour une recherche, 20 items, pour un export 5000 max
  22. $itemPerPage = !empty($_['itemPerPage']) ? $_['itemPerPage'] : 20;
  23. //force le nombre de page max a 50 coté serveur
  24. $itemPerPage = $itemPerPage>50 ? 50 : $itemPerPage;
  25. if($_['export'] == 'true') $itemPerPage = 5000;
  26. $response['pagination'] = AdServer::paginate($itemPerPage,(!empty($_['page'])?$_['page']:0),$query,$data,'main');
  27. $adservers = AdServer::staticQuery($query,$data,true,0);
  28. $response['rows'] = array();
  29. foreach($adservers as $adserver){
  30. $row = $adserver->toArray();
  31. if($_['export'] == 'true'){
  32. $row['created'] = date('d-m-Y',$row['created']);
  33. $row['updated'] = date('d-m-Y',$row['updated']);
  34. }
  35. $response['rows'][] = $row;
  36. }
  37. /* Mode export */
  38. if($_['export'] == 'true'){
  39. if(empty($response['rows'])) $response['rows'][] = array('Vide'=>'Aucune données');
  40. $fieldsMapping = array();
  41. foreach (AdServer::fields(false) as $key => $value)
  42. $fieldsMapping[$value['label']] = $key;
  43. $stream = Excel::exportArray($response['rows'],$fieldsMapping ,'Export');
  44. File::downloadStream($stream,'export-adserver-'.date('d-m-Y').'.xlsx');
  45. exit();
  46. }
  47. });
  48. //Ajout ou modification d'élément serveur active directory
  49. Action::register('activedirectory_adserver_save',function(&$response){
  50. global $_;
  51. User::check_access('activedirectory','edit');
  52. require_once(__DIR__.SLASH.'AdServer.class.php');
  53. $item = AdServer::provide();
  54. $item->ip = $_['ip'];
  55. $item->port = $_['port'];
  56. $item->sslPort = $_['sslPort'];
  57. $item->protocolVersion = $_['protocolVersion'];
  58. $item->domain = $_['domain'];
  59. $item->userRoot = $_['userRoot'];
  60. $item->groupRoot = $_['groupRoot'];
  61. $item->readonlyLogin = $_['readonlyLogin'];
  62. $item->readonlyPassword = encrypt($_['readonlyPassword']);
  63. $item->adminLogin = $_['adminLogin'];
  64. $item->adminPassword = encrypt($_['adminPassword']);
  65. $item->mapping = $_['mapping'];
  66. $item->authenticationMode = $_['authenticationMode'];
  67. $item->save();
  68. $response = $item->toArray();
  69. });
  70. //Suppression d'élement serveur active directory
  71. Action::register('activedirectory_fields_search',function(&$response){
  72. global $_;
  73. User::check_access('activedirectory','read');
  74. require_once(__DIR__.SLASH.'AdServer.class.php');
  75. if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Identifiant incorrect");
  76. $server = AdServer::getById($_['id']);
  77. $server->login();
  78. $response['groups'] = array();
  79. foreach($server->groups() as $group){
  80. $response['groups'][] = $group['label'];
  81. }
  82. $server->logout();
  83. });
  84. //Suppression d'élement serveur active directory
  85. Action::register('activedirectory_adserver_delete',function(&$response){
  86. global $_;
  87. User::check_access('activedirectory','delete');
  88. require_once(__DIR__.SLASH.'AdServer.class.php');
  89. if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Identifiant incorrect");
  90. AdServer::deleteById($_['id']);
  91. });
  92. //Test de connexion au serveur / OUs
  93. Action::register('activedirectory_adserver_test',function(&$response){
  94. global $_;
  95. User::check_access('activedirectory','delete');
  96. require_once(__DIR__.SLASH.'AdServer.class.php');
  97. if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Identifiant incorrect");
  98. $server = AdServer::getById($_['id']);
  99. try{
  100. $response['data_validity'] = true;
  101. $response['server_reachable'] = true;
  102. $response['authentication_readonly'] = true;
  103. $response['authentication_admin'] = true;
  104. $response['users'] = 0;
  105. $response['groups'] = 0;
  106. $server->login();
  107. $groups = $server->groups();
  108. $response['groups'] = count($groups);
  109. $users = $server->users();
  110. $response['users'] = count($users);
  111. $server->logout();
  112. try{
  113. $server->login(array('mode'=>'admin'));
  114. $server->logout();
  115. }catch(Exception $e){
  116. switch($e->getCode()){
  117. case 400 :
  118. $response['data_validity'] = false;
  119. $response['server_reachable'] = false;
  120. $response['authentication_admin'] = false;
  121. $response['authentication_readonly'] = false;
  122. break;
  123. case 404 :
  124. $response['server_reachable'] = false;
  125. $response['authentication_admin'] = false;
  126. $response['authentication_readonly'] = false;
  127. break;
  128. case 403 : $response['authentication_readonly'] = false; break;
  129. }
  130. $response['error_detail'] = $e->getMessage();
  131. }
  132. }catch(Exception $e){
  133. switch($e->getCode()){
  134. case 400 :
  135. $response['data_validity'] = false;
  136. $response['server_reachable'] = false;
  137. $response['authentication_admin'] = false;
  138. $response['authentication_readonly'] = false;
  139. break;
  140. case 404 :
  141. $response['server_reachable'] = false;
  142. $response['authentication_admin'] = false;
  143. $response['authentication_readonly'] = false;
  144. break;
  145. case 403 : $response['authentication_readonly'] = false; break;
  146. }
  147. $response['error_detail'] = $e->getMessage();
  148. }
  149. });
  150. //Sauvegarde des configurations de Active directory
  151. Action::register('activedirectory_setting_save',function(&$response){
  152. global $_,$conf;
  153. User::check_access('activedirectory','configure');
  154. //Si input file "multiple", possibilité de normaliser le
  155. //tableau $_FILES récupéré avec la fonction => normalize_php_files();
  156. foreach(Configuration::setting('activedirectory') as $key=>$value){
  157. if(!is_array($value)) continue;
  158. $allowed[] = $key;
  159. }
  160. foreach ($_['fields'] as $key => $value) {
  161. if(in_array($key, $allowed))
  162. $conf->put($key,$value);
  163. }
  164. });
  165. /** AdFirmRank / LIAISON GROUPE RANG **/
  166. //Récuperation d'une liste de liaison groupe rang
  167. Action::register('activedirectory_ad_firm_rank_search',function(&$response){
  168. global $_;
  169. User::check_access('activedirectory','read');
  170. require_once(__DIR__.SLASH.'AdFirmRank.class.php');
  171. require_once(__ROOT__.SLASH.'plugin/activedirectory/AdServer.class.php');
  172. // OPTIONS DE RECHERCHE, A ACTIVER POUR UNE RECHERCHE AVANCEE
  173. $query = 'SELECT main.*,main.id as id, '.Firm::joinString('f').', '.Rank::joinString('r').' FROM '.AdFirmRank::tableName().' main
  174. LEFT JOIN '.Firm::tableName().' f ON f.id=main.firm
  175. LEFT JOIN '.Rank::tableName().' r ON r.id=main.rank
  176. WHERE 1';
  177. $data = array();
  178. //Recherche simple
  179. if(!empty($_['filters']['keyword'])){
  180. $query .= ' AND main.label LIKE ?';
  181. $data[] = '%'.$_['filters']['keyword'].'%';
  182. }
  183. //Recherche avancée
  184. if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array('main.firm','main.rank','main.group','main.server'),$query,$data);
  185. //Tri des colonnes
  186. if(isset($_['sort'])) sort_secure_query($_['sort'],array('main.firm','main.rank','main.group','main.server'),$query,$data);
  187. //Pagination
  188. //Par défaut pour une recherche, 20 items, pour un export 5000 max
  189. $itemPerPage = !empty($_['itemPerPage']) ? $_['itemPerPage'] : 20;
  190. //force le nombre de page max a 50 coté serveur
  191. $itemPerPage = $itemPerPage>50 ? 50 : $itemPerPage;
  192. if($_['export'] == 'true') $itemPerPage = 5000;
  193. $response['pagination'] = AdFirmRank::paginate($itemPerPage,(!empty($_['page'])?$_['page']:0),$query,$data,'main');
  194. $adFirmRanks = AdFirmRank::staticQuery($query,$data,true,1);
  195. $response['rows'] = array();
  196. foreach($adFirmRanks as $adFirmRank){
  197. $row = $adFirmRank->toArray();
  198. $row['firm'] = $adFirmRank->join('firm');
  199. $row['rank'] = $adFirmRank->join('rank');
  200. if($_['export'] == 'true'){
  201. $row['created'] = date('d-m-Y',$row['created']);
  202. $row['updated'] = date('d-m-Y',$row['updated']);
  203. }
  204. $response['rows'][] = $row;
  205. }
  206. /* Mode export */
  207. if($_['export'] == 'true'){
  208. if(empty($response['rows'])) $response['rows'][] = array('Vide'=>'Aucune données');
  209. $fieldsMapping = array();
  210. foreach (AdFirmRank::fields(false) as $key => $value)
  211. $fieldsMapping[$value['label']] = $key;
  212. $stream = Excel::exportArray($response['rows'],$fieldsMapping ,'Export');
  213. File::downloadStream($stream,'export-AdFirmRank-'.date('d-m-Y').'.xlsx');
  214. exit();
  215. }
  216. });
  217. //Ajout ou modification d'élément liaison groupe rang
  218. Action::register('activedirectory_ad_firm_rank_save',function(&$response){
  219. global $_;
  220. User::check_access('activedirectory','edit');
  221. require_once(__DIR__.SLASH.'AdFirmRank.class.php');
  222. $item = AdFirmRank::provide();
  223. $item->firm = $_['firm'];
  224. $item->rank = $_['rank'];
  225. $item->group = $_['group'];
  226. $item->server = $_['server'];
  227. $item->save();
  228. $response = $item->toArray();
  229. });
  230. //Récuperation ou edition d'élément liaison groupe rang
  231. Action::register('activedirectory_ad_firm_rank_edit',function(&$response){
  232. global $_;
  233. User::check_access('activedirectory','edit');
  234. require_once(__DIR__.SLASH.'AdServer.class.php');
  235. require_once(__DIR__.SLASH.'AdFirmRank.class.php');
  236. $response = AdFirmRank::getById($_['id'],1)->toArray();
  237. });
  238. //Suppression d'élement liaison groupe rang
  239. Action::register('activedirectory_ad_firm_rank_delete',function(&$response){
  240. global $_;
  241. User::check_access('activedirectory','delete');
  242. require_once(__DIR__.SLASH.'AdFirmRank.class.php');
  243. if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Identifiant incorrect");
  244. AdFirmRank::deleteById($_['id']);
  245. });
  246. ?>