1
/****************************************************************************
3
| Copyright (c) 2007 Novell, Inc.
6
| This program is free software; you can redistribute it and/or
7
| modify it under the terms of version 2 of the GNU General Public License as
8
| published by the Free Software Foundation.
10
| This program is distributed in the hope that it will be useful,
11
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
| GNU General Public License for more details.
15
| You should have received a copy of the GNU General Public License
16
| along with this program; if not, contact Novell, Inc.
18
| To contact Novell about this file by physical or electronic mail,
19
| you may find current contact information at www.novell.com
21
| Author: Mike Lasky <mlasky@novell.com>
22
|***************************************************************************/
25
using System.Collections;
31
namespace Simias.Policy
34
/// List used to specify a filter list to the policy.
36
public struct FileTypeEntry
41
/// File extension to add as filter.
43
private string fileName;
46
/// If true then file extension will be allowed to pass through the filter
47
/// If false then file will be disallowed to pass through the filter.
52
/// If true then filter comparision will be case-insensitive.
54
private bool ignoreCase;
59
/// Gets the filter extension name.
63
get { return fileName; }
67
/// Gets whether filter extension is allowed or disallowed through the filter.
71
get { return allowed; }
75
/// Gets whether the filter comparision will be case-insensitive.
77
public bool IgnoreCase
79
get { return ignoreCase; }
85
/// Initializes an instance of the object.
87
/// <param name="fileName">Filename to use as a filter. Can be a regular expression.</param>
88
/// <param name="allowed">If true then all files that have extensions that match the
89
/// fileNameExtension parameter will be allowed to pass through the filter.</param>
90
public FileTypeEntry( string fileName, bool allowed ) :
91
this ( fileName, allowed, false )
96
/// Initializes an instance of the object.
98
/// <param name="fileName">Filename to use as a filter. Can be a regular expression.</param>
99
/// <param name="allowed">If true then all files that have extensions that match the
100
/// fileNameExtension parameter will be allowed to pass through the filter.</param>
101
/// <param name="ignoreCase">If true filter comparision will be case-insensitive.</param>
102
public FileTypeEntry( string fileName, bool allowed, bool ignoreCase )
104
this.fileName = fileName;
105
this.allowed = allowed;
106
this.ignoreCase = ignoreCase;
112
/// Implements the file type filter policy.
114
public class FileTypeFilter
116
#region Class Members
119
/// Used to log messages.
121
static private readonly ISimiasLog log = SimiasLogManager.GetLogger( typeof( Store ) );
124
/// Well known name for the file type filter policy.
126
static public string FileTypeFilterPolicyID = "e69ff680-3f75-412e-a929-1b0247ed4041";
129
/// Well known name for the file type filter policy description.
131
static public string FileTypeFilterShortDescription = "File type filter";
134
/// Policy object that contains the aggregate policy for the domain and member only.
136
private Policy memberPolicy;
139
/// Policy object that contains the aggregate policy including the collection limits.
141
private Policy collectionPolicy = null;
146
/// Gets the file type filter list.
148
public FileTypeEntry[] FilterList
150
get { return GetPatterns( ( collectionPolicy != null ) ? collectionPolicy : memberPolicy ); }
156
/// Initializes a new instance of an object.
158
/// <param name="member">Member that this file type filter is associated with.</param>
159
private FileTypeFilter( Member member )
161
PolicyManager pm = new PolicyManager();
162
this.memberPolicy = pm.GetAggregatePolicy( FileTypeFilterPolicyID, member, true );
166
/// Initializes a new instance of an object.
168
/// <param name="collection">Collection that this disk space quota is associated with.</param>
169
private FileTypeFilter( Collection collection )
171
PolicyManager pm = new PolicyManager();
172
Member member = collection.Owner;
173
this.memberPolicy = pm.GetAggregatePolicy( FileTypeFilterPolicyID, member, true );
174
this.collectionPolicy = pm.GetAggregatePolicy( FileTypeFilterPolicyID, member, collection, true );
178
#region Factory Methods
180
/// Creates a system wide file filter policy.
182
/// <param name="domainID">Domain that the filter will be associated with.</param>
183
/// <param name="patterns">File type patterns that will be used to filter files.</param>
184
static public void Create( string domainID, FileTypeEntry[] patterns )
186
// Need a policy manager.
187
PolicyManager pm = new PolicyManager();
189
// See if the policy already exists.
190
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID, domainID );
191
if ( patterns.Length > 0 )
193
if ( policy == null )
195
// The policy does not exist, create a new one and add the rules.
196
policy = new Policy( FileTypeFilterPolicyID, FileTypeFilterShortDescription );
200
// The policy already exists, delete the old rules.
201
foreach ( Rule r in policy.Rules )
203
policy.DeleteRule( r );
207
// Add the new rules and save the policy.
208
foreach( FileTypeEntry fte in patterns )
210
policy.AddRule( new Rule( fte.Name, fte.IgnoreCase ? Rule.Operation.RegExp_IgnoreCase : Rule.Operation.RegExp, fte.Allowed ? Rule.Result.Allow : Rule.Result.Deny ) );
213
pm.CommitPolicy( policy, domainID );
215
else if ( policy != null )
217
// An empty array is the same thing as deleting the policy.
218
pm.DeletePolicy( policy );
223
/// Creates a file type filter policy for the specified member.
225
/// <param name="member">Member that the filter will be associated with.</param>
226
/// <param name="patterns">File type patterns that will be used to filter files.</param>
227
static public void Create( Member member, FileTypeEntry[] patterns )
229
// Need a policy manager.
230
PolicyManager pm = new PolicyManager();
232
// See if the policy already exists.
233
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID, member );
234
if ( patterns.Length > 0 )
236
if ( policy == null )
238
// The policy does not exist, create a new one and add the rules.
239
policy = new Policy( FileTypeFilterPolicyID, FileTypeFilterShortDescription );
243
// The policy already exists, delete the old rules.
244
foreach ( Rule r in policy.Rules )
246
policy.DeleteRule( r );
250
// Add the new rules and save the policy.
251
foreach( FileTypeEntry fte in patterns )
253
policy.AddRule( new Rule( fte.Name, fte.IgnoreCase ? Rule.Operation.RegExp_IgnoreCase : Rule.Operation.RegExp, fte.Allowed ? Rule.Result.Allow : Rule.Result.Deny ) );
256
pm.CommitPolicy( policy, member );
258
else if ( policy != null )
260
// An empty array is the same thing as deleting the policy.
261
pm.DeletePolicy( policy );
266
/// Creates a file type filter policy for the specified collection.
268
/// <param name="collection">Collection that the filter will be associated with.</param>
269
/// <param name="patterns">File type patterns that will be used to filter files.</param>
270
static public void Create( Collection collection, FileTypeEntry[] patterns )
272
// Need a policy manager.
273
PolicyManager pm = new PolicyManager();
275
// See if the policy already exists.
276
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID, collection );
277
if ( patterns.Length > 0 )
279
if ( policy == null )
281
// The policy does not exist, create a new one and add the rules.
282
policy = new Policy( FileTypeFilterPolicyID, FileTypeFilterShortDescription );
286
// The policy already exists, delete the old rules.
287
foreach ( Rule r in policy.Rules )
289
policy.DeleteRule( r );
293
// Add the new rules and save the policy.
294
foreach( FileTypeEntry fte in patterns )
296
policy.AddRule( new Rule( fte.Name, fte.IgnoreCase ? Rule.Operation.RegExp_IgnoreCase : Rule.Operation.RegExp, fte.Allowed ? Rule.Result.Allow : Rule.Result.Deny ) );
299
pm.CommitPolicy( policy, collection );
301
else if ( policy != null )
303
// An empty array is the same thing as deleting the policy.
304
pm.DeletePolicy( policy );
309
/// Creates a file type filter policy for the current user on the current machine.
311
/// <param name="patterns">File type patterns that will be used to filter files.</param>
312
static public void Create( FileTypeEntry[] patterns )
314
// Need a policy manager.
315
PolicyManager pm = new PolicyManager();
317
// See if the policy already exists.
318
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID );
319
if ( patterns.Length > 0 )
321
if ( policy == null )
323
// The policy does not exist, create a new one and add the rules.
324
policy = new Policy( FileTypeFilterPolicyID, FileTypeFilterShortDescription );
328
// The policy already exists, delete the old rules.
329
foreach ( Rule r in policy.Rules )
331
policy.DeleteRule( r );
335
// Add the new rules and save the policy.
336
foreach( FileTypeEntry fte in patterns )
338
policy.AddRule( new Rule( fte.Name, fte.IgnoreCase ? Rule.Operation.RegExp_IgnoreCase : Rule.Operation.RegExp, fte.Allowed ? Rule.Result.Allow : Rule.Result.Deny ) );
341
pm.CommitLocalMachinePolicy( policy );
343
else if ( policy != null )
345
// An empty array is the same thing as deleting the policy.
346
pm.DeletePolicy( policy );
351
/// Deletes a system wide file filter policy.
353
/// <param name="domainID">Domain that the filter will be associated with.</param>
354
static public void Delete( string domainID )
356
// Need a policy manager.
357
PolicyManager pm = new PolicyManager();
359
// See if the policy already exists.
360
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID, domainID );
361
if ( policy != null )
363
// Delete the policy.
364
pm.DeletePolicy( policy );
369
/// Deletes a file type filter policy for the specified member.
371
/// <param name="member">Member that the filter will be associated with.</param>
372
static public void Delete( Member member )
374
// Need a policy manager.
375
PolicyManager pm = new PolicyManager();
377
// See if the policy already exists.
378
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID, member );
379
if ( policy != null )
381
// Delete the policy.
382
pm.DeletePolicy( policy );
387
/// Deletes a file type filter policy for the specified collection.
389
/// <param name="collection">Collection that the filter will be associated with.</param>
390
static public void Delete( Collection collection )
392
// Need a policy manager.
393
PolicyManager pm = new PolicyManager();
395
// See if the policy already exists.
396
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID, collection );
397
if ( policy != null )
399
// Delete the policy.
400
pm.DeletePolicy( policy );
405
/// Deletes a file type filter policy for the current user on the current machine.
407
static public void Delete()
409
// Need a policy manager.
410
PolicyManager pm = new PolicyManager();
412
// See if the policy already exists.
413
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID );
414
if ( policy != null )
416
// Delete the policy.
417
pm.DeletePolicy( policy );
422
/// Gets the aggregate file type filter policy for the specified member.
424
/// <param name="member">Member that filter is associated with.</param>
425
/// <returns>A FileTypeFilter object that contains the policy for the specified member.</returns>
426
static public FileTypeFilter Get( Member member )
428
return new FileTypeFilter( member );
432
/// Gets the aggregate file type filter policy for the specified member and collection.
434
/// <param name="member">Member that filter is associated with.</param>
435
/// <param name="collection">Collection to add to the aggregate quota policy.</param>
436
/// <returns>A FileTypeFilter object that contains the policy for the specified member.</returns>
437
[ Obsolete( "This method is obsolete. Please use DiskSpaceQuota.Get( Collection collection ) instead.", false ) ]
438
static public FileTypeFilter Get( Member member, Collection collection )
440
return FileTypeFilter.Get( collection );
444
/// Gets the aggregate file type filter policy for the specified member and collection.
446
/// <param name="collection">Collection to add to the aggregate quota policy.</param>
447
/// <returns>A FileTypeFilter object that contains the policy for the specified member.</returns>
448
static public FileTypeFilter Get( Collection collection )
450
return new FileTypeFilter( collection );
454
/// Gets the file type filter patterns associated with the specified domain.
456
/// <param name="domainID">Domain that the filter is associated with.</param>
457
/// <returns>Array of file type filter patterns for this policy if successful. If there are no
458
/// filter patterns then null is returned.</returns>
459
static public FileTypeEntry[] GetPatterns( string domainID )
461
PolicyManager pm = new PolicyManager();
462
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID, domainID );
463
return ( policy != null ) ? GetPatterns( policy ) : null;
467
/// Gets the file type filter patterns associated with the specified member.
469
/// <param name="member">Member that the filter is associated with.</param>
470
/// <returns>Array of file type filter patterns for this policy if successful. If there are no
471
/// filter patterns then null is returned.</returns>
472
static public FileTypeEntry[] GetPatterns( Member member )
474
PolicyManager pm = new PolicyManager();
475
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID, member );
476
return ( policy != null ) ? GetPatterns( policy ) : null;
480
/// Gets the file type filter patterns associated with the specified collection.
482
/// <param name="collection">Collection that the limit is associated with.</param>
483
/// <returns>Array of file type filter patterns for this policy if successful. If there are no
484
/// filter patterns then null is returned.</returns>
485
static public FileTypeEntry[] GetPatterns( Collection collection )
487
PolicyManager pm = new PolicyManager();
488
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID, collection );
489
return ( policy != null ) ? GetPatterns( policy ) : null;
493
/// Gets the file type filter patterns associated with the current user on the current machine.
495
/// <returns>Array of file type filter patterns for this policy if successful. If there are no
496
/// filter patterns then null is returned.</returns>
497
static public FileTypeEntry[] GetPatterns()
499
PolicyManager pm = new PolicyManager();
500
Policy policy = pm.GetPolicy( FileTypeFilterPolicyID );
501
return ( policy != null ) ? GetPatterns( policy ) : null;
505
/// Sets the file type filter associated with the specified domain.
507
/// <param name="domainID">Domain that the filter is associated with.</param>
508
/// <param name="patterns">File type patterns that will be used to filter files.</param>
509
static public void Set( string domainID, FileTypeEntry[] patterns )
511
Create( domainID, patterns );
515
/// Sets the file type filter associated with the specified member.
517
/// <param name="member">Member that the filter is associated with.</param>
518
/// <param name="patterns">File type patterns that will be used to filter files.</param>
519
static public void Set( Member member, FileTypeEntry[] patterns )
521
Create( member, patterns );
525
/// Sets the file type filter associated with the specified collection.
527
/// <param name="collection">Collection that the filter is associated with.</param>
528
/// <param name="patterns">File type patterns that will be used to filter files.</param>
529
static public void Set( Collection collection, FileTypeEntry[] patterns )
531
Create( collection, patterns );
535
/// Sets the file type filter associated with the current user on the current machine.
537
/// <param name="patterns">File type patterns that will be used to filter files.</param>
538
static public void Set( FileTypeEntry[] patterns )
544
#region Private Methods
546
/// Gets the file type filter patterns for the specified policy.
548
/// <param name="policy">Policy to retrieve the filter patterns from.</param>
549
/// <returns>An array of file type filter patterns for the given policy.</returns>
550
static private FileTypeEntry[] GetPatterns( Policy policy )
552
// List to hold the temporary results.
553
ArrayList tempList = new ArrayList();
555
// Return all of the rules.
556
if ( policy != null )
558
foreach ( Rule rule in policy.Rules )
560
FileTypeEntry fte = new FileTypeEntry( rule.Operand as string, ( rule.RuleResult == Rule.Result.Allow ) ? true : false, ( rule.RuleOperation == Rule.Operation.RegExp ) ? false : true );
565
return tempList.ToArray( typeof( FileTypeEntry ) ) as FileTypeEntry[];
569
#region Public Methods
571
/// Returns whether the specified file is allowed to pass through the filter.
573
/// <param name="fileName">Name of the file including its extension.</param>
574
/// <returns>True if the file is allowed to pass through the filter. Otherwise false is returned.</returns>
575
public bool Allowed( string fileName )
577
bool isAllowed = true;
578
string fullPath = Path.GetFileName( fileName );
580
// Check the overall domain/member policy first to make sure that the file type is not excluded.
581
if ( memberPolicy != null )
583
// Apply the rule to see if there is space available.
584
isAllowed = ( memberPolicy.Apply( fullPath ) == Rule.Result.Allow );
587
if ( ( collectionPolicy != null ) && isAllowed )
589
// Apply the rule to see if the file type is allowed in the collection.
590
isAllowed = ( collectionPolicy.Apply( fullPath ) == Rule.Result.Allow );