UserStorageDB.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. Lib::loadClass('UserStorageBase');
  3. Lib::loadClass('ObjectUserDB');
  4. Lib::loadClass('ObjectGroupDB');
  5. class UserStorageDB extends UserStorageBase {
  6. private $_db;
  7. public function __construct($db) {
  8. $this->_db = $db;
  9. }
  10. /**
  11. * @return object $usr
  12. * $usr->primaryKey
  13. * $usr->login
  14. * $usr->password optional (required in createUser)
  15. * $usr->name
  16. * $usr->email
  17. * $usr->phone
  18. * $usr->homeEmail
  19. * $usr->homePhone
  20. * $usr->employeeType 'Pracownik','Kandydat','Partner'
  21. * $usr->isDisabled true, false or null if not set
  22. */
  23. public function getUser($usrLogin) {
  24. if (!$this->_db) return false;
  25. if (empty($usrLogin)) return false;
  26. $user = null;
  27. $sql = "SELECT a.`ID` as primaryKey
  28. , a.`ADM_ACCOUNT` as login
  29. , a.`ADM_PASSWD` as password
  30. , a.`ADM_NAME` as name
  31. , a.`EMAIL_LOCAL_ACCOUNT_ADDRESS` as email
  32. , a.`ADM_PHONE` as phone
  33. , a.`EMAIL` as homeEmail
  34. , '' as homePhone
  35. , a.`EMPLOYEE_TYPE` as employeeType
  36. , IF(a.`A_STATUS`='NORMAL', 0, 1) as isDisabled
  37. -- , a.`ADM_ADMIN_LEVEL`
  38. -- , a.`ADM_ADMIN_DESC` -- stanowisko
  39. -- , a.`ADM_NIP` -- NIP
  40. -- , a.`ADM_PESEL` -- nr. PESEL
  41. from `ADMIN_USERS` as a
  42. where a.`ADM_ACCOUNT`='{$usrLogin}'
  43. ";
  44. $res = $this->_db->query($sql);
  45. if ($r = $this->_db->fetch($res)) {
  46. $user = $this->_buildUserFromRow($r);
  47. }
  48. return $user;
  49. }
  50. private function _buildUserFromRow($r) {
  51. $user = new ObjectUserDB($this);
  52. $user->primaryKey = $r->primaryKey;
  53. $user->login = $r->login;
  54. $user->password = $r->password;
  55. $user->name = trim($r->name);
  56. $user->email = trim($r->email);
  57. $user->phone = trim($r->phone);
  58. $user->homeEmail = trim($r->homeEmail);
  59. $user->homePhone = trim($r->homePhone);
  60. $user->employeeType = $r->employeeType;
  61. $user->isDisabled = ((int)$r->isDisabled)? true : false;
  62. return $user;
  63. }
  64. /**
  65. * Build group realName from zasob.
  66. *
  67. * @param object $zasob {ID, DESC, TYPE}
  68. * @return string realName
  69. */
  70. protected function _buildRealNameFromZasob($zasob) {
  71. $realName = "{$zasob->DESC}";
  72. if ($zasob->TYPE != 'STANOWISKO') $realName = "{$zasob->TYPE} {$realName}";
  73. $realName = "[{$zasob->ID}] {$realName}";
  74. return $realName;
  75. }
  76. /**
  77. * Group.
  78. *
  79. * @return object $group
  80. * $group->primaryKey
  81. * $group->realName
  82. * $group->nestedGroups
  83. * $group->type 'STANOWISKO','PODMIOT','DZIAL','local'
  84. * $group->zasobID
  85. * (optional) $group->zasobDESC
  86. */
  87. public function getGroup($idGroup) {
  88. if (!$this->_db) return false;
  89. if ($idGroup <= 0) return false;
  90. $group = null;
  91. $sql = "SELECT z.`ID`
  92. , z.`DESC`
  93. , z.`TYPE`
  94. -- , IF(a.`A_STATUS`='NORMAL', 0, 1) as isDisabled
  95. from `CRM_LISTA_ZASOBOW` as z
  96. where z.`ID`='{$idGroup}'
  97. and z.`TYPE` in('STANOWISKO','PODMIOT','DZIAL')
  98. ";
  99. $res = $this->_db->query($sql);
  100. if ($r = $this->_db->fetch($res)) {
  101. $group = $this->_buildGroupFromRow($r, $fetchNested = true);
  102. }
  103. return $group;
  104. }
  105. public function getGroupWithoutNested($idGroup) {
  106. if (!$this->_db) return false;
  107. if ($idGroup <= 0) return false;
  108. $group = null;
  109. $sql = "SELECT z.`ID`
  110. , z.`DESC`
  111. , z.`TYPE`
  112. -- , IF(a.`A_STATUS`='NORMAL', 0, 1) as isDisabled
  113. from `CRM_LISTA_ZASOBOW` as z
  114. where z.`ID`='{$idGroup}'
  115. and z.`TYPE` in('STANOWISKO','PODMIOT','DZIAL')
  116. ";
  117. $res = $this->_db->query($sql);
  118. if ($r = $this->_db->fetch($res)) {
  119. $group = $this->_buildGroupFromRow($r, $fetchNested = false);
  120. }
  121. return $group;
  122. }
  123. public function fetchNestedGroups($idGroup) {
  124. if (!$this->_db) return null;
  125. if ($idGroup <= 0) return null;
  126. $groups = array();
  127. $sql = "SELECT l.`TABLE_2_ID` as groupID
  128. , z2.`ID`
  129. , z2.`DESC`
  130. , z2.`TYPE`
  131. from `ITEM_LINKS` as l
  132. join `CRM_LISTA_ZASOBOW` as z1 on(z1.`ID`=l.`TABLE_1_ID`)
  133. join `CRM_LISTA_ZASOBOW` as z2 on(z2.`ID`=l.`TABLE_2_ID`)
  134. where l.`TABLE_1_ID`='{$idGroup}'
  135. and l.`TABLE_2_ID`>0
  136. and l.`LINKS_TYPE_ID`=5
  137. and l.`TABLE_1_NAME`='CRM_LISTA_ZASOBOW'
  138. and l.`A_STATUS` in('NORMAL')
  139. and l.`TABLE_2_NAME`='CRM_LISTA_ZASOBOW'
  140. and z1.`TYPE` in('STANOWISKO','PODMIOT','DZIAL')
  141. and z2.`TYPE` in('STANOWISKO','PODMIOT','DZIAL')
  142. ";
  143. $res = $this->_db->query($sql);
  144. while ($r = $this->_db->fetch($res)) {
  145. $groups[$r->groupID] = $this->_buildGroupFromRow($r, $fetchNested = false);
  146. }
  147. return $groups;
  148. }
  149. public function getParentGroups(ObjectGroup $group) {
  150. return $this->fetchParentGroups($group->primaryKey);
  151. }
  152. public function fetchParentGroups($idGroup) {
  153. if (!$this->_db) return null;
  154. if ($idGroup <= 0) return null;
  155. $groups = array();
  156. $sql = "SELECT l.`TABLE_1_ID` as groupID
  157. , z1.`ID`
  158. , z1.`DESC`
  159. , z1.`TYPE`
  160. from `ITEM_LINKS` as l
  161. join `CRM_LISTA_ZASOBOW` as z1 on(z1.`ID`=l.`TABLE_1_ID`)
  162. join `CRM_LISTA_ZASOBOW` as z2 on(z2.`ID`=l.`TABLE_2_ID`)
  163. where l.`TABLE_1_ID`>0
  164. and l.`TABLE_2_ID`='{$idGroup}'
  165. and l.`LINKS_TYPE_ID`=5
  166. and l.`TABLE_1_NAME`='CRM_LISTA_ZASOBOW'
  167. and l.`A_STATUS` in('NORMAL')
  168. and l.`TABLE_2_NAME`='CRM_LISTA_ZASOBOW'
  169. and z1.`TYPE` in('STANOWISKO','PODMIOT','DZIAL')
  170. and z2.`TYPE` in('STANOWISKO','PODMIOT','DZIAL')
  171. ";
  172. $res = $this->_db->query($sql);
  173. while ($r = $this->_db->fetch($res)) {
  174. $groups[$r->groupID] = $this->_buildGroupFromRow($r, $fetchNested = false);
  175. }
  176. return $groups;
  177. }
  178. private function _getGroupConnection($idParentGroup, $idGroup) {
  179. if (!$this->_db) return null;
  180. if ($idParentGroup <= 0) return null;
  181. if ($idGroup <= 0) return null;
  182. $sql = "SELECT l.*
  183. from `ITEM_LINKS` as l
  184. join `CRM_LISTA_ZASOBOW` as z1 on(z1.`ID`=l.`TABLE_1_ID`)
  185. join `CRM_LISTA_ZASOBOW` as z2 on(z2.`ID`=l.`TABLE_2_ID`)
  186. where l.`TABLE_1_ID`='{$idParentGroup}'
  187. and l.`TABLE_2_ID`='{$idGroup}'
  188. and l.`LINKS_TYPE_ID`=5
  189. and l.`TABLE_1_NAME`='CRM_LISTA_ZASOBOW'
  190. and l.`TABLE_2_NAME`='CRM_LISTA_ZASOBOW'
  191. and z1.`TYPE` in('STANOWISKO','PODMIOT','DZIAL')
  192. and z2.`TYPE` in('STANOWISKO','PODMIOT','DZIAL')
  193. ";
  194. $res = $this->_db->query($sql);
  195. if ($r = $this->_db->fetch($res)) {
  196. return $r;
  197. }
  198. return null;
  199. }
  200. private function _setGroupConnection($idParentGroup, $idGroup, $errorMsg = null) {
  201. if (!$this->_db) throw new Exception("Error: no DB!");
  202. if ($idGroup <= 0) throw new Exception("Wrong param id group!");
  203. if ($idParentGroup <= 0) throw new Exception("Wrong param id parent group!");
  204. if (!$errorMsg) $errorMsg = "Nie udało się utworzyć połączenia grup [{$idGroup}] i [{$idParentGroup}]";
  205. $tblName = 'CRM_LISTA_ZASOBOW';
  206. Lib::loadClass('ProcesHelper');
  207. $tblZasobyID = ProcesHelper::getZasobTableID($tblName);
  208. if (!$tblZasobyID) throw new Exception("Cannot find zasob id for table Zasoby!");
  209. $connObj = $this->_getGroupConnection($idParentGroup, $idGroup);
  210. if ($connObj) {
  211. $connObj->A_STATUS = 'NORMAL';
  212. $affected = $this->_db->UPDATE_OBJ('ITEM_LINKS', $connObj);
  213. if ($affected <= 0) throw new Exception($errorMsg);
  214. }
  215. else {
  216. $sqlObj = new stdClass();
  217. $sqlObj->TABLE_1_ID = $idParentGroup;
  218. $sqlObj->TABLE_2_ID = $idGroup;
  219. $sqlObj->TABLE_1_NAME = $tblName;
  220. $sqlObj->TABLE_2_NAME = $tblName;
  221. $sqlObj->TABLE_1_ZASOB_ID = $tblZasobyID;
  222. $sqlObj->TABLE_2_ZASOB_ID = $tblZasobyID;
  223. $sqlObj->LINKS_TYPE_ID = 5;// NestedGroups
  224. $sqlObj->A_STATUS = 'NORMAL';
  225. $idCreatedRow = $this->_db->ADD_NEW_OBJ('ITEM_LINKS', $sqlObj);
  226. if ($idCreatedRow <= 0) throw new Exception($errorMsg);
  227. }
  228. }
  229. public function addNestedGroup($idGroup, $idNestedGroup) {
  230. if (!$this->_db) throw new Exception("Error: no DB!");
  231. if ($idGroup <= 0) throw new Exception("Wrong param id group!");
  232. if ($idNestedGroup <= 0) throw new Exception("Wrong param id nested group!");
  233. $errorMsg = "Nie udało się dodać grupy zagnieżdżonej [{$idNestedGroup}] do grupy [{$idGroup}]";
  234. return $this->_setGroupConnection($idGroup, $idNestedGroup, $errorMsg);
  235. }
  236. public function addParentGroup($idGroup, $idParentGroup) {
  237. if (!$this->_db) throw new Exception("Error: no DB!");
  238. if ($idGroup <= 0) throw new Exception("Wrong param id group!");
  239. if ($idParentGroup <= 0) throw new Exception("Wrong param id parent group!");
  240. $errorMsg = "Nie udało się dodać grupy nadrzędnej [{$idParentGroup}] do grupy [{$idGroup}]";
  241. return $this->_setGroupConnection($idParentGroup, $idGroup, $errorMsg);
  242. }
  243. public function removeNestedGroup($idGroup, $idNestedGroup) {
  244. if (!$this->_db) throw new Exception("Error: no DB!");
  245. if ($idGroup <= 0) throw new Exception("Wrong param id group!");
  246. if ($idNestedGroup <= 0) throw new Exception("Wrong param id nested group!");
  247. $connObj = $this->_getGroupConnection($idGroup, $idNestedGroup);
  248. if ($connObj) {
  249. $connObj->A_STATUS = 'DELETED';
  250. $affected = $this->_db->UPDATE_OBJ('ITEM_LINKS', $connObj);
  251. if ($affected <= 0) {
  252. throw new Exception("Nie udało się usunąć grupy zagnieżdżonej '{$kGroupID}' do grupy '{$groupID}' w bazie danych");
  253. }
  254. }
  255. }
  256. public function removeParentGroup($idGroup, $idParentGroup) {
  257. if (!$this->_db) throw new Exception("Error: no DB!");
  258. if ($idGroup <= 0) throw new Exception("Wrong param id group!");
  259. if ($idParentGroup <= 0) throw new Exception("Wrong param id parent group!");
  260. $connObj = $this->_getGroupConnection($idParentGroup, $idGroup);
  261. if ($connObj) {
  262. $connObj->A_STATUS = 'DELETED';
  263. $affected = $this->_db->UPDATE_OBJ('ITEM_LINKS', $connObj);
  264. if ($affected <= 0) {
  265. throw new Exception("Nie udało się usunąć grupy nadrzędnej [{$idParentGroupToRemove}] do grupy [{$idGroup}]");
  266. }
  267. }
  268. }
  269. /**
  270. * @return bool
  271. */
  272. public function isDisabled($usr) {
  273. if (null == $usr->isDisabled) {
  274. // TODO: sql IF(a.`A_STATUS`='NORMAL', 1, 0) as isDisabled
  275. }
  276. return $usr->isDisabled;
  277. }
  278. /**
  279. * @return bool
  280. */
  281. public function setDisabled($usrLogin, $isDisabled) {
  282. if (empty($usrLogin) || null == $isDisabled) {
  283. return false;
  284. }
  285. $sqlStatus = '';
  286. if ($isDisabled) {
  287. $sqlStatus = 'OFF_HARD';
  288. } else {
  289. $sqlStatus = 'NORMAL';
  290. }
  291. $sql = "update `ADMIN_USERS`
  292. set `A_STATUS`='{$sqlStatus}'
  293. where
  294. `ADM_ACCOUNT`='{$usrLogin}'
  295. ";
  296. return false;
  297. }
  298. public function createUser($userData) {
  299. // TODO: insert into `ADMIN_USERS` ...
  300. return false;
  301. }
  302. public function updateUser($usrLogin, $updateData) {
  303. // TODO: update `ADMIN_USERS` set ...
  304. return false;
  305. }
  306. private function _getUserGroupsAll($usrLogin) {
  307. $groups = array();
  308. $sql_select = array();
  309. $sql_select[] = "z.`ID`";
  310. $sql_select[] = "z.`DESC`";
  311. $sql_select[] = "z.`TYPE`";
  312. //$sql_select[] = "z.`OPIS`";
  313. $sql_select[] = "z.`A_LDAP_GID`";
  314. $sql_select = implode(', ', $sql_select);
  315. $sql = "select {$sql_select}
  316. from `CRM_AUTH_PROFILE` as up
  317. join `ADMIN_USERS` as a on(a.`ID`=up.`REMOTE_ID` and up.`REMOTE_TABLE`='ADMIN_USERS')
  318. join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  319. where
  320. a.`ADM_ACCOUNT`='{$usrLogin}'
  321. and up.`A_STATUS` in('WAITING', 'NORMAL')
  322. and z.`TYPE` in('STANOWISKO','PODMIOT')
  323. ";
  324. $res = $this->_db->query($sql);
  325. while ($r = $this->_db->fetch($res)) {
  326. $groups[$r->ID] = $this->_buildGroupFromRow($r);
  327. }
  328. return $groups;
  329. }
  330. private function _getUserGroupsBelow($groups) {// TODO: fetch groups below
  331. if (empty($groups)) return null;
  332. $groupsBelow = array();
  333. $sqlGroupIds = array_keys($groups);
  334. $sql = "
  335. ";
  336. return $groupsBelow;
  337. }
  338. private function _getUserGroupsAbove($groups) {// TODO: fetch groups below
  339. if (empty($groups)) return null;
  340. $groupsAbove = array();
  341. $sqlGroupIds = array_keys($groups);
  342. $sql = "
  343. ";
  344. return $groupsAbove;
  345. }
  346. /**
  347. * Build network group object.
  348. *
  349. * @param object $groupDB {ID, DESC, TYPE} @see _getUserGroupsAll
  350. * @return object $group @see getGroup
  351. *
  352. * Example: _buildGroupFromRow($r) => {@see group}
  353. */
  354. private function _buildGroupFromRow($groupDB, $fetchNested = false) {
  355. $group = new ObjectGroupDB($this);
  356. $group->primaryKey = $groupDB->ID;
  357. $group->type = $groupDB->TYPE;
  358. $group->realName = $this->_buildRealNameFromZasob($groupDB);
  359. $group->zasobID = $groupDB->ID;
  360. $group->zasobDESC = $groupDB->DESC;
  361. if ($fetchNested) $group->nestedGroups = $this->fetchNestedGroups($groupDB->ID);
  362. return $group;
  363. }
  364. /**
  365. * User group list by id.
  366. *
  367. * @param bool $fetchNested - contain all groups below connected groups and group PODMIOT from above.
  368. *
  369. * @return array with group objects @see getGroup
  370. */
  371. public function getUserGroups($usrLogin, $fetchNested = false) {
  372. $usrDB = $this->getUser($usrLogin);
  373. if (!$usrDB) return false;
  374. $groups = array();
  375. if ($usrDB->employeeType == 'Pracownik') {
  376. $groups['workgroup'] = $this->_buildGroupNetwork('workgroup');
  377. $groups['com.apple.access_mail'] = $this->_buildGroupLocal('com.apple.access_mail');
  378. $groups['com.apple.access_addressbook'] = $this->_buildGroupLocal('com.apple.access_addressbook');
  379. $groups['com.apple.access_calendar'] = $this->_buildGroupLocal('com.apple.access_calendar');
  380. $groups['com.apple.access_smb'] = $this->_buildGroupLocal('com.apple.access_smb');
  381. $groups['com.apple.access_afp'] = $this->_buildGroupLocal('com.apple.access_afp');
  382. $groups['com.apple.access_vpn'] = $this->_buildGroupLocal('com.apple.access_vpn');
  383. $groups['com.apple.access_chat'] = $this->_buildGroupLocal('com.apple.access_chat');
  384. } else if ($usrDB->employeeType == 'Partner') {
  385. $groups['com.apple.access_smb'] = $this->_buildGroupLocal('com.apple.access_smb');
  386. $groups['com.apple.access_afp'] = $this->_buildGroupLocal('com.apple.access_afp');
  387. }
  388. $groupsAll = $this->_getUserGroupsAll($usrLogin);
  389. if (is_array($groupsAll) && !empty($groupsAll)) {
  390. foreach ($groupsAll as $kId => $vGroup) {
  391. $groups[$vGroup->zasobID] = $vGroup;
  392. }
  393. if (false) {// TODO: $fetchNested) {
  394. $groupsBelow = $this->_getUserGroupsBelow($groupsAll);
  395. if (is_array($groupsBelow) && !empty($groupsBelow)) {
  396. foreach ($groupsBelow as $kId => $vGroup) {
  397. if (!isset($groups[$vGroup->zasobID])) {
  398. $groups[$vGroup->zasobID] = $vGroup;
  399. }
  400. }
  401. }
  402. $groupsAbove = $this->_getUserGroupsAbove($groupsAll);
  403. if (is_array($groupsAbove) && !empty($groupsAbove)) {
  404. foreach ($groupsAbove as $kId => $vGroup) {
  405. if (!isset($groups[$vGroup->zasobID])) {
  406. $groups[$vGroup->zasobID] = $vGroup;
  407. }
  408. }
  409. }
  410. }
  411. }
  412. return $groups;
  413. }
  414. /**
  415. * Add group member.
  416. *
  417. * @param string $usrLogin - user login
  418. * @param object $group - @see getGroup
  419. * @param optional int $telboxID
  420. * @return bool
  421. */
  422. public function addUserGroup($usrLogin, $group, $telboxID = 0) {
  423. if (!$group->zasobID) throw new Exception("Group has no id zasob!");
  424. $usrDB = $this->getUser($usrLogin);
  425. if (!$usrDB) throw new Exception("User '{$usrLogin}' not found");
  426. $sqlObj = new stdClass();
  427. $sqlObj->ID_ZASOB = $group->zasobID;
  428. $sqlObj->REMOTE_TABLE = 'ADMIN_USERS';
  429. $sqlObj->REMOTE_ID = $usrDB->primaryKey;
  430. $sqlObj->T_TELBOX_NEIGHBOUR_IN_ID = $telboxID;
  431. $sqlObj->A_STATUS = 'NORMAL';
  432. // uniq key: (ID_ZASOB, REMOTE_ID, REMOTE_TABLE, T_TELBOX_NEIGHBOUR_IN_ID)
  433. $sql = "select `ID`
  434. from `CRM_AUTH_PROFILE`
  435. where `ID_ZASOB`='{$sqlObj->ID_ZASOB}'
  436. and `REMOTE_TABLE`='{$sqlObj->REMOTE_TABLE}'
  437. and `REMOTE_ID`='{$sqlObj->REMOTE_ID}'
  438. and `T_TELBOX_NEIGHBOUR_IN_ID`='{$sqlObj->T_TELBOX_NEIGHBOUR_IN_ID}'
  439. ";
  440. $db = DB::getDB();
  441. $res = $db->query($sql);
  442. if ($r = $db->fetch($res)) {
  443. $sqlObj->ID = $r->ID;
  444. $affected = $this->_db->UPDATE_OBJ('CRM_AUTH_PROFILE', $sqlObj);
  445. if ($affected <= 0) throw new Exception("Nie udało się przypisać grupy (błąd podczas aktualizacji rekordu)");
  446. }
  447. else {
  448. $rowID = $this->_db->ADD_NEW_OBJ('CRM_AUTH_PROFILE', $sqlObj);
  449. if ($rowID <= 0) throw new Exception("Nie udało się przypisać grupy (błąd podczas tworzenia rekordu)");
  450. }
  451. }
  452. /**
  453. * Get user and group info by profile ID (CRM_AUTH_PROFILE.ID)
  454. * Only in UserStorageDB
  455. *
  456. * @return profile {}
  457. * ID
  458. * usrId
  459. * usrLogin
  460. * group - @see getGroup
  461. */
  462. public function getProfileById($profileID) {
  463. if (!$profileID) return false;
  464. $profile = null;
  465. $sql_select = array();
  466. $sql_select[] = "z.`ID`";
  467. $sql_select[] = "z.`DESC`";
  468. $sql_select[] = "z.`TYPE`";
  469. //$sql_select[] = "z.`OPIS`";
  470. $sql_select[] = "z.`A_LDAP_GID`";
  471. $sql_select[] = "up.`ID` as profileId";
  472. $sql_select[] = "up.`T_TELBOX_NEIGHBOUR_IN_ID` as localisationId";
  473. $sql_select[] = "a.`ADM_ACCOUNT` as usrLogin";
  474. $sql_select[] = "a.`ID` as usrId";
  475. $sql_select = implode(', ', $sql_select);
  476. $sql = "select {$sql_select}
  477. from `CRM_AUTH_PROFILE` as up
  478. join `ADMIN_USERS` as a on(a.`ID`=up.`REMOTE_ID` and up.`REMOTE_TABLE`='ADMIN_USERS')
  479. join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  480. where
  481. up.`ID`='{$profileID}'
  482. and up.`A_STATUS` in('WAITING', 'NORMAL')
  483. and z.`TYPE` in('STANOWISKO','PODMIOT')
  484. ";
  485. $res = $this->_db->query($sql);
  486. if ($r = $this->_db->fetch($res)) {
  487. $profile = new stdClass();
  488. $profile->profileId = $r->profileId;
  489. $profile->localisationId = $r->localisationId;
  490. $profile->usrId = $r->usrId;
  491. $profile->usrLogin = $r->usrLogin;
  492. $profile->group = $this->_buildGroupFromRow($r);
  493. }
  494. return $profile;
  495. }
  496. /**
  497. * Get user and group info by profile ID (CRM_AUTH_PROFILE.ID)
  498. * Only in UserStorageDB
  499. *
  500. * @return array of profile {}
  501. * ID
  502. * usrId
  503. * usrLogin
  504. * group - @see getGroup
  505. */
  506. public function getUserProfiles($usrLogin) {
  507. if (!$usrLogin) return false;
  508. $profiles = array();
  509. $sql_select = array();
  510. $sql_select[] = "z.`ID`";
  511. $sql_select[] = "z.`DESC`";
  512. $sql_select[] = "z.`TYPE`";
  513. //$sql_select[] = "z.`OPIS`";
  514. $sql_select[] = "z.`A_LDAP_GID`";
  515. $sql_select[] = "up.`ID` as profileId";
  516. $sql_select[] = "up.`T_TELBOX_NEIGHBOUR_IN_ID` as localisationId";
  517. $sql_select[] = "a.`ADM_ACCOUNT` as usrLogin";
  518. $sql_select[] = "a.`ID` as usrId";
  519. $sql_select = implode(', ', $sql_select);
  520. $sql = "select {$sql_select}
  521. from `CRM_AUTH_PROFILE` as up
  522. join `ADMIN_USERS` as a on(a.`ID`=up.`REMOTE_ID` and up.`REMOTE_TABLE`='ADMIN_USERS')
  523. join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  524. where
  525. a.`ADM_ACCOUNT`='{$usrLogin}'
  526. and up.`A_STATUS` in('WAITING', 'NORMAL')
  527. and z.`TYPE` in('STANOWISKO','PODMIOT')
  528. ";
  529. $res = $this->_db->query($sql);
  530. while ($r = $this->_db->fetch($res)) {
  531. $profile = new stdClass();
  532. $profile->profileId = $r->profileId;
  533. $profile->localisationId = $r->localisationId;
  534. $profile->usrId = $r->usrId;
  535. $profile->usrLogin = $r->usrLogin;
  536. $profile->group = $this->_buildGroupFromRow($r);
  537. $profiles[] = $profile;
  538. }
  539. return $profiles;
  540. }
  541. /**
  542. * Remove user group by profile ID (CRM_AUTH_PROFILE.ID)
  543. * Only in UserStorageDB
  544. */
  545. public function removeUserGroupByProfileId($usrLogin, $group, $profileID) {
  546. if (!$this->_db) return false;
  547. if (!$usrLogin || !$profileID || !$group || !$group->zasobID) return false;
  548. $usrDB = $this->getUser($usrLogin);
  549. if (!$usrDB) return false;
  550. $sql = "delete from `CRM_AUTH_PROFILE`
  551. where
  552. `ID_ZASOB`='{$group->zasobID}'
  553. and `REMOTE_ID`='{$usrDB->primaryKey}'
  554. and `REMOTE_TABLE`='ADMIN_USERS'
  555. and `ID`='{$profileID}'
  556. ";
  557. $res = $this->_db->query($sql);
  558. return true;
  559. }
  560. public function setSyncUserDate($usrLogin) {
  561. if (!$this->_db) return false;
  562. $sql = "update `ADMIN_USERS` set `A_SYNC_LDAP_DATE`=NOW() where `ADM_ACCOUNT`='{$usrLogin}' ";
  563. $res = $this->_db->query($sql);
  564. }
  565. public function setSyncGroupDate($idGroup) {
  566. if (!$this->_db) return false;
  567. if ($idGroup <= 0) return false;
  568. $sql = "update `CRM_LISTA_ZASOBOW` set `A_SYNC_LDAP_DATE`=NOW() where `ID`='{$idGroup}' ";
  569. $res = $this->_db->query($sql);
  570. }
  571. public function isPasswordChanged($usrLogin) {
  572. $cnt = 0;
  573. $sql = "
  574. SELECT
  575. -- h.`ADM_PASSWD`,
  576. count(1) as cnt
  577. FROM `ADMIN_USERS` as u
  578. JOIN `ADMIN_USERS_HIST` as h on(h.`ID_USERS2`=u.`ID`)
  579. WHERE u.`ADM_ACCOUNT`='{$usrLogin}'
  580. AND h.`A_RECORD_CREATE_DATE`>u.`A_SYNC_LDAP_DATE`
  581. AND h.`ADM_PASSWD`!='N/S;'
  582. AND h.`ADM_PASSWD`!=''
  583. -- GROUP BY h.`ADM_PASSWD`
  584. ";
  585. $res = $this->_db->query($sql);
  586. if ($r = $this->_db->fetch($res)) {
  587. $cnt = $r->cnt;
  588. }
  589. return ($cnt > 0);
  590. }
  591. }