user_edit.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. // including necessary libraries
  4. $cidReset = true;
  5. require_once __DIR__.'/../inc/global.inc.php';
  6. // user permissions
  7. api_block_anonymous_users();
  8. if (!api_is_platform_admin()) {
  9. if (!api_is_drh()) {
  10. api_not_allowed(true);
  11. }
  12. } else {
  13. api_protect_admin_script();
  14. }
  15. // Database table definitions
  16. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  17. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  18. $database = Database::get_main_database();
  19. $userId = isset($_REQUEST['user_id']) ? intval($_REQUEST['user_id']) : '';
  20. $userInfo = api_get_user_info($userId);
  21. if (empty($userInfo)) {
  22. api_not_allowed(true);
  23. }
  24. $userIsFollowed = UserManager::is_user_followed_by_drh($userId, api_get_user_id());
  25. if (api_drh_can_access_all_session_content()) {
  26. $students = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  27. 'drh_all',
  28. api_get_user_id(),
  29. false,
  30. 0, //$from,
  31. null, //$limit,
  32. null, //$column,
  33. 'desc', //$direction,
  34. null, //$keyword,
  35. null, //$active,
  36. null, //$lastConnectionDate,
  37. null,
  38. null,
  39. STUDENT
  40. );
  41. if (empty($students)) {
  42. api_not_allowed(true);
  43. }
  44. $userIdList = array();
  45. foreach ($students as $student) {
  46. $userIdList[] = $student['user_id'];
  47. }
  48. if (!in_array($userId, $userIdList)) {
  49. api_not_allowed(true);
  50. }
  51. } else {
  52. if (!$userIsFollowed) {
  53. api_not_allowed(true);
  54. }
  55. }
  56. $url = api_get_self().'?user_id='.$userId;
  57. $tool_name = get_lang('ModifyUserInfo');
  58. // Create the form
  59. $form = new FormValidator('user_edit', 'post', $url);
  60. // Username
  61. $usernameInput = $form->addElement('text', 'username', get_lang('LoginName'));
  62. $usernameInput->freeze();
  63. // Password
  64. $group = array();
  65. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  66. /*if (count($extAuthSource) > 0) {
  67. $group[] =& $form->createElement('radio', 'password_auto', null, get_lang('ExternalAuthentication').' ', 2);
  68. $auth_sources = array();
  69. foreach ($extAuthSource as $key => $info) {
  70. $auth_sources[$key] = $key;
  71. }
  72. $group[] =& $form->createElement('select', 'auth_source', null, $auth_sources);
  73. $group[] =& $form->createElement('static', '', '', '<br />');
  74. }*/
  75. $group[] = & $form->createElement('radio', 'password_auto', get_lang('Password'), get_lang('AutoGeneratePassword').'<br />', 1);
  76. $group[] = & $form->createElement('radio', 'password_auto', 'id="radio_user_password"', null, 0);
  77. $group[] = & $form->createElement('password', 'password', null, array('onkeydown' => 'javascript: password_switch_radio_button(document.user_add,"password[password_auto]");'));
  78. $form->addGroup($group, 'password', get_lang('Password'));
  79. // Send email
  80. $group = array();
  81. $group[] = & $form->createElement('radio', 'send_mail', null, get_lang('Yes'), 1);
  82. $group[] = & $form->createElement('radio', 'send_mail', null, get_lang('No'), 0);
  83. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'));
  84. // Set default values
  85. $defaults = array();
  86. $defaults['username'] = $userInfo['username'];
  87. $defaults['mail']['send_mail'] = 0;
  88. $defaults['password']['password_auto'] = 1;
  89. $form->setDefaults($defaults);
  90. // Submit button
  91. $select_level = array();
  92. $html_results_enabled[] = $form->addButtonUpdate(get_lang('Update'), 'submit', true);
  93. $form->addGroup($html_results_enabled);
  94. // Validate form
  95. if ($form->validate()) {
  96. $check = Security::check_token('post');
  97. if ($check) {
  98. $user = $form->exportValues();
  99. $email = $userInfo['email'];
  100. $username = $userInfo['username'];
  101. $send_mail = intval($user['mail']['send_mail']);
  102. $auth_source = PLATFORM_AUTH_SOURCE;
  103. $resetPassword = $user['password']['password_auto'] == '1' ? 0 : 2;
  104. if (count($extAuthSource) > 0 && $user['password']['password_auto'] == '2') {
  105. //$auth_source = $user['password']['auth_source'];
  106. //$password = 'PLACEHOLDER';
  107. } else {
  108. //$auth_source = PLATFORM_AUTH_SOURCE;
  109. //$password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  110. }
  111. $auth_source = $userInfo['auth_source'];
  112. $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  113. UserManager::update_user(
  114. $userId,
  115. $userInfo['firstname'],
  116. $userInfo['lastname'],
  117. $userInfo['username'],
  118. $password,
  119. $auth_source,
  120. $userInfo['email'],
  121. $userInfo['status'],
  122. $userInfo['official_code'],
  123. $userInfo['phone'],
  124. $userInfo['picture_uri'],
  125. $userInfo['expiration_date'],
  126. $userInfo['active'],
  127. $userInfo['creator_id'],
  128. $userInfo['hr_dept_id'],
  129. null, //$extra =
  130. $userInfo['language'],
  131. null, //$encrypt_method
  132. false,
  133. $resetPassword
  134. );
  135. if (!empty($email) && $send_mail) {
  136. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  137. $portal_url = api_get_path(WEB_PATH);
  138. if (api_is_multiple_url_enabled()) {
  139. $access_url_id = api_get_current_access_url_id();
  140. if ($access_url_id != -1) {
  141. $url = api_get_access_url($access_url_id);
  142. $portal_url = $url['url'];
  143. }
  144. }
  145. $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($userInfo['firstname'], $userInfo['lastname'])).",\n\n".
  146. get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".
  147. get_lang('Username')." : ".$username."\n".get_lang('Pass')." : ".stripslashes($password)."\n\n".
  148. get_lang('Address')." ".api_get_setting('siteName')." ".
  149. get_lang('Is')." : ".$portal_url."\n\n".
  150. get_lang('Problem')."\n\n".
  151. get_lang('SignatureFormula').",\n\n".
  152. api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".
  153. get_lang('Manager')." ".
  154. api_get_setting('siteName')."\nT. ".
  155. api_get_setting('administratorTelephone')."\n".
  156. get_lang('Email')." : ".api_get_setting('emailAdministrator');
  157. $emailbody = nl2br($emailbody);
  158. api_mail_html(
  159. api_get_person_name($userInfo['firstname'], $userInfo['lastname'], null, PERSON_NAME_EMAIL_ADDRESS),
  160. $email,
  161. $emailsubject,
  162. $emailbody
  163. );
  164. }
  165. Security::clear_token();
  166. $tok = Security::get_token();
  167. header('Location: '.$url.'&message=1');
  168. exit();
  169. }
  170. } else {
  171. if (isset($_POST['submit'])) {
  172. Security::clear_token();
  173. }
  174. $token = Security::get_token();
  175. $form->addElement('hidden', 'sec_token');
  176. $form->setConstants(array('sec_token' => $token));
  177. }
  178. $interbreadcrumb[] = array(
  179. 'url' => api_get_path(WEB_CODE_PATH)."mySpace/student.php",
  180. "name" => get_lang('UserList'),
  181. );
  182. if (isset($_REQUEST['message'])) {
  183. Display::addFlash(Display::return_message(get_lang('Updated'), 'normal'));
  184. }
  185. Display::display_header($tool_name);
  186. // Display form
  187. $form->display();
  188. Display::display_footer();