import rt 3.6.4
[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-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/copyleft/gpl.html.
28
29
30 # CONTRIBUTION SUBMISSION POLICY:
31
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46
47 # END BPS TAGGED BLOCK }}}
48 package RT::Interface::Email::Auth::MailFrom;
49 use RT::Interface::Email qw(ParseSenderAddressFromHead CreateUser);
50
51 # This is what the ordinary, non-enhanced gateway does at the moment.
52
53 sub GetCurrentUser {
54     my %args = ( Message     => undef,
55                  CurrentUser => undef,
56                  AuthLevel   => undef,
57                  Ticket      => undef,
58                  Queue       => undef,
59                  Action      => undef,
60                  @_ );
61
62
63     # We don't need to do any external lookups
64     my ( $Address, $Name ) = ParseSenderAddressFromHead( $args{'Message'}->head );
65
66     unless ($Address) {
67         return ( $args{'CurrentUser'}, -1 );
68     }
69
70     my $CurrentUser = RT::CurrentUser->new();
71     $CurrentUser->LoadByEmail($Address);
72
73     unless ( $CurrentUser->Id ) {
74         $CurrentUser->LoadByName($Address);
75     }
76
77     if ( $CurrentUser->Id ) {
78         return ( $CurrentUser, 1 );
79     }
80     
81
82
83     # If the user can't be loaded, we may need to create one. Figure out the acl situation.
84     my $unpriv = RT::Group->new($RT::SystemUser);
85     $unpriv->LoadSystemInternalGroup('Unprivileged');
86     unless ( $unpriv->Id ) {
87         $RT::Logger->crit( "Auth::MailFrom couldn't find the 'Unprivileged' internal group" );
88         return ( $args{'CurrentUser'}, -1 );
89     }
90
91     my $everyone = RT::Group->new($RT::SystemUser);
92     $everyone->LoadSystemInternalGroup('Everyone');
93     unless ( $everyone->Id ) {
94         $RT::Logger->crit( "Auth::MailFrom couldn't find the 'Everyone' internal group");
95         return ( $args{'CurrentUser'}, -1 );
96     }
97
98     # but before we do that, we need to make sure that the created user would have the right
99     # to do what we're doing.
100     if ( $args{'Ticket'} && $args{'Ticket'}->Id ) {
101         # We have a ticket. that means we're commenting or corresponding
102         if ( $args{'Action'} =~ /^comment$/i ) {
103
104             # check to see whether "Everyone" or "Unprivileged users" can comment on tickets
105             unless ( $everyone->PrincipalObj->HasRight(
106                                                       Object => $args{'Queue'},
107                                                       Right => 'CommentOnTicket'
108                      )
109                      || $unpriv->PrincipalObj->HasRight(
110                                                       Object => $args{'Queue'},
111                                                       Right => 'CommentOnTicket'
112                      )
113               ) {
114                 return ( $args{'CurrentUser'}, 0 );
115             }
116         }
117         elsif ( $args{'Action'} =~ /^correspond$/i ) {
118
119             # check to see whether "Everybody" or "Unprivileged users" can correspond on tickets
120             unless ( $everyone->PrincipalObj->HasRight(Object => $args{'Queue'},
121                                                        Right  => 'ReplyToTicket'
122                      )
123                      || $unpriv->PrincipalObj->HasRight(
124                                                        Object => $args{'Queue'},
125                                                        Right  => 'ReplyToTicket'
126                      )
127               ) {
128                 return ( $args{'CurrentUser'}, 0 );
129             }
130
131         }
132         elsif ( $args{'Action'} =~ /^take$/i ) {
133
134             # check to see whether "Everybody" or "Unprivileged users" can correspond on tickets
135             unless ( $everyone->PrincipalObj->HasRight(Object => $args{'Queue'},
136                                                        Right  => 'OwnTicket'
137                      )
138                      || $unpriv->PrincipalObj->HasRight(
139                                                        Object => $args{'Queue'},
140                                                        Right  => 'OwnTicket'
141                      )
142               ) {
143                 return ( $args{'CurrentUser'}, 0 );
144             }
145
146         }
147         elsif ( $args{'Action'} =~ /^resolve$/i ) {
148
149             # check to see whether "Everybody" or "Unprivileged users" can correspond on tickets
150             unless ( $everyone->PrincipalObj->HasRight(Object => $args{'Queue'},
151                                                        Right  => 'ModifyTicket'
152                      )
153                      || $unpriv->PrincipalObj->HasRight(
154                                                        Object => $args{'Queue'},
155                                                        Right  => 'ModifyTicket'
156                      )
157               ) {
158                 return ( $args{'CurrentUser'}, 0 );
159             }
160
161         }
162         else {
163             return ( $args{'CurrentUser'}, 0 );
164         }
165     }
166
167     # We're creating a ticket
168     elsif ( $args{'Queue'} && $args{'Queue'}->Id ) {
169
170         # check to see whether "Everybody" or "Unprivileged users" can create tickets in this queue
171         unless ( $everyone->PrincipalObj->HasRight( Object => $args{'Queue'},
172                                                     Right  => 'CreateTicket' )
173           ) {
174             return ( $args{'CurrentUser'}, 0 );
175         }
176
177     }
178
179     $CurrentUser = CreateUser( undef, $Address, $Name, $Address, $args{'Message'} );
180
181     return ( $CurrentUser, 1 );
182 }
183
184 eval "require RT::Interface::Email::Auth::MailFrom_Vendor";
185 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Email/Auth/MailFrom_Vendor.pm});
186 eval "require RT::Interface::Email::Auth::MailFrom_Local";
187 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Email/Auth/MailFrom_Local.pm});
188
189 1;