PageRenderTime 25ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/webui/model/saas/ldap.php

https://bitbucket.org/kbandlow/piler
PHP | 119 lines | 74 code | 45 blank | 0 comment | 27 complexity | da762e30dc15f0be6846a0847d7c62ca MD5 | raw file
  1. <?php
  2. class ModelSaasLdap extends Model
  3. {
  4. public function get($id = -1) {
  5. if(is_numeric($id) && $id >= 0) {
  6. $query = $this->db->query("SELECT * FROM " . TABLE_LDAP . " WHERE id=?", array($id));
  7. if($query->num_rows > 0) { return $query->row; }
  8. }
  9. return array();
  10. }
  11. public function search($s = '') {
  12. if($s) {
  13. $query = $this->db->query("SELECT id, description, ldap_type, ldap_host, ldap_base_dn, ldap_bind_dn, ldap_auditor_member_dn FROM " . TABLE_LDAP . " WHERE description LIKE ? ORDER BY description ASC", array('%' . $s . '%'));
  14. } else {
  15. $query = $this->db->query("SELECT id, description, ldap_type, ldap_host, ldap_base_dn, ldap_bind_dn, ldap_auditor_member_dn FROM " . TABLE_LDAP . " ORDER BY description ASC");
  16. }
  17. if($query->num_rows > 0) { return $query->rows; }
  18. return array();
  19. }
  20. public function delete($id = 0, $description = '') {
  21. if($id == 0) { return 0; }
  22. $query = $this->db->query("DELETE FROM " . TABLE_LDAP . " WHERE id=?", array($id));
  23. $rc = $this->db->countAffected();
  24. LOGGER("remove ldap entry: #$id, $description (rc=$rc)");
  25. return $rc;
  26. }
  27. public function add($arr = array()) {
  28. if(!isset($arr['description']) || !isset($arr['ldap_host'])) { return 0; }
  29. $query = $this->db->query("INSERT INTO " . TABLE_LDAP . " (description, ldap_host, ldap_base_dn, ldap_bind_dn, ldap_bind_pw, ldap_type, ldap_auditor_member_dn, ldap_mail_attr, ldap_account_objectclass, ldap_distributionlist_attr, ldap_distributionlist_objectclass) VALUES (?,?,?,?,?,?,?,?,?,?,?)", array($arr['description'], $arr['ldap_host'], $arr['ldap_base_dn'], $arr['ldap_bind_dn'], $arr['ldap_bind_pw'], $arr['ldap_type'], $arr['ldap_auditor_member_dn'], $arr['ldap_mail_attr'], $arr['ldap_account_objectclass'], $arr['ldap_distributionlist_attr'], $arr['ldap_distributionlist_objectclass']));
  30. $rc = $this->db->countAffected();
  31. LOGGER("add ldap entry: " . $arr['description'] . " / " . $arr['ldap_type'] . " / " . $arr['ldap_host'] . " / " . $arr['ldap_base_dn'] . " (rc=$rc)");
  32. if($rc == 1){ return 1; }
  33. return 0;
  34. }
  35. public function update($arr = array()) {
  36. if(!isset($arr['id']) || !isset($arr['description']) || !isset($arr['ldap_host'])) { return 0; }
  37. $query = $this->db->query("UPDATE " . TABLE_LDAP . " SET description=?, ldap_host=?, ldap_base_dn=?, ldap_bind_dn=?, ldap_bind_pw=?, ldap_type=?, ldap_auditor_member_dn=?, ldap_mail_attr=?, ldap_account_objectclass=?, ldap_distributionlist_attr=?, ldap_distributionlist_objectclass=? WHERE id=?", array($arr['description'], $arr['ldap_host'], $arr['ldap_base_dn'], $arr['ldap_bind_dn'], $arr['ldap_bind_pw'], $arr['ldap_type'], $arr['ldap_auditor_member_dn'], $arr['ldap_mail_attr'], $arr['ldap_account_objectclass'], $arr['ldap_distributionlist_attr'], $arr['ldap_distributionlist_objectclass'], $arr['id']));
  38. return $this->db->countAffected();
  39. }
  40. public function get_ldap_params_by_email($email = '') {
  41. $domain = '';
  42. if($email == '') { return array(); }
  43. list($l,$d) = explode("@", $email);
  44. $query = $this->db->query("SELECT d.*, l.* FROM " . TABLE_DOMAIN . " as d, " . TABLE_LDAP . " as l where d.ldap_id=l.id and d.domain=?", array($d));
  45. if($query->num_rows > 0) { return $query->rows; }
  46. return array();
  47. }
  48. public function get_accounts_in_domain($domain = '') {
  49. $ldap_type = '';
  50. $ldap_host = LDAP_HOST;
  51. $ldap_base_dn = LDAP_BASE_DN;
  52. $ldap_helper_dn = LDAP_HELPER_DN;
  53. $ldap_helper_password = LDAP_HELPER_PASSWORD;
  54. if(ENABLE_SAAS == 1) {
  55. $a = $this->model_saas_ldap->get_ldap_params_by_email("aaa@" . $domain);
  56. if(count($a) >= 5) {
  57. $ldap_type = $a[0];
  58. $ldap_host = $a[1];
  59. $ldap_base_dn = $a[2];
  60. $ldap_helper_dn = $a[3];
  61. $ldap_helper_password = $a[4];
  62. }
  63. }
  64. list($ldap_mail_attr, $ldap_account_objectclass, $ldap_distributionlist_attr, $ldap_distributionlist_objectclass) = get_ldap_attribute_names($ldap_type);
  65. if($ldap_host == '' || $ldap_helper_password == '') { return array(); }
  66. $ldap = new LDAP($ldap_host, $ldap_helper_dn, $ldap_helper_password);
  67. if($ldap->is_bind_ok()) {
  68. $query = $ldap->query($ldap_base_dn, "(&(objectClass=$ldap_account_objectclass)($ldap_mail_attr=*@$domain))", array($ldap_mail_attr));
  69. if($query->num_rows > 0) { asort($query->rows); return $query->rows; }
  70. }
  71. return array();
  72. }
  73. }
  74. ?>