123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- <?php
- /**
- * Basic user management interface
- */
- class UserManager {
- /**
- * YalfCore system object placeholder
- *
- * @var object
- */
- protected $system = '';
- /**
- * System messages helper instance
- *
- * @var object
- */
- protected $messages = '';
- /**
- * Some static routes etc
- */
- const URL_ME = '?module=usermanager';
- const ROUTE_DELETE = 'deleteuser';
- const ROUTE_EDIT = 'edituserdata';
- const ROUTE_PERMISSIONS = 'edituserpermissions';
- const ROUTE_NEWUSER = 'registernewuser';
- /**
- * New user parameters here
- */
- const PROUTE_DOREGISTER = 'registernewuserplease'; // just create new user flag
- const PROUTE_DOEDIT = 'editthisuser'; // username to edit user profile data as flag
- const PROUTE_DOPERMS = 'changepermissions'; // username to change permissions as flag
- const PROUTE_USERNAME = 'username';
- const PROUTE_PASSWORD = 'password';
- const PROUTE_PASSWORDCONFIRM = 'confirmation';
- const PROUTE_NICKNAME = 'nickname';
- const PROUTE_EMAIL = 'email';
- const PROUTE_ROOTUSER = 'thisisrealyrootuser'; // root user permission flag
- /**
- * Creates new user manager instance
- */
- public function __construct() {
- $this->initMessages();
- $this->initSystemCore();
- }
- /**
- * Inits current system core instance for further usage
- *
- * @global object $system
- *
- * @return void
- */
- protected function initSystemCore() {
- global $system;
- $this->system = $system;
- }
- /**
- * Inits system messages helper for further usage
- *
- * @return void
- */
- protected function initMessages() {
- $this->messages = new UbillingMessageHelper();
- }
- /**
- * Deletes existing user
- *
- * @param string $userName
- *
- * @return void
- */
- public function deleteUser($userName) {
- if (file_exists(USERS_PATH . $userName)) {
- unlink(USERS_PATH . $userName);
- log_register('USER DELETE {' . $userName . '}');
- }
- }
- /**
- * Renders list of available users with some controls
- *
- * @return string
- */
- public function renderUsersList() {
- $result = '';
- $allUsers = rcms_scandir(USERS_PATH);
- if (!empty($allUsers)) {
- $cells = wf_TableCell(__('User'));
- $cells .= wf_TableCell(__('Actions'));
- $rows = wf_TableRow($cells, 'row1');
- foreach ($allUsers as $index => $eachUser) {
- $cells = wf_TableCell($eachUser);
- $actControls = '';
- $actControls = wf_JSAlert(self::URL_ME . '&' . self::ROUTE_DELETE . '=' . $eachUser, web_delete_icon(), $this->messages->getDeleteAlert()) . ' ';
- $actControls .= wf_JSAlert(self::URL_ME . '&' . self::ROUTE_EDIT . '=' . $eachUser, wf_img('skins/icon_key.gif', __('Edit user')), $this->messages->getEditAlert()) . ' ';
- $actControls .= wf_Link(self::URL_ME . '&' . self::ROUTE_PERMISSIONS . '=' . $eachUser, web_edit_icon(__('Permissions')), $this->messages->getEditAlert());
- $cells .= wf_TableCell($actControls);
- $rows .= wf_TableRow($cells, 'row5');
- }
- $result .= wf_TableBody($rows, '100%', 0, 'sortable');
- } else {
- $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
- }
- $result .= wf_delimiter();
- $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_NEWUSER . '=true', web_add_icon() . ' ' . __('Register new user'), false, 'ubButton');
- return($result);
- }
- /**
- * Renders new user registration form
- *
- * @return string
- */
- public function renderRegisterForm() {
- $result = '';
- $inputs = wf_HiddenInput(self::PROUTE_DOREGISTER, 'true');
- $inputs .= wf_TextInput(self::PROUTE_USERNAME, __('Login'), '', true, 20, 'alphanumeric');
- $inputs .= wf_PasswordInput(self::PROUTE_PASSWORD, __('Password'), '', true, 20);
- $inputs .= wf_PasswordInput(self::PROUTE_PASSWORDCONFIRM, __('Password confirmation'), '', true, 20);
- $inputs .= wf_TextInput(self::PROUTE_NICKNAME, __('NickName'), '', true, 20, 'alphanumeric');
- $inputs .= wf_TextInput(self::PROUTE_EMAIL, __('Email'), '', true, 20);
- $inputs .= wf_Submit(__('Create'));
- $result .= wf_Form('', 'POST', $inputs, 'glamour');
- return($result);
- }
- /**
- * Registers new user
- *
- * @return void/string on error
- */
- public function createUser() {
- $result = '';
- //all of this props are required for normal registration
- $requiredParams = array(
- self::PROUTE_USERNAME,
- self::PROUTE_PASSWORD,
- self::PROUTE_PASSWORDCONFIRM,
- self::PROUTE_NICKNAME,
- self::PROUTE_NICKNAME,
- self::PROUTE_EMAIL
- );
- if (ubRouting::checkPost($requiredParams)) {
- $newLogin = ubRouting::post(self::PROUTE_USERNAME, 'vf');
- $newPasword = ubRouting::post(self::PROUTE_PASSWORD);
- $confirmation = ubRouting::post(self::PROUTE_PASSWORDCONFIRM);
- $newNickName = ubRouting::post(self::PROUTE_NICKNAME, 'mres');
- $newEmail = ubRouting::post(self::PROUTE_EMAIL, 'mres');
- $newUserRights = '';
- if (!empty($newLogin)) {
- $userDataPath = USERS_PATH . $newLogin;
- if (!file_exists($userDataPath)) {
- if ($newPasword == $confirmation) {
- if (!empty($newEmail)) {
- if (!empty($newNickName)) {
- $newUserData = array(
- 'admin' => $newUserRights,
- 'password' => md5($newPasword),
- 'nickname' => $newNickName,
- 'username' => $newLogin,
- 'email' => $newEmail,
- 'hideemail' => '1',
- 'tz' => '2'
- );
- $saveUserData = serialize($newUserData);
- file_put_contents($userDataPath, $saveUserData);
- log_register('USER REGISTER {' . $newLogin . '}');
- } else {
- $result .= __('Empty NickName');
- }
- } else {
- $result .= __('Empty email');
- }
- } else {
- $result .= __('Passwords did not match');
- }
- } else {
- $result .= __('User already exists');
- }
- } else {
- $result .= __('Empty login');
- }
- }
- return($result);
- }
- /**
- * Rdeders existing user editing interface
- *
- * @param string $userName
- *
- * @return string
- */
- public function renderEditForm($userName) {
- $result = '';
- $userName = ubRouting::filters($userName, 'vf');
- if (!empty($userName)) {
- if (file_exists(USERS_PATH . $userName)) {
- $currentUserData = $this->system->getUserData($userName);
- $inputs = wf_HiddenInput(self::PROUTE_DOEDIT, $userName);
- $inputs .= wf_PasswordInput(self::PROUTE_PASSWORD, __('New password'), '', true, 20);
- $inputs .= wf_PasswordInput(self::PROUTE_PASSWORDCONFIRM, __('New password confirmation'), '', true, 20);
- $inputs .= wf_TextInput(self::PROUTE_NICKNAME, __('NickName'), $currentUserData['nickname'], true, 20, 'alphanumeric');
- $inputs .= wf_TextInput(self::PROUTE_EMAIL, __('Email'), $currentUserData['email'], true, 20);
- $inputs .= wf_Submit(__('Save'));
- $result .= wf_Form('', 'POST', $inputs, 'glamour');
- } else {
- $result .= $this->messages->getStyledMessage(__('User not exists'), 'error');
- }
- } else {
- $result .= $this->messages->getStyledMessage(__('Empty username'), 'error');
- }
- return($result);
- }
- /**
- * Saves userdata changes if its required
- *
- * @return void/string on error
- */
- public function saveUser() {
- $result = '';
- if (ubRouting::checkPost(self::PROUTE_DOEDIT)) {
- $editUserName = ubRouting::post(self::PROUTE_DOEDIT, 'vf');
- if (!empty($editUserName)) {
- $saveDataPath = USERS_PATH . $editUserName;
- if (file_exists($saveDataPath)) {
- $currentUserData = $this->system->getUserData($editUserName);
- $newUserData = $currentUserData;
- if (!empty($currentUserData)) {
- $updateProfile = false;
- $newPasword = ubRouting::post(self::PROUTE_PASSWORD);
- $confirmation = ubRouting::post(self::PROUTE_PASSWORDCONFIRM);
- $newNickName = ubRouting::post(self::PROUTE_NICKNAME, 'mres');
- $newEmail = ubRouting::post(self::PROUTE_EMAIL, 'mres');
- //password update?
- if (!empty($newPasword)) {
- if ($newPasword == $confirmation) {
- $newPasswordHash = md5($newPasword);
- if ($currentUserData['password'] != $newPasswordHash) {
- //ok its really new password
- $newUserData['password'] = $newPasswordHash;
- $updateProfile = true;
- }
- } else {
- $result .= __('Passwords did not match');
- }
- }
- //nickname update
- if (!empty($newNickName)) {
- if ($currentUserData['nickname'] != $newNickName) {
- $newUserData['nickname'] = $newNickName;
- $updateProfile = true;
- }
- }
- //email update
- if (!empty($newEmail)) {
- if ($currentUserData['email'] != $newEmail) {
- $newUserData['email'] = $newEmail;
- $updateProfile = true;
- }
- }
- //saving profile changes if required
- if ($updateProfile) {
- if (is_writable($saveDataPath)) {
- $newProfileToSave = serialize($newUserData);
- file_put_contents($saveDataPath, $newProfileToSave);
- log_register('USER CHANGE DATA {' . $editUserName . '}');
- } else {
- $result .= __('Profile write failure');
- }
- }
- } else {
- $result .= __('Profile read failure');
- }
- } else {
- $result .= __('User not exists');
- }
- } else {
- $result .= __('Empty username');
- }
- }
- return($result);
- }
- /**
- * Saves user permissions changes if its required
- *
- * @return void/string on error
- */
- public function savePermissions() {
- if (ubRouting::checkPost(self::PROUTE_DOPERMS)) {
- $editUserName = ubRouting::post(self::PROUTE_DOPERMS, 'vf');
- if (!empty($editUserName)) {
- $saveDataPath = USERS_PATH . $editUserName;
- if (file_exists($saveDataPath)) {
- $currentUserData = $this->system->getUserData($editUserName);
- $newUserData = $currentUserData;
- if (!empty($currentUserData)) {
- $updateProfile = false;
- $currentRootState = ($currentUserData['admin'] == '*') ? true : false;
- $newRootState = (ubRouting::checkPost(self::PROUTE_ROOTUSER)) ? true : false;
- $oldRightString = $currentUserData['admin'];
- $systemRights = $this->system->getRightsDatabase();
- $newRightsString = '';
- if (ubRouting::checkPost('_rights')) {
- $rightsTmp = ubRouting::post('_rights');
- if (!empty($rightsTmp) AND is_array($rightsTmp)) {
- foreach ($rightsTmp as $eachRight => $rightState) {
- if (isset($systemRights[$eachRight])) {
- //skipping unknown rights
- $newRightsString .= '|' . $eachRight . '|';
- }
- }
- }
- }
- //new user state is "have root permisssions"
- if ($newRootState) {
- $newRightsString = '*';
- }
- //take decision to update rights
- if ($newRightsString != $oldRightString) {
- $updateProfile = true;
- $newUserData['admin'] = $newRightsString;
- }
- if ($updateProfile) {
- if (is_writable($saveDataPath)) {
- $newProfileToSave = serialize($newUserData);
- file_put_contents($saveDataPath, $newProfileToSave);
- log_register('USER CHANGE PERMISSIONS {' . $editUserName . '}');
- } else {
- $result .= __('Profile write failure');
- }
- }
- } else {
- $result .= __('Profile read failure');
- }
- } else {
- $result .= __('User not exists');
- }
- } else {
- $result .= __('Empty username');
- }
- }
- }
- /**
- * Renders form for editing users permissions
- *
- * @param string $userName
- *
- * @return string
- */
- public function renderPermissionsForm($userName) {
- $result = '';
- $userName = ubRouting::filters($userName, 'vf');
- if (!empty($userName)) {
- if (file_exists(USERS_PATH . $userName)) {
- $currentUserData = $this->system->getUserData($userName);
- if (!empty($currentUserData)) {
- $rootRights = false;
- $currentRightsString = $currentUserData['admin'];
- $currentRightsArr = array();
- $systemRights = $this->system->getRightsDatabase();
- if ($currentRightsString !== '*') {
- preg_match_all('/\|(.*?)\|/', $currentRightsString, $rights_r);
- if (!empty($rights_r[1])) {
- foreach ($rights_r[1] as $right) {
- if (isset($systemRights[$right])) {
- $currentRightsArr[$right] = $right;
- }
- }
- }
- } else {
- $rootRights = true;
- }
- //form here
- $inputs = wf_HiddenInput(self::PROUTE_DOPERMS, $userName);
- $inputs .= wf_CheckInput(self::PROUTE_ROOTUSER, __('User have all available rights and permissions'), true, $rootRights);
- $inputs .= wf_tag('hr');
- if (!$rootRights) {
- if (!empty($systemRights)) {
- foreach ($systemRights as $eachRightId => $eachRightDesc) {
- $haveThisRight = (isset($currentRightsArr[$eachRightId])) ? true : false;
- $rightLabel = __($eachRightDesc) . ' - ' . $eachRightId;
- $inputs .= wf_CheckInput('_rights[' . $eachRightId . ']', $rightLabel, true, $haveThisRight);
- }
- }
- }
- $inputs .= wf_Submit(__('Save'));
- $result .= wf_Form('', 'POST', $inputs, 'glamour');
- } else {
- $result .= $this->messages->getStyledMessage(__('Profile read failure'), 'error');
- }
- } else {
- $result .= $this->messages->getStyledMessage(__('User not exists'), 'error');
- }
- } else {
- $result .= $this->messages->getStyledMessage(__('Empty username'), 'error');
- }
- return($result);
- }
- }
|