You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdminHandler.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. # $Id: AdminHandler.php 1754 2015-03-17 22:37:34Z christian_boltz $
  3. class AdminHandler extends PFAHandler {
  4. protected $db_table = 'admin';
  5. protected $id_field = 'username';
  6. protected function validate_new_id() {
  7. $email_check = check_email($this->id);
  8. if ($email_check == '') {
  9. return true;
  10. } else {
  11. $this->errormsg[] = $email_check;
  12. $this->errormsg[$this->id_field] = Config::lang('pAdminCreate_admin_username_text_error1');
  13. return false;
  14. }
  15. }
  16. protected function no_domain_field() {
  17. # PFAHandler die()s if domain field is not set. Disable this behaviour for AdminHandler.
  18. }
  19. # init $this->struct, $this->db_table and $this->id_field
  20. protected function initStruct() {
  21. # NOTE: There are dependencies between domains and domain_count
  22. # NOTE: If you disable "display in list" for domain_count, the SQL query for domains might break.
  23. # NOTE: (Disabling both shouldn't be a problem.)
  24. # TODO: move to a db_group_concat() function?
  25. if (db_pgsql()) {
  26. $domains_grouped = "array_to_string(array_agg(domain), ',')";
  27. } else { # mysql
  28. $domains_grouped = 'group_concat(domain)';
  29. }
  30. $this->struct=array(
  31. # field name allow display in... type $PALANG label $PALANG description default / options / ...
  32. # editing? form list
  33. 'username' => pacol( $this->new, 1, 1, 'text', 'admin' , 'email_address' , '', '',
  34. array('linkto' => 'list.php?table=domain&username=%s') ),
  35. 'password' => pacol( 1, 1, 0, 'pass', 'password' , '' ),
  36. 'password2' => pacol( 1, 1, 0, 'pass', 'password_again' , '' , '', '',
  37. /*not_in_db*/ 0,
  38. /*dont_write_to_db*/ 1,
  39. /*select*/ 'password as password2'
  40. ),
  41. 'superadmin' => pacol( 1, 1, 0, 'bool', 'super_admin' , 'super_admin_desc' , 0
  42. # TODO: (finally) replace the ALL domain with a column in the admin table
  43. # TODO: current status: 'superadmin' column exists and is written when storing an admin with AdminHandler,
  44. # TODO: but the superadmin status is still (additionally) stored in the domain_admins table ("ALL" dummy domain)
  45. # TODO: to keep the database backwards-compatible with 2.3.x.
  46. # TODO: Note: superadmins created with 2.3.x after running upgrade_1284() will not work until you re-run upgrade_1284()
  47. # TODO: Create them with the trunk version to avoid this problem.
  48. ),
  49. 'domains' => pacol( 1, 1, 0, 'list', 'domain' , '' , array(), list_domains(),
  50. /*not_in_db*/ 0,
  51. /*dont_write_to_db*/ 1,
  52. /*select*/ "coalesce(domains,'') as domains"
  53. /*extrafrom set in domain_count*/
  54. ),
  55. 'domain_count' => pacol( 0, 0, 1, 'vnum', 'pAdminList_admin_count', '' , '', '',
  56. /*not_in_db*/ 0,
  57. /*dont_write_to_db*/ 1,
  58. /*select*/ 'coalesce(__domain_count,0) as domain_count',
  59. /*extrafrom*/ 'LEFT JOIN ( ' .
  60. ' SELECT count(*) AS __domain_count, ' . $domains_grouped . ' AS domains, username AS __domain_username ' .
  61. ' FROM ' . table_by_key('domain_admins') .
  62. " WHERE domain != 'ALL' GROUP BY username " .
  63. ' ) AS __domain on username = __domain_username'),
  64. 'active' => pacol( 1, 1, 1, 'bool', 'active' , '' , 1 ),
  65. 'created' => pacol( 0, 0, 0, 'ts', 'created' , '' ),
  66. 'modified' => pacol( 0, 0, 1, 'ts', 'last_modified' , '' ),
  67. );
  68. }
  69. protected function initMsg() {
  70. $this->msg['error_already_exists'] = 'admin_already_exists';
  71. $this->msg['error_does_not_exist'] = 'admin_does_not_exist';
  72. $this->msg['confirm_delete'] = 'confirm_delete_admin';
  73. if ($this->new) {
  74. $this->msg['logname'] = 'create_admin';
  75. $this->msg['store_error'] = 'pAdminCreate_admin_result_error';
  76. $this->msg['successmessage'] = 'pAdminCreate_admin_result_success';
  77. } else {
  78. $this->msg['logname'] = 'edit_admin';
  79. $this->msg['store_error'] = 'pAdminEdit_admin_result_error';
  80. $this->msg['successmessage'] = 'pAdminEdit_admin_result_success';
  81. }
  82. }
  83. public function webformConfig() {
  84. return array(
  85. # $PALANG labels
  86. 'formtitle_create' => 'pAdminCreate_admin_welcome',
  87. 'formtitle_edit' => 'pAdminEdit_admin_welcome',
  88. 'create_button' => 'pAdminCreate_admin_button',
  89. # various settings
  90. 'required_role' => 'global-admin',
  91. 'listview' => 'list.php?table=admin',
  92. 'early_init' => 0,
  93. );
  94. }
  95. /**
  96. * called by $this->store() after storing $this->values in the database
  97. * can be used to update additional tables, call scripts etc.
  98. */
  99. protected function storemore() {
  100. # store list of allowed domains in the domain_admins table
  101. if (isset($this->values['domains'])) {
  102. if (is_array($this->values['domains'])) {
  103. $domains = $this->values['domains'];
  104. } elseif ($this->values['domains'] == '') {
  105. $domains = array();
  106. } else {
  107. $domains = explode(',', $this->values['domains']);
  108. }
  109. db_delete('domain_admins', 'username', $this->id, "AND domain != 'ALL'");
  110. foreach ($domains as $domain) {
  111. $values = array(
  112. 'username' => $this->id,
  113. 'domain' => $domain,
  114. );
  115. db_insert('domain_admins', $values, array('created'));
  116. # TODO: check for errors
  117. }
  118. }
  119. # Temporary workaround to keep the database compatible with 2.3.x
  120. if (isset($this->values['superadmin'])) {
  121. if ($this->values['superadmin'] == 1) {
  122. $values = array(
  123. 'username' => $this->id,
  124. 'domain' => 'ALL',
  125. );
  126. $where = db_where_clause(array('username' => $this->id, 'domain' => 'ALL'), $this->struct);
  127. $result = db_query("SELECT username from " . table_by_key('domain_admins') . " " . $where);
  128. if ($result['rows'] == 0) {
  129. db_insert('domain_admins', $values, array('created'));
  130. # TODO: check for errors
  131. }
  132. } else {
  133. db_delete('domain_admins', 'username', $this->id, "AND domain = 'ALL'");
  134. }
  135. }
  136. return true; # TODO: don't hardcode
  137. }
  138. protected function read_from_db_postprocess($db_result) {
  139. foreach ($db_result as $key => $row) {
  140. # convert 'domains' field to an array
  141. if ($row['domains'] == '') {
  142. $db_result[$key]['domains'] = array();
  143. } else {
  144. $db_result[$key]['domains'] = explode(',', $row['domains']);
  145. }
  146. if ($row['superadmin']) {
  147. $db_result[$key]['domain_count'] = Config::lang('super_admin');
  148. }
  149. }
  150. return $db_result;
  151. }
  152. /**
  153. * @return true on success false on failure
  154. */
  155. public function delete() {
  156. if ( ! $this->view() ) {
  157. $this->errormsg[] = Config::Lang($this->msg['error_does_not_exist']);
  158. return false;
  159. }
  160. db_delete('domain_admins', $this->id_field, $this->id);
  161. db_delete($this->db_table, $this->id_field, $this->id);
  162. db_log ('admin', 'delete_admin', $this->id); # TODO delete_admin is not a valid db_log keyword yet, and 'admin' is not displayed in viewlog.php
  163. $this->infomsg[] = Config::Lang_f('pDelete_delete_success', $this->id);
  164. return true;
  165. }
  166. # TODO: generate password if $new, no password specified and $CONF['generate_password'] is set
  167. # TODO: except if $this->admin_username == setup.php --- this exception should be handled directly in setup.php ("if $values['password'] == '' error_out")
  168. /**
  169. * compare password / password2 field
  170. * error message will be displayed at the password2 field
  171. */
  172. protected function _validate_password2($field, $val) {
  173. return $this->compare_password_fields('password', 'password2');
  174. }
  175. }
  176. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */