UserStorageDB.php 20 KB

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