api.usermanager.php 17 KB

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