UserStorageDB.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 1, 0 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 = $r->name;
  56. $user->email = $r->email;
  57. $user->phone = $r->phone;
  58. $user->homeEmail = $r->homeEmail;
  59. $user->homePhone = $r->homePhone;
  60. $user->employeeType = $r->employeeType;
  61. $user->isDisabled = (int)$r->isDisabled;
  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($groupID) {
  88. if (!$this->_db) return false;
  89. if ($groupID <= 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`='{$groupID}'
  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($groupID) {
  106. if (!$this->_db) return false;
  107. if ($groupID <= 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`='{$groupID}'
  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($groupID) {
  124. if (!$this->_db) return null;
  125. if ($groupID <= 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`='{$groupID}'
  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($groupID) {
  153. if (!$this->_db) return null;
  154. if ($groupID <= 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`='{$groupID}'
  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($parentGroupID, $groupID) {
  179. if (!$this->_db) return null;
  180. if ($parentGroupID <= 0) return null;
  181. if ($groupID <= 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`='{$parentGroupID}'
  187. and l.`TABLE_2_ID`='{$groupID}'
  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($parentGroupID, $groupID) {
  201. if (!$this->_db) return null;
  202. if ($groupID <= 0 || $parentGroupID <= 0) return null;
  203. $tblName = 'CRM_LISTA_ZASOBOW';
  204. Lib::loadClass('ProcesHelper');
  205. $tblZasobyID = ProcesHelper::getZasobTableID($tblName);
  206. if (!$tblZasobyID) return false;
  207. $connObj = $this->_getGroupConnection($parentGroupID, $groupID);
  208. if ($connObj) {
  209. $connObj->A_STATUS = 'NORMAL';
  210. $affected = $this->_db->UPDATE_OBJ('ITEM_LINKS', $connObj);
  211. if ($affected > 0) {
  212. return true;
  213. }
  214. }
  215. else {
  216. $sqlObj = new stdClass();
  217. $sqlObj->TABLE_1_ID = $parentGroupID;
  218. $sqlObj->TABLE_2_ID = $groupID;
  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. $rowID = $this->_db->ADD_NEW_OBJ('ITEM_LINKS', $sqlObj);
  226. if ($rowID > 0) {
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. public function addNestedGroup($groupID, $nestedGroupID) {
  233. if (!$this->_db) return null;
  234. if ($groupID <= 0) return null;
  235. if ($nestedGroupID) return null;
  236. return $this->_setGroupConnection($groupID, $nestedGroupID);
  237. }
  238. public function addParentGroup($groupID, $parentGroupID) {
  239. if (!$this->_db) return null;
  240. if ($groupID <= 0) return null;
  241. if ($parentGroupID <= 0) return null;
  242. return $this->_setGroupConnection($parentGroupID, $groupID);
  243. }
  244. public function removeNestedGroup($groupID, $nestedGroupID) {
  245. if (!$this->_db) return null;
  246. if ($groupID <= 0) return null;
  247. if ($nestedGroupID <= 0) return null;
  248. $connObj = $this->_getGroupConnection($groupID, $nestedGroupID);
  249. if ($connObj) {
  250. $connObj->A_STATUS = 'DELETED';
  251. $affected = $this->_db->UPDATE_OBJ('ITEM_LINKS', $connObj);
  252. if ($affected > 0) {
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. public function removeParentGroup($groupID, $parentGroupID) {
  259. if (!$this->_db) return null;
  260. if ($groupID <= 0) return null;
  261. if ($parentGroupID <= 0) return null;
  262. $connObj = $this->_getGroupConnection($parentGroupID, $groupID);
  263. if ($connObj) {
  264. $connObj->A_STATUS = 'DELETED';
  265. $affected = $this->_db->UPDATE_OBJ('ITEM_LINKS', $connObj);
  266. if ($affected > 0) {
  267. return true;
  268. }
  269. }
  270. return false;
  271. }
  272. /**
  273. * @return bool
  274. */
  275. public function isDisabled($usr) {
  276. if (null == $usr->isDisabled) {
  277. // TODO: sql IF(a.`A_STATUS`='NORMAL', 1, 0) as isDisabled
  278. }
  279. return $usr->isDisabled;
  280. }
  281. /**
  282. * @return bool
  283. */
  284. public function setDisabled($usrLogin, $isDisabled) {
  285. if (empty($usrLogin) || null == $isDisabled) {
  286. return false;
  287. }
  288. $sqlStatus = '';
  289. if ($isDisabled) {
  290. $sqlStatus = 'OFF_HARD';
  291. } else {
  292. $sqlStatus = 'NORMAL';
  293. }
  294. $sql = "update `ADMIN_USERS`
  295. set `A_STATUS`='{$sqlStatus}'
  296. where
  297. `ADM_ACCOUNT`='{$usrLogin}'
  298. ";
  299. return false;
  300. }
  301. public function createUser($userData) {
  302. // TODO: insert into `ADMIN_USERS` ...
  303. return false;
  304. }
  305. public function updateUser($usrLogin, $updateData) {
  306. // TODO: update `ADMIN_USERS` set ...
  307. return false;
  308. }
  309. private function _getUserGroupsAll($usrLogin) {
  310. $groups = array();
  311. $sql_select = array();
  312. $sql_select[] = "z.`ID`";
  313. $sql_select[] = "z.`DESC`";
  314. $sql_select[] = "z.`TYPE`";
  315. //$sql_select[] = "z.`OPIS`";
  316. $sql_select[] = "z.`A_LDAP_GID`";
  317. $sql_select = implode(', ', $sql_select);
  318. $sql = "select {$sql_select}
  319. from `CRM_AUTH_PROFILE` as up
  320. join `ADMIN_USERS` as a on(a.`ID`=up.`REMOTE_ID` and up.`REMOTE_TABLE`='ADMIN_USERS')
  321. join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  322. where
  323. a.`ADM_ACCOUNT`='{$usrLogin}'
  324. and up.`A_STATUS` in('WAITING', 'NORMAL')
  325. and z.`TYPE` in('STANOWISKO','PODMIOT')
  326. ";
  327. $res = $this->_db->query($sql);
  328. while ($r = $this->_db->fetch($res)) {
  329. $groups[$r->ID] = $this->_buildGroupFromRow($r);
  330. }
  331. return $groups;
  332. }
  333. private function _getUserGroupsBelow($groups) {// TODO: fetch groups below
  334. if (empty($groups)) return null;
  335. $groupsBelow = array();
  336. $sqlGroupIds = array_keys($groups);
  337. $sql = "
  338. ";
  339. return $groupsBelow;
  340. }
  341. private function _getUserGroupsAbove($groups) {// TODO: fetch groups below
  342. if (empty($groups)) return null;
  343. $groupsAbove = array();
  344. $sqlGroupIds = array_keys($groups);
  345. $sql = "
  346. ";
  347. return $groupsAbove;
  348. }
  349. /**
  350. * Build network group object.
  351. *
  352. * @param object $groupDB {ID, DESC, TYPE} @see _getUserGroupsAll
  353. * @return object $group @see getGroup
  354. *
  355. * Example: _buildGroupFromRow($r) => {@see group}
  356. */
  357. private function _buildGroupFromRow($groupDB, $fetchNested = false) {
  358. $group = new ObjectGroupDB($this);
  359. $group->primaryKey = $groupDB->ID;
  360. $group->type = $groupDB->TYPE;
  361. $group->realName = $this->_buildRealNameFromZasob($groupDB);
  362. $group->zasobID = $groupDB->ID;
  363. $group->zasobDESC = $groupDB->DESC;
  364. if ($fetchNested) $group->nestedGroups = $this->fetchNestedGroups($groupDB->ID);
  365. return $group;
  366. }
  367. /**
  368. * User group list by id.
  369. *
  370. * @param bool $fetchNested - contain all groups below connected groups and group PODMIOT from above.
  371. *
  372. * @return array with group objects @see getGroup
  373. */
  374. public function getUserGroups($usrLogin, $fetchNested = false) {
  375. $usrDB = $this->getUser($usrLogin);
  376. if (!$usrDB) return false;
  377. $groups = array();
  378. if ($usrDB->employeeType == 'Pracownik') {
  379. $groups['workgroup'] = $this->_buildGroupNetwork('workgroup');
  380. $groups['com.apple.access_mail'] = $this->_buildGroupLocal('com.apple.access_mail');
  381. $groups['com.apple.access_addressbook'] = $this->_buildGroupLocal('com.apple.access_addressbook');
  382. $groups['com.apple.access_calendar'] = $this->_buildGroupLocal('com.apple.access_calendar');
  383. $groups['com.apple.access_smb'] = $this->_buildGroupLocal('com.apple.access_smb');
  384. $groups['com.apple.access_afp'] = $this->_buildGroupLocal('com.apple.access_afp');
  385. $groups['com.apple.access_vpn'] = $this->_buildGroupLocal('com.apple.access_vpn');
  386. $groups['com.apple.access_chat'] = $this->_buildGroupLocal('com.apple.access_chat');
  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) return false;
  424. $usrDB = $this->getUser($usrLogin);
  425. if (!$usrDB) return false;
  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. $rowID = $this->_db->ADD_NEW_OBJ('CRM_AUTH_PROFILE', $sqlObj);
  432. if ($rowID > 0) {
  433. return true;
  434. }
  435. return false;
  436. }
  437. /**
  438. * Get user and group info by profile ID (CRM_AUTH_PROFILE.ID)
  439. * Only in UserStorageDB
  440. *
  441. * @return profile {}
  442. * ID
  443. * usrId
  444. * usrLogin
  445. * group - @see getGroup
  446. */
  447. public function getProfileById($profileID) {
  448. if (!$profileID) return false;
  449. $profile = null;
  450. $sql_select = array();
  451. $sql_select[] = "z.`ID`";
  452. $sql_select[] = "z.`DESC`";
  453. $sql_select[] = "z.`TYPE`";
  454. //$sql_select[] = "z.`OPIS`";
  455. $sql_select[] = "z.`A_LDAP_GID`";
  456. $sql_select[] = "up.`ID` as profileId";
  457. $sql_select[] = "up.`T_TELBOX_NEIGHBOUR_IN_ID` as localisationId";
  458. $sql_select[] = "a.`ADM_ACCOUNT` as usrLogin";
  459. $sql_select[] = "a.`ID` as usrId";
  460. $sql_select = implode(', ', $sql_select);
  461. $sql = "select {$sql_select}
  462. from `CRM_AUTH_PROFILE` as up
  463. join `ADMIN_USERS` as a on(a.`ID`=up.`REMOTE_ID` and up.`REMOTE_TABLE`='ADMIN_USERS')
  464. join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  465. where
  466. up.`ID`='{$profileID}'
  467. and up.`A_STATUS` in('WAITING', 'NORMAL')
  468. and z.`TYPE` in('STANOWISKO','PODMIOT')
  469. ";
  470. $res = $this->_db->query($sql);
  471. if ($r = $this->_db->fetch($res)) {
  472. $profile = new stdClass();
  473. $profile->profileId = $r->profileId;
  474. $profile->localisationId = $r->localisationId;
  475. $profile->usrId = $r->usrId;
  476. $profile->usrLogin = $r->usrLogin;
  477. $profile->group = $this->_buildGroupFromRow($r);
  478. }
  479. return $profile;
  480. }
  481. /**
  482. * Get user and group info by profile ID (CRM_AUTH_PROFILE.ID)
  483. * Only in UserStorageDB
  484. *
  485. * @return array of profile {}
  486. * ID
  487. * usrId
  488. * usrLogin
  489. * group - @see getGroup
  490. */
  491. public function getUserProfiles($usrLogin) {
  492. if (!$usrLogin) return false;
  493. $profiles = array();
  494. $sql_select = array();
  495. $sql_select[] = "z.`ID`";
  496. $sql_select[] = "z.`DESC`";
  497. $sql_select[] = "z.`TYPE`";
  498. //$sql_select[] = "z.`OPIS`";
  499. $sql_select[] = "z.`A_LDAP_GID`";
  500. $sql_select[] = "up.`ID` as profileId";
  501. $sql_select[] = "up.`T_TELBOX_NEIGHBOUR_IN_ID` as localisationId";
  502. $sql_select[] = "a.`ADM_ACCOUNT` as usrLogin";
  503. $sql_select[] = "a.`ID` as usrId";
  504. $sql_select = implode(', ', $sql_select);
  505. $sql = "select {$sql_select}
  506. from `CRM_AUTH_PROFILE` as up
  507. join `ADMIN_USERS` as a on(a.`ID`=up.`REMOTE_ID` and up.`REMOTE_TABLE`='ADMIN_USERS')
  508. join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  509. where
  510. a.`ADM_ACCOUNT`='{$usrLogin}'
  511. and up.`A_STATUS` in('WAITING', 'NORMAL')
  512. and z.`TYPE` in('STANOWISKO','PODMIOT')
  513. ";
  514. $res = $this->_db->query($sql);
  515. while ($r = $this->_db->fetch($res)) {
  516. $profile = new stdClass();
  517. $profile->profileId = $r->profileId;
  518. $profile->localisationId = $r->localisationId;
  519. $profile->usrId = $r->usrId;
  520. $profile->usrLogin = $r->usrLogin;
  521. $profile->group = $this->_buildGroupFromRow($r);
  522. $profiles[] = $profile;
  523. }
  524. return $profiles;
  525. }
  526. /**
  527. * Remove user group by profile ID (CRM_AUTH_PROFILE.ID)
  528. * Only in UserStorageDB
  529. */
  530. public function removeUserGroupByProfileId($usrLogin, $group, $profileID) {
  531. if (!$this->_db) return false;
  532. if (!$usrLogin || !$profileID || !$group || !$group->zasobID) return false;
  533. $usrDB = $this->getUser($usrLogin);
  534. if (!$usrDB) return false;
  535. $sql = "delete from `CRM_AUTH_PROFILE`
  536. where
  537. `ID_ZASOB`='{$group->zasobID}'
  538. and `REMOTE_ID`='{$usrDB->primaryKey}'
  539. and `REMOTE_TABLE`='ADMIN_USERS'
  540. and `ID`='{$profileID}'
  541. ";
  542. $res = $this->_db->query($sql);
  543. return true;
  544. }
  545. public function setSyncUserDate($usrLogin) {
  546. if (!$this->_db) return false;
  547. $sql = "update `ADMIN_USERS` set `A_SYNC_LDAP_DATE`=NOW() where `ADM_ACCOUNT`='{$usrLogin}' ";
  548. $res = $this->_db->query($sql);
  549. }
  550. public function setSyncGroupDate($groupID) {
  551. if (!$this->_db) return false;
  552. if ($groupID <= 0) return false;
  553. $sql = "update `CRM_LISTA_ZASOBOW` set `A_SYNC_LDAP_DATE`=NOW() where `ID`='{$groupID}' ";
  554. $res = $this->_db->query($sql);
  555. }
  556. public function isPasswordChanged($usrLogin) {
  557. $cnt = 0;
  558. $sql = "
  559. SELECT
  560. -- h.`ADM_PASSWD`,
  561. count(1) as cnt
  562. FROM `ADMIN_USERS` as u
  563. JOIN `ADMIN_USERS_HIST` as h on(h.`ID_USERS2`=u.`ID`)
  564. WHERE u.`ADM_ACCOUNT`='{$usrLogin}'
  565. AND h.`A_RECORD_CREATE_DATE`>u.`A_SYNC_LDAP_DATE`
  566. AND h.`ADM_PASSWD`!='N/S;'
  567. AND h.`ADM_PASSWD`!=''
  568. -- GROUP BY h.`ADM_PASSWD`
  569. ";
  570. $res = $this->_db->query($sql);
  571. if ($r = $this->_db->fetch($res)) {
  572. $cnt = $r->cnt;
  573. }
  574. return ($cnt > 0);
  575. }
  576. }