UserStorageDB.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 = $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)? 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($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 <= 0) 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. } else if ($usrDB->employeeType == 'Partner') {
  388. $groups['com.apple.access_smb'] = $this->_buildGroupLocal('com.apple.access_smb');
  389. $groups['com.apple.access_afp'] = $this->_buildGroupLocal('com.apple.access_afp');
  390. }
  391. $groupsAll = $this->_getUserGroupsAll($usrLogin);
  392. if (is_array($groupsAll) && !empty($groupsAll)) {
  393. foreach ($groupsAll as $kId => $vGroup) {
  394. $groups[$vGroup->zasobID] = $vGroup;
  395. }
  396. if (false) {// TODO: $fetchNested) {
  397. $groupsBelow = $this->_getUserGroupsBelow($groupsAll);
  398. if (is_array($groupsBelow) && !empty($groupsBelow)) {
  399. foreach ($groupsBelow as $kId => $vGroup) {
  400. if (!isset($groups[$vGroup->zasobID])) {
  401. $groups[$vGroup->zasobID] = $vGroup;
  402. }
  403. }
  404. }
  405. $groupsAbove = $this->_getUserGroupsAbove($groupsAll);
  406. if (is_array($groupsAbove) && !empty($groupsAbove)) {
  407. foreach ($groupsAbove as $kId => $vGroup) {
  408. if (!isset($groups[$vGroup->zasobID])) {
  409. $groups[$vGroup->zasobID] = $vGroup;
  410. }
  411. }
  412. }
  413. }
  414. }
  415. return $groups;
  416. }
  417. /**
  418. * Add group member.
  419. *
  420. * @param string $usrLogin - user login
  421. * @param object $group - @see getGroup
  422. * @param optional int $telboxID
  423. * @return bool
  424. */
  425. public function addUserGroup($usrLogin, $group, $telboxID = 0) {
  426. if (!$group->zasobID) return false;
  427. $usrDB = $this->getUser($usrLogin);
  428. if (!$usrDB) return false;
  429. $sqlObj = new stdClass();
  430. $sqlObj->ID_ZASOB = $group->zasobID;
  431. $sqlObj->REMOTE_TABLE = 'ADMIN_USERS';
  432. $sqlObj->REMOTE_ID = $usrDB->primaryKey;
  433. $sqlObj->T_TELBOX_NEIGHBOUR_IN_ID = $telboxID;
  434. $sqlObj->A_STATUS = 'NORMAL';
  435. // uniq key: (ID_ZASOB, REMOTE_ID, REMOTE_TABLE, T_TELBOX_NEIGHBOUR_IN_ID)
  436. $sql = "select `ID`
  437. from `CRM_AUTH_PROFILE`
  438. where `ID_ZASOB`='{$sqlObj->ID_ZASOB}'
  439. and `REMOTE_TABLE`='{$sqlObj->REMOTE_TABLE}'
  440. and `REMOTE_ID`='{$sqlObj->REMOTE_ID}'
  441. and `T_TELBOX_NEIGHBOUR_IN_ID`='{$sqlObj->T_TELBOX_NEIGHBOUR_IN_ID}'
  442. ";
  443. $db = DB::getDB();
  444. $res = $db->query($sql);
  445. if ($r = $db->fetch($res)) {
  446. $sqlObj->ID = $r->ID;
  447. $affected = $this->_db->UPDATE_OBJ('CRM_AUTH_PROFILE', $sqlObj);
  448. if ($affected > 0) {
  449. return true;
  450. }
  451. }
  452. else {
  453. $rowID = $this->_db->ADD_NEW_OBJ('CRM_AUTH_PROFILE', $sqlObj);
  454. if ($rowID > 0) {
  455. return true;
  456. }
  457. }
  458. return false;
  459. }
  460. /**
  461. * Get user and group info by profile ID (CRM_AUTH_PROFILE.ID)
  462. * Only in UserStorageDB
  463. *
  464. * @return profile {}
  465. * ID
  466. * usrId
  467. * usrLogin
  468. * group - @see getGroup
  469. */
  470. public function getProfileById($profileID) {
  471. if (!$profileID) return false;
  472. $profile = null;
  473. $sql_select = array();
  474. $sql_select[] = "z.`ID`";
  475. $sql_select[] = "z.`DESC`";
  476. $sql_select[] = "z.`TYPE`";
  477. //$sql_select[] = "z.`OPIS`";
  478. $sql_select[] = "z.`A_LDAP_GID`";
  479. $sql_select[] = "up.`ID` as profileId";
  480. $sql_select[] = "up.`T_TELBOX_NEIGHBOUR_IN_ID` as localisationId";
  481. $sql_select[] = "a.`ADM_ACCOUNT` as usrLogin";
  482. $sql_select[] = "a.`ID` as usrId";
  483. $sql_select = implode(', ', $sql_select);
  484. $sql = "select {$sql_select}
  485. from `CRM_AUTH_PROFILE` as up
  486. join `ADMIN_USERS` as a on(a.`ID`=up.`REMOTE_ID` and up.`REMOTE_TABLE`='ADMIN_USERS')
  487. join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  488. where
  489. up.`ID`='{$profileID}'
  490. and up.`A_STATUS` in('WAITING', 'NORMAL')
  491. and z.`TYPE` in('STANOWISKO','PODMIOT')
  492. ";
  493. $res = $this->_db->query($sql);
  494. if ($r = $this->_db->fetch($res)) {
  495. $profile = new stdClass();
  496. $profile->profileId = $r->profileId;
  497. $profile->localisationId = $r->localisationId;
  498. $profile->usrId = $r->usrId;
  499. $profile->usrLogin = $r->usrLogin;
  500. $profile->group = $this->_buildGroupFromRow($r);
  501. }
  502. return $profile;
  503. }
  504. /**
  505. * Get user and group info by profile ID (CRM_AUTH_PROFILE.ID)
  506. * Only in UserStorageDB
  507. *
  508. * @return array of profile {}
  509. * ID
  510. * usrId
  511. * usrLogin
  512. * group - @see getGroup
  513. */
  514. public function getUserProfiles($usrLogin) {
  515. if (!$usrLogin) return false;
  516. $profiles = array();
  517. $sql_select = array();
  518. $sql_select[] = "z.`ID`";
  519. $sql_select[] = "z.`DESC`";
  520. $sql_select[] = "z.`TYPE`";
  521. //$sql_select[] = "z.`OPIS`";
  522. $sql_select[] = "z.`A_LDAP_GID`";
  523. $sql_select[] = "up.`ID` as profileId";
  524. $sql_select[] = "up.`T_TELBOX_NEIGHBOUR_IN_ID` as localisationId";
  525. $sql_select[] = "a.`ADM_ACCOUNT` as usrLogin";
  526. $sql_select[] = "a.`ID` as usrId";
  527. $sql_select = implode(', ', $sql_select);
  528. $sql = "select {$sql_select}
  529. from `CRM_AUTH_PROFILE` as up
  530. join `ADMIN_USERS` as a on(a.`ID`=up.`REMOTE_ID` and up.`REMOTE_TABLE`='ADMIN_USERS')
  531. join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  532. where
  533. a.`ADM_ACCOUNT`='{$usrLogin}'
  534. and up.`A_STATUS` in('WAITING', 'NORMAL')
  535. and z.`TYPE` in('STANOWISKO','PODMIOT')
  536. ";
  537. $res = $this->_db->query($sql);
  538. while ($r = $this->_db->fetch($res)) {
  539. $profile = new stdClass();
  540. $profile->profileId = $r->profileId;
  541. $profile->localisationId = $r->localisationId;
  542. $profile->usrId = $r->usrId;
  543. $profile->usrLogin = $r->usrLogin;
  544. $profile->group = $this->_buildGroupFromRow($r);
  545. $profiles[] = $profile;
  546. }
  547. return $profiles;
  548. }
  549. /**
  550. * Remove user group by profile ID (CRM_AUTH_PROFILE.ID)
  551. * Only in UserStorageDB
  552. */
  553. public function removeUserGroupByProfileId($usrLogin, $group, $profileID) {
  554. if (!$this->_db) return false;
  555. if (!$usrLogin || !$profileID || !$group || !$group->zasobID) return false;
  556. $usrDB = $this->getUser($usrLogin);
  557. if (!$usrDB) return false;
  558. $sql = "delete from `CRM_AUTH_PROFILE`
  559. where
  560. `ID_ZASOB`='{$group->zasobID}'
  561. and `REMOTE_ID`='{$usrDB->primaryKey}'
  562. and `REMOTE_TABLE`='ADMIN_USERS'
  563. and `ID`='{$profileID}'
  564. ";
  565. $res = $this->_db->query($sql);
  566. return true;
  567. }
  568. public function setSyncUserDate($usrLogin) {
  569. if (!$this->_db) return false;
  570. $sql = "update `ADMIN_USERS` set `A_SYNC_LDAP_DATE`=NOW() where `ADM_ACCOUNT`='{$usrLogin}' ";
  571. $res = $this->_db->query($sql);
  572. }
  573. public function setSyncGroupDate($groupID) {
  574. if (!$this->_db) return false;
  575. if ($groupID <= 0) return false;
  576. $sql = "update `CRM_LISTA_ZASOBOW` set `A_SYNC_LDAP_DATE`=NOW() where `ID`='{$groupID}' ";
  577. $res = $this->_db->query($sql);
  578. }
  579. public function isPasswordChanged($usrLogin) {
  580. $cnt = 0;
  581. $sql = "
  582. SELECT
  583. -- h.`ADM_PASSWD`,
  584. count(1) as cnt
  585. FROM `ADMIN_USERS` as u
  586. JOIN `ADMIN_USERS_HIST` as h on(h.`ID_USERS2`=u.`ID`)
  587. WHERE u.`ADM_ACCOUNT`='{$usrLogin}'
  588. AND h.`A_RECORD_CREATE_DATE`>u.`A_SYNC_LDAP_DATE`
  589. AND h.`ADM_PASSWD`!='N/S;'
  590. AND h.`ADM_PASSWD`!=''
  591. -- GROUP BY h.`ADM_PASSWD`
  592. ";
  593. $res = $this->_db->query($sql);
  594. if ($r = $this->_db->fetch($res)) {
  595. $cnt = $r->cnt;
  596. }
  597. return ($cnt > 0);
  598. }
  599. }