From 64d1a484bdaece5146642f565172794e5d04319a Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 23 Jan 2014 17:11:58 +1100 Subject: [PATCH] Fixes User Id issues with HasIdentity, wraps legacy UserType to the new service, fixes the uninstalling db schema to not throw exceptions and clog up the unit test log. --- src/Umbraco.Core/Models/Membership/User.cs | 47 +++--- .../Initial/DatabaseSchemaCreation.cs | 5 +- src/Umbraco.Core/Services/IUserService.cs | 56 +++++-- src/Umbraco.Core/Services/UserService.cs | 19 ++- .../Services/UserServiceTests.cs | 1 + .../config/umbracoSettings.config | 2 +- src/umbraco.businesslogic/User.cs | 4 +- src/umbraco.businesslogic/UserType.cs | 143 ++++++------------ 8 files changed, 143 insertions(+), 134 deletions(-) diff --git a/src/Umbraco.Core/Models/Membership/User.cs b/src/Umbraco.Core/Models/Membership/User.cs index 6d2f104013..d1ecd0971f 100644 --- a/src/Umbraco.Core/Models/Membership/User.cs +++ b/src/Umbraco.Core/Models/Membership/User.cs @@ -80,23 +80,42 @@ namespace Umbraco.Core.Models.Membership private static readonly PropertyInfo LanguageSelector = ExpressionHelper.GetPropertyInfo(x => x.Language); private static readonly PropertyInfo DefaultPermissionsSelector = ExpressionHelper.GetPropertyInfo>(x => x.DefaultPermissions); private static readonly PropertyInfo DefaultToLiveEditingSelector = ExpressionHelper.GetPropertyInfo(x => x.DefaultToLiveEditing); + private static readonly PropertyInfo HasIdentitySelector = ExpressionHelper.GetPropertyInfo(x => x.HasIdentity); #region Implementation of IEntity [IgnoreDataMember] - public bool HasIdentity { get { return Id != null || _hasIdentity; } } - - [IgnoreDataMember] - int IEntity.Id + public bool HasIdentity { get { - return int.Parse(Id.ToString()); + return _hasIdentity; + } + protected set + { + SetPropertyValueAndDetectChanges(o => + { + _hasIdentity = value; + return _hasIdentity; + }, _hasIdentity, HasIdentitySelector); + } + } + + [DataMember] + public int Id + { + get + { + return _id; } set { - Id = value; - _hasIdentity = true; + SetPropertyValueAndDetectChanges(o => + { + _id = value; + HasIdentity = true; //set the has Identity + return _id; + }, _id, IdSelector); } } @@ -354,20 +373,6 @@ namespace Umbraco.Core.Models.Membership } } - [DataMember] - public int Id - { - get { return _id; } - set - { - SetPropertyValueAndDetectChanges(o => - { - _id = value; - return _id; - }, _id, IdSelector); - } - } - [DataMember] public string Language { diff --git a/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaCreation.cs b/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaCreation.cs index df0cf13052..53f1938c00 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaCreation.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaCreation.cs @@ -80,7 +80,10 @@ namespace Umbraco.Core.Persistence.Migrations.Initial try { - _database.DropTable(tableName); + if (_database.TableExist(tableName)) + { + _database.DropTable(tableName); + } } catch (Exception ex) { diff --git a/src/Umbraco.Core/Services/IUserService.cs b/src/Umbraco.Core/Services/IUserService.cs index a55bd4b771..d6ca2514d5 100644 --- a/src/Umbraco.Core/Services/IUserService.cs +++ b/src/Umbraco.Core/Services/IUserService.cs @@ -15,10 +15,45 @@ namespace Umbraco.Core.Services /// Id of the User to retrieve /// IProfile GetProfileById(int id); + + /// + /// Get profile by username + /// + /// + /// IProfile GetProfileByUserName(string username); + /// + /// Get user by Id + /// + /// + /// IUser GetUserById(int id); + + /// + /// This is useful when an entire section is removed from config + /// + /// + void DeleteSectionFromAllUsers(string sectionAlias); + /// + /// Returns a list of the sections that the user is allowed access to + /// + /// + IEnumerable GetUserSections(IUser user); + + /// + /// Get permissions set for user and specified node ids + /// + /// + /// + /// + IEnumerable GetPermissions(IUser user, params int[] nodeIds); + + #region User types + + IEnumerable GetAllUserTypes(params int[] ids); + /// /// Gets an IUserType by its Alias /// @@ -26,6 +61,13 @@ namespace Umbraco.Core.Services /// IUserType GetUserTypeByAlias(string alias); + /// + /// Gets an IUserType by its Id + /// + /// + /// + IUserType GetUserTypeById(int id); + /// /// Gets an IUserType by its Name /// @@ -45,19 +87,7 @@ namespace Umbraco.Core.Services /// /// void DeleteUserType(IUserType userType); - - /// - /// This is useful when an entire section is removed from config - /// - /// - void DeleteSectionFromAllUsers(string sectionAlias); - /// - /// Returns a list of the sections that the user is allowed access to - /// - /// - IEnumerable GetUserSections(IUser user); - - IEnumerable GetPermissions(IUser user, params int[] nodeIds); + #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/UserService.cs b/src/Umbraco.Core/Services/UserService.cs index fd43b496b1..92ff2d5dc9 100644 --- a/src/Umbraco.Core/Services/UserService.cs +++ b/src/Umbraco.Core/Services/UserService.cs @@ -351,7 +351,7 @@ namespace Umbraco.Core.Services var user = GetByUsername(login); return user.ProfileData; } - + public IUser GetUserById(int id) { using (var repository = _repositoryFactory.CreateUserRepository(_uowProvider.GetUnitOfWork())) @@ -360,6 +360,15 @@ namespace Umbraco.Core.Services } } + public IEnumerable GetAllUserTypes(params int[] ids) + { + var uow = _uowProvider.GetUnitOfWork(); + using (var repository = _repositoryFactory.CreateUserTypeRepository(uow)) + { + return repository.GetAll(ids); + } + } + /// /// Gets an IUserType by its Alias /// @@ -375,6 +384,14 @@ namespace Umbraco.Core.Services } } + public IUserType GetUserTypeById(int id) + { + using (var repository = _repositoryFactory.CreateUserTypeRepository(_uowProvider.GetUnitOfWork())) + { + return repository.Get(id); + } + } + /// /// Gets an IUserType by its Name /// diff --git a/src/Umbraco.Tests/Services/UserServiceTests.cs b/src/Umbraco.Tests/Services/UserServiceTests.cs index e76b07e30c..8658957ab9 100644 --- a/src/Umbraco.Tests/Services/UserServiceTests.cs +++ b/src/Umbraco.Tests/Services/UserServiceTests.cs @@ -329,6 +329,7 @@ namespace Umbraco.Tests.Services // Assert Assert.That(membershipUser.HasIdentity, Is.True); + Assert.That(membershipUser.Id, Is.GreaterThan(0)); IUser user = membershipUser as User; Assert.That(user, Is.Not.Null); Assert.That(user.DefaultPermissions, Is.EqualTo(userType.Permissions)); diff --git a/src/Umbraco.Web.UI/config/umbracoSettings.config b/src/Umbraco.Web.UI/config/umbracoSettings.config index ff21ef8249..a467040eba 100644 --- a/src/Umbraco.Web.UI/config/umbracoSettings.config +++ b/src/Umbraco.Web.UI/config/umbracoSettings.config @@ -177,7 +177,7 @@ - + 0 diff --git a/src/umbraco.businesslogic/User.cs b/src/umbraco.businesslogic/User.cs index e2dd21d104..9a5bfc38e6 100644 --- a/src/umbraco.businesslogic/User.cs +++ b/src/umbraco.businesslogic/User.cs @@ -31,7 +31,7 @@ namespace umbraco.BusinessLogic private bool _userDisabled; private bool _defaultToLiveEditing; - private Hashtable _notifications = new Hashtable(); + private readonly Hashtable _notifications = new Hashtable(); private bool _notificationsInitialized = false; [Obsolete("Obsolete, For querying the database use the new UmbracoDatabase object ApplicationContext.Current.DatabaseContext.Database", false)] @@ -42,7 +42,7 @@ namespace umbraco.BusinessLogic internal User(IUser user) { - _id = (int)user.Id; + _id = user.Id; _userNoConsole = user.IsLockedOut; _userDisabled = user.IsApproved; _name = user.Name; diff --git a/src/umbraco.businesslogic/UserType.cs b/src/umbraco.businesslogic/UserType.cs index e049a4023a..57308423b0 100644 --- a/src/umbraco.businesslogic/UserType.cs +++ b/src/umbraco.businesslogic/UserType.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Data; +using System.Globalization; +using System.Linq; using System.Runtime.CompilerServices; using System.Web; using System.Web.Caching; @@ -17,10 +19,20 @@ namespace umbraco.BusinessLogic public class UserType { + private Umbraco.Core.Models.Membership.IUserType _userType; + /// /// Creates a new empty instance of a UserType /// - public UserType() { } + public UserType() + { + _userType = new Umbraco.Core.Models.Membership.UserType(); + } + + internal UserType(Umbraco.Core.Models.Membership.IUserType userType) + { + _userType = userType; + } /// /// Creates a new instance of a UserType and attempts to @@ -43,8 +55,8 @@ namespace umbraco.BusinessLogic /// The name. public UserType(int id, string name) { - _id = id; - _name = name; + _userType.Id = id; + _userType.Name = name; } /// @@ -56,18 +68,12 @@ namespace umbraco.BusinessLogic /// public UserType(int id, string name, string defaultPermissions, string alias) { - _name = name; - _id = id; - _defaultPermissions = defaultPermissions; - _alias = alias; + _userType.Id = id; + _userType.Name = name; + _userType.Alias = alias; + _userType.Permissions = defaultPermissions.ToCharArray().Select(x => x.ToString(CultureInfo.InvariantCulture)); } - - private int _id; - private string _name; - private string _defaultPermissions; - private string _alias; - /// /// The cache storage for all user types /// @@ -75,40 +81,20 @@ namespace umbraco.BusinessLogic { get { - return ApplicationContext.Current.ApplicationCache.GetCacheItem( - CacheKeys.UserTypeCacheKey, - () => - { - var tmp = new List(); - using (var dr = SqlHelper.ExecuteReader("select id, userTypeName, userTypeAlias, userTypeDefaultPermissions from umbracoUserType")) - { - while (dr.Read()) - { - tmp.Add(new UserType( - dr.GetShort("id"), - dr.GetString("userTypeName"), - dr.GetString("userTypeDefaultPermissions"), - dr.GetString("userTypeAlias"))); - } - } - return tmp; - }); + return ApplicationContext.Current.Services.UserService.GetAllUserTypes() + .Select(x => new UserType(x)) + .ToList(); } } - - private static ISqlHelper SqlHelper - { - get { return Application.SqlHelper; } - } - + #region Public Properties /// /// Gets or sets the user type alias. /// public string Alias { - get { return _alias; } - set { _alias = value; } + get { return _userType.Alias; } + set { _userType.Alias = value; } } /// @@ -116,8 +102,8 @@ namespace umbraco.BusinessLogic /// public string Name { - get { return _name; } - set { _name = value; } + get { return _userType.Name; } + set { _userType.Name = value; } } /// @@ -125,7 +111,7 @@ namespace umbraco.BusinessLogic /// public int Id { - get { return _id; } + get { return _userType.Id; } } /// @@ -133,8 +119,8 @@ namespace umbraco.BusinessLogic /// public string DefaultPermissions { - get { return _defaultPermissions; } - set { _defaultPermissions = value; } + get { return string.Join("", _userType.Permissions); } + set { _userType.Permissions = value.ToCharArray().Select(x => x.ToString(CultureInfo.InvariantCulture)); } } /// @@ -153,19 +139,11 @@ namespace umbraco.BusinessLogic public void Save() { //ensure that this object has an ID specified (it exists in the database) - if (_id <= 0) + if (_userType.HasIdentity == false) throw new Exception("The current UserType object does not exist in the database. New UserTypes should be created with the MakeNew method"); - SqlHelper.ExecuteNonQuery(@" - update umbracoUserType set - userTypeAlias=@alias,userTypeName=@name,userTypeDefaultPermissions=@permissions - where id=@id", - SqlHelper.CreateParameter("@alias", _alias), - SqlHelper.CreateParameter("@name", _name), - SqlHelper.CreateParameter("@permissions", _defaultPermissions), - SqlHelper.CreateParameter("@id", _id) - ); - + ApplicationContext.Current.Services.UserService.SaveUserType(_userType); + //raise event OnUpdated(this, new EventArgs()); } @@ -176,10 +154,10 @@ namespace umbraco.BusinessLogic public void Delete() { //ensure that this object has an ID specified (it exists in the database) - if (_id <= 0) + if (_userType.HasIdentity == false) throw new Exception("The current UserType object does not exist in the database. New UserTypes should be created with the MakeNew method"); - SqlHelper.ExecuteNonQuery(@"delete from umbracoUserType where id=@id", SqlHelper.CreateParameter("@id", _id)); + ApplicationContext.Current.Services.UserService.DeleteUserType(_userType); //raise event OnDeleted(this, new EventArgs()); @@ -193,16 +171,8 @@ namespace umbraco.BusinessLogic /// and the data was loaded, false if it wasn't public bool LoadByPrimaryKey(int id) { - var userType = GetUserType(id); - if (userType == null) - return false; - - _id = userType.Id; - _alias = userType.Alias; - _defaultPermissions = userType.DefaultPermissions; - _name = userType.Name; - - return true; + _userType = ApplicationContext.Current.Services.UserService.GetUserTypeById(id); + return _userType != null; } /// @@ -221,37 +191,20 @@ namespace umbraco.BusinessLogic if (existing != null) throw new Exception("The UserType alias specified already exists"); - SqlHelper.ExecuteNonQuery(@" - insert into umbracoUserType - (userTypeAlias,userTypeName,userTypeDefaultPermissions) - values (@alias,@name,@permissions)", - SqlHelper.CreateParameter("@alias", alias), - SqlHelper.CreateParameter("@name", name), - SqlHelper.CreateParameter("@permissions", defaultPermissions)); - - //get it's id - var newId = SqlHelper.ExecuteScalar("SELECT MAX(id) FROM umbracoUserType WHERE userTypeAlias=@alias", SqlHelper.CreateParameter("@alias", alias)); - - //load the instance and return it - using (var dr = SqlHelper.ExecuteReader( - "select id, userTypeName, userTypeAlias, userTypeDefaultPermissions from umbracoUserType where id=@id", - SqlHelper.CreateParameter("@id", newId))) + var userType = new Umbraco.Core.Models.Membership.UserType { - if (dr.Read()) - { - var ut = new UserType( - dr.GetShort("id"), - dr.GetString("userTypeName"), - dr.GetString("userTypeDefaultPermissions"), - dr.GetString("userTypeAlias")); + Alias = alias, + Name = name, + Permissions = defaultPermissions.ToCharArray().Select(x => x.ToString(CultureInfo.InvariantCulture)) + }; + ApplicationContext.Current.Services.UserService.SaveUserType(userType); - //raise event - OnNew(ut, new EventArgs()); + var legacy = new UserType(userType); + + //raise event + OnNew(legacy, new EventArgs()); - return ut; - } - throw new InvalidOperationException("Could not read the new User Type with id of " + newId); - } + return legacy; } ///