PageRenderTime 24ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/bitrix/modules/main/lib/user.php

https://bitbucket.org/alex_poluektov/itech_test
PHP | 482 lines | 433 code | 41 blank | 8 comment | 14 complexity | c9aeb1347a462e8bbe2764bc8647fc1e MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage main
  6. * @copyright 2001-2018 Bitrix
  7. */
  8. namespace Bitrix\Main;
  9. use Bitrix\Main\DB\SqlExpression;
  10. use Bitrix\Main\Entity;
  11. use Bitrix\Main\Localization\Loc;
  12. use \Bitrix\Main\Search\MapBuilder;
  13. Loc::loadMessages(__FILE__);
  14. class UserTable extends Entity\DataManager
  15. {
  16. public static function getTableName()
  17. {
  18. return 'b_user';
  19. }
  20. public static function getUfId()
  21. {
  22. return 'USER';
  23. }
  24. public static function getMap()
  25. {
  26. $connection = Application::getConnection();
  27. $helper = $connection->getSqlHelper();
  28. return array(
  29. 'ID' => array(
  30. 'data_type' => 'integer',
  31. 'primary' => true,
  32. 'autocomplete' => true,
  33. ),
  34. 'LOGIN' => array(
  35. 'data_type' => 'string'
  36. ),
  37. 'PASSWORD' => array(
  38. 'data_type' => 'string'
  39. ),
  40. 'EMAIL' => array(
  41. 'data_type' => 'string'
  42. ),
  43. 'ACTIVE' => array(
  44. 'data_type' => 'boolean',
  45. 'values' => array('N','Y')
  46. ),
  47. 'DATE_REGISTER' => array(
  48. 'data_type' => 'datetime'
  49. ),
  50. 'DATE_REG_SHORT' => array(
  51. 'data_type' => 'datetime',
  52. 'expression' => array(
  53. $helper->getDatetimeToDateFunction('%s'), 'DATE_REGISTER'
  54. )
  55. ),
  56. 'LAST_LOGIN' => array(
  57. 'data_type' => 'datetime'
  58. ),
  59. 'LAST_LOGIN_SHORT' => array(
  60. 'data_type' => 'datetime',
  61. 'expression' => array(
  62. $helper->getDatetimeToDateFunction('%s'), 'LAST_LOGIN'
  63. )
  64. ),
  65. 'LAST_ACTIVITY_DATE' => array(
  66. 'data_type' => 'datetime'
  67. ),
  68. 'TIMESTAMP_X' => array(
  69. 'data_type' => 'datetime'
  70. ),
  71. 'NAME' => array(
  72. 'data_type' => 'string'
  73. ),
  74. 'SECOND_NAME' => array(
  75. 'data_type' => 'string'
  76. ),
  77. 'LAST_NAME' => array(
  78. 'data_type' => 'string'
  79. ),
  80. 'TITLE' => array(
  81. 'data_type' => 'string'
  82. ),
  83. 'EXTERNAL_AUTH_ID' => array(
  84. 'data_type' => 'string'
  85. ),
  86. 'XML_ID' => array(
  87. 'data_type' => 'string'
  88. ),
  89. 'BX_USER_ID' => array(
  90. 'data_type' => 'string'
  91. ),
  92. 'CONFIRM_CODE' => array(
  93. 'data_type' => 'string'
  94. ),
  95. 'LID' => array(
  96. 'data_type' => 'string'
  97. ),
  98. 'LANGUAGE_ID' => array(
  99. 'data_type' => 'string'
  100. ),
  101. 'TIME_ZONE_OFFSET' => array(
  102. 'data_type' => 'integer'
  103. ),
  104. 'PERSONAL_PROFESSION' => array(
  105. 'data_type' => 'string'
  106. ),
  107. 'PERSONAL_PHONE' => array(
  108. 'data_type' => 'string'
  109. ),
  110. 'PERSONAL_MOBILE' => array(
  111. 'data_type' => 'string'
  112. ),
  113. 'PERSONAL_WWW' => array(
  114. 'data_type' => 'string'
  115. ),
  116. 'PERSONAL_ICQ' => array(
  117. 'data_type' => 'string'
  118. ),
  119. 'PERSONAL_FAX' => array(
  120. 'data_type' => 'string'
  121. ),
  122. 'PERSONAL_PAGER' => array(
  123. 'data_type' => 'string'
  124. ),
  125. 'PERSONAL_STREET' => array(
  126. 'data_type' => 'text'
  127. ),
  128. 'PERSONAL_MAILBOX' => array(
  129. 'data_type' => 'string'
  130. ),
  131. 'PERSONAL_CITY' => array(
  132. 'data_type' => 'string'
  133. ),
  134. 'PERSONAL_STATE' => array(
  135. 'data_type' => 'string'
  136. ),
  137. 'PERSONAL_ZIP' => array(
  138. 'data_type' => 'string'
  139. ),
  140. 'PERSONAL_COUNTRY' => array(
  141. 'data_type' => 'string'
  142. ),
  143. 'PERSONAL_BIRTHDAY' => array(
  144. 'data_type' => 'date'
  145. ),
  146. 'PERSONAL_GENDER' => array(
  147. 'data_type' => 'string'
  148. ),
  149. 'PERSONAL_PHOTO' => array(
  150. 'data_type' => 'integer'
  151. ),
  152. 'PERSONAL_NOTES' => array(
  153. 'data_type' => 'text'
  154. ),
  155. 'WORK_COMPANY' => array(
  156. 'data_type' => 'string'
  157. ),
  158. 'WORK_DEPARTMENT' => array(
  159. 'data_type' => 'string'
  160. ),
  161. 'WORK_PHONE' => array(
  162. 'data_type' => 'string'
  163. ),
  164. 'WORK_POSITION' => array(
  165. 'data_type' => 'string'
  166. ),
  167. 'WORK_WWW' => array(
  168. 'data_type' => 'string'
  169. ),
  170. 'WORK_FAX' => array(
  171. 'data_type' => 'string'
  172. ),
  173. 'WORK_PAGER' => array(
  174. 'data_type' => 'string'
  175. ),
  176. 'WORK_STREET' => array(
  177. 'data_type' => 'text'
  178. ),
  179. 'WORK_MAILBOX' => array(
  180. 'data_type' => 'string'
  181. ),
  182. 'WORK_CITY' => array(
  183. 'data_type' => 'string'
  184. ),
  185. 'WORK_STATE' => array(
  186. 'data_type' => 'string'
  187. ),
  188. 'WORK_ZIP' => array(
  189. 'data_type' => 'string'
  190. ),
  191. 'WORK_COUNTRY' => array(
  192. 'data_type' => 'string'
  193. ),
  194. 'WORK_PROFILE' => array(
  195. 'data_type' => 'text'
  196. ),
  197. 'WORK_LOGO' => array(
  198. 'data_type' => 'integer'
  199. ),
  200. 'WORK_NOTES' => array(
  201. 'data_type' => 'text'
  202. ),
  203. 'ADMIN_NOTES' => array(
  204. 'data_type' => 'text'
  205. ),
  206. 'SHORT_NAME' => array(
  207. 'data_type' => 'string',
  208. 'expression' => array(
  209. $helper->getConcatFunction("%s","' '", "UPPER(".$helper->getSubstrFunction("%s", 1, 1).")", "'.'"),
  210. 'LAST_NAME', 'NAME'
  211. )
  212. ),
  213. 'IS_ONLINE' => array(
  214. 'data_type' => 'boolean',
  215. 'values' => array('N', 'Y'),
  216. 'expression' => array(
  217. 'CASE WHEN %s > '.$helper->addSecondsToDateTime('(-'.self::getSecondsForLimitOnline().')').' THEN \'Y\' ELSE \'N\' END',
  218. 'LAST_ACTIVITY_DATE',
  219. )
  220. ),
  221. 'IS_REAL_USER' => array(
  222. 'data_type' => 'boolean',
  223. 'values' => array('N', 'Y'),
  224. 'expression' => array(
  225. 'CASE WHEN %s IN ("'.join('", "', static::getExternalUserTypes()).'") THEN \'N\' ELSE \'Y\' END',
  226. 'EXTERNAL_AUTH_ID',
  227. )
  228. ),
  229. 'INDEX' => array(
  230. 'data_type' => 'Bitrix\Main\UserIndex',
  231. 'reference' => array('=this.ID' => 'ref.USER_ID'),
  232. 'join_type' => 'INNER',
  233. ),
  234. );
  235. }
  236. public static function getSecondsForLimitOnline()
  237. {
  238. $seconds = intval(ini_get("session.gc_maxlifetime"));
  239. if ($seconds == 0)
  240. {
  241. $seconds = 1440;
  242. }
  243. else if ($seconds < 120)
  244. {
  245. $seconds = 120;
  246. }
  247. return intval($seconds);
  248. }
  249. public static function getActiveUsersCount()
  250. {
  251. if (ModuleManager::isModuleInstalled("intranet"))
  252. {
  253. $sql = "SELECT COUNT(U.ID) ".
  254. "FROM b_user U ".
  255. "WHERE U.ACTIVE = 'Y' ".
  256. " AND U.LAST_LOGIN IS NOT NULL ".
  257. " AND EXISTS(".
  258. " SELECT 'x' ".
  259. " FROM b_utm_user UF, b_user_field F ".
  260. " WHERE F.ENTITY_ID = 'USER' ".
  261. " AND F.FIELD_NAME = 'UF_DEPARTMENT' ".
  262. " AND UF.FIELD_ID = F.ID ".
  263. " AND UF.VALUE_ID = U.ID ".
  264. " AND UF.VALUE_INT IS NOT NULL ".
  265. " AND UF.VALUE_INT <> 0".
  266. " )";
  267. }
  268. else
  269. {
  270. $sql = "SELECT COUNT(ID) ".
  271. "FROM b_user ".
  272. "WHERE ACTIVE = 'Y' ".
  273. " AND LAST_LOGIN IS NOT NULL";
  274. }
  275. $connection = Application::getConnection();
  276. return $connection->queryScalar($sql);
  277. }
  278. public static function getUserGroupIds($userId)
  279. {
  280. $groups = array();
  281. // anonymous groups
  282. $result = GroupTable::getList(array(
  283. 'select' => array('ID'),
  284. 'filter' => array(
  285. '=ANONYMOUS' => 'Y',
  286. '=ACTIVE' => 'Y'
  287. )
  288. ));
  289. while ($row = $result->fetch())
  290. {
  291. $groups[] = $row['ID'];
  292. }
  293. if(!in_array(2, $groups))
  294. $groups[] = 2;
  295. if($userId > 0)
  296. {
  297. // private groups
  298. $nowTimeExpression = new SqlExpression(
  299. static::getEntity()->getConnection()->getSqlHelper()->getCurrentDateTimeFunction()
  300. );
  301. $result = GroupTable::getList(array(
  302. 'select' => array('ID'),
  303. 'filter' => array(
  304. '=UserGroup:GROUP.USER_ID' => $userId,
  305. '=ACTIVE' => 'Y',
  306. array(
  307. 'LOGIC' => 'OR',
  308. '=UserGroup:GROUP.DATE_ACTIVE_FROM' => null,
  309. '<=UserGroup:GROUP.DATE_ACTIVE_FROM' => $nowTimeExpression,
  310. ),
  311. array(
  312. 'LOGIC' => 'OR',
  313. '=UserGroup:GROUP.DATE_ACTIVE_TO' => null,
  314. '>=UserGroup:GROUP.DATE_ACTIVE_TO' => $nowTimeExpression,
  315. ),
  316. array(
  317. 'LOGIC' => 'OR',
  318. '!=ANONYMOUS' => 'Y',
  319. '=ANONYMOUS' => null
  320. )
  321. )
  322. ));
  323. while ($row = $result->fetch())
  324. {
  325. $groups[] = $row['ID'];
  326. }
  327. }
  328. sort($groups);
  329. return $groups;
  330. }
  331. public static function getExternalUserTypes()
  332. {
  333. return array("bot", "email", "controller", "replica", "imconnector", "sale");
  334. }
  335. public static function indexRecord($id)
  336. {
  337. $id = intval($id);
  338. if($id == 0)
  339. {
  340. return false;
  341. }
  342. $intranetInstalled = \Bitrix\Main\ModuleManager::isModuleInstalled('intranet');
  343. $select = Array('ID', 'NAME', 'SECOND_NAME', 'LAST_NAME', 'WORK_POSITION', 'LOGIN', 'EMAIL');
  344. if ($intranetInstalled)
  345. {
  346. $select[] = 'UF_DEPARTMENT';
  347. }
  348. $record = parent::getList(Array(
  349. 'select' => $select,
  350. 'filter' => Array('=ID' => $id)
  351. ))->fetch();
  352. if(!is_array($record))
  353. {
  354. return false;
  355. }
  356. $record['UF_DEPARTMENT_NAMES'] = Array();
  357. if ($intranetInstalled)
  358. {
  359. $departmentNames = \Bitrix\Main\UserUtils::getDepartmentNames($record['UF_DEPARTMENT']);
  360. foreach ($departmentNames as $departmentName)
  361. {
  362. $record['UF_DEPARTMENT_NAMES'][] = $departmentName['NAME'];
  363. }
  364. }
  365. $departmentName = isset($record['UF_DEPARTMENT_NAMES'][0])? $record['UF_DEPARTMENT_NAMES'][0]: '';
  366. $searchDepartmentContent = implode(' ', $record['UF_DEPARTMENT_NAMES']);
  367. UserIndexTable::merge(array(
  368. 'USER_ID' => $id,
  369. 'NAME' => (string)$record['NAME'],
  370. 'SECOND_NAME' => (string)$record['SECOND_NAME'],
  371. 'LAST_NAME' => (string)$record['LAST_NAME'],
  372. 'WORK_POSITION' => (string)$record['WORK_POSITION'],
  373. 'UF_DEPARTMENT_NAME' => (string)$departmentName,
  374. 'SEARCH_USER_CONTENT' => self::generateSearchUserContent($record),
  375. 'SEARCH_ADMIN_CONTENT' => self::generateSearchAdminContent($record),
  376. 'SEARCH_DEPARTMENT_CONTENT' => MapBuilder::create()->addText($searchDepartmentContent)->build(),
  377. ));
  378. return true;
  379. }
  380. public static function deleteIndexRecord($id)
  381. {
  382. UserIndexTable::delete($id);
  383. }
  384. private static function generateSearchUserContent(array $fields)
  385. {
  386. $result = MapBuilder::create()
  387. ->addInteger($fields['ID'])
  388. ->addText($fields['NAME'])
  389. ->addText($fields['SECOND_NAME'])
  390. ->addText($fields['LAST_NAME'])
  391. ->addText($fields['WORK_POSITION'])
  392. ->addText(implode(' ', $fields['UF_DEPARTMENT_NAMES']))
  393. ->build();
  394. return $result;
  395. }
  396. private static function generateSearchAdminContent(array $fields)
  397. {
  398. $result = MapBuilder::create()
  399. ->addInteger($fields['ID'])
  400. ->addText($fields['NAME'])
  401. ->addText($fields['SECOND_NAME'])
  402. ->addText($fields['LAST_NAME'])
  403. ->addEmail($fields['EMAIL'])
  404. ->addText($fields['WORK_POSITION'])
  405. ->addText($fields['LOGIN'])
  406. ->build();
  407. return $result;
  408. }
  409. public static function add(array $data)
  410. {
  411. throw new NotImplementedException("Use CUser class.");
  412. }
  413. public static function update($primary, array $data)
  414. {
  415. throw new NotImplementedException("Use CUser class.");
  416. }
  417. public static function delete($primary)
  418. {
  419. throw new NotImplementedException("Use CUser class.");
  420. }
  421. public static function onAfterAdd(Entity\Event $event)
  422. {
  423. $id = $event->getParameter("id");
  424. static::indexRecord($id);
  425. return new Entity\EventResult();
  426. }
  427. public static function onAfterUpdate(Entity\Event $event)
  428. {
  429. $primary = $event->getParameter("id");
  430. $id = $primary["ID"];
  431. static::indexRecord($id);
  432. return new Entity\EventResult();
  433. }
  434. public static function onAfterDelete(Entity\Event $event)
  435. {
  436. $primary = $event->getParameter("id");
  437. $id = $primary["ID"];
  438. static::deleteIndexRecord($id);
  439. return new Entity\EventResult();
  440. }
  441. }