UserStorageDB.php 17 KB

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