Moving UserSection implementation to one-many relationship with User object.

This commit is contained in:
Shannon
2013-07-03 10:29:05 +10:00
parent ff410a9d7f
commit 3e31faee3b
12 changed files with 114 additions and 176 deletions

View File

@@ -19,5 +19,9 @@ namespace Umbraco.Core.Models.Membership
bool NoConsole { get; set; }
IUserType UserType { get; }
string Permissions { get; set; }
IEnumerable<UserSection> UserSections { get; }
void RemoveUserSection(string sectionAlias);
void AddUserSection(string sectionAlias);
}
}

View File

@@ -19,10 +19,12 @@ namespace Umbraco.Core.Models.Membership
internal class User : UserProfile, IUser
{
private bool _hasIdentity;
private readonly UserSectionCollection _sectionCollection;
public User(IUserType userType)
{
Groups = new List<object> {userType};
_sectionCollection = new UserSectionCollection();
}
#region Implementation of IEntity
@@ -92,6 +94,23 @@ namespace Umbraco.Core.Models.Membership
public string Language { get; set; }
[DataMember]
public string Permissions { get; set; }
public IEnumerable<UserSection> UserSections
{
get { return _sectionCollection; }
}
public void RemoveUserSection(string sectionAlias)
{
//NOTE: we're casting to int here but might have to change that in the future.
_sectionCollection.Remove(new Tuple<object, string>((int) Id, sectionAlias));
}
public void AddUserSection(string sectionAlias)
{
_sectionCollection.Add(new UserSection(this, sectionAlias));
}
[DataMember]
public bool DefaultToLiveEditing { get; set; }
[DataMember]

View File

@@ -1,43 +1,97 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Reflection;
using System.Runtime.Serialization;
using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Models.Membership;
namespace Umbraco.Core.Models
{
internal class UserSectionCollection : KeyedCollection<Tuple<object, string>, UserSection>, INotifyCollectionChanged
{
protected override Tuple<object, string> GetKeyForItem(UserSection item)
{
return new Tuple<object, string>(item.UserId, item.SectionAlias);
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
/// <summary>
/// Represents an allowed section for a user
/// </summary>
[Serializable]
[DataContract(IsReference = true)]
internal class UserSection : IAggregateRoot
internal class UserSection : TracksChangesEntityBase
{
private int? _userId;
private bool _hasIdentity;
[DataMember]
public string SectionAlias { get; set; }
//NOTE: WE cannot implement these as the columns don't exist in the db.
[IgnoreDataMember]
Guid IEntity.Key { get; set; }
[IgnoreDataMember]
DateTime IEntity.CreateDate { get; set; }
[IgnoreDataMember]
DateTime IEntity.UpdateDate { get; set; }
[IgnoreDataMember]
public bool HasIdentity { get { return _userId != null || _hasIdentity; } }
public int Id
public UserSection(IUser user, string sectionAlias)
{
get
{
return _userId.HasValue ? _userId.Value : -1;
}
Mandate.ParameterNotNull(user, "user");
Mandate.ParameterNotNullOrEmpty(sectionAlias, "sectionAlias");
_userId = user.Id;
_sectionAlias = sectionAlias;
}
private string _sectionAlias;
private object _userId;
private static readonly PropertyInfo SectionSelector = ExpressionHelper.GetPropertyInfo<UserSection, string>(x => x.SectionAlias);
private static readonly PropertyInfo UserIdSelector = ExpressionHelper.GetPropertyInfo<UserSection, object>(x => x.UserId);
/// <summary>
/// Gets or sets the section alias
/// </summary>
[DataMember]
public object UserId
{
get { return _userId; }
set
{
_userId = value;
_hasIdentity = true;
SetPropertyValueAndDetectChanges(o =>
{
_userId = value;
return _userId;
}, _userId, UserIdSelector);
}
}
/// <summary>
/// Gets or sets the section alias
/// </summary>
[DataMember]
public string SectionAlias
{
get { return _sectionAlias; }
set
{
SetPropertyValueAndDetectChanges(o =>
{
_sectionAlias = value;
return _sectionAlias;
}, _sectionAlias, SectionSelector);
}
}
protected bool Equals(UserSection other)
{
return string.Equals(_sectionAlias, other._sectionAlias) && _userId.Equals(other._userId);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((UserSection) obj);
}
public override int GetHashCode()
{
unchecked
{
return (_sectionAlias.GetHashCode()*397) ^ _userId.GetHashCode();
}
}
}