Group.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. # -*- Mode: perl; indent-tabs-mode: nil -*-
  2. #
  3. # The contents of this file are subject to the Mozilla Public
  4. # License Version 1.1 (the "License"); you may not use this file
  5. # except in compliance with the License. You may obtain a copy of
  6. # the License at http://www.mozilla.org/MPL/
  7. #
  8. # Software distributed under the License is distributed on an "AS
  9. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. # implied. See the License for the specific language governing
  11. # rights and limitations under the License.
  12. #
  13. # The Original Code is the Bugzilla Bug Tracking System.
  14. #
  15. # The Initial Developer of the Original Code is Netscape Communications
  16. # Corporation. Portions created by Netscape are
  17. # Copyright (C) 1998 Netscape Communications Corporation. All
  18. # Rights Reserved.
  19. #
  20. # Contributor(s): Joel Peshkin <bugreport@peshkin.net>
  21. # Erik Stambaugh <erik@dasbistro.com>
  22. # Tiago R. Mello <timello@async.com.br>
  23. # Max Kanat-Alexander <mkanat@bugzilla.org>
  24. use strict;
  25. package Bugzilla::Group;
  26. use base qw(Bugzilla::Object);
  27. use Bugzilla::Constants;
  28. use Bugzilla::Util;
  29. use Bugzilla::Error;
  30. use Bugzilla::Config qw(:admin);
  31. ###############################
  32. ##### Module Initialization ###
  33. ###############################
  34. use constant DB_COLUMNS => qw(
  35. groups.id
  36. groups.name
  37. groups.description
  38. groups.isbuggroup
  39. groups.userregexp
  40. groups.isactive
  41. groups.icon_url
  42. );
  43. use constant DB_TABLE => 'groups';
  44. use constant LIST_ORDER => 'isbuggroup, name';
  45. use constant VALIDATORS => {
  46. name => \&_check_name,
  47. description => \&_check_description,
  48. userregexp => \&_check_user_regexp,
  49. isactive => \&_check_is_active,
  50. isbuggroup => \&_check_is_bug_group,
  51. icon_url => \&_check_icon_url,
  52. };
  53. use constant REQUIRED_CREATE_FIELDS => qw(name description isbuggroup);
  54. use constant UPDATE_COLUMNS => qw(
  55. name
  56. description
  57. userregexp
  58. isactive
  59. icon_url
  60. );
  61. # Parameters that are lists of groups.
  62. use constant GROUP_PARAMS => qw(chartgroup insidergroup timetrackinggroup
  63. querysharegroup);
  64. ###############################
  65. #### Accessors ######
  66. ###############################
  67. sub description { return $_[0]->{'description'}; }
  68. sub is_bug_group { return $_[0]->{'isbuggroup'}; }
  69. sub user_regexp { return $_[0]->{'userregexp'}; }
  70. sub is_active { return $_[0]->{'isactive'}; }
  71. sub icon_url { return $_[0]->{'icon_url'}; }
  72. sub members_direct {
  73. my ($self) = @_;
  74. return $self->{members_direct} if defined $self->{members_direct};
  75. my $dbh = Bugzilla->dbh;
  76. my $user_ids = $dbh->selectcol_arrayref(
  77. "SELECT user_group_map.user_id
  78. FROM user_group_map
  79. WHERE user_group_map.group_id = ?
  80. AND grant_type = " . GRANT_DIRECT . "
  81. AND isbless = 0", undef, $self->id);
  82. require Bugzilla::User;
  83. $self->{members_direct} = Bugzilla::User->new_from_list($user_ids);
  84. return $self->{members_direct};
  85. }
  86. sub grant_direct {
  87. my ($self, $type) = @_;
  88. $self->{grant_direct} ||= {};
  89. return $self->{grant_direct}->{$type}
  90. if defined $self->{members_direct}->{$type};
  91. my $dbh = Bugzilla->dbh;
  92. my $ids = $dbh->selectcol_arrayref(
  93. "SELECT member_id FROM group_group_map
  94. WHERE grantor_id = ? AND grant_type = $type",
  95. undef, $self->id) || [];
  96. $self->{grant_direct}->{$type} = $self->new_from_list($ids);
  97. return $self->{grant_direct}->{$type};
  98. }
  99. sub granted_by_direct {
  100. my ($self, $type) = @_;
  101. $self->{granted_by_direct} ||= {};
  102. return $self->{granted_by_direct}->{$type}
  103. if defined $self->{granted_by_direct}->{$type};
  104. my $dbh = Bugzilla->dbh;
  105. my $ids = $dbh->selectcol_arrayref(
  106. "SELECT grantor_id FROM group_group_map
  107. WHERE member_id = ? AND grant_type = $type",
  108. undef, $self->id) || [];
  109. $self->{granted_by_direct}->{$type} = $self->new_from_list($ids);
  110. return $self->{granted_by_direct}->{$type};
  111. }
  112. ###############################
  113. #### Methods ####
  114. ###############################
  115. sub set_description { $_[0]->set('description', $_[1]); }
  116. sub set_is_active { $_[0]->set('isactive', $_[1]); }
  117. sub set_name { $_[0]->set('name', $_[1]); }
  118. sub set_user_regexp { $_[0]->set('userregexp', $_[1]); }
  119. sub set_icon_url { $_[0]->set('icon_url', $_[1]); }
  120. sub update {
  121. my $self = shift;
  122. my $changes = $self->SUPER::update(@_);
  123. if (exists $changes->{name}) {
  124. my ($old_name, $new_name) = @{$changes->{name}};
  125. my $update_params;
  126. foreach my $group (GROUP_PARAMS) {
  127. if ($old_name eq Bugzilla->params->{$group}) {
  128. SetParam($group, $new_name);
  129. $update_params = 1;
  130. }
  131. }
  132. write_params() if $update_params;
  133. }
  134. # If we've changed this group to be active, fix any Mandatory groups.
  135. $self->_enforce_mandatory if (exists $changes->{isactive}
  136. && $changes->{isactive}->[1]);
  137. $self->_rederive_regexp() if exists $changes->{userregexp};
  138. return $changes;
  139. }
  140. # Add missing entries in bug_group_map for bugs created while
  141. # a mandatory group was disabled and which is now enabled again.
  142. sub _enforce_mandatory {
  143. my ($self) = @_;
  144. my $dbh = Bugzilla->dbh;
  145. my $gid = $self->id;
  146. my $bug_ids =
  147. $dbh->selectcol_arrayref('SELECT bugs.bug_id
  148. FROM bugs
  149. INNER JOIN group_control_map
  150. ON group_control_map.product_id = bugs.product_id
  151. LEFT JOIN bug_group_map
  152. ON bug_group_map.bug_id = bugs.bug_id
  153. AND bug_group_map.group_id = group_control_map.group_id
  154. WHERE group_control_map.group_id = ?
  155. AND group_control_map.membercontrol = ?
  156. AND bug_group_map.group_id IS NULL',
  157. undef, ($gid, CONTROLMAPMANDATORY));
  158. my $sth = $dbh->prepare('INSERT INTO bug_group_map (bug_id, group_id) VALUES (?, ?)');
  159. foreach my $bug_id (@$bug_ids) {
  160. $sth->execute($bug_id, $gid);
  161. }
  162. }
  163. sub is_active_bug_group {
  164. my $self = shift;
  165. return $self->is_active && $self->is_bug_group;
  166. }
  167. sub _rederive_regexp {
  168. my ($self) = @_;
  169. RederiveRegexp($self->user_regexp, $self->id);
  170. }
  171. sub members_non_inherited {
  172. my ($self) = @_;
  173. return $self->{members_non_inherited}
  174. if exists $self->{members_non_inherited};
  175. my $member_ids = Bugzilla->dbh->selectcol_arrayref(
  176. 'SELECT DISTINCT user_id FROM user_group_map
  177. WHERE isbless = 0 AND group_id = ?',
  178. undef, $self->id) || [];
  179. require Bugzilla::User;
  180. $self->{members_non_inherited} = Bugzilla::User->new_from_list($member_ids);
  181. return $self->{members_non_inherited};
  182. }
  183. ################################
  184. ##### Module Subroutines ###
  185. ################################
  186. sub create {
  187. my $class = shift;
  188. my ($params) = @_;
  189. my $dbh = Bugzilla->dbh;
  190. print get_text('install_group_create', { name => $params->{name} }) . "\n"
  191. if Bugzilla->usage_mode == USAGE_MODE_CMDLINE;
  192. my $group = $class->SUPER::create(@_);
  193. # Since we created a new group, give the "admin" group all privileges
  194. # initially.
  195. my $admin = new Bugzilla::Group({name => 'admin'});
  196. # This function is also used to create the "admin" group itself,
  197. # so there's a chance it won't exist yet.
  198. if ($admin) {
  199. my $sth = $dbh->prepare('INSERT INTO group_group_map
  200. (member_id, grantor_id, grant_type)
  201. VALUES (?, ?, ?)');
  202. $sth->execute($admin->id, $group->id, GROUP_MEMBERSHIP);
  203. $sth->execute($admin->id, $group->id, GROUP_BLESS);
  204. $sth->execute($admin->id, $group->id, GROUP_VISIBLE);
  205. }
  206. $group->_rederive_regexp() if $group->user_regexp;
  207. return $group;
  208. }
  209. sub ValidateGroupName {
  210. my ($name, @users) = (@_);
  211. my $dbh = Bugzilla->dbh;
  212. my $query = "SELECT id FROM groups " .
  213. "WHERE name = ?";
  214. if (Bugzilla->params->{'usevisibilitygroups'}) {
  215. my @visible = (-1);
  216. foreach my $user (@users) {
  217. $user && push @visible, @{$user->visible_groups_direct};
  218. }
  219. my $visible = join(', ', @visible);
  220. $query .= " AND id IN($visible)";
  221. }
  222. my $sth = $dbh->prepare($query);
  223. $sth->execute($name);
  224. my ($ret) = $sth->fetchrow_array();
  225. return $ret;
  226. }
  227. # This sub is not perldoc'ed because we expect it to go away and
  228. # just become the _rederive_regexp private method.
  229. sub RederiveRegexp {
  230. my ($regexp, $gid) = @_;
  231. my $dbh = Bugzilla->dbh;
  232. my $sth = $dbh->prepare("SELECT userid, login_name, group_id
  233. FROM profiles
  234. LEFT JOIN user_group_map
  235. ON user_group_map.user_id = profiles.userid
  236. AND group_id = ?
  237. AND grant_type = ?
  238. AND isbless = 0");
  239. my $sthadd = $dbh->prepare("INSERT INTO user_group_map
  240. (user_id, group_id, grant_type, isbless)
  241. VALUES (?, ?, ?, 0)");
  242. my $sthdel = $dbh->prepare("DELETE FROM user_group_map
  243. WHERE user_id = ? AND group_id = ?
  244. AND grant_type = ? and isbless = 0");
  245. $sth->execute($gid, GRANT_REGEXP);
  246. while (my ($uid, $login, $present) = $sth->fetchrow_array()) {
  247. if (($regexp =~ /\S+/) && ($login =~ m/$regexp/i))
  248. {
  249. $sthadd->execute($uid, $gid, GRANT_REGEXP) unless $present;
  250. } else {
  251. $sthdel->execute($uid, $gid, GRANT_REGEXP) if $present;
  252. }
  253. }
  254. }
  255. ###############################
  256. ### Validators ###
  257. ###############################
  258. sub _check_name {
  259. my ($invocant, $name) = @_;
  260. $name = trim($name);
  261. $name || ThrowUserError("empty_group_name");
  262. # If we're creating a Group or changing the name...
  263. if (!ref($invocant) || $invocant->name ne $name) {
  264. my $exists = new Bugzilla::Group({name => $name });
  265. ThrowUserError("group_exists", { name => $name }) if $exists;
  266. }
  267. return $name;
  268. }
  269. sub _check_description {
  270. my ($invocant, $desc) = @_;
  271. $desc = trim($desc);
  272. $desc || ThrowUserError("empty_group_description");
  273. return $desc;
  274. }
  275. sub _check_user_regexp {
  276. my ($invocant, $regex) = @_;
  277. $regex = trim($regex) || '';
  278. ThrowUserError("invalid_regexp") unless (eval {qr/$regex/});
  279. return $regex;
  280. }
  281. sub _check_is_active { return $_[1] ? 1 : 0; }
  282. sub _check_is_bug_group {
  283. return $_[1] ? 1 : 0;
  284. }
  285. sub _check_icon_url { return $_[1] ? clean_text($_[1]) : undef; }
  286. 1;
  287. __END__
  288. =head1 NAME
  289. Bugzilla::Group - Bugzilla group class.
  290. =head1 SYNOPSIS
  291. use Bugzilla::Group;
  292. my $group = new Bugzilla::Group(1);
  293. my $group = new Bugzilla::Group({name => 'AcmeGroup'});
  294. my $id = $group->id;
  295. my $name = $group->name;
  296. my $description = $group->description;
  297. my $user_reg_exp = $group->user_reg_exp;
  298. my $is_active = $group->is_active;
  299. my $icon_url = $group->icon_url;
  300. my $is_active_bug_group = $group->is_active_bug_group;
  301. my $group_id = Bugzilla::Group::ValidateGroupName('admin', @users);
  302. my @groups = Bugzilla::Group->get_all;
  303. =head1 DESCRIPTION
  304. Group.pm represents a Bugzilla Group object. It is an implementation
  305. of L<Bugzilla::Object>, and thus has all the methods that L<Bugzilla::Object>
  306. provides, in addition to any methods documented below.
  307. =head1 SUBROUTINES
  308. =over
  309. =item C<create>
  310. Note that in addition to what L<Bugzilla::Object/create($params)>
  311. normally does, this function also makes the new group be inherited
  312. by the C<admin> group. That is, the C<admin> group will automatically
  313. be a member of this group.
  314. =item C<ValidateGroupName($name, @users)>
  315. Description: ValidateGroupName checks to see if ANY of the users
  316. in the provided list of user objects can see the
  317. named group.
  318. Params: $name - String with the group name.
  319. @users - An array with Bugzilla::User objects.
  320. Returns: It returns the group id if successful
  321. and undef otherwise.
  322. =back
  323. =head1 METHODS
  324. =over
  325. =item C<members_non_inherited>
  326. Returns an arrayref of L<Bugzilla::User> objects representing people who are
  327. "directly" in this group, meaning that they're in it because they match
  328. the group regular expression, or they have been actually added to the
  329. group manually.
  330. =back