using System; using System.Collections.Generic; using System.Linq; using System.Text; using ScrewTurn.Wiki.AclEngine; namespace ScrewTurn.Wiki.Plugins.SqlCommon { /// /// Implements a SQL ACL Manager. /// public class SqlAclManager : IAclManager { // This class is similar to AclManagerBase in AclEngine // but it does not work with in-memory data. // All operations are actually handled by a backend database, via delegates // The AclChanged event is never fired private StoreEntry _storeEntry; private DeleteEntries _deleteEntries; private RenameResource _renameResource; private RetrieveAllEntries _retrieveAllEntries; private RetrieveEntriesForResource _retrieveEntriesForResource; private RetrieveEntriesForSubject _retrieveEntriesForSubject; /// /// Initializes a new instance of the class. /// /// The delegate. /// The delegate. /// The delegate. /// The delegate. /// The delegate. /// The delegate. public SqlAclManager(StoreEntry storeEntry, DeleteEntries deleteEntries, RenameResource renameResource, RetrieveAllEntries retrieveAllEntries, RetrieveEntriesForResource retrieveEntriesForResource, RetrieveEntriesForSubject retrieveEntriesForSubject) { if(storeEntry == null) throw new ArgumentNullException("storeEntry"); if(deleteEntries == null) throw new ArgumentNullException("deleteEntries"); if(renameResource == null) throw new ArgumentNullException("renameResource"); if(retrieveAllEntries == null) throw new ArgumentNullException("retrieveAllEntries"); if(retrieveEntriesForResource == null) throw new ArgumentNullException("retrieveEntriesForResource"); if(retrieveEntriesForSubject == null) throw new ArgumentNullException("retrieveEntriesForSubject"); _storeEntry = storeEntry; _deleteEntries = deleteEntries; _renameResource = renameResource; _retrieveAllEntries = retrieveAllEntries; _retrieveEntriesForResource = retrieveEntriesForResource; _retrieveEntriesForSubject = retrieveEntriesForSubject; } /// /// Handles the invokation of event. /// /// The changed entries. /// The change. [Obsolete] private void OnAclChanged(AclEntry[] entries, Change change) { if(AclChanged != null) { AclChanged(this, new AclChangedEventArgs(entries, change)); } } /// /// Stores a new ACL entry. /// /// The controlled resource. /// The action on the controlled resource. /// The subject whose access to the resource/action is controlled. /// The value of the entry. /// true if the entry is stored, false otherwise. /// If , or are null. /// If , or are empty. public bool StoreEntry(string resource, string action, string subject, Value value) { if(resource == null) throw new ArgumentNullException("resource"); if(resource.Length == 0) throw new ArgumentException("Resource cannot be empty", "resource"); if(action == null) throw new ArgumentNullException("action"); if(action.Length == 0) throw new ArgumentException("Action cannot be empty", "action"); if(subject == null) throw new ArgumentNullException("subject"); if(subject.Length == 0) throw new ArgumentException("Subject cannot be empty", "subject"); AclEntry entry = new AclEntry(resource, action, subject, value); return _storeEntry(entry); } /// /// Deletes an ACL entry. /// /// The controlled resource. /// The action on the controlled resource. /// The subject whose access to the resource/action is controlled. /// true if the entry is deleted, false otherwise. /// If , or are null. /// If , or are empty. public bool DeleteEntry(string resource, string action, string subject) { if(resource == null) throw new ArgumentNullException("resource"); if(resource.Length == 0) throw new ArgumentException("Resource cannot be empty", "resource"); if(action == null) throw new ArgumentNullException("action"); if(action.Length == 0) throw new ArgumentException("Action cannot be empty", "action"); if(subject == null) throw new ArgumentNullException("subject"); if(subject.Length == 0) throw new ArgumentException("Subject cannot be empty", "subject"); AclEntry entry = new AclEntry(resource, action, subject, Value.Deny); return _deleteEntries(new[] { entry }); } /// /// Deletes all the ACL entries for a resource. /// /// The controlled resource. /// true if the entries are deleted, false otherwise. /// If is null. /// If is empty. public bool DeleteEntriesForResource(string resource) { if(resource == null) throw new ArgumentNullException("resource"); if(resource.Length == 0) throw new ArgumentException("Resource cannot be empty", "resource"); AclEntry[] entries = _retrieveEntriesForResource(resource); return _deleteEntries(entries); } /// /// Deletes all the ACL entries for a subject. /// /// The subject. /// true if the entries are deleted, false otherwise. /// If is null. /// If is empty. public bool DeleteEntriesForSubject(string subject) { if(subject == null) throw new ArgumentNullException("subject"); if(subject.Length == 0) throw new ArgumentException("Subject cannot be empty", "subject"); AclEntry[] entries = _retrieveEntriesForSubject(subject); return _deleteEntries(entries); } /// /// Renames a resource. /// /// The resource. /// The new name of the resource. /// true if the resource is renamed, false otherwise. /// If or are null. /// If or are empty. public bool RenameResource(string resource, string newName) { if(resource == null) throw new ArgumentNullException("resource"); if(resource.Length == 0) throw new ArgumentException("Resource cannot be empty", "resource"); if(newName == null) throw new ArgumentNullException("newName"); if(newName.Length == 0) throw new ArgumentException("New Name cannot be empty", "newName"); return _renameResource(resource, newName); } /// /// Retrieves all the ACL entries for a resource. /// /// The entries. public AclEntry[] RetrieveAllEntries() { return _retrieveAllEntries(); } /// /// Retrieves all the ACL entries for a resource. /// /// The resource. /// The entries. /// If is null. /// If is empty. public AclEntry[] RetrieveEntriesForResource(string resource) { if(resource == null) throw new ArgumentNullException("resource"); if(resource.Length == 0) throw new ArgumentException("Resource cannot be empty", "resource"); return _retrieveEntriesForResource(resource); } /// /// Retrieves all the ACL entries for a subject. /// /// The subject. /// The entries. /// If is null. /// If is empty. public AclEntry[] RetrieveEntriesForSubject(string subject) { if(subject == null) throw new ArgumentNullException("subject"); if(subject.Length == 0) throw new ArgumentException("Subject cannot be empty", "subject"); return _retrieveEntriesForSubject(subject); } /// /// Initializes the manager data. /// /// The ACL entries. /// If is null. public void InitializeData(AclEntry[] entries) { if(entries == null) throw new ArgumentNullException("entries"); } /// /// Gets the total number of ACL entries. /// public int TotalEntries { get { return RetrieveAllEntries().Length; } } /// /// Event fired when an ACL entry is stored or deleted. /// public event EventHandler AclChanged; } /// /// Defines a delegate for a method that stores a ACL entry in the storage. /// /// The entry to store. /// true if the entry was stored, false otherwise. public delegate bool StoreEntry(AclEntry entry); /// /// Defines a delegate for a method that deletes a ACL entry in the storage. /// /// The entry to delete. /// true if the entry was deleted, false otherwise. public delegate bool DeleteEntry(AclEntry entry); /// /// Defines a delegate for a method that deletes ACL entries in the storage. /// /// The entries to delete. /// true if one or more enties were deleted, false otherwise. public delegate bool DeleteEntries(AclEntry[] entries); /// /// Defines a delegate for a method that renames a resource. /// /// The resource to rename. /// The new name of the resource. /// true if the resource was renamed, false otherwise. public delegate bool RenameResource(string resource, string newName); /// /// Defines a delegate for a method that retrieves all entries. /// /// The entries. public delegate AclEntry[] RetrieveAllEntries(); /// /// Defines a delegate for a method that retrieves all entries for a resource. /// /// The resource. /// The entries of the resource. public delegate AclEntry[] RetrieveEntriesForResource(string resource); /// /// Defines a delegate for a method that retrieves all entries for a subject. /// /// The subject. /// The entries of the subject. public delegate AclEntry[] RetrieveEntriesForSubject(string subject); }