diff --git a/umbraco/businesslogic/User.cs b/umbraco/businesslogic/User.cs index ffbd844db9..b43e95283e 100644 --- a/umbraco/businesslogic/User.cs +++ b/umbraco/businesslogic/User.cs @@ -6,12 +6,13 @@ using umbraco.DataLayer; using System.Collections.Generic; using System.Linq; -namespace umbraco.BusinessLogic { +namespace umbraco.BusinessLogic +{ /// /// represents a Umbraco back end user /// - public class User { - + public class User + { private int _id; private bool _isInitialized; private string _name; @@ -31,7 +32,8 @@ namespace umbraco.BusinessLogic { private Hashtable _notifications = new Hashtable(); private bool _notificationsInitialized = false; - private static ISqlHelper SqlHelper { + private static ISqlHelper SqlHelper + { get { return Application.SqlHelper; } } @@ -39,7 +41,8 @@ namespace umbraco.BusinessLogic { /// Initializes a new instance of the class. /// /// The ID. - public User(int ID) { + public User(int ID) + { setupUser(ID); } @@ -48,7 +51,8 @@ namespace umbraco.BusinessLogic { /// /// The ID. /// if set to true [no setup]. - public User(int ID, bool noSetup) { + public User(int ID, bool noSetup) + { _id = ID; } @@ -57,7 +61,8 @@ namespace umbraco.BusinessLogic { /// /// The login. /// The password. - public User(string Login, string Password) { + public User(string Login, string Password) + { setupUser(getUserId(Login, Password)); } @@ -65,34 +70,33 @@ namespace umbraco.BusinessLogic { /// Initializes a new instance of the class. /// /// The login. - public User(string Login) { + public User(string Login) + { setupUser(getUserId(Login)); } - private void setupUser(int ID) { + private void setupUser(int ID) + { _id = ID; using (IRecordsReader dr = SqlHelper.ExecuteReader( "Select userNoConsole, userDisabled, userType,startStructureID, startMediaId, userName,userLogin,userEmail,userDefaultPermissions, userLanguage, defaultToLiveEditing from umbracoUser where id = @id", - SqlHelper.CreateParameter("@id", ID))) { - if (dr.Read()) - { - _userNoConsole = dr.GetBoolean("usernoconsole"); - _userDisabled = dr.GetBoolean("userDisabled"); - _name = dr.GetString("userName"); - _loginname = dr.GetString("userLogin"); - _email = dr.GetString("userEmail"); - _language = dr.GetString("userLanguage"); - _startnodeid = dr.GetInt("startStructureID"); - if (!dr.IsNull("startMediaId")) - _startmediaid = dr.GetInt("startMediaID"); - _usertype = UserType.GetUserType(dr.GetShort("UserType")); - _defaultToLiveEditing = dr.GetBoolean("defaultToLiveEditing"); - } - else - { - throw new ArgumentException("No User exists with ID " + ID.ToString()); - } + SqlHelper.CreateParameter("@id", ID))) + { + if (dr.Read()) + { + _userNoConsole = dr.GetBoolean("usernoconsole"); + _userDisabled = dr.GetBoolean("userDisabled"); + _name = dr.GetString("userName"); + _loginname = dr.GetString("userLogin"); + _email = dr.GetString("userEmail"); + _language = dr.GetString("userLanguage"); + _startnodeid = dr.GetInt("startStructureID"); + if (!dr.IsNull("startMediaId")) + _startmediaid = dr.GetInt("startMediaID"); + _usertype = UserType.GetUserType(dr.GetShort("UserType")); + _defaultToLiveEditing = dr.GetBoolean("defaultToLiveEditing"); + } } _isInitialized = true; } @@ -100,7 +104,8 @@ namespace umbraco.BusinessLogic { /// /// Used to persist object changes to the database. In Version3.0 it's just a stub for future compatibility /// - public void Save() { + public void Save() + { OnSaving(EventArgs.Empty); } @@ -108,13 +113,16 @@ namespace umbraco.BusinessLogic { /// Gets or sets the users name. /// /// The name. - public string Name { - get { + public string Name + { + get + { if (!_isInitialized) setupUser(_id); return _name; } - set { + set + { _name = value; SqlHelper.ExecuteNonQuery("Update umbracoUser set UserName = @userName where id = @id", SqlHelper.CreateParameter("@userName", value), SqlHelper.CreateParameter("@id", Id)); FlushFromCache(); @@ -125,13 +133,16 @@ namespace umbraco.BusinessLogic { /// Gets or sets the users email. /// /// The email. - public string Email { - get { + public string Email + { + get + { if (!_isInitialized) setupUser(_id); return _email; } - set { + set + { _email = value; SqlHelper.ExecuteNonQuery("Update umbracoUser set UserEmail = @email where id = @id", SqlHelper.CreateParameter("@id", this.Id), SqlHelper.CreateParameter("@email", value)); FlushFromCache(); @@ -142,13 +153,16 @@ namespace umbraco.BusinessLogic { /// Gets or sets the users language. /// /// The language. - public string Language { - get { + public string Language + { + get + { if (!_isInitialized) setupUser(_id); return _language; } - set { + set + { _language = value; SqlHelper.ExecuteNonQuery("Update umbracoUser set userLanguage = @language where id = @id", SqlHelper.CreateParameter("@language", value), SqlHelper.CreateParameter("@id", Id)); FlushFromCache(); @@ -159,11 +173,14 @@ namespace umbraco.BusinessLogic { /// Gets or sets the users password. /// /// The password. - public string Password { - get { + public string Password + { + get + { return GetPassword(); } - set { + set + { SqlHelper.ExecuteNonQuery("Update umbracoUser set UserPassword = @pw where id = @id", SqlHelper.CreateParameter("@pw", value), SqlHelper.CreateParameter("@id", Id)); FlushFromCache(); } @@ -173,8 +190,9 @@ namespace umbraco.BusinessLogic { /// Gets the password. /// /// - public string GetPassword() { - return + public string GetPassword() + { + return SqlHelper.ExecuteScalar("select UserPassword from umbracoUser where id = @id", SqlHelper.CreateParameter("@id", this.Id)); } @@ -186,13 +204,15 @@ namespace umbraco.BusinessLogic { /// /// true if this user is admin; otherwise, false. /// - public bool IsAdmin() { + public bool IsAdmin() + { return UserType.Alias == "admin"; } - public bool ValidatePassword(string password) { + public bool ValidatePassword(string password) + { string userLogin = - SqlHelper.ExecuteScalar("select userLogin from umbracoUser where userLogin = @login and UserPassword = @pw", + SqlHelper.ExecuteScalar("select userLogin from umbracoUser where userLogin = @login and UserPassword = @pw", SqlHelper.CreateParameter("@pw", password), SqlHelper.CreateParameter("@login", LoginName) ); @@ -205,7 +225,8 @@ namespace umbraco.BusinessLogic { /// /// true if this user is root; otherwise, false. /// - public bool IsRoot() { + public bool IsRoot() + { return Id == 0; } @@ -213,14 +234,18 @@ namespace umbraco.BusinessLogic { /// Gets the applications which the user has access to. /// /// The users applications. - public Application[] Applications { - get { + public Application[] Applications + { + get + { if (!_isInitialized) setupUser(_id); ArrayList al = new ArrayList(); - using (IRecordsReader appIcons = SqlHelper.ExecuteReader("select appAlias, appIcon, appname from umbracoApp app join umbracoUser2app u2a on u2a.app = app.appAlias and u2a.[user] = @userID order by app.sortOrder", SqlHelper.CreateParameter("@userID", this.Id))) { - while (appIcons.Read()) { + using (IRecordsReader appIcons = SqlHelper.ExecuteReader("select appAlias, appIcon, appname from umbracoApp app join umbracoUser2app u2a on u2a.app = app.appAlias and u2a.[user] = @userID order by app.sortOrder", SqlHelper.CreateParameter("@userID", this.Id))) + { + while (appIcons.Read()) + { Application tmp = new Application(); tmp.name = appIcons.GetString("appName"); tmp.icon = appIcons.GetString("appIcon"); @@ -231,7 +256,8 @@ namespace umbraco.BusinessLogic { Application[] retVal = new Application[al.Count]; - for (int i = 0; i < al.Count; i++) { + for (int i = 0; i < al.Count; i++) + { retVal[i] = (Application)al[i]; } return retVal; @@ -242,13 +268,16 @@ namespace umbraco.BusinessLogic { /// Gets or sets the users login name /// /// The loginname. - public string LoginName { - get { + public string LoginName + { + get + { if (!_isInitialized) setupUser(_id); return _loginname; } - set { + set + { if (!ensureUniqueLoginName(value, this)) throw new Exception(String.Format("A user with the login '{0}' already exists", value)); _loginname = value; @@ -257,7 +286,8 @@ namespace umbraco.BusinessLogic { } } - private static bool ensureUniqueLoginName(string loginName, User currentUser) { + private static bool ensureUniqueLoginName(string loginName, User currentUser) + { User[] u = User.getAllByLoginName(loginName); if (u.Length != 0) { @@ -274,7 +304,8 @@ namespace umbraco.BusinessLogic { /// The login name. /// The password. /// - public static bool validateCredentials(string lname, string passw) { + public static bool validateCredentials(string lname, string passw) + { return validateCredentials(lname, passw, true); } @@ -285,7 +316,8 @@ namespace umbraco.BusinessLogic { /// The password. /// if set to true [check for umbraco console access]. /// - public static bool validateCredentials(string lname, string passw, bool checkForUmbracoConsoleAccess) { + public static bool validateCredentials(string lname, string passw, bool checkForUmbracoConsoleAccess) + { string consoleCheckSql = ""; if (checkForUmbracoConsoleAccess) consoleCheckSql = "and userNoConsole = 0 "; @@ -304,13 +336,16 @@ namespace umbraco.BusinessLogic { /// Gets or sets the type of the user. /// /// The type of the user. - public UserType UserType { - get { + public UserType UserType + { + get + { if (!_isInitialized) setupUser(_id); return _usertype; } - set { + set + { _usertype = value; SqlHelper.ExecuteNonQuery( @"Update umbracoUser set userType = @type where id = @id", @@ -325,14 +360,15 @@ namespace umbraco.BusinessLogic { /// Gets all users /// /// - public static User[] getAll() { + public static User[] getAll() + { IRecordsReader dr; dr = SqlHelper.ExecuteReader("Select id from umbracoUser"); List users = new List(); - while (dr.Read()) + while (dr.Read()) { users.Add(User.GetUser(dr.GetInt("id"))); } @@ -360,21 +396,24 @@ namespace umbraco.BusinessLogic { /// /// The email. /// - public static User[] getAllByEmail(string email) { + public static User[] getAllByEmail(string email) + { System.Collections.ArrayList tmpContainer = new System.Collections.ArrayList(); IRecordsReader dr; dr = SqlHelper.ExecuteReader( "Select id from umbracoUser where userEmail LIKE %@email%", SqlHelper.CreateParameter("@email", email)); - while (dr.Read()) { + while (dr.Read()) + { tmpContainer.Add(BusinessLogic.User.GetUser(dr.GetInt("id"))); } dr.Close(); User[] retVal = new User[tmpContainer.Count]; int c = 0; - foreach (User u in tmpContainer) { + foreach (User u in tmpContainer) + { retVal[c] = u; c++; } @@ -386,21 +425,24 @@ namespace umbraco.BusinessLogic { /// /// The login. /// - public static User[] getAllByLoginName(string login) { + public static User[] getAllByLoginName(string login) + { System.Collections.ArrayList tmpContainer = new System.Collections.ArrayList(); IRecordsReader dr; dr = SqlHelper.ExecuteReader( "Select id from umbracoUser where userLogin LIKE @login", SqlHelper.CreateParameter("@login", String.Format("%{0}%", login))); - while (dr.Read()) { + while (dr.Read()) + { tmpContainer.Add(BusinessLogic.User.GetUser(dr.GetInt("id"))); } dr.Close(); User[] retVal = new User[tmpContainer.Count]; int c = 0; - foreach (User u in tmpContainer) { + foreach (User u in tmpContainer) + { retVal[c] = u; c++; } @@ -414,7 +456,8 @@ namespace umbraco.BusinessLogic { /// The login name. /// The password. /// The user type. - public static void MakeNew(string name, string lname, string passw, UserType ut) { + public static void MakeNew(string name, string lname, string passw, UserType ut) + { SqlHelper.ExecuteNonQuery(@" insert into umbracoUser @@ -438,7 +481,8 @@ namespace umbraco.BusinessLogic { /// The passw. /// The email. /// The ut. - public static void MakeNew(string name, string lname, string passw, string email, UserType ut) { + public static void MakeNew(string name, string lname, string passw, string email, UserType ut) + { SqlHelper.ExecuteNonQuery(@" insert into umbracoUser (UserType,startStructureId,startMediaId, UserName, userLogin, userPassword, userEmail,userLanguage) @@ -462,11 +506,12 @@ namespace umbraco.BusinessLogic { /// The lname. /// The email. /// The ut. - public static void Update(int id, string name, string lname, string email, UserType ut) { + public static void Update(int id, string name, string lname, string email, UserType ut) + { if (!ensureUniqueLoginName(lname, User.GetUser(id))) throw new Exception(String.Format("A user with the login '{0}' already exists", lname)); - + SqlHelper.ExecuteNonQuery(@"Update umbracoUser set userName=@name, userLogin=@lname, userEmail=@email, UserType=@type where id = @id", SqlHelper.CreateParameter("@name", name), SqlHelper.CreateParameter("@lname", lname), @@ -481,7 +526,8 @@ namespace umbraco.BusinessLogic { /// The login name. /// The password. /// a user ID - public static int getUserId(string lname, string passw) { + public static int getUserId(string lname, string passw) + { return getUserId("select id from umbracoUser where userDisabled = 0 and userNoConsole = 0 and userLogin = @login and userPassword = @pw", SqlHelper.CreateParameter("@login", lname), SqlHelper.CreateParameter("@pw", passw)); @@ -492,21 +538,24 @@ namespace umbraco.BusinessLogic { /// /// The login name. /// a user ID - public static int getUserId(string lname) { + public static int getUserId(string lname) + { return getUserId("select id from umbracoUser where userLogin = @login", SqlHelper.CreateParameter("@login", lname)); } - private static int getUserId(string query, params IParameter[] parameterValues) { + private static int getUserId(string query, params IParameter[] parameterValues) + { object userId = SqlHelper.ExecuteScalar(query, parameterValues); - return (userId != null && userId!=DBNull.Value) ? int.Parse(userId.ToString()) : -1; + return (userId != null && userId != DBNull.Value) ? int.Parse(userId.ToString()) : -1; } /// /// Deletes this instance. /// [Obsolete("Deleting users are NOT supported as history needs to be kept. Please use the disable() method instead")] - public void delete() { + public void delete() + { OnDeleting(EventArgs.Empty); @@ -519,7 +568,8 @@ namespace umbraco.BusinessLogic { /// /// Disables this instance. /// - public void disable() { + public void disable() + { OnDisabling(EventArgs.Empty); this.Disabled = true; } @@ -529,7 +579,8 @@ namespace umbraco.BusinessLogic { /// /// The path. /// - public string GetPermissions(string Path) { + public string GetPermissions(string Path) + { if (!_isInitialized) setupUser(_id); string cruds = UserType.DefaultPermissions; @@ -537,7 +588,8 @@ namespace umbraco.BusinessLogic { if (!_crudsInitialized) initCruds(); - foreach (string nodeId in Path.Split(',')) { + foreach (string nodeId in Path.Split(',')) + { if (_cruds.ContainsKey(int.Parse(nodeId))) cruds = _cruds[int.Parse(nodeId)].ToString(); } @@ -548,7 +600,8 @@ namespace umbraco.BusinessLogic { /// /// Initializes the user node permissions /// - public void initCruds() { + public void initCruds() + { if (!_isInitialized) setupUser(_id); @@ -557,9 +610,11 @@ namespace umbraco.BusinessLogic { _cruds.Clear(); System.Web.HttpContext.Current.Application.UnLock(); - using (IRecordsReader dr = SqlHelper.ExecuteReader("select * from umbracoUser2NodePermission where userId = @userId order by nodeId", SqlHelper.CreateParameter("@userId", this.Id))) { + using (IRecordsReader dr = SqlHelper.ExecuteReader("select * from umbracoUser2NodePermission where userId = @userId order by nodeId", SqlHelper.CreateParameter("@userId", this.Id))) + { // int currentId = -1; - while (dr.Read()) { + while (dr.Read()) + { if (!_cruds.ContainsKey(dr.GetInt("nodeId"))) _cruds.Add(dr.GetInt("nodeId"), String.Empty); @@ -574,13 +629,15 @@ namespace umbraco.BusinessLogic { /// /// The node path. /// - public string GetNotifications(string Path) { + public string GetNotifications(string Path) + { string notifications = ""; if (!_notificationsInitialized) initNotifications(); - foreach (string nodeId in Path.Split(',')) { + foreach (string nodeId in Path.Split(',')) + { if (_notifications.ContainsKey(int.Parse(nodeId))) notifications = _notifications[int.Parse(nodeId)].ToString(); } @@ -591,7 +648,8 @@ namespace umbraco.BusinessLogic { /// /// Clears the internal hashtable containing cached information about notifications for the user /// - public void resetNotificationCache() { + public void resetNotificationCache() + { _notificationsInitialized = false; _notifications.Clear(); } @@ -599,12 +657,15 @@ namespace umbraco.BusinessLogic { /// /// Initializes the notifications and caches them. /// - public void initNotifications() { + public void initNotifications() + { if (!_isInitialized) setupUser(_id); - using (IRecordsReader dr = SqlHelper.ExecuteReader("select * from umbracoUser2NodeNotify where userId = @userId order by nodeId", SqlHelper.CreateParameter("@userId", this.Id))) { - while (dr.Read()) { + using (IRecordsReader dr = SqlHelper.ExecuteReader("select * from umbracoUser2NodeNotify where userId = @userId order by nodeId", SqlHelper.CreateParameter("@userId", this.Id))) + { + while (dr.Read()) + { int nodeId = dr.GetInt("nodeId"); if (!_notifications.ContainsKey(nodeId)) _notifications.Add(nodeId, String.Empty); @@ -619,14 +680,16 @@ namespace umbraco.BusinessLogic { /// Gets the user id. /// /// The id. - public int Id { + public int Id + { get { return _id; } } /// /// Clears the list of applications the user has access to. /// - public void clearApplications() { + public void clearApplications() + { SqlHelper.ExecuteNonQuery("delete from umbracoUser2app where [user] = @id", SqlHelper.CreateParameter("@id", this.Id)); } @@ -634,7 +697,8 @@ namespace umbraco.BusinessLogic { /// Adds a application to the list of allowed applications /// /// The app alias. - public void addApplication(string AppAlias) { + public void addApplication(string AppAlias) + { SqlHelper.ExecuteNonQuery("insert into umbracoUser2app ([user],app) values (@id, @app)", SqlHelper.CreateParameter("@id", this.Id), SqlHelper.CreateParameter("@app", AppAlias)); } @@ -642,13 +706,16 @@ namespace umbraco.BusinessLogic { /// Gets or sets a value indicating whether the user has access to the Umbraco back end. /// /// true if the user has access to the back end; otherwise, false. - public bool NoConsole { - get { + public bool NoConsole + { + get + { if (!_isInitialized) setupUser(_id); return _userNoConsole; } - set { + set + { _userNoConsole = value; SqlHelper.ExecuteNonQuery("update umbracoUser set userNoConsole = @userNoConsole where id = @id", SqlHelper.CreateParameter("@id", this.Id), SqlHelper.CreateParameter("@userNoConsole", _userNoConsole)); FlushFromCache(); @@ -701,13 +768,16 @@ namespace umbraco.BusinessLogic { /// Gets or sets the start content node id. /// /// The start node id. - public int StartNodeId { - get { + public int StartNodeId + { + get + { if (!_isInitialized) setupUser(_id); return _startnodeid; } - set { + set + { _startnodeid = value; SqlHelper.ExecuteNonQuery("update umbracoUser set startStructureId = @start where id = @id", SqlHelper.CreateParameter("@start", value), SqlHelper.CreateParameter("@id", this.Id)); @@ -719,13 +789,16 @@ namespace umbraco.BusinessLogic { /// Gets or sets the start media id. /// /// The start media id. - public int StartMediaId { - get { + public int StartMediaId + { + get + { if (!_isInitialized) setupUser(_id); return _startmediaid; } - set { + set + { _startmediaid = value; SqlHelper.ExecuteNonQuery("update umbracoUser set startMediaId = @start where id = @id", SqlHelper.CreateParameter("@start", value), SqlHelper.CreateParameter("@id", this.Id)); @@ -736,7 +809,8 @@ namespace umbraco.BusinessLogic { /// /// Flushes the user from cache. /// - protected void FlushFromCache() { + protected void FlushFromCache() + { OnFlushingFromCache(EventArgs.Empty); if (System.Web.HttpRuntime.Cache[string.Format("UmbracoUser{0}", Id.ToString())] != null) @@ -748,8 +822,10 @@ namespace umbraco.BusinessLogic { /// /// The id. /// - public static User GetUser(int id) { - if (System.Web.HttpRuntime.Cache[string.Format("UmbracoUser{0}", id.ToString())] == null) { + public static User GetUser(int id) + { + if (System.Web.HttpRuntime.Cache[string.Format("UmbracoUser{0}", id.ToString())] == null) + { try { @@ -760,8 +836,8 @@ namespace umbraco.BusinessLogic { { //no user was found return null; - } - + } + } return (User)System.Web.HttpRuntime.Cache[string.Format("UmbracoUser{0}", id.ToString())]; } @@ -797,7 +873,8 @@ namespace umbraco.BusinessLogic { /// Raises the event. /// /// The instance containing the event data. - protected virtual void OnSaving(EventArgs e) { + protected virtual void OnSaving(EventArgs e) + { if (Saving != null) Saving(this, e); } @@ -810,7 +887,8 @@ namespace umbraco.BusinessLogic { /// Raises the event. /// /// The instance containing the event data. - protected virtual void OnNew(EventArgs e) { + protected virtual void OnNew(EventArgs e) + { if (New != null) New(this, e); } @@ -823,7 +901,8 @@ namespace umbraco.BusinessLogic { /// Raises the event. /// /// The instance containing the event data. - protected virtual void OnDisabling(EventArgs e) { + protected virtual void OnDisabling(EventArgs e) + { if (Disabling != null) Disabling(this, e); } @@ -836,7 +915,8 @@ namespace umbraco.BusinessLogic { /// Raises the event. /// /// The instance containing the event data. - protected virtual void OnDeleting(EventArgs e) { + protected virtual void OnDeleting(EventArgs e) + { if (Deleting != null) Deleting(this, e); } @@ -849,7 +929,8 @@ namespace umbraco.BusinessLogic { /// Raises the event. /// /// The instance containing the event data. - protected virtual void OnFlushingFromCache(EventArgs e) { + protected virtual void OnFlushingFromCache(EventArgs e) + { if (FlushingFromCache != null) FlushingFromCache(this, e); }