api.usermanager.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. /**
  3. * Basic user management interface
  4. */
  5. class UserManager {
  6. /**
  7. * YalfCore system object placeholder
  8. *
  9. * @var object
  10. */
  11. protected $system = '';
  12. /**
  13. * System messages helper instance
  14. *
  15. * @var object
  16. */
  17. protected $messages = '';
  18. /**
  19. * Contains predefined user roles as role=>name
  20. *
  21. * @var array
  22. */
  23. protected $userRoles = array();
  24. /**
  25. * Contains predefined roles rights
  26. *
  27. * @var array
  28. */
  29. protected $rolesRights = array();
  30. /**
  31. * Some static routes etc
  32. */
  33. const URL_ME = '?module=usermanager';
  34. const ROUTE_DELETE = 'deleteuser';
  35. const ROUTE_EDIT = 'edituserdata';
  36. const ROUTE_PERMISSIONS = 'edituserpermissions';
  37. const ROUTE_NEWUSER = 'registernewuser';
  38. const ROUTE_GHOSTMODE = 'ghostmode';
  39. /**
  40. * New user parameters here
  41. */
  42. const PROUTE_DOREGISTER = 'registernewuserplease'; // just create new user flag
  43. const PROUTE_DOEDIT = 'editthisuser'; // username to edit user profile data as flag
  44. const PROUTE_DOPERMS = 'changepermissions'; // username to change permissions as flag
  45. const PROUTE_USERNAME = 'username';
  46. const PROUTE_PASSWORD = 'password';
  47. const PROUTE_PASSWORDCONFIRM = 'confirmation';
  48. const PROUTE_NICKNAME = 'nickname';
  49. const PROUTE_EMAIL = 'email';
  50. const PROUTE_USERROLE = 'userrole';
  51. const PROUTE_ROOTUSER = 'thisisrealyrootuser'; // root user permission flag
  52. /**
  53. * Creates new user manager instance
  54. */
  55. public function __construct() {
  56. $this->initMessages();
  57. $this->initSystemCore();
  58. $this->setUserRoles();
  59. }
  60. /**
  61. * Sets some predefined user roles
  62. *
  63. * @return void
  64. */
  65. protected function setUserRoles() {
  66. global $ubillingConfig;
  67. $this->userRoles = array(
  68. 'LIMITED' => __('User'),
  69. 'ADMINISTRATOR' => __('Administrator'),
  70. 'OPERATOR' => __('Operator'),
  71. );
  72. $limitedRights = $ubillingConfig->getAlterParam('LIMITED_RIGHTS');
  73. if (!empty($limitedRights)) {
  74. $limitedRights = explode(',', $limitedRights);
  75. $rightsString = '';
  76. foreach ($limitedRights as $io => $each) {
  77. $rightsString .= '|' . $each . '|';
  78. }
  79. $this->rolesRights['LIMITED'] = $rightsString;
  80. //operator have limited users rights + access to all cameras/channels
  81. $this->rolesRights['OPERATOR'] = $rightsString . '|OPERATOR|';
  82. }
  83. $this->rolesRights['ADMINISTRATOR'] = '*';
  84. }
  85. /**
  86. * Inits current system core instance for further usage
  87. *
  88. * @global object $system
  89. *
  90. * @return void
  91. */
  92. protected function initSystemCore() {
  93. global $system;
  94. $this->system = $system;
  95. }
  96. /**
  97. * Inits system messages helper for further usage
  98. *
  99. * @return void
  100. */
  101. protected function initMessages() {
  102. $this->messages = new UbillingMessageHelper();
  103. }
  104. /**
  105. * Deletes existing user
  106. *
  107. * @param string $userName
  108. *
  109. * @return void
  110. */
  111. public function deleteUser($userName) {
  112. if (file_exists(USERS_PATH . $userName)) {
  113. //flushing ACLs
  114. $acl = new ACL();
  115. $acl->flushUser($userName);
  116. //flushing user records
  117. $this->flushUserRecords($userName);
  118. //deleting user
  119. unlink(USERS_PATH . $userName);
  120. log_register('USER DELETE {' . $userName . '}');
  121. }
  122. }
  123. /**
  124. * Returns all available users data
  125. *
  126. * @return array
  127. */
  128. public function getAllUsersData() {
  129. $result = array();
  130. $allUsers = rcms_scandir(USERS_PATH);
  131. if (!empty($allUsers)) {
  132. foreach ($allUsers as $index => $eachLogin) {
  133. $eachUserData = $this->system->getUserData($eachLogin);
  134. if (!empty($eachUserData)) {
  135. $result[$eachLogin]['login'] = $eachUserData['username'];
  136. $result[$eachLogin]['password'] = $eachUserData['password'];
  137. $result[$eachLogin]['rights'] = $eachUserData['admin'];
  138. }
  139. }
  140. }
  141. return ($result);
  142. }
  143. /**
  144. * Checks is user registered or not?
  145. *
  146. * @param string $login
  147. *
  148. * @return bool
  149. */
  150. public function isUserRegistered($login) {
  151. $result = false;
  152. $allUsers = rcms_scandir(USERS_PATH);
  153. if (!empty($allUsers)) {
  154. foreach ($allUsers as $index => $eachLogin) {
  155. if ($eachLogin == $login) {
  156. $result = true;
  157. break;
  158. }
  159. }
  160. }
  161. return ($result);
  162. }
  163. /**
  164. * Returns bytes count allocated by users saved records
  165. *
  166. * @param string $userLogin
  167. *
  168. * @return int
  169. */
  170. protected function getUserSize($userLogin) {
  171. $result = 0;
  172. if (!empty($userLogin)) {
  173. $basePath = Export::PATH_RECORDS;
  174. if (file_exists($basePath)) {
  175. $userRecPath = $basePath . $userLogin . '/';
  176. if (file_exists($userRecPath)) {
  177. $allFiles = rcms_scandir($userRecPath, '*' . Export::RECORDS_EXT);
  178. if (!empty($allFiles)) {
  179. foreach ($allFiles as $io => $each) {
  180. $result += filesize($userRecPath . $each);
  181. }
  182. }
  183. }
  184. }
  185. }
  186. return ($result);
  187. }
  188. /**
  189. * Flushes all user saved records
  190. *
  191. * @param string $userName
  192. *
  193. * @return void
  194. */
  195. protected function flushUserRecords($userName) {
  196. if (!empty($userName)) {
  197. $basePath = Export::PATH_RECORDS;
  198. if (file_exists($basePath)) {
  199. $userRecPath = $basePath . $userName . '/';
  200. if (file_exists($userRecPath)) {
  201. rcms_delete_files($userRecPath, true);
  202. log_register('USER FLUSH RECORDS {' . $userName . '}');
  203. }
  204. }
  205. }
  206. }
  207. /**
  208. * Renders list of available users with some controls
  209. *
  210. * @return string
  211. */
  212. public function renderUsersList() {
  213. $result = '';
  214. $allUsers = rcms_scandir(USERS_PATH);
  215. $myLogin = whoami();
  216. if (!empty($allUsers)) {
  217. $cells = wf_TableCell(__('User'));
  218. $cells .= wf_TableCell(__('Size'));
  219. $cells .= wf_TableCell(__('Actions'));
  220. $rows = wf_TableRow($cells, 'row1');
  221. foreach ($allUsers as $index => $eachUser) {
  222. $userExportsSize = $this->getUserSize($eachUser);
  223. $cells = wf_TableCell($eachUser);
  224. $actControls = '';
  225. $cells .= wf_TableCell(wr_convertSize($userExportsSize), '', '', 'sorttable_customkey="' . $userExportsSize . '"');
  226. $actControls = wf_JSAlert(self::URL_ME . '&' . self::ROUTE_DELETE . '=' . $eachUser, web_delete_icon(), $this->messages->getDeleteAlert()) . ' ';
  227. $actControls .= wf_JSAlert(self::URL_ME . '&' . self::ROUTE_EDIT . '=' . $eachUser, wf_img('skins/icon_key.gif', __('Edit user')), $this->messages->getEditAlert()) . ' ';
  228. $actControls .= wf_Link(self::URL_ME . '&' . self::ROUTE_PERMISSIONS . '=' . $eachUser, web_edit_icon(__('Permissions')), false);
  229. if (cfr('ROOT')) {
  230. if ($myLogin != $eachUser) {
  231. $ghostModeLabel = __('Login as') . ' ' . $eachUser . ' ' . __('in ghost mode');
  232. $actControls .= wf_JSAlert(self::URL_ME . '&' . self::ROUTE_GHOSTMODE . '=' . $eachUser, wf_img('skins/ghost.png', $ghostModeLabel), $ghostModeLabel . '?');
  233. }
  234. }
  235. $cells .= wf_TableCell($actControls);
  236. $rows .= wf_TableRow($cells, 'row5');
  237. }
  238. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  239. } else {
  240. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  241. }
  242. $result .= wf_delimiter();
  243. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_NEWUSER . '=true', web_icon_create() . ' ' . __('Register new user'), false, 'ubButton');
  244. return ($result);
  245. }
  246. /**
  247. * Renders new user registration form
  248. *
  249. * @return string
  250. */
  251. public function renderRegisterForm() {
  252. $result = '';
  253. $inputs = wf_HiddenInput(self::PROUTE_DOREGISTER, 'true');
  254. $inputs .= wf_TextInput(self::PROUTE_USERNAME, __('Login'), '', true, 20, 'alphanumeric');
  255. $inputs .= wf_PasswordInput(self::PROUTE_PASSWORD, __('Password'), '', true, 20);
  256. $inputs .= wf_PasswordInput(self::PROUTE_PASSWORDCONFIRM, __('Password confirmation'), '', true, 20);
  257. $inputs .= wf_delimiter(0);
  258. $inputs .= wf_Selector(self::PROUTE_USERROLE, $this->userRoles, __('Permissions'), '', true);
  259. $inputs .= wf_delimiter(0);
  260. $inputs .= wf_Submit(__('Create'));
  261. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  262. return ($result);
  263. }
  264. /**
  265. * Registers new user
  266. *
  267. * @param string $login
  268. * @param string $password
  269. * @param string $confirmation
  270. * @param string $role
  271. *
  272. * @return void/string on error
  273. */
  274. public function createUser($login, $password, $confirmation, $role = '') {
  275. $result = '';
  276. $newLogin = ubRouting::filters($login, 'vf');
  277. $newPasword = ubRouting::filters($password);
  278. $confirmation = ubRouting::filters($confirmation);
  279. $newNickName = ubRouting::filters($login, 'vf');
  280. $newRole = ubRouting::filters($role, 'vf');
  281. $newEmail = $newLogin . '@wolfrecorder.com';
  282. $newUserRights = '';
  283. if (!empty($newRole)) {
  284. if (isset($this->rolesRights[$newRole])) {
  285. $newUserRights = $this->rolesRights[$newRole];
  286. }
  287. }
  288. if (!empty($newLogin)) {
  289. $userDataPath = USERS_PATH . $newLogin;
  290. if (!file_exists($userDataPath)) {
  291. if ($newPasword == $confirmation) {
  292. if (!empty($newEmail)) {
  293. if (!empty($newNickName)) {
  294. $newUserData = array(
  295. 'admin' => $newUserRights,
  296. 'password' => md5($newPasword),
  297. 'nickname' => $newNickName,
  298. 'username' => $newLogin,
  299. 'email' => $newEmail,
  300. 'hideemail' => '1',
  301. 'tz' => '2'
  302. );
  303. $saveUserData = serialize($newUserData);
  304. file_put_contents($userDataPath, $saveUserData);
  305. log_register('USER REGISTER {' . $newLogin . '}');
  306. } else {
  307. $result .= __('Empty NickName');
  308. }
  309. } else {
  310. $result .= __('Empty email');
  311. }
  312. } else {
  313. $result .= __('Passwords did not match');
  314. }
  315. } else {
  316. $result .= __('User already exists');
  317. }
  318. } else {
  319. $result .= __('Empty login');
  320. }
  321. return ($result);
  322. }
  323. /**
  324. * Rdeders existing user editing interface
  325. *
  326. * @param string $userName
  327. *
  328. * @return string
  329. */
  330. public function renderEditForm($userName) {
  331. $result = '';
  332. $userName = ubRouting::filters($userName, 'vf');
  333. if (!empty($userName)) {
  334. if (file_exists(USERS_PATH . $userName)) {
  335. $currentUserData = $this->system->getUserData($userName);
  336. $inputs = wf_HiddenInput(self::PROUTE_DOEDIT, $userName);
  337. $inputs .= wf_PasswordInput(self::PROUTE_PASSWORD, __('New password'), '', true, 20);
  338. $inputs .= wf_PasswordInput(self::PROUTE_PASSWORDCONFIRM, __('New password confirmation'), '', true, 20);
  339. $inputs .= wf_HiddenInput(self::PROUTE_NICKNAME, $currentUserData['nickname']);
  340. $inputs .= wf_HiddenInput(self::PROUTE_EMAIL, $currentUserData['email']);
  341. $inputs .= wf_delimiter(0);
  342. $inputs .= wf_Submit(__('Save'));
  343. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  344. } else {
  345. $result .= $this->messages->getStyledMessage(__('User not exists'), 'error');
  346. }
  347. } else {
  348. $result .= $this->messages->getStyledMessage(__('Empty username'), 'error');
  349. }
  350. return ($result);
  351. }
  352. /**
  353. * Saves userdata changes if its required
  354. *
  355. * @param string $login
  356. * @param string $password
  357. * @param string $confirmation
  358. *
  359. * @return void/string on error
  360. */
  361. public function saveUser($login, $password, $confirmation) {
  362. $result = '';
  363. $editUserName = ubRouting::filters($login, 'vf');
  364. if (!empty($editUserName)) {
  365. $saveDataPath = USERS_PATH . $editUserName;
  366. if (file_exists($saveDataPath)) {
  367. $currentUserData = $this->system->getUserData($editUserName);
  368. $newUserData = $currentUserData;
  369. if (!empty($currentUserData)) {
  370. $updateProfile = false;
  371. $newPasword = ubRouting::filters($password);
  372. $confirmation = ubRouting::filters($confirmation);
  373. $newNickName = $currentUserData['nickname'];
  374. $newEmail = $currentUserData['email'];
  375. //password update?
  376. if (!empty($newPasword)) {
  377. if ($newPasword == $confirmation) {
  378. $newPasswordHash = md5($newPasword);
  379. if ($currentUserData['password'] != $newPasswordHash) {
  380. //ok its really new password
  381. $newUserData['password'] = $newPasswordHash;
  382. $updateProfile = true;
  383. }
  384. } else {
  385. $result .= __('Passwords did not match');
  386. }
  387. }
  388. //saving profile changes if required
  389. if ($updateProfile) {
  390. if (is_writable($saveDataPath)) {
  391. $newProfileToSave = serialize($newUserData);
  392. file_put_contents($saveDataPath, $newProfileToSave);
  393. log_register('USER CHANGE DATA {' . $editUserName . '}');
  394. } else {
  395. $result .= __('Profile write failure');
  396. }
  397. }
  398. } else {
  399. $result .= __('Profile read failure');
  400. }
  401. } else {
  402. $result .= __('User not exists');
  403. }
  404. } else {
  405. $result .= __('Empty username');
  406. }
  407. return ($result);
  408. }
  409. /**
  410. * Saves user permissions changes if its required
  411. *
  412. * @return void/string on error
  413. */
  414. public function savePermissions() {
  415. $result = '';
  416. if (ubRouting::checkPost(self::PROUTE_DOPERMS)) {
  417. $editUserName = ubRouting::post(self::PROUTE_DOPERMS, 'vf');
  418. if (!empty($editUserName)) {
  419. $saveDataPath = USERS_PATH . $editUserName;
  420. if (file_exists($saveDataPath)) {
  421. $currentUserData = $this->system->getUserData($editUserName);
  422. $newUserData = $currentUserData;
  423. if (!empty($currentUserData)) {
  424. $updateProfile = false;
  425. $currentRootState = ($currentUserData['admin'] == '*') ? true : false;
  426. $newRootState = (ubRouting::checkPost(self::PROUTE_ROOTUSER)) ? true : false;
  427. $oldRightString = $currentUserData['admin'];
  428. $systemRights = $this->system->getRightsDatabase();
  429. $newRightsString = '';
  430. if (ubRouting::checkPost('_rights')) {
  431. $rightsTmp = ubRouting::post('_rights');
  432. if (!empty($rightsTmp) and is_array($rightsTmp)) {
  433. foreach ($rightsTmp as $eachRight => $rightState) {
  434. if (isset($systemRights[$eachRight])) {
  435. //skipping unknown rights
  436. $newRightsString .= '|' . $eachRight . '|';
  437. }
  438. }
  439. }
  440. }
  441. //new user state is "have root permisssions"
  442. if ($newRootState) {
  443. $newRightsString = '*';
  444. }
  445. //take decision to update rights
  446. if ($newRightsString != $oldRightString) {
  447. $updateProfile = true;
  448. $newUserData['admin'] = $newRightsString;
  449. }
  450. if ($updateProfile) {
  451. if (is_writable($saveDataPath)) {
  452. $newProfileToSave = serialize($newUserData);
  453. file_put_contents($saveDataPath, $newProfileToSave);
  454. log_register('USER CHANGE PERMISSIONS {' . $editUserName . '}');
  455. } else {
  456. $result .= __('Profile write failure');
  457. }
  458. }
  459. } else {
  460. $result .= __('Profile read failure');
  461. }
  462. } else {
  463. $result .= __('User not exists');
  464. }
  465. } else {
  466. $result .= __('Empty username');
  467. }
  468. }
  469. return ($result);
  470. }
  471. /**
  472. * Renders form for editing users permissions
  473. *
  474. * @param string $userName
  475. *
  476. * @return string
  477. */
  478. public function renderPermissionsForm($userName) {
  479. $result = '';
  480. $userName = ubRouting::filters($userName, 'vf');
  481. if (!empty($userName)) {
  482. if (file_exists(USERS_PATH . $userName)) {
  483. $currentUserData = $this->system->getUserData($userName);
  484. if (!empty($currentUserData)) {
  485. $rootRights = false;
  486. $currentRightsString = $currentUserData['admin'];
  487. $currentRightsArr = array();
  488. $systemRights = $this->system->getRightsDatabase();
  489. if ($currentRightsString !== '*') {
  490. preg_match_all('/\|(.*?)\|/', $currentRightsString, $rights_r);
  491. if (!empty($rights_r[1])) {
  492. foreach ($rights_r[1] as $right) {
  493. if (isset($systemRights[$right])) {
  494. $currentRightsArr[$right] = $right;
  495. }
  496. }
  497. }
  498. } else {
  499. $rootRights = true;
  500. }
  501. //form here
  502. $inputs = wf_HiddenInput(self::PROUTE_DOPERMS, $userName);
  503. $inputs .= wf_CheckInput(self::PROUTE_ROOTUSER, __('User have all available rights and permissions'), true, $rootRights);
  504. $inputs .= wf_tag('hr');
  505. if (!$rootRights) {
  506. if (!empty($systemRights)) {
  507. foreach ($systemRights as $eachRightId => $eachRightDesc) {
  508. $haveThisRight = (isset($currentRightsArr[$eachRightId])) ? true : false;
  509. $rightLabel = __($eachRightDesc) . ' - ' . $eachRightId;
  510. $inputs .= wf_CheckInput('_rights[' . $eachRightId . ']', $rightLabel, true, $haveThisRight);
  511. }
  512. }
  513. }
  514. $inputs .= wf_Submit(__('Save'));
  515. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  516. } else {
  517. $result .= $this->messages->getStyledMessage(__('Profile read failure'), 'error');
  518. }
  519. } else {
  520. $result .= $this->messages->getStyledMessage(__('User not exists'), 'error');
  521. }
  522. } else {
  523. $result .= $this->messages->getStyledMessage(__('Empty username'), 'error');
  524. }
  525. return ($result);
  526. }
  527. }