import rt 3.4.6
[freeside.git] / rt / lib / RT / Interface / Email / Auth / MailFrom.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
6 #                                          <jesse@bestpractical.com>
7
8 # (Except where explicitly superseded by other copyright notices)
9
10
11 # LICENSE:
12
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27
28 # CONTRIBUTION SUBMISSION POLICY:
29
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
35
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
44
45 # END BPS TAGGED BLOCK }}}
46 package RT::Interface::Email::Auth::MailFrom;
47 use RT::Interface::Email qw(ParseSenderAddressFromHead CreateUser);
48
49 # This is what the ordinary, non-enhanced gateway does at the moment.
50
51 sub GetCurrentUser {
52     my %args = ( Message     => undef,
53                  CurrentUser => undef,
54                  AuthLevel   => undef,
55                  Ticket      => undef,
56                  Queue       => undef,
57                  Action      => undef,
58                  @_ );
59
60
61     # We don't need to do any external lookups
62     my ( $Address, $Name ) = ParseSenderAddressFromHead( $args{'Message'}->head );
63
64     unless ($Address) {
65         return ( $args{'CurrentUser'}, -1 );
66     }
67
68     my $CurrentUser = RT::CurrentUser->new();
69     $CurrentUser->LoadByEmail($Address);
70
71     unless ( $CurrentUser->Id ) {
72         $CurrentUser->LoadByName($Address);
73     }
74
75     if ( $CurrentUser->Id ) {
76         return ( $CurrentUser, 1 );
77     }
78     
79
80
81     # If the user can't be loaded, we may need to create one. Figure out the acl situation.
82     my $unpriv = RT::Group->new($RT::SystemUser);
83     $unpriv->LoadSystemInternalGroup('Unprivileged');
84     unless ( $unpriv->Id ) {
85         $RT::Logger->crit( "Auth::MailFrom couldn't find the 'Unprivileged' internal group" );
86         return ( $args{'CurrentUser'}, -1 );
87     }
88
89     my $everyone = RT::Group->new($RT::SystemUser);
90     $everyone->LoadSystemInternalGroup('Everyone');
91     unless ( $everyone->Id ) {
92         $RT::Logger->crit( "Auth::MailFrom couldn't find the 'Everyone' internal group");
93         return ( $args{'CurrentUser'}, -1 );
94     }
95
96     # but before we do that, we need to make sure that the created user would have the right
97     # to do what we're doing.
98     if ( $args{'Ticket'} && $args{'Ticket'}->Id ) {
99         # We have a ticket. that means we're commenting or corresponding
100         if ( $args{'Action'} =~ /^comment$/i ) {
101
102             # check to see whether "Everyone" or "Unprivileged users" can comment on tickets
103             unless ( $everyone->PrincipalObj->HasRight(
104                                                       Object => $args{'Queue'},
105                                                       Right => 'CommentOnTicket'
106                      )
107                      || $unpriv->PrincipalObj->HasRight(
108                                                       Object => $args{'Queue'},
109                                                       Right => 'CommentOnTicket'
110                      )
111               ) {
112                 return ( $args{'CurrentUser'}, 0 );
113             }
114         }
115         elsif ( $args{'Action'} =~ /^correspond$/i ) {
116
117             # check to see whether "Everybody" or "Unprivileged users" can correspond on tickets
118             unless ( $everyone->PrincipalObj->HasRight(Object => $args{'Queue'},
119                                                        Right  => 'ReplyToTicket'
120                      )
121                      || $unpriv->PrincipalObj->HasRight(
122                                                        Object => $args{'Queue'},
123                                                        Right  => 'ReplyToTicket'
124                      )
125               ) {
126                 return ( $args{'CurrentUser'}, 0 );
127             }
128
129         }
130         elsif ( $args{'Action'} =~ /^take$/i ) {
131
132             # check to see whether "Everybody" or "Unprivileged users" can correspond on tickets
133             unless ( $everyone->PrincipalObj->HasRight(Object => $args{'Queue'},
134                                                        Right  => 'OwnTicket'
135                      )
136                      || $unpriv->PrincipalObj->HasRight(
137                                                        Object => $args{'Queue'},
138                                                        Right  => 'OwnTicket'
139                      )
140               ) {
141                 return ( $args{'CurrentUser'}, 0 );
142             }
143
144         }
145         elsif ( $args{'Action'} =~ /^resolve$/i ) {
146
147             # check to see whether "Everybody" or "Unprivileged users" can correspond on tickets
148             unless ( $everyone->PrincipalObj->HasRight(Object => $args{'Queue'},
149                                                        Right  => 'ModifyTicket'
150                      )
151                      || $unpriv->PrincipalObj->HasRight(
152                                                        Object => $args{'Queue'},
153                                                        Right  => 'ModifyTicket'
154                      )
155               ) {
156                 return ( $args{'CurrentUser'}, 0 );
157             }
158
159         }
160         else {
161             return ( $args{'CurrentUser'}, 0 );
162         }
163     }
164
165     # We're creating a ticket
166     elsif ( $args{'Queue'} && $args{'Queue'}->Id ) {
167
168         # check to see whether "Everybody" or "Unprivileged users" can create tickets in this queue
169         unless ( $everyone->PrincipalObj->HasRight( Object => $args{'Queue'},
170                                                     Right  => 'CreateTicket' )
171           ) {
172             return ( $args{'CurrentUser'}, 0 );
173         }
174
175     }
176
177     $CurrentUser = CreateUser( undef, $Address, $Name, $Address, $args{'Message'} );
178
179     return ( $CurrentUser, 1 );
180 }
181
182 eval "require RT::Interface::Email::Auth::MailFrom_Vendor";
183 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Email/Auth/MailFrom_Vendor.pm});
184 eval "require RT::Interface::Email::Auth::MailFrom_Local";
185 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Email/Auth/MailFrom_Local.pm});
186
187 1;