Template_Overlay.pm 8.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# BEGIN LICENSE BLOCK
# 
# Copyright (c) 1996-2002 Jesse Vincent <jesse@bestpractical.com>
# 
# (Except where explictly superceded by other copyright notices)
# 
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org
# 
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# 
# Unless otherwise specified, all modifications, corrections or
# extensions to this work which alter its source code become the
# property of Best Practical Solutions, LLC when submitted for
# inclusion in the work.
# 
# 
# END LICENSE BLOCK
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Portions Copyright 2000 Tobias Brox <tobix@cpan.org> 

=head1 NAME

  RT::Template - RT's template object

=head1 SYNOPSIS

  use RT::Template;

=head1 DESCRIPTION


=head1 METHODS

=begin testing

ok(require RT::Template);

=end testing

=cut

48
use strict;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
49
50
no warnings qw(redefine);

51
use Text::Template;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
52
53
use MIME::Entity;
use MIME::Parser;
54
55
use File::Temp qw /tempdir/;

Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

# {{{ sub _Accessible 

sub _Accessible {
    my $self = shift;
    my %Cols = (
        id            => 'read',
        Name          => 'read/write',
        Description   => 'read/write',
        Type          => 'read/write',    #Type is one of Action or Message
        Content       => 'read/write',
        Queue         => 'read/write',
        Creator       => 'read/auto',
        Created       => 'read/auto',
        LastUpdatedBy => 'read/auto',
        LastUpdated   => 'read/auto'
    );
    return $self->SUPER::_Accessible( @_, %Cols );
}

# }}}

# {{{ sub _Set

sub _Set {
    my $self = shift;

    # use super::value or we get acl blocked
    if ( ( defined $self->SUPER::_Value('Queue') )
        && ( $self->SUPER::_Value('Queue') == 0 ) )
    {
87
        unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'ModifyTemplate') ) {
88
            return ( 0, $self->loc('Permission Denied') );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
89
90
91
92
93
        }
    }
    else {

        unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
94
            return ( 0, $self->loc('Permission Denied') );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
        }
    }
    return ( $self->SUPER::_Set(@_) );

}

# }}}

# {{{ sub _Value 

=head2 _Value

Takes the name of a table column.
Returns its value as a string, if the user passes an ACL check

110
111
112
113
114
115
116
117
118
119
120
121
122

=begin testing

my $t = RT::Template->new($RT::SystemUser);
$t->Create(Name => "Foo", Queue => 1);
my $t2 = RT::Template->new($RT::Nobody);
$t2->Load($t->Id);
ok($t2->QueueObj->id, "Got the template's queue objet");

=end testing



Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
123
124
125
126
127
128
129
=cut

sub _Value {

    my $self  = shift;
    my $field = shift;

130
   
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
131
132
133
134
135
    #If the current user doesn't have ACLs, don't let em at it.  
    #use super::value or we get acl blocked
    if ( ( !defined $self->__Value('Queue') )
        || ( $self->__Value('Queue') == 0 ) )
    {
136
        unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'ShowTemplate') ) {
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
            return (undef);
        }
    }
    else {
        unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) {
            return (undef);
        }
    }
    return ( $self->__Value($field) );

}

# }}}

# {{{ sub Load

=head2 Load <identifer>

Load a template, either by number or by name

=cut

sub Load {
    my $self       = shift;
    my $identifier = shift;

    if ( !$identifier ) {
        return (undef);
    }

    if ( $identifier !~ /\D/ ) {
        $self->SUPER::LoadById($identifier);
    }
    else {
        $self->LoadByCol( 'Name', $identifier );

    }
}

# }}}

# {{{ sub LoadGlobalTemplate

=head2 LoadGlobalTemplate NAME

Load the global tempalte with the name NAME

=cut

sub LoadGlobalTemplate {
    my $self = shift;
    my $id   = shift;

    return ( $self->LoadQueueTemplate( Queue => 0, Name => $id ) );
}

# }}}

# {{{ sub LoadQueueTemplate

=head2  LoadQueueTemplate (Queue => QUEUEID, Name => NAME)

Loads the Queue template named NAME for Queue QUEUE.

=cut

sub LoadQueueTemplate {
    my $self = shift;
    my %args = (
        Queue => undef,
        Name  => undef
    );

    return ( $self->LoadByCols( Name => $args{'Name'}, Queue => {'Queue'} ) );

}

# }}}

# {{{ sub Create

=head2 Create

Takes a paramhash of Content, Queue, Name and Description.
Name should be a unique string identifying this Template.
Description and Content should be the template's title and content.
Queue should be 0 for a global template and the queue # for a queue-specific 
template.

Returns the Template's id # if the create was successful. Returns undef for
unknown database failure.


=cut

sub Create {
    my $self = shift;
    my %args = (
        Content     => undef,
        Queue       => 0,
        Description => '[no description]',
        Type => 'Action',    #By default, template are 'Action' templates
        Name => undef,
        @_
    );

243
    if ( !$args{'Queue'}  ) {
244
        unless ( $self->CurrentUser->HasRight(Right =>'ModifyTemplate', Object => $RT::System) ) {
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
245
246
            return (undef);
        }
247
        $args{'Queue'} = 0;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
248
249
250
    }
    else {
        my $QueueObj = new RT::Queue( $self->CurrentUser );
251
        $QueueObj->Load( $args{'Queue'} ) || return ( 0, $self->loc('Invalid queue') );
252
    
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
253
254
255
        unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) {
            return (undef);
        }
256
        $args{'Queue'} = $QueueObj->Id;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
257
258
259
260
    }

    my $result = $self->SUPER::Create(
        Content => $args{'Content'},
261
        Queue   =>  $args{'Queue'},
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
        Description => $args{'Description'},
        Name        => $args{'Name'}
    );

    return ($result);

}

# }}}

# {{{ sub Delete

=head2 Delete

Delete this template.

=cut

sub Delete {
    my $self = shift;

283
    unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
284
        return ( 0, $self->loc('Permission Denied') );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
    }

    return ( $self->SUPER::Delete(@_) );
}

# }}}

# {{{ sub MIMEObj
sub MIMEObj {
    my $self = shift;
    return ( $self->{'MIMEObj'} );
}

# }}}

# {{{ sub Parse 

=item Parse

304
305
 This routine performs Text::Template parsing on the template and then
 imports the results into a MIME::Entity so we can really use it
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
 It returns a tuple of (val, message)
 If val is 0, the message contains an error message

=cut

sub Parse {
    my $self = shift;

    #We're passing in whatever we were passed. it's destined for _ParseContent
    my $content = $self->_ParseContent(@_);

    #Lets build our mime Entity

    my $parser = MIME::Parser->new();

321
322
323
324
325
326
327
328
329
330
331
332
333
    # Setup output directory for files. from RT::EmailParser::_SetupMIMEParser
    my $AttachmentDir = File::Temp::tempdir( TMPDIR => 1, CLEANUP => 1 );
    # Set up output directory for files:
    $parser->output_dir("$AttachmentDir");
    #If someone includes a message, don't extract it
    $parser->extract_nested_messages(1);
    # Set up the prefix for files with auto-generated names:
    $parser->output_prefix("part");
    # If content length is <= 50000 bytes, store each msg as in-core scalar;
    # Else, write to a disk file (the default action):
    $parser->output_to_core(50000);


Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
334
335
336
    ### Should we forgive normally-fatal errors?
    $parser->ignore_errors(1);
    $self->{'MIMEObj'} = eval { $parser->parse_data($content) };
337
    my $error = ( $@ || $parser->last_error );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
338
339
340
341
342
343
344
345
346

    if ($error) {
        $RT::Logger->error("$error");
        return ( 0, $error );
    }

    # Unfold all headers
    $self->{'MIMEObj'}->head->unfold();

347
    return ( 1, $self->loc("Template parsed") );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
   

}

# }}}

# {{{ sub _ParseContent

# Perform Template substitutions on the template

sub _ParseContent {
    my $self = shift;
    my %args = (
        Argument       => undef,
        TicketObj      => undef,
        TransactionObj => undef,
        @_
    );


    $T::Ticket      = $args{'TicketObj'};
    $T::Transaction = $args{'TransactionObj'};
    $T::Argument    = $args{'Argument'};
    $T::rtname      = $RT::rtname;

    # We need to untaint the content of the template, since we'll be working
    # with it
    my $content = $self->Content();
    $content =~ s/^(.*)$/$1/;
377
378
    my $template = Text::Template->new(
        TYPE   => 'STRING',
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
379
380
381
        SOURCE => $content
    );

382
    my $retval = $template->fill_in( PACKAGE => 'T' );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
    return ($retval);
}

# }}}

# {{{ sub CurrentUserHasQueueRight

=head2 CurrentUserHasQueueRight

Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.

=cut

sub CurrentUserHasQueueRight {
    my $self = shift;
    return ( $self->QueueObj->CurrentUserHasRight(@_) );
}

# }}}
1;