User.pm 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180
  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): Myk Melez <myk@mozilla.org>
  21. # Erik Stambaugh <erik@dasbistro.com>
  22. # Bradley Baetz <bbaetz@acm.org>
  23. # Joel Peshkin <bugreport@peshkin.net>
  24. # Byron Jones <bugzilla@glob.com.au>
  25. # Shane H. W. Travis <travis@sedsystems.ca>
  26. # Max Kanat-Alexander <mkanat@bugzilla.org>
  27. # Gervase Markham <gerv@gerv.net>
  28. # Lance Larsh <lance.larsh@oracle.com>
  29. # Justin C. De Vries <judevries@novell.com>
  30. # Dennis Melentyev <dennis.melentyev@infopulse.com.ua>
  31. # Frédéric Buclin <LpSolit@gmail.com>
  32. # Mads Bondo Dydensborg <mbd@dbc.dk>
  33. ################################################################################
  34. # Module Initialization
  35. ################################################################################
  36. # Make it harder for us to do dangerous things in Perl.
  37. use strict;
  38. # This module implements utilities for dealing with Bugzilla users.
  39. package Bugzilla::User;
  40. use Bugzilla::Error;
  41. use Bugzilla::Util;
  42. use Bugzilla::Constants;
  43. use Bugzilla::User::Setting;
  44. use Bugzilla::Product;
  45. use Bugzilla::Classification;
  46. use Bugzilla::Field;
  47. use base qw(Bugzilla::Object Exporter);
  48. @Bugzilla::User::EXPORT = qw(is_available_username
  49. login_to_id user_id_to_login validate_password
  50. USER_MATCH_MULTIPLE USER_MATCH_FAILED USER_MATCH_SUCCESS
  51. MATCH_SKIP_CONFIRM
  52. );
  53. #####################################################################
  54. # Constants
  55. #####################################################################
  56. use constant USER_MATCH_MULTIPLE => -1;
  57. use constant USER_MATCH_FAILED => 0;
  58. use constant USER_MATCH_SUCCESS => 1;
  59. use constant MATCH_SKIP_CONFIRM => 1;
  60. use constant DEFAULT_USER => {
  61. 'userid' => 0,
  62. 'realname' => '',
  63. 'login_name' => '',
  64. 'showmybugslink' => 0,
  65. 'disabledtext' => '',
  66. 'disable_mail' => 0,
  67. };
  68. use constant DB_TABLE => 'profiles';
  69. # XXX Note that Bugzilla::User->name does not return the same thing
  70. # that you passed in for "name" to new(). That's because historically
  71. # Bugzilla::User used "name" for the realname field. This should be
  72. # fixed one day.
  73. use constant DB_COLUMNS => (
  74. 'profiles.userid',
  75. 'profiles.login_name',
  76. 'profiles.realname',
  77. 'profiles.mybugslink AS showmybugslink',
  78. 'profiles.disabledtext',
  79. 'profiles.disable_mail',
  80. );
  81. use constant NAME_FIELD => 'login_name';
  82. use constant ID_FIELD => 'userid';
  83. use constant LIST_ORDER => NAME_FIELD;
  84. use constant REQUIRED_CREATE_FIELDS => qw(login_name cryptpassword);
  85. use constant VALIDATORS => {
  86. cryptpassword => \&_check_password,
  87. disable_mail => \&_check_disable_mail,
  88. disabledtext => \&_check_disabledtext,
  89. login_name => \&check_login_name_for_creation,
  90. realname => \&_check_realname,
  91. };
  92. sub UPDATE_COLUMNS {
  93. my $self = shift;
  94. my @cols = qw(
  95. disable_mail
  96. disabledtext
  97. login_name
  98. realname
  99. );
  100. push(@cols, 'cryptpassword') if exists $self->{cryptpassword};
  101. return @cols;
  102. };
  103. ################################################################################
  104. # Functions
  105. ################################################################################
  106. sub new {
  107. my $invocant = shift;
  108. my $class = ref($invocant) || $invocant;
  109. my ($param) = @_;
  110. my $user = DEFAULT_USER;
  111. bless ($user, $class);
  112. return $user unless $param;
  113. return $class->SUPER::new(@_);
  114. }
  115. sub update {
  116. my $self = shift;
  117. my $changes = $self->SUPER::update(@_);
  118. my $dbh = Bugzilla->dbh;
  119. if (exists $changes->{login_name}) {
  120. # If we changed the login, silently delete any tokens.
  121. $dbh->do('DELETE FROM tokens WHERE userid = ?', undef, $self->id);
  122. # And rederive regex groups
  123. $self->derive_regexp_groups();
  124. }
  125. # Logout the user if necessary.
  126. Bugzilla->logout_user($self)
  127. if (exists $changes->{login_name} || exists $changes->{disabledtext}
  128. || exists $changes->{cryptpassword});
  129. # XXX Can update profiles_activity here as soon as it understands
  130. # field names like login_name.
  131. return $changes;
  132. }
  133. ################################################################################
  134. # Validators
  135. ################################################################################
  136. sub _check_disable_mail { return $_[1] ? 1 : 0; }
  137. sub _check_disabledtext { return trim($_[1]) || ''; }
  138. # This is public since createaccount.cgi needs to use it before issuing
  139. # a token for account creation.
  140. sub check_login_name_for_creation {
  141. my ($invocant, $name) = @_;
  142. $name = trim($name);
  143. $name || ThrowUserError('user_login_required');
  144. validate_email_syntax($name)
  145. || ThrowUserError('illegal_email_address', { addr => $name });
  146. # Check the name if it's a new user, or if we're changing the name.
  147. if (!ref($invocant) || $invocant->login ne $name) {
  148. is_available_username($name)
  149. || ThrowUserError('account_exists', { email => $name });
  150. }
  151. return $name;
  152. }
  153. sub _check_password {
  154. my ($self, $pass) = @_;
  155. # If the password is '*', do not encrypt it or validate it further--we
  156. # are creating a user who should not be able to log in using DB
  157. # authentication.
  158. return $pass if $pass eq '*';
  159. validate_password($pass);
  160. my $cryptpassword = bz_crypt($pass);
  161. return $cryptpassword;
  162. }
  163. sub _check_realname { return trim($_[1]) || ''; }
  164. ################################################################################
  165. # Mutators
  166. ################################################################################
  167. sub set_disabledtext { $_[0]->set('disabledtext', $_[1]); }
  168. sub set_disable_mail { $_[0]->set('disable_mail', $_[1]); }
  169. sub set_login {
  170. my ($self, $login) = @_;
  171. $self->set('login_name', $login);
  172. delete $self->{identity};
  173. delete $self->{nick};
  174. }
  175. sub set_name {
  176. my ($self, $name) = @_;
  177. $self->set('realname', $name);
  178. delete $self->{identity};
  179. }
  180. sub set_password { $_[0]->set('cryptpassword', $_[1]); }
  181. ################################################################################
  182. # Methods
  183. ################################################################################
  184. # Accessors for user attributes
  185. sub name { $_[0]->{realname}; }
  186. sub login { $_[0]->{login_name}; }
  187. sub email { $_[0]->login . Bugzilla->params->{'emailsuffix'}; }
  188. sub disabledtext { $_[0]->{'disabledtext'}; }
  189. sub is_disabled { $_[0]->disabledtext ? 1 : 0; }
  190. sub showmybugslink { $_[0]->{showmybugslink}; }
  191. sub email_disabled { $_[0]->{disable_mail}; }
  192. sub email_enabled { !($_[0]->{disable_mail}); }
  193. sub set_authorizer {
  194. my ($self, $authorizer) = @_;
  195. $self->{authorizer} = $authorizer;
  196. }
  197. sub authorizer {
  198. my ($self) = @_;
  199. if (!$self->{authorizer}) {
  200. require Bugzilla::Auth;
  201. $self->{authorizer} = new Bugzilla::Auth();
  202. }
  203. return $self->{authorizer};
  204. }
  205. # Generate a string to identify the user by name + login if the user
  206. # has a name or by login only if she doesn't.
  207. sub identity {
  208. my $self = shift;
  209. return "" unless $self->id;
  210. if (!defined $self->{identity}) {
  211. $self->{identity} =
  212. $self->name ? $self->name . " <" . $self->login. ">" : $self->login;
  213. }
  214. return $self->{identity};
  215. }
  216. sub nick {
  217. my $self = shift;
  218. return "" unless $self->id;
  219. if (!defined $self->{nick}) {
  220. $self->{nick} = (split(/@/, $self->login, 2))[0];
  221. }
  222. return $self->{nick};
  223. }
  224. sub queries {
  225. my $self = shift;
  226. return $self->{queries} if defined $self->{queries};
  227. return [] unless $self->id;
  228. my $dbh = Bugzilla->dbh;
  229. my $query_ids = $dbh->selectcol_arrayref(
  230. 'SELECT id FROM namedqueries WHERE userid = ?', undef, $self->id);
  231. require Bugzilla::Search::Saved;
  232. $self->{queries} = Bugzilla::Search::Saved->new_from_list($query_ids);
  233. # We preload link_in_footer from here as this information is always requested.
  234. # This only works if the user object represents the current logged in user.
  235. Bugzilla::Search::Saved::preload($self->{queries}) if $self->id == Bugzilla->user->id;
  236. return $self->{queries};
  237. }
  238. sub queries_subscribed {
  239. my $self = shift;
  240. return $self->{queries_subscribed} if defined $self->{queries_subscribed};
  241. return [] unless $self->id;
  242. # Exclude the user's own queries.
  243. my @my_query_ids = map($_->id, @{$self->queries});
  244. my $query_id_string = join(',', @my_query_ids) || '-1';
  245. # Only show subscriptions that we can still actually see. If a
  246. # user changes the shared group of a query, our subscription
  247. # will remain but we won't have access to the query anymore.
  248. my $subscribed_query_ids = Bugzilla->dbh->selectcol_arrayref(
  249. "SELECT lif.namedquery_id
  250. FROM namedqueries_link_in_footer lif
  251. INNER JOIN namedquery_group_map ngm
  252. ON ngm.namedquery_id = lif.namedquery_id
  253. WHERE lif.user_id = ?
  254. AND lif.namedquery_id NOT IN ($query_id_string)
  255. AND ngm.group_id IN (" . $self->groups_as_string . ")",
  256. undef, $self->id);
  257. require Bugzilla::Search::Saved;
  258. $self->{queries_subscribed} =
  259. Bugzilla::Search::Saved->new_from_list($subscribed_query_ids);
  260. return $self->{queries_subscribed};
  261. }
  262. sub queries_available {
  263. my $self = shift;
  264. return $self->{queries_available} if defined $self->{queries_available};
  265. return [] unless $self->id;
  266. # Exclude the user's own queries.
  267. my @my_query_ids = map($_->id, @{$self->queries});
  268. my $query_id_string = join(',', @my_query_ids) || '-1';
  269. my $avail_query_ids = Bugzilla->dbh->selectcol_arrayref(
  270. 'SELECT namedquery_id FROM namedquery_group_map
  271. WHERE group_id IN (' . $self->groups_as_string . ")
  272. AND namedquery_id NOT IN ($query_id_string)");
  273. require Bugzilla::Search::Saved;
  274. $self->{queries_available} =
  275. Bugzilla::Search::Saved->new_from_list($avail_query_ids);
  276. return $self->{queries_available};
  277. }
  278. sub settings {
  279. my ($self) = @_;
  280. return $self->{'settings'} if (defined $self->{'settings'});
  281. # IF the user is logged in
  282. # THEN get the user's settings
  283. # ELSE get default settings
  284. if ($self->id) {
  285. $self->{'settings'} = get_all_settings($self->id);
  286. } else {
  287. $self->{'settings'} = get_defaults();
  288. }
  289. return $self->{'settings'};
  290. }
  291. sub flush_queries_cache {
  292. my $self = shift;
  293. delete $self->{queries};
  294. delete $self->{queries_subscribed};
  295. delete $self->{queries_available};
  296. }
  297. sub groups {
  298. my $self = shift;
  299. return $self->{groups} if defined $self->{groups};
  300. return {} unless $self->id;
  301. my $dbh = Bugzilla->dbh;
  302. my $groups = $dbh->selectcol_arrayref(q{SELECT DISTINCT groups.name, group_id
  303. FROM groups, user_group_map
  304. WHERE groups.id=user_group_map.group_id
  305. AND user_id=?
  306. AND isbless=0},
  307. { Columns=>[1,2] },
  308. $self->id);
  309. # The above gives us an arrayref [name, id, name, id, ...]
  310. # Convert that into a hashref
  311. my %groups = @$groups;
  312. my @groupidstocheck = values(%groups);
  313. my %groupidschecked = ();
  314. my $rows = $dbh->selectall_arrayref(
  315. "SELECT DISTINCT groups.name, groups.id, member_id
  316. FROM group_group_map
  317. INNER JOIN groups
  318. ON groups.id = grantor_id
  319. WHERE grant_type = " . GROUP_MEMBERSHIP);
  320. my %group_names = ();
  321. my %group_membership = ();
  322. foreach my $row (@$rows) {
  323. my ($member_name, $grantor_id, $member_id) = @$row;
  324. # Just save the group names
  325. $group_names{$grantor_id} = $member_name;
  326. # And group membership
  327. push (@{$group_membership{$member_id}}, $grantor_id);
  328. }
  329. # Let's walk the groups hierarchy tree (using FIFO)
  330. # On the first iteration it's pre-filled with direct groups
  331. # membership. Later on, each group can add its own members into the
  332. # FIFO. Circular dependencies are eliminated by checking
  333. # $groupidschecked{$member_id} hash values.
  334. # As a result, %groups will have all the groups we are the member of.
  335. while ($#groupidstocheck >= 0) {
  336. # Pop the head group from FIFO
  337. my $member_id = shift @groupidstocheck;
  338. # Skip the group if we have already checked it
  339. if (!$groupidschecked{$member_id}) {
  340. # Mark group as checked
  341. $groupidschecked{$member_id} = 1;
  342. # Add all its members to the FIFO check list
  343. # %group_membership contains arrays of group members
  344. # for all groups. Accessible by group number.
  345. foreach my $newgroupid (@{$group_membership{$member_id}}) {
  346. push @groupidstocheck, $newgroupid
  347. if (!$groupidschecked{$newgroupid});
  348. }
  349. # Note on if clause: we could have group in %groups from 1st
  350. # query and do not have it in second one
  351. $groups{$group_names{$member_id}} = $member_id
  352. if $group_names{$member_id} && $member_id;
  353. }
  354. }
  355. $self->{groups} = \%groups;
  356. return $self->{groups};
  357. }
  358. sub groups_as_string {
  359. my $self = shift;
  360. return (join(',',values(%{$self->groups})) || '-1');
  361. }
  362. sub bless_groups {
  363. my $self = shift;
  364. return $self->{'bless_groups'} if defined $self->{'bless_groups'};
  365. return [] unless $self->id;
  366. my $dbh = Bugzilla->dbh;
  367. my $query;
  368. my $connector;
  369. my @bindValues;
  370. if ($self->in_group('editusers')) {
  371. # Users having editusers permissions may bless all groups.
  372. $query = 'SELECT DISTINCT id, name, description FROM groups';
  373. $connector = 'WHERE';
  374. }
  375. else {
  376. # Get all groups for the user where:
  377. # + They have direct bless privileges
  378. # + They are a member of a group that inherits bless privs.
  379. $query = q{
  380. SELECT DISTINCT groups.id, groups.name, groups.description
  381. FROM groups, user_group_map, group_group_map AS ggm
  382. WHERE user_group_map.user_id = ?
  383. AND ((user_group_map.isbless = 1
  384. AND groups.id=user_group_map.group_id)
  385. OR (groups.id = ggm.grantor_id
  386. AND ggm.grant_type = ?
  387. AND ggm.member_id IN(} .
  388. $self->groups_as_string .
  389. q{)))};
  390. $connector = 'AND';
  391. @bindValues = ($self->id, GROUP_BLESS);
  392. }
  393. # If visibilitygroups are used, restrict the set of groups.
  394. if (!$self->in_group('editusers')
  395. && Bugzilla->params->{'usevisibilitygroups'})
  396. {
  397. # Users need to see a group in order to bless it.
  398. my $visibleGroups = join(', ', @{$self->visible_groups_direct()})
  399. || return $self->{'bless_groups'} = [];
  400. $query .= " $connector id in ($visibleGroups)";
  401. }
  402. $query .= ' ORDER BY name';
  403. return $self->{'bless_groups'} =
  404. $dbh->selectall_arrayref($query, {'Slice' => {}}, @bindValues);
  405. }
  406. sub in_group {
  407. my ($self, $group, $product_id) = @_;
  408. if (exists $self->groups->{$group}) {
  409. return 1;
  410. }
  411. elsif ($product_id && detaint_natural($product_id)) {
  412. # Make sure $group exists on a per-product basis.
  413. return 0 unless (grep {$_ eq $group} PER_PRODUCT_PRIVILEGES);
  414. $self->{"product_$product_id"} = {} unless exists $self->{"product_$product_id"};
  415. if (!defined $self->{"product_$product_id"}->{$group}) {
  416. my $dbh = Bugzilla->dbh;
  417. my $in_group = $dbh->selectrow_array(
  418. "SELECT 1
  419. FROM group_control_map
  420. WHERE product_id = ?
  421. AND $group != 0
  422. AND group_id IN (" . $self->groups_as_string . ") " .
  423. $dbh->sql_limit(1),
  424. undef, $product_id);
  425. $self->{"product_$product_id"}->{$group} = $in_group ? 1 : 0;
  426. }
  427. return $self->{"product_$product_id"}->{$group};
  428. }
  429. # If we come here, then the user is not in the requested group.
  430. return 0;
  431. }
  432. sub in_group_id {
  433. my ($self, $id) = @_;
  434. my %j = reverse(%{$self->groups});
  435. return exists $j{$id} ? 1 : 0;
  436. }
  437. sub get_products_by_permission {
  438. my ($self, $group) = @_;
  439. # Make sure $group exists on a per-product basis.
  440. return [] unless (grep {$_ eq $group} PER_PRODUCT_PRIVILEGES);
  441. my $product_ids = Bugzilla->dbh->selectcol_arrayref(
  442. "SELECT DISTINCT product_id
  443. FROM group_control_map
  444. WHERE $group != 0
  445. AND group_id IN(" . $self->groups_as_string . ")");
  446. # No need to go further if the user has no "special" privs.
  447. return [] unless scalar(@$product_ids);
  448. # We will restrict the list to products the user can see.
  449. my $selectable_products = $self->get_selectable_products;
  450. my @products = grep {lsearch($product_ids, $_->id) > -1} @$selectable_products;
  451. return \@products;
  452. }
  453. sub can_see_user {
  454. my ($self, $otherUser) = @_;
  455. my $query;
  456. if (Bugzilla->params->{'usevisibilitygroups'}) {
  457. # If the user can see no groups, then no users are visible either.
  458. my $visibleGroups = $self->visible_groups_as_string() || return 0;
  459. $query = qq{SELECT COUNT(DISTINCT userid)
  460. FROM profiles, user_group_map
  461. WHERE userid = ?
  462. AND user_id = userid
  463. AND isbless = 0
  464. AND group_id IN ($visibleGroups)
  465. };
  466. } else {
  467. $query = qq{SELECT COUNT(userid)
  468. FROM profiles
  469. WHERE userid = ?
  470. };
  471. }
  472. return Bugzilla->dbh->selectrow_array($query, undef, $otherUser->id);
  473. }
  474. sub can_edit_product {
  475. my ($self, $prod_id) = @_;
  476. my $dbh = Bugzilla->dbh;
  477. my $has_external_groups =
  478. $dbh->selectrow_array('SELECT 1
  479. FROM group_control_map
  480. WHERE product_id = ?
  481. AND canedit != 0
  482. AND group_id NOT IN(' . $self->groups_as_string . ')',
  483. undef, $prod_id);
  484. return !$has_external_groups;
  485. }
  486. sub can_see_bug {
  487. my ($self, $bugid) = @_;
  488. my $dbh = Bugzilla->dbh;
  489. #if WEBKIT_CHANGES
  490. # FIXME: disable memoization since it results in stale handle
  491. my $sth;
  492. #my $sth = $self->{sthCanSeeBug};
  493. #endif WEBKIT_CHANGES
  494. my $userid = $self->id;
  495. # Get fields from bug, presence of user on cclist, and determine if
  496. # the user is missing any groups required by the bug. The prepared query
  497. # is cached because this may be called for every row in buglists or
  498. # every bug in a dependency list.
  499. unless ($sth) {
  500. $sth = $dbh->prepare("SELECT 1, reporter, assigned_to, qa_contact,
  501. reporter_accessible, cclist_accessible,
  502. COUNT(cc.who), COUNT(bug_group_map.bug_id)
  503. FROM bugs
  504. LEFT JOIN cc
  505. ON cc.bug_id = bugs.bug_id
  506. AND cc.who = $userid
  507. LEFT JOIN bug_group_map
  508. ON bugs.bug_id = bug_group_map.bug_id
  509. AND bug_group_map.group_ID NOT IN(" .
  510. $self->groups_as_string .
  511. ") WHERE bugs.bug_id = ?
  512. AND creation_ts IS NOT NULL " .
  513. $dbh->sql_group_by('bugs.bug_id', 'reporter, ' .
  514. 'assigned_to, qa_contact, reporter_accessible, ' .
  515. 'cclist_accessible'));
  516. }
  517. $sth->execute($bugid);
  518. my ($ready, $reporter, $owner, $qacontact, $reporter_access, $cclist_access,
  519. $isoncclist, $missinggroup) = $sth->fetchrow_array();
  520. $sth->finish;
  521. #if WEBKIT_CHANGES
  522. # FIXME: disable memoization since it results in stale handle
  523. #$self->{sthCanSeeBug} = $sth;
  524. #endif WEBKIT_CHANGES
  525. return ($ready
  526. && ((($reporter == $userid) && $reporter_access)
  527. || (Bugzilla->params->{'useqacontact'}
  528. && $qacontact && ($qacontact == $userid))
  529. || ($owner == $userid)
  530. || ($isoncclist && $cclist_access)
  531. || (!$missinggroup)));
  532. }
  533. sub can_see_product {
  534. my ($self, $product_name) = @_;
  535. return scalar(grep {$_->name eq $product_name} @{$self->get_selectable_products});
  536. }
  537. sub get_selectable_products {
  538. my $self = shift;
  539. my $class_id = shift;
  540. my $class_restricted = Bugzilla->params->{'useclassification'} && $class_id;
  541. if (!defined $self->{selectable_products}) {
  542. my $query = "SELECT id " .
  543. " FROM products " .
  544. "LEFT JOIN group_control_map " .
  545. " ON group_control_map.product_id = products.id ";
  546. if (Bugzilla->params->{'useentrygroupdefault'}) {
  547. $query .= " AND group_control_map.entry != 0 ";
  548. } else {
  549. $query .= " AND group_control_map.membercontrol = " . CONTROLMAPMANDATORY;
  550. }
  551. $query .= " AND group_id NOT IN(" . $self->groups_as_string . ") " .
  552. " WHERE group_id IS NULL " .
  553. "ORDER BY name";
  554. my $prod_ids = Bugzilla->dbh->selectcol_arrayref($query);
  555. $self->{selectable_products} = Bugzilla::Product->new_from_list($prod_ids);
  556. }
  557. # Restrict the list of products to those being in the classification, if any.
  558. if ($class_restricted) {
  559. return [grep {$_->classification_id == $class_id} @{$self->{selectable_products}}];
  560. }
  561. # If we come here, then we want all selectable products.
  562. return $self->{selectable_products};
  563. }
  564. sub get_selectable_classifications {
  565. my ($self) = @_;
  566. if (defined $self->{selectable_classifications}) {
  567. return $self->{selectable_classifications};
  568. }
  569. my $products = $self->get_selectable_products;
  570. my $class;
  571. foreach my $product (@$products) {
  572. $class->{$product->classification_id} ||=
  573. new Bugzilla::Classification($product->classification_id);
  574. }
  575. my @sorted_class = sort {$a->sortkey <=> $b->sortkey
  576. || lc($a->name) cmp lc($b->name)} (values %$class);
  577. $self->{selectable_classifications} = \@sorted_class;
  578. return $self->{selectable_classifications};
  579. }
  580. sub can_enter_product {
  581. my ($self, $product_name, $warn) = @_;
  582. my $dbh = Bugzilla->dbh;
  583. if (!defined($product_name)) {
  584. return unless $warn == THROW_ERROR;
  585. ThrowUserError('no_products');
  586. }
  587. my $product = new Bugzilla::Product({name => $product_name});
  588. my $can_enter =
  589. $product && grep($_->name eq $product->name, @{$self->get_enterable_products});
  590. return 1 if $can_enter;
  591. return 0 unless $warn == THROW_ERROR;
  592. # Check why access was denied. These checks are slow,
  593. # but that's fine, because they only happen if we fail.
  594. # The product could not exist or you could be denied...
  595. if (!$product || !$product->user_has_access($self)) {
  596. ThrowUserError('entry_access_denied', {product => $product_name});
  597. }
  598. # It could be closed for bug entry...
  599. elsif ($product->disallow_new) {
  600. ThrowUserError('product_disabled', {product => $product});
  601. }
  602. # It could have no components...
  603. elsif (!@{$product->components}) {
  604. ThrowUserError('missing_component', {product => $product});
  605. }
  606. # It could have no versions...
  607. elsif (!@{$product->versions}) {
  608. ThrowUserError ('missing_version', {product => $product});
  609. }
  610. die "can_enter_product reached an unreachable location.";
  611. }
  612. sub get_enterable_products {
  613. my $self = shift;
  614. my $dbh = Bugzilla->dbh;
  615. if (defined $self->{enterable_products}) {
  616. return $self->{enterable_products};
  617. }
  618. # All products which the user has "Entry" access to.
  619. my @enterable_ids =@{$dbh->selectcol_arrayref(
  620. 'SELECT products.id FROM products
  621. LEFT JOIN group_control_map
  622. ON group_control_map.product_id = products.id
  623. AND group_control_map.entry != 0
  624. AND group_id NOT IN (' . $self->groups_as_string . ')
  625. WHERE group_id IS NULL
  626. AND products.disallownew = 0') || []};
  627. if (@enterable_ids) {
  628. # And all of these products must have at least one component
  629. # and one version.
  630. @enterable_ids = @{$dbh->selectcol_arrayref(
  631. 'SELECT DISTINCT products.id FROM products
  632. INNER JOIN components ON components.product_id = products.id
  633. INNER JOIN versions ON versions.product_id = products.id
  634. WHERE products.id IN (' . (join(',', @enterable_ids)) .
  635. ')') || []};
  636. }
  637. $self->{enterable_products} =
  638. Bugzilla::Product->new_from_list(\@enterable_ids);
  639. return $self->{enterable_products};
  640. }
  641. sub get_accessible_products {
  642. my $self = shift;
  643. # Map the objects into a hash using the ids as keys
  644. my %products = map { $_->id => $_ }
  645. @{$self->get_selectable_products},
  646. @{$self->get_enterable_products};
  647. return [ values %products ];
  648. }
  649. sub check_can_admin_product {
  650. my ($self, $product_name) = @_;
  651. # First make sure the product name is valid.
  652. my $product = Bugzilla::Product::check_product($product_name);
  653. ($self->in_group('editcomponents', $product->id)
  654. && $self->can_see_product($product->name))
  655. || ThrowUserError('product_admin_denied', {product => $product->name});
  656. # Return the validated product object.
  657. return $product;
  658. }
  659. sub can_request_flag {
  660. my ($self, $flag_type) = @_;
  661. return ($self->can_set_flag($flag_type)
  662. || !$flag_type->request_group_id
  663. || $self->in_group_id($flag_type->request_group_id)) ? 1 : 0;
  664. }
  665. sub can_set_flag {
  666. my ($self, $flag_type) = @_;
  667. return (!$flag_type->grant_group_id
  668. || $self->in_group_id($flag_type->grant_group_id)) ? 1 : 0;
  669. }
  670. sub direct_group_membership {
  671. my $self = shift;
  672. my $dbh = Bugzilla->dbh;
  673. if (!$self->{'direct_group_membership'}) {
  674. my $gid = $dbh->selectcol_arrayref('SELECT id
  675. FROM groups
  676. INNER JOIN user_group_map
  677. ON groups.id = user_group_map.group_id
  678. WHERE user_id = ?
  679. AND isbless = 0',
  680. undef, $self->id);
  681. $self->{'direct_group_membership'} = Bugzilla::Group->new_from_list($gid);
  682. }
  683. return $self->{'direct_group_membership'};
  684. }
  685. # visible_groups_inherited returns a reference to a list of all the groups
  686. # whose members are visible to this user.
  687. sub visible_groups_inherited {
  688. my $self = shift;
  689. return $self->{visible_groups_inherited} if defined $self->{visible_groups_inherited};
  690. return [] unless $self->id;
  691. my @visgroups = @{$self->visible_groups_direct};
  692. @visgroups = @{$self->flatten_group_membership(@visgroups)};
  693. $self->{visible_groups_inherited} = \@visgroups;
  694. return $self->{visible_groups_inherited};
  695. }
  696. # visible_groups_direct returns a reference to a list of all the groups that
  697. # are visible to this user.
  698. sub visible_groups_direct {
  699. my $self = shift;
  700. my @visgroups = ();
  701. return $self->{visible_groups_direct} if defined $self->{visible_groups_direct};
  702. return [] unless $self->id;
  703. my $dbh = Bugzilla->dbh;
  704. my $sth;
  705. if (Bugzilla->params->{'usevisibilitygroups'}) {
  706. my $glist = join(',',(-1,values(%{$self->groups})));
  707. $sth = $dbh->prepare("SELECT DISTINCT grantor_id
  708. FROM group_group_map
  709. WHERE member_id IN($glist)
  710. AND grant_type=" . GROUP_VISIBLE);
  711. }
  712. else {
  713. # All groups are visible if usevisibilitygroups is off.
  714. $sth = $dbh->prepare('SELECT id FROM groups');
  715. }
  716. $sth->execute();
  717. while (my ($row) = $sth->fetchrow_array) {
  718. push @visgroups,$row;
  719. }
  720. $self->{visible_groups_direct} = \@visgroups;
  721. return $self->{visible_groups_direct};
  722. }
  723. sub visible_groups_as_string {
  724. my $self = shift;
  725. return join(', ', @{$self->visible_groups_inherited()});
  726. }
  727. # This function defines the groups a user may share a query with.
  728. # More restrictive sites may want to build this reference to a list of group IDs
  729. # from bless_groups instead of mirroring visible_groups_inherited, perhaps.
  730. sub queryshare_groups {
  731. my $self = shift;
  732. my @queryshare_groups;
  733. return $self->{queryshare_groups} if defined $self->{queryshare_groups};
  734. if ($self->in_group(Bugzilla->params->{'querysharegroup'})) {
  735. # We want to be allowed to share with groups we're in only.
  736. # If usevisibilitygroups is on, then we need to restrict this to groups
  737. # we may see.
  738. if (Bugzilla->params->{'usevisibilitygroups'}) {
  739. foreach(@{$self->visible_groups_inherited()}) {
  740. next unless $self->in_group_id($_);
  741. push(@queryshare_groups, $_);
  742. }
  743. }
  744. else {
  745. @queryshare_groups = values(%{$self->groups});
  746. }
  747. }
  748. return $self->{queryshare_groups} = \@queryshare_groups;
  749. }
  750. sub queryshare_groups_as_string {
  751. my $self = shift;
  752. return join(', ', @{$self->queryshare_groups()});
  753. }
  754. sub derive_regexp_groups {
  755. my ($self) = @_;
  756. my $id = $self->id;
  757. return unless $id;
  758. my $dbh = Bugzilla->dbh;
  759. my $sth;
  760. # add derived records for any matching regexps
  761. $sth = $dbh->prepare("SELECT id, userregexp, user_group_map.group_id
  762. FROM groups
  763. LEFT JOIN user_group_map
  764. ON groups.id = user_group_map.group_id
  765. AND user_group_map.user_id = ?
  766. AND user_group_map.grant_type = ?");
  767. $sth->execute($id, GRANT_REGEXP);
  768. my $group_insert = $dbh->prepare(q{INSERT INTO user_group_map
  769. (user_id, group_id, isbless, grant_type)
  770. VALUES (?, ?, 0, ?)});
  771. my $group_delete = $dbh->prepare(q{DELETE FROM user_group_map
  772. WHERE user_id = ?
  773. AND group_id = ?
  774. AND isbless = 0
  775. AND grant_type = ?});
  776. while (my ($group, $regexp, $present) = $sth->fetchrow_array()) {
  777. if (($regexp ne '') && ($self->login =~ m/$regexp/i)) {
  778. $group_insert->execute($id, $group, GRANT_REGEXP) unless $present;
  779. } else {
  780. $group_delete->execute($id, $group, GRANT_REGEXP) if $present;
  781. }
  782. }
  783. }
  784. sub product_responsibilities {
  785. my $self = shift;
  786. my $dbh = Bugzilla->dbh;
  787. return $self->{'product_resp'} if defined $self->{'product_resp'};
  788. return [] unless $self->id;
  789. my $list = $dbh->selectall_arrayref('SELECT product_id, id
  790. FROM components
  791. WHERE initialowner = ?
  792. OR initialqacontact = ?',
  793. {Slice => {}}, ($self->id, $self->id));
  794. unless ($list) {
  795. $self->{'product_resp'} = [];
  796. return $self->{'product_resp'};
  797. }
  798. my @prod_ids = map {$_->{'product_id'}} @$list;
  799. my $products = Bugzilla::Product->new_from_list(\@prod_ids);
  800. # We cannot |use| it, because Component.pm already |use|s User.pm.
  801. require Bugzilla::Component;
  802. my @comp_ids = map {$_->{'id'}} @$list;
  803. my $components = Bugzilla::Component->new_from_list(\@comp_ids);
  804. my @prod_list;
  805. # @$products is already sorted alphabetically.
  806. foreach my $prod (@$products) {
  807. # We use @components instead of $prod->components because we only want
  808. # components where the user is either the default assignee or QA contact.
  809. push(@prod_list, {product => $prod,
  810. components => [grep {$_->product_id == $prod->id} @$components]});
  811. }
  812. $self->{'product_resp'} = \@prod_list;
  813. return $self->{'product_resp'};
  814. }
  815. sub can_bless {
  816. my $self = shift;
  817. if (!scalar(@_)) {
  818. # If we're called without an argument, just return
  819. # whether or not we can bless at all.
  820. return scalar(@{$self->bless_groups}) ? 1 : 0;
  821. }
  822. # Otherwise, we're checking a specific group
  823. my $group_id = shift;
  824. return (grep {$$_{'id'} eq $group_id} (@{$self->bless_groups})) ? 1 : 0;
  825. }
  826. sub flatten_group_membership {
  827. my ($self, @groups) = @_;
  828. my $dbh = Bugzilla->dbh;
  829. my $sth;
  830. my @groupidstocheck = @groups;
  831. my %groupidschecked = ();
  832. $sth = $dbh->prepare("SELECT member_id FROM group_group_map
  833. WHERE grantor_id = ?
  834. AND grant_type = " . GROUP_MEMBERSHIP);
  835. while (my $node = shift @groupidstocheck) {
  836. $sth->execute($node);
  837. my $member;
  838. while (($member) = $sth->fetchrow_array) {
  839. if (!$groupidschecked{$member}) {
  840. $groupidschecked{$member} = 1;
  841. push @groupidstocheck, $member;
  842. push @groups, $member unless grep $_ == $member, @groups;
  843. }
  844. }
  845. }
  846. return \@groups;
  847. }
  848. sub match {
  849. # Generates a list of users whose login name (email address) or real name
  850. # matches a substring or wildcard.
  851. # This is also called if matches are disabled (for error checking), but
  852. # in this case only the exact match code will end up running.
  853. # $str contains the string to match, while $limit contains the
  854. # maximum number of records to retrieve.
  855. my ($str, $limit, $exclude_disabled) = @_;
  856. my $user = Bugzilla->user;
  857. my $dbh = Bugzilla->dbh;
  858. my @users = ();
  859. return \@users if $str =~ /^\s*$/;
  860. # The search order is wildcards, then exact match, then substring search.
  861. # Wildcard matching is skipped if there is no '*', and exact matches will
  862. # not (?) have a '*' in them. If any search comes up with something, the
  863. # ones following it will not execute.
  864. # first try wildcards
  865. my $wildstr = $str;
  866. if ($wildstr =~ s/\*/\%/g # don't do wildcards if no '*' in the string
  867. # or if we only want exact matches
  868. && Bugzilla->params->{'usermatchmode'} ne 'off')
  869. {
  870. # Build the query.
  871. trick_taint($wildstr);
  872. my $query = "SELECT DISTINCT login_name FROM profiles ";
  873. if (Bugzilla->params->{'usevisibilitygroups'}) {
  874. $query .= "INNER JOIN user_group_map
  875. ON user_group_map.user_id = profiles.userid ";
  876. }
  877. $query .= "WHERE ("
  878. . $dbh->sql_istrcmp('login_name', '?', "LIKE") . " OR " .
  879. $dbh->sql_istrcmp('realname', '?', "LIKE") . ") ";
  880. if (Bugzilla->params->{'usevisibilitygroups'}) {
  881. $query .= "AND isbless = 0 " .
  882. "AND group_id IN(" .
  883. join(', ', (-1, @{$user->visible_groups_inherited})) . ") ";
  884. }
  885. $query .= " AND disabledtext = '' " if $exclude_disabled;
  886. $query .= " ORDER BY login_name ";
  887. $query .= $dbh->sql_limit($limit) if $limit;
  888. # Execute the query, retrieve the results, and make them into
  889. # User objects.
  890. my $user_logins = $dbh->selectcol_arrayref($query, undef, ($wildstr, $wildstr));
  891. foreach my $login_name (@$user_logins) {
  892. push(@users, new Bugzilla::User({ name => $login_name }));
  893. }
  894. }
  895. else { # try an exact match
  896. # Exact matches don't care if a user is disabled.
  897. trick_taint($str);
  898. my $user_id = $dbh->selectrow_array('SELECT userid FROM profiles
  899. WHERE ' . $dbh->sql_istrcmp('login_name', '?'),
  900. undef, $str);
  901. push(@users, new Bugzilla::User($user_id)) if $user_id;
  902. }
  903. # then try substring search
  904. if ((scalar(@users) == 0)
  905. && (Bugzilla->params->{'usermatchmode'} eq 'search')
  906. && (length($str) >= 3))
  907. {
  908. $str = lc($str);
  909. trick_taint($str);
  910. my $query = "SELECT DISTINCT login_name FROM profiles ";
  911. if (Bugzilla->params->{'usevisibilitygroups'}) {
  912. $query .= "INNER JOIN user_group_map
  913. ON user_group_map.user_id = profiles.userid ";
  914. }
  915. $query .= " WHERE (" .
  916. $dbh->sql_position('?', 'LOWER(login_name)') . " > 0" . " OR " .
  917. $dbh->sql_position('?', 'LOWER(realname)') . " > 0) ";
  918. if (Bugzilla->params->{'usevisibilitygroups'}) {
  919. $query .= " AND isbless = 0" .
  920. " AND group_id IN(" .
  921. join(', ', (-1, @{$user->visible_groups_inherited})) . ") ";
  922. }
  923. $query .= " AND disabledtext = '' " if $exclude_disabled;
  924. $query .= " ORDER BY login_name ";
  925. $query .= $dbh->sql_limit($limit) if $limit;
  926. my $user_logins = $dbh->selectcol_arrayref($query, undef, ($str, $str));
  927. foreach my $login_name (@$user_logins) {
  928. push(@users, new Bugzilla::User({ name => $login_name }));
  929. }
  930. }
  931. return \@users;
  932. }
  933. # match_field() is a CGI wrapper for the match() function.
  934. #
  935. # Here's what it does:
  936. #
  937. # 1. Accepts a list of fields along with whether they may take multiple values
  938. # 2. Takes the values of those fields from the first parameter, a $cgi object
  939. # and passes them to match()
  940. # 3. Checks the results of the match and displays confirmation or failure
  941. # messages as appropriate.
  942. #
  943. # The confirmation screen functions the same way as verify-new-product and
  944. # confirm-duplicate, by rolling all of the state information into a
  945. # form which is passed back, but in this case the searched fields are
  946. # replaced with the search results.
  947. #
  948. # The act of displaying the confirmation or failure messages means it must
  949. # throw a template and terminate. When confirmation is sent, all of the
  950. # searchable fields have been replaced by exact fields and the calling script
  951. # is executed as normal.
  952. #
  953. # You also have the choice of *never* displaying the confirmation screen.
  954. # In this case, match_field will return one of the three USER_MATCH
  955. # constants described in the POD docs. To make match_field behave this
  956. # way, pass in MATCH_SKIP_CONFIRM as the third argument.
  957. #
  958. # match_field must be called early in a script, before anything external is
  959. # done with the form data.
  960. #
  961. # In order to do a simple match without dealing with templates, confirmation,
  962. # or globals, simply calling Bugzilla::User::match instead will be
  963. # sufficient.
  964. # How to call it:
  965. #
  966. # Bugzilla::User::match_field($cgi, {
  967. # 'field_name' => { 'type' => fieldtype },
  968. # 'field_name2' => { 'type' => fieldtype },
  969. # [...]
  970. # });
  971. #
  972. # fieldtype can be either 'single' or 'multi'.
  973. #
  974. sub match_field {
  975. my $cgi = shift; # CGI object to look up fields in
  976. my $fields = shift; # arguments as a hash
  977. my $behavior = shift || 0; # A constant that tells us how to act
  978. my $matches = {}; # the values sent to the template
  979. my $matchsuccess = 1; # did the match fail?
  980. my $need_confirm = 0; # whether to display confirmation screen
  981. my $match_multiple = 0; # whether we ever matched more than one user
  982. my $params = Bugzilla->params;
  983. # prepare default form values
  984. # Fields can be regular expressions matching multiple form fields
  985. # (f.e. "requestee-(\d+)"), so expand each non-literal field
  986. # into the list of form fields it matches.
  987. my $expanded_fields = {};
  988. foreach my $field_pattern (keys %{$fields}) {
  989. # Check if the field has any non-word characters. Only those fields
  990. # can be regular expressions, so don't expand the field if it doesn't
  991. # have any of those characters.
  992. if ($field_pattern =~ /^\w+$/) {
  993. $expanded_fields->{$field_pattern} = $fields->{$field_pattern};
  994. }
  995. else {
  996. my @field_names = grep(/$field_pattern/, $cgi->param());
  997. foreach my $field_name (@field_names) {
  998. $expanded_fields->{$field_name} =
  999. { type => $fields->{$field_pattern}->{'type'} };
  1000. # The field is a requestee field; in order for its name
  1001. # to show up correctly on the confirmation page, we need
  1002. # to find out the name of its flag type.
  1003. if ($field_name =~ /^requestee(_type)?-(\d+)$/) {
  1004. my $flag_type;
  1005. if ($1) {
  1006. require Bugzilla::FlagType;
  1007. $flag_type = new Bugzilla::FlagType($2);
  1008. }
  1009. else {
  1010. require Bugzilla::Flag;
  1011. my $flag = new Bugzilla::Flag($2);
  1012. $flag_type = $flag->type if $flag;
  1013. }
  1014. if ($flag_type) {
  1015. $expanded_fields->{$field_name}->{'flag_type'} = $flag_type;
  1016. }
  1017. else {
  1018. # No need to look for a valid requestee if the flag(type)
  1019. # has been deleted (may occur in race conditions).
  1020. delete $expanded_fields->{$field_name};
  1021. $cgi->delete($field_name);
  1022. }
  1023. }
  1024. }
  1025. }
  1026. }
  1027. $fields = $expanded_fields;
  1028. for my $field (keys %{$fields}) {
  1029. # Tolerate fields that do not exist.
  1030. #
  1031. # This is so that fields like qa_contact can be specified in the code
  1032. # and it won't break if the CGI object does not know about them.
  1033. #
  1034. # It has the side-effect that if a bad field name is passed it will be
  1035. # quietly ignored rather than raising a code error.
  1036. next if !defined $cgi->param($field);
  1037. # We need to move the query to $raw_field, where it will be split up,
  1038. # modified by the search, and put back into the CGI environment
  1039. # incrementally.
  1040. my $raw_field = join(" ", $cgi->param($field));
  1041. # When we add back in values later, it matters that we delete
  1042. # the field here, and not set it to '', so that we will add
  1043. # things to an empty list, and not to a list containing one
  1044. # empty string.
  1045. # If the field accepts only one match (type eq "single") and
  1046. # no match or more than one match is found for this field,
  1047. # we will set it back to '' so that the field remains defined
  1048. # outside this function (it was if we came here; else we would
  1049. # have returned earlier above).
  1050. # If the field accepts several matches (type eq "multi") and no match
  1051. # is found, we leave this field undefined (= empty array).
  1052. $cgi->delete($field);
  1053. my @queries = ();
  1054. # Now we either split $raw_field by spaces/commas and put the list
  1055. # into @queries, or in the case of fields which only accept single
  1056. # entries, we simply use the verbatim text.
  1057. $raw_field =~ s/^\s+|\s+$//sg; # trim leading/trailing space
  1058. # single field
  1059. if ($fields->{$field}->{'type'} eq 'single') {
  1060. @queries = ($raw_field) unless $raw_field =~ /^\s*$/;
  1061. # multi-field
  1062. }
  1063. elsif ($fields->{$field}->{'type'} eq 'multi') {
  1064. @queries = split(/[\s,]+/, $raw_field);
  1065. }
  1066. else {
  1067. # bad argument
  1068. ThrowCodeError('bad_arg',
  1069. { argument => $fields->{$field}->{'type'},
  1070. function => 'Bugzilla::User::match_field',
  1071. });
  1072. }
  1073. my $limit = 0;
  1074. if ($params->{'maxusermatches'}) {
  1075. $limit = $params->{'maxusermatches'} + 1;
  1076. }
  1077. for my $query (@queries) {
  1078. my $users = match(
  1079. $query, # match string
  1080. $limit, # match limit
  1081. 1 # exclude_disabled
  1082. );
  1083. # skip confirmation for exact matches
  1084. if ((scalar(@{$users}) == 1)
  1085. && (lc(@{$users}[0]->login) eq lc($query)))
  1086. {
  1087. $cgi->append(-name=>$field,
  1088. -values=>[@{$users}[0]->login]);
  1089. next;
  1090. }
  1091. $matches->{$field}->{$query}->{'users'} = $users;
  1092. $matches->{$field}->{$query}->{'status'} = 'success';
  1093. # here is where it checks for multiple matches
  1094. if (scalar(@{$users}) == 1) { # exactly one match
  1095. $cgi->append(-name=>$field,
  1096. -values=>[@{$users}[0]->login]);
  1097. $need_confirm = 1 if $params->{'confirmuniqueusermatch'};
  1098. }
  1099. elsif ((scalar(@{$users}) > 1)
  1100. && ($params->{'maxusermatches'} != 1)) {
  1101. $need_confirm = 1;
  1102. $match_multiple = 1;
  1103. if (($params->{'maxusermatches'})
  1104. && (scalar(@{$users}) > $params->{'maxusermatches'}))
  1105. {
  1106. $matches->{$field}->{$query}->{'status'} = 'trunc';
  1107. pop @{$users}; # take the last one out
  1108. }
  1109. }
  1110. else {
  1111. # everything else fails
  1112. $matchsuccess = 0; # fail
  1113. $matches->{$field}->{$query}->{'status'} = 'fail';
  1114. $need_confirm = 1; # confirmation screen shows failures
  1115. }
  1116. }
  1117. # Above, we deleted the field before adding matches. If no match
  1118. # or more than one match has been found for a field expecting only
  1119. # one match (type eq "single"), we set it back to '' so
  1120. # that the caller of this function can still check whether this
  1121. # field was defined or not (and it was if we came here).
  1122. if (!defined $cgi->param($field)
  1123. && $fields->{$field}->{'type'} eq 'single') {
  1124. $cgi->param($field, '');
  1125. }
  1126. }
  1127. my $retval;
  1128. if (!$matchsuccess) {
  1129. $retval = USER_MATCH_FAILED;
  1130. }
  1131. elsif ($match_multiple) {
  1132. $retval = USER_MATCH_MULTIPLE;
  1133. }
  1134. else {
  1135. $retval = USER_MATCH_SUCCESS;
  1136. }
  1137. # Skip confirmation if we were told to, or if we don't need to confirm.
  1138. return $retval if ($behavior == MATCH_SKIP_CONFIRM || !$need_confirm);
  1139. my $template = Bugzilla->template;
  1140. my $vars = {};
  1141. $vars->{'script'} = Bugzilla->cgi->url(-relative => 1); # for self-referencing URLs
  1142. $vars->{'fields'} = $fields; # fields being matched
  1143. $vars->{'matches'} = $matches; # matches that were made
  1144. $vars->{'matchsuccess'} = $matchsuccess; # continue or fail
  1145. $vars->{'matchmultiple'} = $match_multiple;
  1146. print Bugzilla->cgi->header();
  1147. $template->process("global/confirm-user-match.html.tmpl", $vars)
  1148. || ThrowTemplateError($template->error());
  1149. exit;
  1150. }
  1151. # Changes in some fields automatically trigger events. The 'field names' are
  1152. # from the fielddefs table. We really should be using proper field names
  1153. # throughout.
  1154. our %names_to_events = (
  1155. 'Resolution' => EVT_OPENED_CLOSED,
  1156. 'Keywords' => EVT_KEYWORD,
  1157. 'CC' => EVT_CC,
  1158. 'Severity' => EVT_PROJ_MANAGEMENT,
  1159. 'Priority' => EVT_PROJ_MANAGEMENT,
  1160. 'Status' => EVT_PROJ_MANAGEMENT,
  1161. 'Target Milestone' => EVT_PROJ_MANAGEMENT,
  1162. 'Attachment description' => EVT_ATTACHMENT_DATA,
  1163. 'Attachment mime type' => EVT_ATTACHMENT_DATA,
  1164. 'Attachment is patch' => EVT_ATTACHMENT_DATA,
  1165. 'Depends on' => EVT_DEPEND_BLOCK,
  1166. 'Blocks' => EVT_DEPEND_BLOCK);
  1167. # Returns true if the user wants mail for a given bug change.
  1168. # Note: the "+" signs before the constants suppress bareword quoting.
  1169. sub wants_bug_mail {
  1170. my $self = shift;
  1171. my ($bug_id, $relationship, $fieldDiffs, $commentField, $dependencyText,
  1172. $changer, $bug_is_new) = @_;
  1173. # Make a list of the events which have happened during this bug change,
  1174. # from the point of view of this user.
  1175. my %events;
  1176. foreach my $ref (@$fieldDiffs) {
  1177. my ($who, $whoname, $fieldName, $when, $old, $new) = @$ref;
  1178. # A change to any of the above fields sets the corresponding event
  1179. if (defined($names_to_events{$fieldName})) {
  1180. $events{$names_to_events{$fieldName}} = 1;
  1181. }
  1182. else {
  1183. # Catch-all for any change not caught by a more specific event
  1184. $events{+EVT_OTHER} = 1;
  1185. }
  1186. # If the user is in a particular role and the value of that role
  1187. # changed, we need the ADDED_REMOVED event.
  1188. if (($fieldName eq "AssignedTo" && $relationship == REL_ASSIGNEE) ||
  1189. ($fieldName eq "QAContact" && $relationship == REL_QA))
  1190. {
  1191. $events{+EVT_ADDED_REMOVED} = 1;
  1192. }
  1193. if ($fieldName eq "CC") {
  1194. my $login = $self->login;
  1195. my $inold = ($old =~ /^(.*,\s*)?\Q$login\E(,.*)?$/);
  1196. my $innew = ($new =~ /^(.*,\s*)?\Q$login\E(,.*)?$/);
  1197. if ($inold != $innew)
  1198. {
  1199. $events{+EVT_ADDED_REMOVED} = 1;
  1200. }
  1201. }
  1202. }
  1203. # You role is new if the bug itself is.
  1204. # Only makes sense for the assignee, QA contact and the CC list.
  1205. if ($bug_is_new
  1206. && ($relationship == REL_ASSIGNEE
  1207. || $relationship == REL_QA
  1208. || $relationship == REL_CC))
  1209. {
  1210. $events{+EVT_ADDED_REMOVED} = 1;
  1211. }
  1212. if ($commentField =~ /Created an attachment \(/) {
  1213. $events{+EVT_ATTACHMENT} = 1;
  1214. }
  1215. elsif ($commentField ne '') {
  1216. $events{+EVT_COMMENT} = 1;
  1217. }
  1218. # Dependent changed bugmails must have an event to ensure the bugmail is
  1219. # emailed.
  1220. if ($dependencyText ne '') {
  1221. $events{+EVT_DEPEND_BLOCK} = 1;
  1222. }
  1223. my @event_list = keys %events;
  1224. my $wants_mail = $self->wants_mail(\@event_list, $relationship);
  1225. # The negative events are handled separately - they can't be incorporated
  1226. # into the first wants_mail call, because they are of the opposite sense.
  1227. #
  1228. # We do them separately because if _any_ of them are set, we don't want
  1229. # the mail.
  1230. if ($wants_mail && $changer && ($self->login eq $changer)) {
  1231. $wants_mail &= $self->wants_mail([EVT_CHANGED_BY_ME], $relationship);
  1232. }
  1233. if ($wants_mail) {
  1234. my $dbh = Bugzilla->dbh;
  1235. # We don't create a Bug object from the bug_id here because we only
  1236. # need one piece of information, and doing so (as of 2004-11-23) slows
  1237. # down bugmail sending by a factor of 2. If Bug creation was more
  1238. # lazy, this might not be so bad.
  1239. my $bug_status = $dbh->selectrow_array('SELECT bug_status
  1240. FROM bugs WHERE bug_id = ?',
  1241. undef, $bug_id);
  1242. if ($bug_status eq "UNCONFIRMED") {
  1243. $wants_mail &= $self->wants_mail([EVT_UNCONFIRMED], $relationship);
  1244. }
  1245. }
  1246. return $wants_mail;
  1247. }
  1248. # Returns true if the user wants mail for a given set of events.
  1249. sub wants_mail {
  1250. my $self = shift;
  1251. my ($events, $relationship) = @_;
  1252. # Don't send any mail, ever, if account is disabled
  1253. # XXX Temporary Compatibility Change 1 of 2:
  1254. # This code is disabled for the moment to make the behaviour like the old
  1255. # system, which sent bugmail to disabled accounts.
  1256. # return 0 if $self->{'disabledtext'};
  1257. # No mail if there are no events
  1258. return 0 if !scalar(@$events);
  1259. # If a relationship isn't given, default to REL_ANY.
  1260. if (!defined($relationship)) {
  1261. $relationship = REL_ANY;
  1262. }
  1263. # Skip DB query if relationship is explicit
  1264. return 1 if $relationship == REL_GLOBAL_WATCHER;
  1265. my $dbh = Bugzilla->dbh;
  1266. my $wants_mail =
  1267. $dbh->selectrow_array('SELECT 1
  1268. FROM email_setting
  1269. WHERE user_id = ?
  1270. AND relationship = ?
  1271. AND event IN (' . join(',', @$events) . ') ' .
  1272. $dbh->sql_limit(1),
  1273. undef, ($self->id, $relationship));
  1274. return defined($wants_mail) ? 1 : 0;
  1275. }
  1276. sub is_mover {
  1277. my $self = shift;
  1278. if (!defined $self->{'is_mover'}) {
  1279. my @movers = map { trim($_) } split(',', Bugzilla->params->{'movers'});
  1280. $self->{'is_mover'} = ($self->id
  1281. && lsearch(\@movers, $self->login) != -1);
  1282. }
  1283. return $self->{'is_mover'};
  1284. }
  1285. sub is_insider {
  1286. my $self = shift;
  1287. if (!defined $self->{'is_insider'}) {
  1288. my $insider_group = Bugzilla->params->{'insidergroup'};
  1289. $self->{'is_insider'} =
  1290. ($insider_group && $self->in_group($insider_group)) ? 1 : 0;
  1291. }
  1292. return $self->{'is_insider'};
  1293. }
  1294. sub is_global_watcher {
  1295. my $self = shift;
  1296. if (!defined $self->{'is_global_watcher'}) {
  1297. my @watchers = split(/[,\s]+/, Bugzilla->params->{'globalwatchers'});
  1298. $self->{'is_global_watcher'} = scalar(grep { $_ eq $self->login } @watchers) ? 1 : 0;
  1299. }
  1300. return $self->{'is_global_watcher'};
  1301. }
  1302. sub get_userlist {
  1303. my $self = shift;
  1304. return $self->{'userlist'} if defined $self->{'userlist'};
  1305. my $dbh = Bugzilla->dbh;
  1306. my $query = "SELECT DISTINCT login_name, realname,";
  1307. if (Bugzilla->params->{'usevisibilitygroups'}) {
  1308. $query .= " COUNT(group_id) ";
  1309. } else {
  1310. $query .= " 1 ";
  1311. }
  1312. $query .= "FROM profiles ";
  1313. if (Bugzilla->params->{'usevisibilitygroups'}) {
  1314. $query .= "LEFT JOIN user_group_map " .
  1315. "ON user_group_map.user_id = userid AND isbless = 0 " .
  1316. "AND group_id IN(" .
  1317. join(', ', (-1, @{$self->visible_groups_inherited})) . ")";
  1318. }
  1319. $query .= " WHERE disabledtext = '' ";
  1320. $query .= $dbh->sql_group_by('userid', 'login_name, realname');
  1321. my $sth = $dbh->prepare($query);
  1322. $sth->execute;
  1323. my @userlist;
  1324. while (my($login, $name, $visible) = $sth->fetchrow_array) {
  1325. push @userlist, {
  1326. login => $login,
  1327. identity => $name ? "$name <$login>" : $login,
  1328. visible => $visible,
  1329. };
  1330. }
  1331. @userlist = sort { lc $$a{'identity'} cmp lc $$b{'identity'} } @userlist;
  1332. $self->{'userlist'} = \@userlist;
  1333. return $self->{'userlist'};
  1334. }
  1335. sub create {
  1336. my $invocant = shift;
  1337. my $class = ref($invocant) || $invocant;
  1338. my $dbh = Bugzilla->dbh;
  1339. $dbh->bz_start_transaction();
  1340. my $user = $class->SUPER::create(@_);
  1341. # Turn on all email for the new user
  1342. foreach my $rel (RELATIONSHIPS) {
  1343. foreach my $event (POS_EVENTS, NEG_EVENTS) {
  1344. # These "exceptions" define the default email preferences.
  1345. #
  1346. # We enable mail unless the change was made by the user, or it's
  1347. # just a CC list addition and the user is not the reporter.
  1348. next if ($event == EVT_CHANGED_BY_ME);
  1349. next if (($event == EVT_CC) && ($rel != REL_REPORTER));
  1350. $dbh->do('INSERT INTO email_setting (user_id, relationship, event)
  1351. VALUES (?, ?, ?)', undef, ($user->id, $rel, $event));
  1352. }
  1353. }
  1354. foreach my $event (GLOBAL_EVENTS) {
  1355. $dbh->do('INSERT INTO email_setting (user_id, relationship, event)
  1356. VALUES (?, ?, ?)', undef, ($user->id, REL_ANY, $event));
  1357. }
  1358. $user->derive_regexp_groups();
  1359. # Add the creation date to the profiles_activity table.
  1360. # $who is the user who created the new user account, i.e. either an
  1361. # admin or the new user himself.
  1362. my $who = Bugzilla->user->id || $user->id;
  1363. my $creation_date_fieldid = get_field_id('creation_ts');
  1364. $dbh->do('INSERT INTO profiles_activity
  1365. (userid, who, profiles_when, fieldid, newvalue)
  1366. VALUES (?, ?, NOW(), ?, NOW())',
  1367. undef, ($user->id, $who, $creation_date_fieldid));
  1368. $dbh->bz_commit_transaction();
  1369. # Return the newly created user account.
  1370. return $user;
  1371. }
  1372. sub is_available_username {
  1373. my ($username, $old_username) = @_;
  1374. if(login_to_id($username) != 0) {
  1375. return 0;
  1376. }
  1377. my $dbh = Bugzilla->dbh;
  1378. # $username is safe because it is only used in SELECT placeholders.
  1379. trick_taint($username);
  1380. # Reject if the new login is part of an email change which is
  1381. # still in progress
  1382. #
  1383. # substring/locate stuff: bug 165221; this used to use regexes, but that
  1384. # was unsafe and required weird escaping; using substring to pull out
  1385. # the new/old email addresses and sql_position() to find the delimiter (':')
  1386. # is cleaner/safer
  1387. my $eventdata = $dbh->selectrow_array(
  1388. "SELECT eventdata
  1389. FROM tokens
  1390. WHERE (tokentype = 'emailold'
  1391. AND SUBSTRING(eventdata, 1, (" .
  1392. $dbh->sql_position(q{':'}, 'eventdata') . "- 1)) = ?)
  1393. OR (tokentype = 'emailnew'
  1394. AND SUBSTRING(eventdata, (" .
  1395. $dbh->sql_position(q{':'}, 'eventdata') . "+ 1)) = ?)",
  1396. undef, ($username, $username));
  1397. if ($eventdata) {
  1398. # Allow thru owner of token
  1399. if($old_username && ($eventdata eq "$old_username:$username")) {
  1400. return 1;
  1401. }
  1402. return 0;
  1403. }
  1404. return 1;
  1405. }
  1406. sub login_to_id {
  1407. my ($login, $throw_error) = @_;
  1408. my $dbh = Bugzilla->dbh;
  1409. # No need to validate $login -- it will be used by the following SELECT
  1410. # statement only, so it's safe to simply trick_taint.
  1411. trick_taint($login);
  1412. my $user_id = $dbh->selectrow_array("SELECT userid FROM profiles WHERE " .
  1413. $dbh->sql_istrcmp('login_name', '?'),
  1414. undef, $login);
  1415. if ($user_id) {
  1416. return $user_id;
  1417. } elsif ($throw_error) {
  1418. ThrowUserError('invalid_username', { name => $login });
  1419. } else {
  1420. return 0;
  1421. }
  1422. }
  1423. sub user_id_to_login {
  1424. my $user_id = shift;
  1425. my $dbh = Bugzilla->dbh;
  1426. return '' unless ($user_id && detaint_natural($user_id));
  1427. my $login = $dbh->selectrow_array('SELECT login_name FROM profiles
  1428. WHERE userid = ?', undef, $user_id);
  1429. return $login || '';
  1430. }
  1431. sub validate_password {
  1432. my ($password, $matchpassword) = @_;
  1433. if (length($password) < USER_PASSWORD_MIN_LENGTH) {
  1434. ThrowUserError('password_too_short');
  1435. } elsif (length($password) > USER_PASSWORD_MAX_LENGTH) {
  1436. ThrowUserError('password_too_long');
  1437. } elsif ((defined $matchpassword) && ($password ne $matchpassword)) {
  1438. ThrowUserError('passwords_dont_match');
  1439. }
  1440. # Having done these checks makes us consider the password untainted.
  1441. trick_taint($_[0]);
  1442. return 1;
  1443. }
  1444. 1;
  1445. __END__
  1446. =head1 NAME
  1447. Bugzilla::User - Object for a Bugzilla user
  1448. =head1 SYNOPSIS
  1449. use Bugzilla::User;
  1450. my $user = new Bugzilla::User($id);
  1451. my @get_selectable_classifications =
  1452. $user->get_selectable_classifications;
  1453. # Class Functions
  1454. $user = Bugzilla::User->create({
  1455. login_name => $username,
  1456. realname => $realname,
  1457. cryptpassword => $plaintext_password,
  1458. disabledtext => $disabledtext,
  1459. disable_mail => 0});
  1460. =head1 DESCRIPTION
  1461. This package handles Bugzilla users. Data obtained from here is read-only;
  1462. there is currently no way to modify a user from this package.
  1463. Note that the currently logged in user (if any) is available via
  1464. L<Bugzilla-E<gt>user|Bugzilla/"user">.
  1465. C<Bugzilla::User> is an implementation of L<Bugzilla::Object>, and thus
  1466. provides all the methods of L<Bugzilla::Object> in addition to the
  1467. methods listed below.
  1468. =head1 CONSTANTS
  1469. =over
  1470. =item C<USER_MATCH_MULTIPLE>
  1471. Returned by C<match_field()> when at least one field matched more than
  1472. one user, but no matches failed.
  1473. =item C<USER_MATCH_FAILED>
  1474. Returned by C<match_field()> when at least one field failed to match
  1475. anything.
  1476. =item C<USER_MATCH_SUCCESS>
  1477. Returned by C<match_field()> when all fields successfully matched only one
  1478. user.
  1479. =item C<MATCH_SKIP_CONFIRM>
  1480. Passed in to match_field to tell match_field to never display a
  1481. confirmation screen.
  1482. =back
  1483. =head1 METHODS
  1484. =head2 Saved and Shared Queries
  1485. =over
  1486. =item C<queries>
  1487. Returns an arrayref of the user's own saved queries, sorted by name. The
  1488. array contains L<Bugzilla::Search::Saved> objects.
  1489. =item C<queries_subscribed>
  1490. Returns an arrayref of shared queries that the user has subscribed to.
  1491. That is, these are shared queries that the user sees in their footer.
  1492. This array contains L<Bugzilla::Search::Saved> objects.
  1493. =item C<queries_available>
  1494. Returns an arrayref of all queries to which the user could possibly
  1495. subscribe. This includes the contents of L</queries_subscribed>.
  1496. An array of L<Bugzilla::Search::Saved> objects.
  1497. =item C<flush_queries_cache>
  1498. Some code modifies the set of stored queries. Because C<Bugzilla::User> does
  1499. not handle these modifications, but does cache the result of calling C<queries>
  1500. internally, such code must call this method to flush the cached result.
  1501. =item C<queryshare_groups>
  1502. An arrayref of group ids. The user can share their own queries with these
  1503. groups.
  1504. =back
  1505. =head2 Other Methods
  1506. =over
  1507. =item C<id>
  1508. Returns the userid for this user.
  1509. =item C<login>
  1510. Returns the login name for this user.
  1511. =item C<email>
  1512. Returns the user's email address. Currently this is the same value as the
  1513. login.
  1514. =item C<name>
  1515. Returns the 'real' name for this user, if any.
  1516. =item C<showmybugslink>
  1517. Returns C<1> if the user has set his preference to show the 'My Bugs' link in
  1518. the page footer, and C<0> otherwise.
  1519. =item C<identity>
  1520. Returns a string for the identity of the user. This will be of the form
  1521. C<name E<lt>emailE<gt>> if the user has specified a name, and C<email>
  1522. otherwise.
  1523. =item C<nick>
  1524. Returns a user "nickname" -- i.e. a shorter, not-necessarily-unique name by
  1525. which to identify the user. Currently the part of the user's email address
  1526. before the at sign (@), but that could change, especially if we implement
  1527. usernames not dependent on email address.
  1528. =item C<authorizer>
  1529. This is the L<Bugzilla::Auth> object that the User logged in with.
  1530. If the user hasn't logged in yet, a new, empty Bugzilla::Auth() object is
  1531. returned.
  1532. =item C<set_authorizer($authorizer)>
  1533. Sets the L<Bugzilla::Auth> object to be returned by C<authorizer()>.
  1534. Should only be called by C<Bugzilla::Auth::login>, for the most part.
  1535. =item C<disabledtext>
  1536. Returns the disable text of the user, if any.
  1537. =item C<settings>
  1538. Returns a hash of hashes which holds the user's settings. The first key is
  1539. the name of the setting, as found in setting.name. The second key is one of:
  1540. is_enabled - true if the user is allowed to set the preference themselves;
  1541. false to force the site defaults
  1542. for themselves or must accept the global site default value
  1543. default_value - the global site default for this setting
  1544. value - the value of this setting for this user. Will be the same
  1545. as the default_value if the user is not logged in, or if
  1546. is_default is true.
  1547. is_default - a boolean to indicate whether the user has chosen to make
  1548. a preference for themself or use the site default.
  1549. =item C<groups>
  1550. Returns a hashref of group names for groups the user is a member of. The keys
  1551. are the names of the groups, whilst the values are the respective group ids.
  1552. (This is so that a set of all groupids for groups the user is in can be
  1553. obtained by C<values(%{$user-E<gt>groups})>.)
  1554. =item C<groups_as_string>
  1555. Returns a string containing a comma-separated list of numeric group ids. If
  1556. the user is not a member of any groups, returns "-1". This is most often used
  1557. within an SQL IN() function.
  1558. =item C<in_group($group_name, $product_id)>
  1559. Determines whether or not a user is in the given group by name.
  1560. If $product_id is given, it also checks for local privileges for
  1561. this product.
  1562. =item C<in_group_id>
  1563. Determines whether or not a user is in the given group by id.
  1564. =item C<bless_groups>
  1565. Returns an arrayref of hashes of C<groups> entries, where the keys of each hash
  1566. are the names of C<id>, C<name> and C<description> columns of the C<groups>
  1567. table.
  1568. The arrayref consists of the groups the user can bless, taking into account
  1569. that having editusers permissions means that you can bless all groups, and
  1570. that you need to be aware of a group in order to bless a group.
  1571. =item C<get_products_by_permission($group)>
  1572. Returns a list of product objects for which the user has $group privileges
  1573. and which he can access.
  1574. $group must be one of the groups defined in PER_PRODUCT_PRIVILEGES.
  1575. =item C<can_see_user(user)>
  1576. Returns 1 if the specified user account exists and is visible to the user,
  1577. 0 otherwise.
  1578. =item C<can_edit_product(prod_id)>
  1579. Determines if, given a product id, the user can edit bugs in this product
  1580. at all.
  1581. =item C<can_see_bug(bug_id)>
  1582. Determines if the user can see the specified bug.
  1583. =item C<can_see_product(product_name)>
  1584. Returns 1 if the user can access the specified product, and 0 if the user
  1585. should not be aware of the existence of the product.
  1586. =item C<derive_regexp_groups>
  1587. Bugzilla allows for group inheritance. When data about the user (or any of the
  1588. groups) changes, the database must be updated. Handling updated groups is taken
  1589. care of by the constructor. However, when updating the email address, the
  1590. user may be placed into different groups, based on a new email regexp. This
  1591. method should be called in such a case to force reresolution of these groups.
  1592. =item C<get_selectable_products>
  1593. Description: Returns all products the user is allowed to access. This list
  1594. is restricted to some given classification if $classification_id
  1595. is given.
  1596. Params: $classification_id - (optional) The ID of the classification
  1597. the products belong to.
  1598. Returns: An array of product objects, sorted by the product name.
  1599. =item C<get_selectable_classifications>
  1600. Description: Returns all classifications containing at least one product
  1601. the user is allowed to view.
  1602. Params: none
  1603. Returns: An array of Bugzilla::Classification objects, sorted by
  1604. the classification name.
  1605. =item C<can_enter_product($product_name, $warn)>
  1606. Description: Returns 1 if the user can enter bugs into the specified product.
  1607. If the user cannot enter bugs into the product, the behavior of
  1608. this method depends on the value of $warn:
  1609. - if $warn is false (or not given), a 'false' value is returned;
  1610. - if $warn is true, an error is thrown.
  1611. Params: $product_name - a product name.
  1612. $warn - optional parameter, indicating whether an error
  1613. must be thrown if the user cannot enter bugs
  1614. into the specified product.
  1615. Returns: 1 if the user can enter bugs into the product,
  1616. 0 if the user cannot enter bugs into the product and if $warn
  1617. is false (an error is thrown if $warn is true).
  1618. =item C<get_enterable_products>
  1619. Description: Returns an array of product objects into which the user is
  1620. allowed to enter bugs.
  1621. Params: none
  1622. Returns: an array of product objects.
  1623. =item C<check_can_admin_product($product_name)>
  1624. Description: Checks whether the user is allowed to administrate the product.
  1625. Params: $product_name - a product name.
  1626. Returns: On success, a product object. On failure, an error is thrown.
  1627. =item C<can_request_flag($flag_type)>
  1628. Description: Checks whether the user can request flags of the given type.
  1629. Params: $flag_type - a Bugzilla::FlagType object.
  1630. Returns: 1 if the user can request flags of the given type,
  1631. 0 otherwise.
  1632. =item C<can_set_flag($flag_type)>
  1633. Description: Checks whether the user can set flags of the given type.
  1634. Params: $flag_type - a Bugzilla::FlagType object.
  1635. Returns: 1 if the user can set flags of the given type,
  1636. 0 otherwise.
  1637. =item C<get_userlist>
  1638. Returns a reference to an array of users. The array is populated with hashrefs
  1639. containing the login, identity and visibility. Users that are not visible to this
  1640. user will have 'visible' set to zero.
  1641. =item C<flatten_group_membership>
  1642. Accepts a list of groups and returns a list of all the groups whose members
  1643. inherit membership in any group on the list. So, we can determine if a user
  1644. is in any of the groups input to flatten_group_membership by querying the
  1645. user_group_map for any user with DIRECT or REGEXP membership IN() the list
  1646. of groups returned.
  1647. =item C<direct_group_membership>
  1648. Returns a reference to an array of group objects. Groups the user belong to
  1649. by group inheritance are excluded from the list.
  1650. =item C<visible_groups_inherited>
  1651. Returns a list of all groups whose members should be visible to this user.
  1652. Since this list is flattened already, there is no need for all users to
  1653. be have derived groups up-to-date to select the users meeting this criteria.
  1654. =item C<visible_groups_direct>
  1655. Returns a list of groups that the user is aware of.
  1656. =item C<visible_groups_as_string>
  1657. Returns the result of C<visible_groups_inherited> as a string (a comma-separated
  1658. list).
  1659. =item C<product_responsibilities>
  1660. Retrieve user's product responsibilities as a list of component objects.
  1661. Each object is a component the user has a responsibility for.
  1662. =item C<can_bless>
  1663. When called with no arguments:
  1664. Returns C<1> if the user can bless at least one group, returns C<0> otherwise.
  1665. When called with one argument:
  1666. Returns C<1> if the user can bless the group with that id, returns
  1667. C<0> otherwise.
  1668. =item C<wants_bug_mail>
  1669. Returns true if the user wants mail for a given bug change.
  1670. =item C<wants_mail>
  1671. Returns true if the user wants mail for a given set of events. This method is
  1672. more general than C<wants_bug_mail>, allowing you to check e.g. permissions
  1673. for flag mail.
  1674. =item C<is_mover>
  1675. Returns true if the user is in the list of users allowed to move bugs
  1676. to another database. Note that this method doesn't check whether bug
  1677. moving is enabled.
  1678. =item C<is_insider>
  1679. Returns true if the user can access private comments and attachments,
  1680. i.e. if the 'insidergroup' parameter is set and the user belongs to this group.
  1681. =item C<is_global_watcher>
  1682. Returns true if the user is a global watcher,
  1683. i.e. if the 'globalwatchers' parameter contains the user.
  1684. =back
  1685. =head1 CLASS FUNCTIONS
  1686. These are functions that are not called on a User object, but instead are
  1687. called "statically," just like a normal procedural function.
  1688. =over 4
  1689. =item C<create>
  1690. The same as L<Bugzilla::Object/create>.
  1691. Params: login_name - B<Required> The login name for the new user.
  1692. realname - The full name for the new user.
  1693. cryptpassword - B<Required> The password for the new user.
  1694. Even though the name says "crypt", you should just specify
  1695. a plain-text password. If you specify '*', the user will not
  1696. be able to log in using DB authentication.
  1697. disabledtext - The disable-text for the new user. If given, the user
  1698. will be disabled, meaning he cannot log in. Defaults to an
  1699. empty string.
  1700. disable_mail - If 1, bug-related mail will not be sent to this user;
  1701. if 0, mail will be sent depending on the user's email preferences.
  1702. =item C<check>
  1703. Takes a username as its only argument. Throws an error if there is no
  1704. user with that username. Returns a C<Bugzilla::User> object.
  1705. =item C<is_available_username>
  1706. Returns a boolean indicating whether or not the supplied username is
  1707. already taken in Bugzilla.
  1708. Params: $username (scalar, string) - The full login name of the username
  1709. that you are checking.
  1710. $old_username (scalar, string) - If you are checking an email-change
  1711. token, insert the "old" username that the user is changing from,
  1712. here. Then, as long as it's the right user for that token, he
  1713. can change his username to $username. (That is, this function
  1714. will return a boolean true value).
  1715. =item C<login_to_id($login, $throw_error)>
  1716. Takes a login name of a Bugzilla user and changes that into a numeric
  1717. ID for that user. This ID can then be passed to Bugzilla::User::new to
  1718. create a new user.
  1719. If no valid user exists with that login name, then the function returns 0.
  1720. However, if $throw_error is set, the function will throw a user error
  1721. instead of returning.
  1722. This function can also be used when you want to just find out the userid
  1723. of a user, but you don't want the full weight of Bugzilla::User.
  1724. However, consider using a Bugzilla::User object instead of this function
  1725. if you need more information about the user than just their ID.
  1726. =item C<user_id_to_login($user_id)>
  1727. Returns the login name of the user account for the given user ID. If no
  1728. valid user ID is given or the user has no entry in the profiles table,
  1729. we return an empty string.
  1730. =item C<validate_password($passwd1, $passwd2)>
  1731. Returns true if a password is valid (i.e. meets Bugzilla's
  1732. requirements for length and content), else returns false.
  1733. Untaints C<$passwd1> if successful.
  1734. If a second password is passed in, this function also verifies that
  1735. the two passwords match.
  1736. =back
  1737. =head1 SEE ALSO
  1738. L<Bugzilla|Bugzilla>