[Nmailserver-commits] SF.net SVN: nmailserver: [184] NMail/trunk/NMail
Brought to you by:
dframpton-oss,
tmyroadctfig
|
From: <tmy...@us...> - 2007-04-19 12:09:29
|
Revision: 184
http://svn.sourceforge.net/nmailserver/?rev=184&view=rev
Author: tmyroadctfig
Date: 2007-04-19 05:09:29 -0700 (Thu, 19 Apr 2007)
Log Message:
-----------
More work on unit tests and NHibernate local store data.
Modified Paths:
--------------
NMail/trunk/NMail/DataTypes/LocalStore/ILocalStoreData.cs
NMail/trunk/NMail.LocalStore/LocalStore.cs
NMail/trunk/NMail.LocalStoreData.MySql/MySqlLocalStoreData.cs
NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.ACLs.StoreFolderAcl.hbm.xml
NMail/trunk/NMail.LocalStoreData.NHibernate/NHibernateLocalStoreData.cs
NMail/trunk/NMail.UnitTests/NMail.UnitTests.csproj
Added Paths:
-----------
NMail/trunk/NMail.UnitTests/LocalStoreData/SetStoreFolderAceTest1.cs
Modified: NMail/trunk/NMail/DataTypes/LocalStore/ILocalStoreData.cs
===================================================================
--- NMail/trunk/NMail/DataTypes/LocalStore/ILocalStoreData.cs 2007-04-18 13:02:58 UTC (rev 183)
+++ NMail/trunk/NMail/DataTypes/LocalStore/ILocalStoreData.cs 2007-04-19 12:09:29 UTC (rev 184)
@@ -134,15 +134,15 @@
/// Subscribes to a folder.
/// </summary>
/// <param name="userId">The Id of the user to subscribe for.</param>
- /// <param name="folder">The folder to subscribe to.</param>
- void Subscribe(int userId, StoreFolder folder);
+ /// <param name="folder">The Id of the folder to subscribe to.</param>
+ void Subscribe(int userId, int folderId);
/// <summary>
/// Un-subscribes from a folder.
/// </summary>
/// <param name="userId">The Id of the user to unsubscribe for.</param>
- /// <param name="folder">The folder to unsubscribe from.</param>
- void UnSubscribe(int userId, StoreFolder folder);
+ /// <param name="folder">The Id of the folder to unsubscribe from.</param>
+ void UnSubscribe(int userId, int folderId);
#endregion
#region Message Id and Offset
Modified: NMail/trunk/NMail.LocalStore/LocalStore.cs
===================================================================
--- NMail/trunk/NMail.LocalStore/LocalStore.cs 2007-04-18 13:02:58 UTC (rev 183)
+++ NMail/trunk/NMail.LocalStore/LocalStore.cs 2007-04-19 12:09:29 UTC (rev 184)
@@ -592,7 +592,7 @@
// Subscribe to the folder
LocalStoreUser user = LocalStoreData.GetUser(authToken.Username);
- LocalStoreData.Subscribe(user.UserId, folder);
+ LocalStoreData.Subscribe(user.UserId, folder.FolderId);
return LocalStoreFolderResult.OkSuccessful;
} else {
@@ -610,7 +610,7 @@
public void UnSubscribe(IAuthenticationToken authToken, StoreFolder folder) {
// Un-subscribe from the folder
LocalStoreUser user = LocalStoreData.GetUser(authToken.Username);
- LocalStoreData.UnSubscribe(user.UserId, folder);
+ LocalStoreData.UnSubscribe(user.UserId, folder.FolderId);
}
#endregion
#endregion
Modified: NMail/trunk/NMail.LocalStoreData.MySql/MySqlLocalStoreData.cs
===================================================================
--- NMail/trunk/NMail.LocalStoreData.MySql/MySqlLocalStoreData.cs 2007-04-18 13:02:58 UTC (rev 183)
+++ NMail/trunk/NMail.LocalStoreData.MySql/MySqlLocalStoreData.cs 2007-04-19 12:09:29 UTC (rev 184)
@@ -453,12 +453,12 @@
}
}
- public void Subscribe(int userId, StoreFolder folder) {
+ public void Subscribe(int userId, int folderId) {
using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
- if (!folderSubscribed(userId, folder.FolderId)) {
+ if (!folderSubscribed(userId, folderId)) {
cmd.CommandText = "INSERT INTO Subscription (UserId, FolderId) VALUES(?UserId, ?FolderId)";
- cmd.Parameters.Add("?FolderId", folder.FolderId);
+ cmd.Parameters.Add("?FolderId", folderId);
cmd.Parameters.Add("?UserId", userId);
if (cmd.ExecuteNonQuery() != 1) {
@@ -471,12 +471,12 @@
#endregion
#region UnSubscribe
- public void UnSubscribe(int userId, StoreFolder folder) {
+ public void UnSubscribe(int userId, int folderId) {
using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "DELETE FROM Subscription WHERE UserId = ?UserId AND FolderId = ?FolderId";
cmd.Parameters.Add("?UserId", userId);
- cmd.Parameters.Add("?FolderId", folder.FolderId);
+ cmd.Parameters.Add("?FolderId", folderId);
if (cmd.ExecuteNonQuery() != 1) {
throw new System.Data.DataException("Error updating subscription.");
Modified: NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.ACLs.StoreFolderAcl.hbm.xml
===================================================================
--- NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.ACLs.StoreFolderAcl.hbm.xml 2007-04-18 13:02:58 UTC (rev 183)
+++ NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.ACLs.StoreFolderAcl.hbm.xml 2007-04-19 12:09:29 UTC (rev 184)
@@ -12,7 +12,7 @@
<map name="entries" access="field" table="StoreFolderAclEntries">
<key column="IdentifierId" />
<index column="Identifier" type="String" />
- <element column="StoreFolderPrivilege" type="Int32" />
+ <element column="StoreFolderPrivilege" type="Serializable" />
</map>
</class>
Modified: NMail/trunk/NMail.LocalStoreData.NHibernate/NHibernateLocalStoreData.cs
===================================================================
--- NMail/trunk/NMail.LocalStoreData.NHibernate/NHibernateLocalStoreData.cs 2007-04-18 13:02:58 UTC (rev 183)
+++ NMail/trunk/NMail.LocalStoreData.NHibernate/NHibernateLocalStoreData.cs 2007-04-19 12:09:29 UTC (rev 184)
@@ -284,6 +284,10 @@
// Checks done, save!
session.Save(newFolder);
+ // Add an ACL to the folder
+ GenericAce<StoreFolderPrivilege> ace = new GenericAce<StoreFolderPrivilege>(user.Username, StoreFolderPrivilege.Admin, AcePrivilegeType.Allow);
+ SetStoreFolderAce(newFolder.FolderId, ace, session);
+
// Add the new folder to the parent's children
if (parentByName != null) {
parentByName.ChildIds.Add(newFolder.FolderId);
@@ -317,8 +321,9 @@
DeleteChildrenWithoutUpdate(folder, session);
- // TODO: delete ACLs
- // TODO: delete folder subscriptions
+ DeleteStoreFolderAcl(folderId);
+ DeleteSubscriptions(folderId);
+
session.Delete(folder);
// Remove the folder from the parent's list of children if present
@@ -354,8 +359,8 @@
DeleteChildrenWithoutUpdate(child, session);
// Delete the child at the current level
- // TODO: remove child ACLs
- // TODO: delete folder subscriptions
+ DeleteStoreFolderAcl(childId);
+ DeleteSubscriptions(childId);
session.Delete(child);
}
}
@@ -415,9 +420,10 @@
}
newFolder.ParentId = parentId;
newFolder.OwnerUserId = currentFolder.OwnerUserId;
- // TODO: ACLs
session.Save(newFolder);
+ MoveStoreFolderAcl(folderId, newFolder.FolderId);
+ MoveSubscriptions(folderId, newFolder.FolderId);
// Adjust the details of the child folders and move them across
MoveChildren(newFolder, currentFolder, session);
@@ -448,9 +454,11 @@
StoreFolder newChild = new StoreFolder(newParent, currentChild.FolderName);
newChild.ParentId = newParent.FolderId;
newChild.OwnerUserId = currentChild.OwnerUserId;
- // TODO: child ACLs
session.Save(newChild);
+ MoveStoreFolderAcl(childId, newChild.FolderId);
+ MoveSubscriptions(childId, newChild.FolderId);
+
// Add the to parent
newParent.ChildIds.Add(newChild.FolderId);
@@ -500,6 +508,54 @@
#region Folder Subscription
+ #region Delete Subscriptions
+ /// <summary>
+ /// Deletes all subscriptions from a folder.
+ /// </summary>
+ /// <param name="folderId">The Id of the folder to delete the subscriptions from.</param>
+ protected void DeleteSubscriptions(int folderId) {
+ using (ISession session = this.hibernateFactory.OpenSession()) {
+ using (ITransaction tx = session.BeginTransaction()) {
+ IList<FolderSubscription> current = session.CreateCriteria(typeof(FolderSubscription))
+ .Add(Expression.Eq("FolderId", folderId))
+ .List<FolderSubscription>();
+
+ for (int i = 0; i < current.Count; i++) {
+ session.Delete(current[i]);
+ }
+
+ tx.Commit();
+ session.Close();
+ }
+ }
+ }
+ #endregion
+
+ #region Move Subscriptions
+ /// <summary>
+ /// Moves all subscriptions to a new folder.
+ /// </summary>
+ /// <param name="currentFolderId">The current folder Id.</param>
+ /// <param name="newFolderId">The new folder Id.</param>
+ protected void MoveSubscriptions(int currentFolderId, int newFolderId) {
+ using (ISession session = this.hibernateFactory.OpenSession()) {
+ using (ITransaction tx = session.BeginTransaction()) {
+ IList<FolderSubscription> current = session.CreateCriteria(typeof(FolderSubscription))
+ .Add(Expression.Eq("FolderId", currentFolderId))
+ .List<FolderSubscription>();
+
+ for (int i = 0; i < current.Count; i++) {
+ current[i].FolderId = newFolderId;
+ session.Update(current[i]);
+ }
+
+ tx.Commit();
+ session.Close();
+ }
+ }
+ }
+ #endregion
+
#region GetSubscribedFolders
/// <summary>
/// Gets a list of subscribed folders that match the query folder.
@@ -536,18 +592,18 @@
/// Subscribes to a folder.
/// </summary>
/// <param name="userId">The Id of the user to subscribe for.</param>
- /// <param name="folder">The folder to subscribe to.</param>
- public void Subscribe(int userId, StoreFolder folder) {
+ /// <param name="folder">The Id of the folder to subscribe to.</param>
+ public void Subscribe(int userId, int folderId) {
using (ISession session = this.hibernateFactory.OpenSession()) {
using (ITransaction tx = session.BeginTransaction()) {
IList<FolderSubscription> current = session.CreateCriteria(typeof(FolderSubscription))
.Add(Expression.Eq("UserId", userId))
- .Add(Expression.Eq("FolderId", folder.FolderId))
+ .Add(Expression.Eq("FolderId", folderId))
.List<FolderSubscription>();
if (current.Count == 0) {
FolderSubscription subscription = new FolderSubscription();
- subscription.FolderId = folder.FolderId;
+ subscription.FolderId = folderId;
subscription.UserId = userId;
session.Save(subscription);
@@ -565,13 +621,13 @@
/// Un-subscribes from a folder.
/// </summary>
/// <param name="userId">The Id of the user to unsubscribe for.</param>
- /// <param name="folder">The folder to unsubscribe from.</param>
- public void UnSubscribe(int userId, StoreFolder folder) {
+ /// <param name="folder">The Id of the folder to unsubscribe from.</param>
+ public void UnSubscribe(int userId, int folderId) {
using (ISession session = this.hibernateFactory.OpenSession()) {
using (ITransaction tx = session.BeginTransaction()) {
IList<FolderSubscription> current = session.CreateCriteria(typeof(FolderSubscription))
.Add(Expression.Eq("UserId", userId))
- .Add(Expression.Eq("FolderId", folder.FolderId))
+ .Add(Expression.Eq("FolderId", folderId))
.List<FolderSubscription>();
if (current.Count != 0) {
@@ -1266,14 +1322,12 @@
#region Folder ACLs
- #region Set StoreFolder ACE
+ #region Delete StoreFolder ACL
/// <summary>
- /// Sets the privileges on the folder. If no ACE exists for the associated
- /// identifier one is added.
+ /// Completely deletes a store folder's ACL.
/// </summary>
- /// <param name="folder">The Id of the folder to set the ACE on.</param>
- /// <param name="ace">The privileges and the identifier to put.</param>
- public void SetStoreFolderAce(int folderId, GenericAce<StoreFolderPrivilege> ace) {
+ /// <param name="folderId">The Id of the folder to delete the ACL for.</param>
+ protected void DeleteStoreFolderAcl(int folderId) {
using (ISession session = this.hibernateFactory.OpenSession()) {
using (ITransaction tx = session.BeginTransaction()) {
IList<StoreFolderAcl> current = session.CreateCriteria(typeof(StoreFolderAcl))
@@ -1282,18 +1336,44 @@
.List<StoreFolderAcl>();
if (current.Count == 0) {
- // Create a new ACL
- StoreFolder folder = session.Load<StoreFolder>(folderId);
- StoreFolderAcl acl = new StoreFolderAcl(folder);
- acl[ace.Identifier] = ace;
+ // No such ACL
+ throw new ArgumentException("Invalid folder Id.");
- session.Save(acl);
+ } else {
+ // Update the current ACL
+ StoreFolderAcl acl = current[0];
+ session.Delete(acl);
+ }
+ tx.Commit();
+ session.Close();
+ }
+ }
+ }
+ #endregion
+
+ #region Move StoreFolder ACL
+ /// <summary>
+ /// Moves an ACL to a new folder Id.
+ /// </summary>
+ /// <param name="currentFolderId">The Id of the current folder.</param>
+ /// <param name="newFolderId">The Id of the new folder.</param>
+ protected void MoveStoreFolderAcl(int currentFolderId, int newFolderId) {
+ using (ISession session = this.hibernateFactory.OpenSession()) {
+ using (ITransaction tx = session.BeginTransaction()) {
+ IList<StoreFolderAcl> current = session.CreateCriteria(typeof(StoreFolderAcl))
+ .CreateCriteria("Folder")
+ .Add(Expression.Eq("FolderId", currentFolderId))
+ .List<StoreFolderAcl>();
+
+ if (current.Count == 0) {
+ // No such ACL
+ throw new ArgumentException("Invalid folder Id.");
+
} else {
// Update the current ACL
StoreFolderAcl acl = current[0];
- acl[ace.Identifier] = ace;
-
+ acl.Folder = session.Load<StoreFolder>(newFolderId);
session.Update(acl);
}
@@ -1304,6 +1384,53 @@
}
#endregion
+ #region Set StoreFolder ACE
+ /// <summary>
+ /// Sets the privileges on the folder. If no ACE exists for the associated
+ /// identifier one is added.
+ /// </summary>
+ /// <param name="folder">The Id of the folder to set the ACE on.</param>
+ /// <param name="ace">The privileges and the identifier to put.</param>
+ public void SetStoreFolderAce(int folderId, GenericAce<StoreFolderPrivilege> ace) {
+ using (ISession session = this.hibernateFactory.OpenSession()) {
+ using (ITransaction tx = session.BeginTransaction()) {
+ SetStoreFolderAce(folderId, ace, session);
+
+ tx.Commit();
+ session.Close();
+ }
+ }
+ }
+
+ protected void SetStoreFolderAce(int folderId, GenericAce<StoreFolderPrivilege> ace, ISession session) {
+ // Ensure session has started a transaction
+ if (!session.Transaction.IsActive) {
+ throw new InvalidOperationException("A transaction is required for this operation.");
+ }
+
+ IList<StoreFolderAcl> current = session.CreateCriteria(typeof(StoreFolderAcl))
+ .CreateCriteria("Folder")
+ .Add(Expression.Eq("FolderId", folderId))
+ .List<StoreFolderAcl>();
+
+ if (current.Count == 0) {
+ // Create a new ACL
+ StoreFolder folder = session.Load<StoreFolder>(folderId);
+ StoreFolderAcl acl = new StoreFolderAcl(folder);
+ acl[ace.Identifier] = ace;
+
+ session.Save(acl);
+
+ } else {
+ // Update the current ACL
+ StoreFolderAcl acl = current[0];
+ acl[ace.Identifier] = ace;
+
+ session.Update(acl);
+ }
+ }
+ #endregion
+
#region Remove StoreFolder ACE
/// <summary>
/// Removes any ACE associated with the given user.
Added: NMail/trunk/NMail.UnitTests/LocalStoreData/SetStoreFolderAceTest1.cs
===================================================================
--- NMail/trunk/NMail.UnitTests/LocalStoreData/SetStoreFolderAceTest1.cs (rev 0)
+++ NMail/trunk/NMail.UnitTests/LocalStoreData/SetStoreFolderAceTest1.cs 2007-04-19 12:09:29 UTC (rev 184)
@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+using NUnit.Framework;
+
+using NMail.Configuration;
+using NMail.DataTypes.ACLs;
+using NMail.DataTypes.LocalStore;
+
+namespace NMail.UnitTests.LocalStoreData {
+ /// <summary>
+ /// Tests for setting a store folder ACE
+ /// </summary>
+ [TestFixture]
+ public class SetStoreFolderAceTest1 : BaseLocalStoreDataTest {
+
+ [Test]
+ public void TestSetAce() {
+ // Create a user
+ LocalStoreUser u1 = new LocalStoreUser();
+ u1.Username = "Test1";
+ this.localStoreData.CreateUser(u1);
+ Assert.IsTrue(u1.UserFolderId > 0, "Valid user folder Id.");
+ Assert.IsTrue(u1.UserId > 0, "Valid user Id.");
+
+ // Create a user
+ LocalStoreUser u2 = new LocalStoreUser();
+ u2.Username = "Test2";
+ this.localStoreData.CreateUser(u2);
+ Assert.IsTrue(u2.UserId > 0, "Valid user Id.");
+
+ // Check ACL
+ StoreFolderAcl acl1 = this.localStoreData.GetStoreFolderAcl(u1.UserFolderId);
+ Assert.IsNull(acl1[u2.Username], "No ACE for user 2.");
+
+ // Add ACE
+ GenericAce<StoreFolderPrivilege> ace1 = new GenericAce<StoreFolderPrivilege>(u2.Username, StoreFolderPrivilege.Admin, AcePrivilegeType.Allow);
+ this.localStoreData.SetStoreFolderAce(u1.UserFolderId, ace1);
+
+ // Check ACL
+ StoreFolderAcl acl2 = this.localStoreData.GetStoreFolderAcl(u1.UserFolderId);
+ Assert.IsNotNull(acl2[u2.Username], "Valid ACE for user 2.");
+ Assert.AreEqual(StoreFolderPrivilege.Admin, acl2[u2.Username].Privilege, "Correct ACE privilege.");
+ Assert.AreEqual(AcePrivilegeType.Allow, acl2[u2.Username].AceType, "Correct ACE type.");
+ }
+ }
+}
Modified: NMail/trunk/NMail.UnitTests/NMail.UnitTests.csproj
===================================================================
--- NMail/trunk/NMail.UnitTests/NMail.UnitTests.csproj 2007-04-18 13:02:58 UTC (rev 183)
+++ NMail/trunk/NMail.UnitTests/NMail.UnitTests.csproj 2007-04-19 12:09:29 UTC (rev 184)
@@ -52,6 +52,7 @@
<Compile Include="DataTypes\Helper\MimeHelperTests.cs" />
<Compile Include="DataTypes\Helper\StringTokenizerTests.cs" />
<Compile Include="LocalStoreData\BaseLocalStoreDataTest.cs" />
+ <Compile Include="LocalStoreData\SetStoreFolderAceTest1.cs" />
<Compile Include="LocalStoreData\DeleteMailDomainTest1.cs" />
<Compile Include="LocalStoreData\CreateMailDomainTest2.cs" />
<Compile Include="LocalStoreData\CreateMailDomainTest1.cs" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|