UserStorageDB.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. $sql = "select `ID`
  448. from `CRM_AUTH_PROFILE`
  449. where `ID_ZASOB`='{$sqlObj->ID_ZASOB}'
  450. and `REMOTE_TABLE`='{$sqlObj->REMOTE_TABLE}'
  451. and `REMOTE_ID`='{$sqlObj->REMOTE_ID}'
  452. and `T_TELBOX_NEIGHBOUR_IN_ID`='{$sqlObj->T_TELBOX_NEIGHBOUR_IN_ID}'
  453. ";
  454. $db = DB::getDB();
  455. $res = $db->query($sql);
  456. if ($r = $db->fetch($res)) {
  457. $sqlObj->ID = $r->ID;
  458. $affected = $this->_db->UPDATE_OBJ('CRM_AUTH_PROFILE', $sqlObj);
  459. if ($affected <= 0) throw new Exception("Nie udało się przypisać grupy (błąd podczas aktualizacji rekordu)");
  460. }
  461. else {
  462. $rowID = $this->_db->ADD_NEW_OBJ('CRM_AUTH_PROFILE', $sqlObj);
  463. if ($rowID <= 0) throw new Exception("Nie udało się przypisać grupy (błąd podczas tworzenia rekordu)");
  464. }
  465. }
  466. /**
  467. * Get user and group info by profile ID (CRM_AUTH_PROFILE.ID)
  468. * Only in UserStorageDB
  469. *
  470. * @return profile {}
  471. * ID
  472. * usrId
  473. * usrLogin
  474. * group - @see getGroup
  475. */
  476. public function getProfileById($profileID) {
  477. if (!$profileID) return false;
  478. $profile = null;
  479. $sql_select = array();
  480. $sql_select[] = "z.`ID`";
  481. $sql_select[] = "z.`DESC`";
  482. $sql_select[] = "z.`TYPE`";
  483. //$sql_select[] = "z.`OPIS`";
  484. $sql_select[] = "z.`A_LDAP_GID`";
  485. $sql_select[] = "up.`ID` as profileId";
  486. $sql_select[] = "up.`T_TELBOX_NEIGHBOUR_IN_ID` as localisationId";
  487. $sql_select[] = "a.`ADM_ACCOUNT` as usrLogin";
  488. $sql_select[] = "a.`ID` as usrId";
  489. $sql_select = implode(', ', $sql_select);
  490. $sql = "select {$sql_select}
  491. from `CRM_AUTH_PROFILE` as up
  492. join `ADMIN_USERS` as a on(a.`ID`=up.`REMOTE_ID` and up.`REMOTE_TABLE`='ADMIN_USERS')
  493. join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  494. where
  495. up.`ID`='{$profileID}'
  496. and up.`A_STATUS` in('WAITING', 'NORMAL')
  497. and z.`TYPE` in('STANOWISKO','DZIAL','PODMIOT')
  498. ";
  499. $res = $this->_db->query($sql);
  500. if ($r = $this->_db->fetch($res)) {
  501. $profile = new stdClass();
  502. $profile->profileId = $r->profileId;
  503. $profile->localisationId = $r->localisationId;
  504. $profile->usrId = $r->usrId;
  505. $profile->usrLogin = $r->usrLogin;
  506. $profile->group = $this->_buildGroupFromRow($r);
  507. }
  508. return $profile;
  509. }
  510. /**
  511. * Get user and group info by profile ID (CRM_AUTH_PROFILE.ID)
  512. * Only in UserStorageDB
  513. *
  514. * @return array of profile {}
  515. * ID
  516. * usrId
  517. * usrLogin
  518. * group - @see getGroup
  519. */
  520. public function getUserProfiles($usrLogin) {
  521. if (!$usrLogin) return false;
  522. $profiles = array();
  523. $sql_select = array();
  524. $sql_select[] = "z.`ID`";
  525. $sql_select[] = "z.`DESC`";
  526. $sql_select[] = "z.`TYPE`";
  527. //$sql_select[] = "z.`OPIS`";
  528. $sql_select[] = "z.`A_LDAP_GID`";
  529. $sql_select[] = "up.`ID` as profileId";
  530. $sql_select[] = "up.`T_TELBOX_NEIGHBOUR_IN_ID` as localisationId";
  531. $sql_select[] = "a.`ADM_ACCOUNT` as usrLogin";
  532. $sql_select[] = "a.`ID` as usrId";
  533. $sql_select = implode(', ', $sql_select);
  534. $sql = "select {$sql_select}
  535. from `CRM_AUTH_PROFILE` as up
  536. join `ADMIN_USERS` as a on(a.`ID`=up.`REMOTE_ID` and up.`REMOTE_TABLE`='ADMIN_USERS')
  537. join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  538. where
  539. a.`ADM_ACCOUNT`='{$usrLogin}'
  540. and up.`A_STATUS` in('WAITING', 'NORMAL')
  541. and z.`TYPE` in('STANOWISKO','DZIAL','PODMIOT')
  542. ";
  543. $res = $this->_db->query($sql);
  544. while ($r = $this->_db->fetch($res)) {
  545. $profile = new stdClass();
  546. $profile->profileId = $r->profileId;
  547. $profile->localisationId = $r->localisationId;
  548. $profile->usrId = $r->usrId;
  549. $profile->usrLogin = $r->usrLogin;
  550. $profile->group = $this->_buildGroupFromRow($r);
  551. $profiles[] = $profile;
  552. }
  553. return $profiles;
  554. }
  555. /**
  556. * Remove user group by profile ID (CRM_AUTH_PROFILE.ID)
  557. * Only in UserStorageDB
  558. */
  559. public function removeUserGroupByProfileId($usrLogin, $group, $profileID) {
  560. if (!$this->_db) return false;
  561. if (!$usrLogin || !$profileID || !$group || !$group->zasobID) return false;
  562. $usrDB = $this->getUser($usrLogin);
  563. if (!$usrDB) return false;
  564. $sql = "delete from `CRM_AUTH_PROFILE`
  565. where
  566. `ID_ZASOB`='{$group->zasobID}'
  567. and `REMOTE_ID`='{$usrDB->primaryKey}'
  568. and `REMOTE_TABLE`='ADMIN_USERS'
  569. and `ID`='{$profileID}'
  570. ";
  571. $res = $this->_db->query($sql);
  572. return true;
  573. }
  574. public function setSyncUserDate($usrLogin) {
  575. if (!$this->_db) return false;
  576. $sql = "update `ADMIN_USERS` set `A_SYNC_LDAP_DATE`=NOW() where `ADM_ACCOUNT`='{$usrLogin}' ";
  577. $res = $this->_db->query($sql);
  578. }
  579. public function setSyncGroupDate($idGroup) {
  580. if (!$this->_db) return false;
  581. if ($idGroup <= 0) return false;
  582. $sql = "update `CRM_LISTA_ZASOBOW` set `A_SYNC_LDAP_DATE`=NOW() where `ID`='{$idGroup}' ";
  583. $res = $this->_db->query($sql);
  584. }
  585. public function isPasswordChanged($usrLogin) {
  586. $cnt = 0;
  587. $sql = "
  588. SELECT
  589. -- h.`ADM_PASSWD`,
  590. count(1) as cnt
  591. FROM `ADMIN_USERS` as u
  592. JOIN `ADMIN_USERS_HIST` as h on(h.`ID_USERS2`=u.`ID`)
  593. WHERE u.`ADM_ACCOUNT`='{$usrLogin}'
  594. AND h.`A_RECORD_CREATE_DATE`>u.`A_SYNC_LDAP_DATE`
  595. AND h.`ADM_PASSWD`!='N/S;'
  596. AND h.`ADM_PASSWD`!=''
  597. -- GROUP BY h.`ADM_PASSWD`
  598. ";
  599. $res = $this->_db->query($sql);
  600. if ($r = $this->_db->fetch($res)) {
  601. $cnt = $r->cnt;
  602. }
  603. return ($cnt > 0);
  604. }
  605. }