UserStorageDB.php 20 KB

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