UserStorageDB.php 21 KB

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