api.usermanager.php 20 KB

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