editcomponents.cgi 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/usr/bin/env perl -wT
  2. # -*- Mode: perl; indent-tabs-mode: nil -*-
  3. #
  4. # The contents of this file are subject to the Mozilla Public
  5. # License Version 1.1 (the "License"); you may not use this file
  6. # except in compliance with the License. You may obtain a copy of
  7. # the License at http://www.mozilla.org/MPL/
  8. #
  9. # Software distributed under the License is distributed on an "AS
  10. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11. # implied. See the License for the specific language governing
  12. # rights and limitations under the License.
  13. #
  14. # The Original Code is mozilla.org code.
  15. #
  16. # The Initial Developer of the Original Code is Holger
  17. # Schurig. Portions created by Holger Schurig are
  18. # Copyright (C) 1999 Holger Schurig. All
  19. # Rights Reserved.
  20. #
  21. # Contributor(s): Holger Schurig <holgerschurig@nikocity.de>
  22. # Terry Weissman <terry@mozilla.org>
  23. # Frédéric Buclin <LpSolit@gmail.com>
  24. # Akamai Technologies <bugzilla-dev@akamai.com>
  25. use strict;
  26. use lib qw(. lib);
  27. use Bugzilla;
  28. use Bugzilla::Constants;
  29. use Bugzilla::Util;
  30. use Bugzilla::Error;
  31. use Bugzilla::User;
  32. use Bugzilla::Component;
  33. use Bugzilla::Token;
  34. my $cgi = Bugzilla->cgi;
  35. my $template = Bugzilla->template;
  36. my $vars = {};
  37. # There is only one section about components in the documentation,
  38. # so all actions point to the same page.
  39. $vars->{'doc_section'} = 'components.html';
  40. #
  41. # Preliminary checks:
  42. #
  43. my $user = Bugzilla->login(LOGIN_REQUIRED);
  44. print $cgi->header();
  45. $user->in_group('editcomponents')
  46. || scalar(@{$user->get_products_by_permission('editcomponents')})
  47. || ThrowUserError("auth_failure", {group => "editcomponents",
  48. action => "edit",
  49. object => "components"});
  50. #
  51. # often used variables
  52. #
  53. my $product_name = trim($cgi->param('product') || '');
  54. my $comp_name = trim($cgi->param('component') || '');
  55. my $action = trim($cgi->param('action') || '');
  56. my $showbugcounts = (defined $cgi->param('showbugcounts'));
  57. my $token = $cgi->param('token');
  58. #
  59. # product = '' -> Show nice list of products
  60. #
  61. unless ($product_name) {
  62. my $selectable_products = $user->get_selectable_products;
  63. # If the user has editcomponents privs for some products only,
  64. # we have to restrict the list of products to display.
  65. unless ($user->in_group('editcomponents')) {
  66. $selectable_products = $user->get_products_by_permission('editcomponents');
  67. }
  68. $vars->{'products'} = $selectable_products;
  69. $vars->{'showbugcounts'} = $showbugcounts;
  70. $template->process("admin/components/select-product.html.tmpl", $vars)
  71. || ThrowTemplateError($template->error());
  72. exit;
  73. }
  74. my $product = $user->check_can_admin_product($product_name);
  75. #
  76. # action='' -> Show nice list of components
  77. #
  78. unless ($action) {
  79. $vars->{'showbugcounts'} = $showbugcounts;
  80. $vars->{'product'} = $product;
  81. $template->process("admin/components/list.html.tmpl", $vars)
  82. || ThrowTemplateError($template->error());
  83. exit;
  84. }
  85. #
  86. # action='add' -> present form for parameters for new component
  87. #
  88. # (next action will be 'new')
  89. #
  90. if ($action eq 'add') {
  91. $vars->{'token'} = issue_session_token('add_component');
  92. $vars->{'product'} = $product;
  93. $template->process("admin/components/create.html.tmpl", $vars)
  94. || ThrowTemplateError($template->error());
  95. exit;
  96. }
  97. #
  98. # action='new' -> add component entered in the 'action=add' screen
  99. #
  100. if ($action eq 'new') {
  101. check_token_data($token, 'add_component');
  102. # Do the user matching
  103. Bugzilla::User::match_field ($cgi, {
  104. 'initialowner' => { 'type' => 'single' },
  105. 'initialqacontact' => { 'type' => 'single' },
  106. 'initialcc' => { 'type' => 'multi' },
  107. });
  108. my $default_assignee = trim($cgi->param('initialowner') || '');
  109. my $default_qa_contact = trim($cgi->param('initialqacontact') || '');
  110. my $description = trim($cgi->param('description') || '');
  111. my @initial_cc = $cgi->param('initialcc');
  112. my $component =
  113. Bugzilla::Component->create({ name => $comp_name,
  114. product => $product,
  115. description => $description,
  116. initialowner => $default_assignee,
  117. initialqacontact => $default_qa_contact,
  118. initial_cc => \@initial_cc });
  119. $vars->{'message'} = 'component_created';
  120. $vars->{'comp'} = $component;
  121. $vars->{'product'} = $product;
  122. delete_token($token);
  123. $template->process("admin/components/list.html.tmpl", $vars)
  124. || ThrowTemplateError($template->error());
  125. exit;
  126. }
  127. #
  128. # action='del' -> ask if user really wants to delete
  129. #
  130. # (next action would be 'delete')
  131. #
  132. if ($action eq 'del') {
  133. $vars->{'token'} = issue_session_token('delete_component');
  134. $vars->{'comp'} =
  135. Bugzilla::Component->check({ product => $product, name => $comp_name });
  136. $vars->{'product'} = $product;
  137. $template->process("admin/components/confirm-delete.html.tmpl", $vars)
  138. || ThrowTemplateError($template->error());
  139. exit;
  140. }
  141. #
  142. # action='delete' -> really delete the component
  143. #
  144. if ($action eq 'delete') {
  145. check_token_data($token, 'delete_component');
  146. my $component =
  147. Bugzilla::Component->check({ product => $product, name => $comp_name });
  148. $component->remove_from_db;
  149. $vars->{'message'} = 'component_deleted';
  150. $vars->{'comp'} = $component;
  151. $vars->{'product'} = $product;
  152. $vars->{'no_edit_component_link'} = 1;
  153. delete_token($token);
  154. $template->process("admin/components/list.html.tmpl", $vars)
  155. || ThrowTemplateError($template->error());
  156. exit;
  157. }
  158. #
  159. # action='edit' -> present the edit component form
  160. #
  161. # (next action would be 'update')
  162. #
  163. if ($action eq 'edit') {
  164. $vars->{'token'} = issue_session_token('edit_component');
  165. my $component =
  166. Bugzilla::Component->check({ product => $product, name => $comp_name });
  167. $vars->{'comp'} = $component;
  168. $vars->{'initial_cc_names'} =
  169. join(', ', map($_->login, @{$component->initial_cc}));
  170. $vars->{'product'} = $product;
  171. $template->process("admin/components/edit.html.tmpl", $vars)
  172. || ThrowTemplateError($template->error());
  173. exit;
  174. }
  175. #
  176. # action='update' -> update the component
  177. #
  178. if ($action eq 'update') {
  179. check_token_data($token, 'edit_component');
  180. # Do the user matching
  181. Bugzilla::User::match_field ($cgi, {
  182. 'initialowner' => { 'type' => 'single' },
  183. 'initialqacontact' => { 'type' => 'single' },
  184. 'initialcc' => { 'type' => 'multi' },
  185. });
  186. my $comp_old_name = trim($cgi->param('componentold') || '');
  187. my $default_assignee = trim($cgi->param('initialowner') || '');
  188. my $default_qa_contact = trim($cgi->param('initialqacontact') || '');
  189. my $description = trim($cgi->param('description') || '');
  190. my @initial_cc = $cgi->param('initialcc');
  191. my $component =
  192. Bugzilla::Component->check({ product => $product, name => $comp_old_name });
  193. $component->set_name($comp_name);
  194. $component->set_description($description);
  195. $component->set_default_assignee($default_assignee);
  196. $component->set_default_qa_contact($default_qa_contact);
  197. $component->set_cc_list(\@initial_cc);
  198. my $changes = $component->update();
  199. $vars->{'message'} = 'component_updated';
  200. $vars->{'comp'} = $component;
  201. $vars->{'product'} = $product;
  202. $vars->{'changes'} = $changes;
  203. delete_token($token);
  204. $template->process("admin/components/list.html.tmpl", $vars)
  205. || ThrowTemplateError($template->error());
  206. exit;
  207. }
  208. #
  209. # No valid action found
  210. #
  211. ThrowUserError('no_valid_action', {'field' => "component"});