Revision: 205
http://svn.sourceforge.net/nmailserver/?rev=205&view=rev
Author: tmyroadctfig
Date: 2007-05-27 06:41:01 -0700 (Sun, 27 May 2007)
Log Message:
-----------
Fixed defect involved updating items with the same name. Added the ability to validate group/mail domain user/group Ids.
Modified Paths:
--------------
NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.LocalStore.LocalStoreGroup.hbm.xml
NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.LocalStore.MailDomain.hbm.xml
NMail/trunk/NMail.LocalStoreData.NHibernate/NHibernateLocalStoreData.cs
Modified: NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.LocalStore.LocalStoreGroup.hbm.xml
===================================================================
--- NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.LocalStore.LocalStoreGroup.hbm.xml 2007-05-27 13:38:47 UTC (rev 204)
+++ NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.LocalStore.LocalStoreGroup.hbm.xml 2007-05-27 13:41:01 UTC (rev 205)
@@ -12,13 +12,13 @@
<list name="SubGroupIds" table="GroupSubGroups">
<key column="ParentGroupId" />
<index column="ChildIndex" type="Int32" />
- <many-to-many column="GroupId" class="NMail.DataTypes.LocalStore.LocalStoreGroup" />
+ <element column="GroupId" type="Int32" />
</list>
<list name="UserIds" table="GroupUsers">
<key column="GroupId" />
<index column="ChildIndex" type="Int32" />
- <many-to-many column="UserId" class="NMail.DataTypes.LocalStore.LocalStoreUser" />
+ <element column="UserId" type="Int32" />
</list>
</class>
Modified: NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.LocalStore.MailDomain.hbm.xml
===================================================================
--- NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.LocalStore.MailDomain.hbm.xml 2007-05-27 13:38:47 UTC (rev 204)
+++ NMail/trunk/NMail.LocalStoreData.NHibernate/Mappings/NMail.DataTypes.LocalStore.MailDomain.hbm.xml 2007-05-27 13:41:01 UTC (rev 205)
@@ -12,13 +12,13 @@
<list name="GroupIds" table="MailDomainGroups">
<key column="MailDomainId" />
<index column="ChildIndex" type="Int32" />
- <many-to-many column="GroupId" class="NMail.DataTypes.LocalStore.LocalStoreGroup" />
+ <element column="GroupId" type="Int32" />
</list>
<list name="UserIds" table="MailDomainUsers">
<key column="MailDomainId" />
<index column="ChildIndex" type="Int32" />
- <many-to-many column="UserId" class="NMail.DataTypes.LocalStore.LocalStoreUser" />
+ <element column="UserId" type="Int32" />
</list>
<list name="AdditionalHosts" table="MailDomainHosts">
Modified: NMail/trunk/NMail.LocalStoreData.NHibernate/NHibernateLocalStoreData.cs
===================================================================
--- NMail/trunk/NMail.LocalStoreData.NHibernate/NHibernateLocalStoreData.cs 2007-05-27 13:38:47 UTC (rev 204)
+++ NMail/trunk/NMail.LocalStoreData.NHibernate/NHibernateLocalStoreData.cs 2007-05-27 13:41:01 UTC (rev 205)
@@ -16,6 +16,7 @@
*/
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Security.Cryptography;
@@ -1083,15 +1084,27 @@
public void UpdateUser(LocalStoreUser user) {
using (ISession session = NHibernateHelper.Factory.OpenSession()) {
using (ITransaction tx = session.BeginTransaction()) {
- // Check if any other user with the same name exists in the system
- IList<LocalStoreUser> existing = session.CreateCriteria(typeof(LocalStoreUser))
- .Add(Expression.Like("Username", user.Username))
- .List<LocalStoreUser>();
- if (existing.Count > 0) {
- throw new DuplicateNameException("User with the same name already exists in the system.");
+ LocalStoreUser current = session.Get<LocalStoreUser>(user.UserId);
+
+ if (current == null) {
+ throw new ArgumentException("Invalid user Id.");
}
+ // Check if the name has changed
+ if (CaseInsensitiveComparer.Default.Compare(user.Username, current.Username) != 0) {
+
+ // Check if any other user with the same name exists in the system
+ IList<LocalStoreUser> existing = session.CreateCriteria(typeof(LocalStoreUser))
+ .Add(Expression.Like("Username", user.Username))
+ .List<LocalStoreUser>();
+
+ if (existing.Count > 0) {
+ throw new DuplicateNameException("User with the same name already exists in the system.");
+ }
+ }
+ session.Evict(current);
+
session.Update(user);
tx.Commit();
@@ -1192,6 +1205,10 @@
throw new DuplicateNameException("Group with the same name already exists in the system.");
}
+ // Validate user and group Ids
+ ValidateGroupIds(group.SubGroupIds, session);
+ ValidateUserIds(group.UserIds, session);
+
session.Save(group);
tx.Commit();
@@ -1230,15 +1247,31 @@
public void UpdateGroup(LocalStoreGroup group) {
using (ISession session = NHibernateHelper.Factory.OpenSession()) {
using (ITransaction tx = session.BeginTransaction()) {
- // Check if any other group with the same name exists in the system
- IList<LocalStoreUser> existing = session.CreateCriteria(typeof(LocalStoreGroup))
- .Add(Expression.Like("Name", group.Name))
- .List<LocalStoreUser>();
+
+ LocalStoreGroup current = session.Get<LocalStoreGroup>(group.GroupId);
- if (existing.Count > 0) {
- throw new DuplicateNameException("Group with the same name already exists in the system.");
+ if (current == null) {
+ throw new ArgumentException("Invalid group Id.");
}
+ // Check if the name has changed
+ if (CaseInsensitiveComparer.Default.Compare(group.Name, current.Name) != 0) {
+
+ // Check if any other group with the same name exists in the system
+ IList<LocalStoreGroup> existing = session.CreateCriteria(typeof(LocalStoreGroup))
+ .Add(Expression.Like("Name", group.Name))
+ .List<LocalStoreGroup>();
+
+ if (existing.Count > 0) {
+ throw new DuplicateNameException("Group with the same name already exists in the system.");
+ }
+ }
+ session.Evict(current);
+
+ // Validate user and group Ids
+ ValidateGroupIds(group.SubGroupIds, session);
+ ValidateUserIds(group.UserIds, session);
+
session.Update(group);
tx.Commit();
@@ -1449,6 +1482,10 @@
throw new DuplicateNameException("Mail domain with the same primary host already exists in the system.");
}
+ // Validate user and group Ids
+ ValidateGroupIds(mailDomain.GroupIds, session);
+ ValidateUserIds(mailDomain.UserIds, session);
+
session.Save(mailDomain);
tx.Commit();
@@ -1486,15 +1523,31 @@
public void UpdateMailDomain(MailDomain updatedMailDomain) {
using (ISession session = NHibernateHelper.Factory.OpenSession()) {
using (ITransaction tx = session.BeginTransaction()) {
- // Check if any other group with the same name exists in the system
- IList<MailDomain> existing = session.CreateCriteria(typeof(MailDomain))
- .Add(Expression.Like("PrimaryHost", updatedMailDomain.PrimaryHost))
- .List<MailDomain>();
- if (existing.Count > 0) {
- throw new DuplicateNameException("Mail domain with the same primary host already exists in the system.");
+ MailDomain current = session.Get<MailDomain>(updatedMailDomain.MailDomainId);
+
+ if (current == null) {
+ throw new ArgumentException("Invalid mail domain Id.");
}
+ // Check if the primary host has changed
+ if (current.PrimaryHost != updatedMailDomain.PrimaryHost) {
+
+ // Check if any other group with the same name exists in the system
+ IList<MailDomain> existing = session.CreateCriteria(typeof(MailDomain))
+ .Add(Expression.Like("PrimaryHost", updatedMailDomain.PrimaryHost))
+ .List<MailDomain>();
+
+ if (existing.Count > 0) {
+ throw new DuplicateNameException("Mail domain with the same primary host already exists in the system.");
+ }
+ }
+ session.Evict(current);
+
+ // Validate user and group Ids
+ ValidateGroupIds(updatedMailDomain.GroupIds, session);
+ ValidateUserIds(updatedMailDomain.UserIds, session);
+
session.Update(updatedMailDomain);
tx.Commit();
@@ -1832,8 +1885,50 @@
LocalStoreUser administrator = new LocalStoreUser();
administrator.Username = "Administrator"; // TODO: get from a global admin name or something
CreateUser(administrator);
+
+ LocalStoreGroup adminGroup = new LocalStoreGroup();
+ adminGroup.Name = "Administrators"; // TODO: get from a global admin name or something
+ CreateGroup(adminGroup);
+
+ MailDomain localMailDomain = new MailDomain();
+ localMailDomain.PrimaryHost = new Host(Domain.LocalHost);
+ localMailDomain.UserIds.Add(administrator.UserId);
+ localMailDomain.GroupIds.Add(adminGroup.GroupId);
+ CreateMailDomain(localMailDomain);
}
#endregion
+
+ protected void ValidateUserIds(IList<int> userIds, ISession session) {
+ // Ensure session has started a transaction
+ if (!session.Transaction.IsActive) {
+ throw new InvalidOperationException("A transaction is required for this operation.");
+ }
+
+ // Validate each user Id
+ foreach (int userId in userIds) {
+ LocalStoreUser user = session.Get<LocalStoreUser>(userId);
+
+ if (user == null) {
+ throw new ArgumentException("Invalid user Id.");
+ }
+ }
+ }
+
+ protected void ValidateGroupIds(IList<int> groupIds, ISession session) {
+ // Ensure session has started a transaction
+ if (!session.Transaction.IsActive) {
+ throw new InvalidOperationException("A transaction is required for this operation.");
+ }
+
+ // Validate each group Id
+ foreach (int groupId in groupIds) {
+ LocalStoreGroup group = session.Get<LocalStoreGroup>(groupId);
+
+ if (group == null) {
+ throw new ArgumentException("Invalid group Id.");
+ }
+ }
+ }
#endregion
/// <summary>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|