api.usermanager.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. * Some static routes etc
  20. */
  21. const URL_ME = '?module=usermanager';
  22. const ROUTE_DELETE = 'deleteuser';
  23. const ROUTE_EDIT = 'edituserdata';
  24. const ROUTE_PERMISSIONS = 'edituserpermissions';
  25. const ROUTE_NEWUSER = 'registernewuser';
  26. const ROUTE_GHOSTMODE = 'ghostmode';
  27. /**
  28. * New user parameters here
  29. */
  30. const PROUTE_DOREGISTER = 'registernewuserplease'; // just create new user flag
  31. const PROUTE_DOEDIT = 'editthisuser'; // username to edit user profile data as flag
  32. const PROUTE_DOPERMS = 'changepermissions'; // username to change permissions as flag
  33. const PROUTE_USERNAME = 'username';
  34. const PROUTE_PASSWORD = 'password';
  35. const PROUTE_PASSWORDCONFIRM = 'confirmation';
  36. const PROUTE_NICKNAME = 'nickname';
  37. const PROUTE_EMAIL = 'email';
  38. const PROUTE_ROOTUSER = 'thisisrealyrootuser'; // root user permission flag
  39. /**
  40. * Creates new user manager instance
  41. */
  42. public function __construct() {
  43. $this->initMessages();
  44. $this->initSystemCore();
  45. }
  46. /**
  47. * Inits current system core instance for further usage
  48. *
  49. * @global object $system
  50. *
  51. * @return void
  52. */
  53. protected function initSystemCore() {
  54. global $system;
  55. $this->system = $system;
  56. }
  57. /**
  58. * Inits system messages helper for further usage
  59. *
  60. * @return void
  61. */
  62. protected function initMessages() {
  63. $this->messages = new UbillingMessageHelper();
  64. }
  65. /**
  66. * Deletes existing user
  67. *
  68. * @param string $userName
  69. *
  70. * @return void
  71. */
  72. public function deleteUser($userName) {
  73. if (file_exists(USERS_PATH . $userName)) {
  74. unlink(USERS_PATH . $userName);
  75. log_register('USER DELETE {' . $userName . '}');
  76. }
  77. }
  78. /**
  79. * Renders list of available users with some controls
  80. *
  81. * @return string
  82. */
  83. public function renderUsersList() {
  84. $result = '';
  85. $allUsers = rcms_scandir(USERS_PATH);
  86. $myLogin = whoami();
  87. if (!empty($allUsers)) {
  88. $cells = wf_TableCell(__('User'));
  89. $cells .= wf_TableCell(__('Actions'));
  90. $rows = wf_TableRow($cells, 'row1');
  91. foreach ($allUsers as $index => $eachUser) {
  92. $cells = wf_TableCell($eachUser);
  93. $actControls = '';
  94. $actControls = wf_JSAlert(self::URL_ME . '&' . self::ROUTE_DELETE . '=' . $eachUser, web_delete_icon(), $this->messages->getDeleteAlert()) . ' ';
  95. $actControls .= wf_JSAlert(self::URL_ME . '&' . self::ROUTE_EDIT . '=' . $eachUser, wf_img('skins/icon_key.gif', __('Edit user')), $this->messages->getEditAlert()) . ' ';
  96. $actControls .= wf_Link(self::URL_ME . '&' . self::ROUTE_PERMISSIONS . '=' . $eachUser, web_edit_icon(__('Permissions')), false);
  97. if (cfr('ROOT')) {
  98. if ($myLogin != $eachUser) {
  99. $ghostModeLabel = __('Login as') . ' ' . $eachUser . ' ' . __('in ghost mode');
  100. $actControls .= wf_JSAlert(self::URL_ME . '&' . self::ROUTE_GHOSTMODE . '=' . $eachUser, wf_img('skins/ghost.png', $ghostModeLabel), $ghostModeLabel . '?');
  101. }
  102. }
  103. $cells .= wf_TableCell($actControls);
  104. $rows .= wf_TableRow($cells, 'row5');
  105. }
  106. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  107. } else {
  108. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  109. }
  110. $result .= wf_delimiter();
  111. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_NEWUSER . '=true', web_add_icon() . ' ' . __('Register new user'), false, 'ubButton');
  112. return ($result);
  113. }
  114. /**
  115. * Renders new user registration form
  116. *
  117. * @return string
  118. */
  119. public function renderRegisterForm() {
  120. $result = '';
  121. $inputs = wf_HiddenInput(self::PROUTE_DOREGISTER, 'true');
  122. $inputs .= wf_TextInput(self::PROUTE_USERNAME, __('Login'), '', true, 20, 'alphanumeric');
  123. $inputs .= wf_PasswordInput(self::PROUTE_PASSWORD, __('Password'), '', true, 20);
  124. $inputs .= wf_PasswordInput(self::PROUTE_PASSWORDCONFIRM, __('Password confirmation'), '', true, 20);
  125. $inputs .= wf_TextInput(self::PROUTE_NICKNAME, __('NickName'), '', true, 20, 'alphanumeric');
  126. $inputs .= wf_TextInput(self::PROUTE_EMAIL, __('Email'), '', true, 20);
  127. $inputs .= wf_Submit(__('Create'));
  128. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  129. return ($result);
  130. }
  131. /**
  132. * Registers new user
  133. *
  134. * @return void/string on error
  135. */
  136. public function createUser() {
  137. $result = '';
  138. //all of this props are required for normal registration
  139. $requiredParams = array(
  140. self::PROUTE_USERNAME,
  141. self::PROUTE_PASSWORD,
  142. self::PROUTE_PASSWORDCONFIRM,
  143. self::PROUTE_NICKNAME,
  144. self::PROUTE_NICKNAME,
  145. self::PROUTE_EMAIL
  146. );
  147. if (ubRouting::checkPost($requiredParams)) {
  148. $newLogin = ubRouting::post(self::PROUTE_USERNAME, 'vf');
  149. $newPasword = ubRouting::post(self::PROUTE_PASSWORD);
  150. $confirmation = ubRouting::post(self::PROUTE_PASSWORDCONFIRM);
  151. $newNickName = ubRouting::post(self::PROUTE_NICKNAME, 'safe');
  152. $newEmail = ubRouting::post(self::PROUTE_EMAIL, 'safe', '@');
  153. $newUserRights = '';
  154. if (!empty($newLogin)) {
  155. $userDataPath = USERS_PATH . $newLogin;
  156. if (!file_exists($userDataPath)) {
  157. if ($newPasword == $confirmation) {
  158. if (!empty($newEmail)) {
  159. if (!empty($newNickName)) {
  160. $newUserData = array(
  161. 'admin' => $newUserRights,
  162. 'password' => md5($newPasword),
  163. 'nickname' => $newNickName,
  164. 'username' => $newLogin,
  165. 'email' => $newEmail,
  166. 'hideemail' => '1',
  167. 'tz' => '2'
  168. );
  169. $saveUserData = serialize($newUserData);
  170. file_put_contents($userDataPath, $saveUserData);
  171. log_register('USER REGISTER {' . $newLogin . '}');
  172. } else {
  173. $result .= __('Empty NickName');
  174. }
  175. } else {
  176. $result .= __('Empty email');
  177. }
  178. } else {
  179. $result .= __('Passwords did not match');
  180. }
  181. } else {
  182. $result .= __('User already exists');
  183. }
  184. } else {
  185. $result .= __('Empty login');
  186. }
  187. }
  188. return ($result);
  189. }
  190. /**
  191. * Rdeders existing user editing interface
  192. *
  193. * @param string $userName
  194. *
  195. * @return string
  196. */
  197. public function renderEditForm($userName) {
  198. $result = '';
  199. $userName = ubRouting::filters($userName, 'vf');
  200. if (!empty($userName)) {
  201. if (file_exists(USERS_PATH . $userName)) {
  202. $currentUserData = $this->system->getUserData($userName);
  203. $inputs = wf_HiddenInput(self::PROUTE_DOEDIT, $userName);
  204. $inputs .= wf_PasswordInput(self::PROUTE_PASSWORD, __('New password'), '', true, 20);
  205. $inputs .= wf_PasswordInput(self::PROUTE_PASSWORDCONFIRM, __('New password confirmation'), '', true, 20);
  206. $inputs .= wf_TextInput(self::PROUTE_NICKNAME, __('NickName'), $currentUserData['nickname'], true, 20, 'alphanumeric');
  207. $inputs .= wf_TextInput(self::PROUTE_EMAIL, __('Email'), $currentUserData['email'], true, 20);
  208. $inputs .= wf_Submit(__('Save'));
  209. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  210. } else {
  211. $result .= $this->messages->getStyledMessage(__('User not exists'), 'error');
  212. }
  213. } else {
  214. $result .= $this->messages->getStyledMessage(__('Empty username'), 'error');
  215. }
  216. return ($result);
  217. }
  218. /**
  219. * Saves userdata changes if its required
  220. *
  221. * @return void/string on error
  222. */
  223. public function saveUser() {
  224. $result = '';
  225. if (ubRouting::checkPost(self::PROUTE_DOEDIT)) {
  226. $editUserName = ubRouting::post(self::PROUTE_DOEDIT, 'vf');
  227. if (!empty($editUserName)) {
  228. $saveDataPath = USERS_PATH . $editUserName;
  229. if (file_exists($saveDataPath)) {
  230. $currentUserData = $this->system->getUserData($editUserName);
  231. $newUserData = $currentUserData;
  232. if (!empty($currentUserData)) {
  233. $updateProfile = false;
  234. $newPasword = ubRouting::post(self::PROUTE_PASSWORD);
  235. $confirmation = ubRouting::post(self::PROUTE_PASSWORDCONFIRM);
  236. $newNickName = ubRouting::post(self::PROUTE_NICKNAME, 'mres');
  237. $newEmail = ubRouting::post(self::PROUTE_EMAIL, 'mres');
  238. //password update?
  239. if (!empty($newPasword)) {
  240. if ($newPasword == $confirmation) {
  241. $newPasswordHash = md5($newPasword);
  242. if ($currentUserData['password'] != $newPasswordHash) {
  243. //ok its really new password
  244. $newUserData['password'] = $newPasswordHash;
  245. $updateProfile = true;
  246. }
  247. } else {
  248. $result .= __('Passwords did not match');
  249. }
  250. }
  251. //nickname update
  252. if (!empty($newNickName)) {
  253. if ($currentUserData['nickname'] != $newNickName) {
  254. $newUserData['nickname'] = $newNickName;
  255. $updateProfile = true;
  256. }
  257. }
  258. //email update
  259. if (!empty($newEmail)) {
  260. if ($currentUserData['email'] != $newEmail) {
  261. $newUserData['email'] = $newEmail;
  262. $updateProfile = true;
  263. }
  264. }
  265. //saving profile changes if required
  266. if ($updateProfile) {
  267. if (is_writable($saveDataPath)) {
  268. $newProfileToSave = serialize($newUserData);
  269. file_put_contents($saveDataPath, $newProfileToSave);
  270. log_register('USER CHANGE DATA {' . $editUserName . '}');
  271. } else {
  272. $result .= __('Profile write failure');
  273. }
  274. }
  275. } else {
  276. $result .= __('Profile read failure');
  277. }
  278. } else {
  279. $result .= __('User not exists');
  280. }
  281. } else {
  282. $result .= __('Empty username');
  283. }
  284. }
  285. return ($result);
  286. }
  287. /**
  288. * Saves user permissions changes if its required
  289. *
  290. * @return void/string on error
  291. */
  292. public function savePermissions() {
  293. $result = '';
  294. if (ubRouting::checkPost(self::PROUTE_DOPERMS)) {
  295. $editUserName = ubRouting::post(self::PROUTE_DOPERMS, 'vf');
  296. if (!empty($editUserName)) {
  297. $saveDataPath = USERS_PATH . $editUserName;
  298. if (file_exists($saveDataPath)) {
  299. $currentUserData = $this->system->getUserData($editUserName);
  300. $newUserData = $currentUserData;
  301. if (!empty($currentUserData)) {
  302. $updateProfile = false;
  303. $currentRootState = ($currentUserData['admin'] == '*') ? true : false;
  304. $newRootState = (ubRouting::checkPost(self::PROUTE_ROOTUSER)) ? true : false;
  305. $oldRightString = $currentUserData['admin'];
  306. $systemRights = $this->system->getRightsDatabase();
  307. $newRightsString = '';
  308. if (ubRouting::checkPost('_rights')) {
  309. $rightsTmp = ubRouting::post('_rights');
  310. if (!empty($rightsTmp) and is_array($rightsTmp)) {
  311. foreach ($rightsTmp as $eachRight => $rightState) {
  312. if (isset($systemRights[$eachRight])) {
  313. //skipping unknown rights
  314. $newRightsString .= '|' . $eachRight . '|';
  315. }
  316. }
  317. }
  318. }
  319. //new user state is "have root permisssions"
  320. if ($newRootState) {
  321. $newRightsString = '*';
  322. }
  323. //take decision to update rights
  324. if ($newRightsString != $oldRightString) {
  325. $updateProfile = true;
  326. $newUserData['admin'] = $newRightsString;
  327. }
  328. if ($updateProfile) {
  329. if (is_writable($saveDataPath)) {
  330. $newProfileToSave = serialize($newUserData);
  331. file_put_contents($saveDataPath, $newProfileToSave);
  332. log_register('USER CHANGE PERMISSIONS {' . $editUserName . '}');
  333. } else {
  334. $result .= __('Profile write failure');
  335. }
  336. }
  337. } else {
  338. $result .= __('Profile read failure');
  339. }
  340. } else {
  341. $result .= __('User not exists');
  342. }
  343. } else {
  344. $result .= __('Empty username');
  345. }
  346. }
  347. return ($result);
  348. }
  349. /**
  350. * Renders form for editing users permissions
  351. *
  352. * @param string $userName
  353. *
  354. * @return string
  355. */
  356. public function renderPermissionsForm($userName) {
  357. $result = '';
  358. $userName = ubRouting::filters($userName, 'vf');
  359. if (!empty($userName)) {
  360. if (file_exists(USERS_PATH . $userName)) {
  361. $currentUserData = $this->system->getUserData($userName);
  362. if (!empty($currentUserData)) {
  363. $rootRights = false;
  364. $currentRightsString = $currentUserData['admin'];
  365. $currentRightsArr = array();
  366. $systemRights = $this->system->getRightsDatabase();
  367. if ($currentRightsString !== '*') {
  368. preg_match_all('/\|(.*?)\|/', $currentRightsString, $rights_r);
  369. if (!empty($rights_r[1])) {
  370. foreach ($rights_r[1] as $right) {
  371. if (isset($systemRights[$right])) {
  372. $currentRightsArr[$right] = $right;
  373. }
  374. }
  375. }
  376. } else {
  377. $rootRights = true;
  378. }
  379. //form here
  380. $inputs = wf_HiddenInput(self::PROUTE_DOPERMS, $userName);
  381. $inputs .= wf_CheckInput(self::PROUTE_ROOTUSER, __('User have all available rights and permissions'), true, $rootRights);
  382. $inputs .= wf_tag('hr');
  383. if (!$rootRights) {
  384. if (!empty($systemRights)) {
  385. foreach ($systemRights as $eachRightId => $eachRightDesc) {
  386. $haveThisRight = (isset($currentRightsArr[$eachRightId])) ? true : false;
  387. $rightLabel = __($eachRightDesc) . ' - ' . $eachRightId;
  388. $inputs .= wf_CheckInput('_rights[' . $eachRightId . ']', $rightLabel, true, $haveThisRight);
  389. }
  390. }
  391. }
  392. $inputs .= wf_Submit(__('Save'));
  393. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  394. } else {
  395. $result .= $this->messages->getStyledMessage(__('Profile read failure'), 'error');
  396. }
  397. } else {
  398. $result .= $this->messages->getStyledMessage(__('User not exists'), 'error');
  399. }
  400. } else {
  401. $result .= $this->messages->getStyledMessage(__('Empty username'), 'error');
  402. }
  403. return ($result);
  404. }
  405. }