public static void VersionPost(Post old_Post) { VersionStore vs = new VersionStore(); vs.Type = "post/xml"; vs.ItemId = old_Post.Id; vs.Name = "Post:" + old_Post.Id; vs.UniqueId = Guid.NewGuid(); vs.Data = old_Post.ToXML(); vs.Version = old_Post.Version; vs.Save(GraffitiUsers.Current.Name); }
/// <summary> /// Renames a user account /// </summary> public static void RenameUser(string oldUserName, string newUserName) { if (!controller.CanDeleteUsers) { throw new Exception("The membership system in use does not support deleting users"); } IGraffitiUser user = GetUser(oldUserName); if (user == null) { throw new Exception("The supplied username does not exist!"); } oldUserName = oldUserName.ToLower(); newUserName = newUserName.ToLower(); controller.RenameUser(oldUserName, newUserName); // Check if the user has created/modified any content PostCollection pc = new PostCollection(); Query q = Post.CreateQuery(); q.OrWhere(Post.Columns.UserName, oldUserName); q.OrWhere(Post.Columns.CreatedBy, oldUserName); q.OrWhere(Post.Columns.ModifiedBy, oldUserName); pc.LoadAndCloseReader(q.ExecuteReader()); if (pc != null && pc.Count > 0) { foreach (Post p in pc) { if (p.UserName == oldUserName) { p.UserName = newUserName; } if (p.ModifiedBy == oldUserName) { p.ModifiedBy = newUserName; } if (p.CreatedBy == oldUserName) { p.CreatedBy = newUserName; } p.Save(); } } // Check if user has created any comments CommentCollection cc = new CommentCollection(); q = Comment.CreateQuery(); q.OrWhere(Comment.Columns.UserName, oldUserName); q.OrWhere(Comment.Columns.CreatedBy, oldUserName); q.OrWhere(Comment.Columns.ModifiedBy, oldUserName); cc.LoadAndCloseReader(q.ExecuteReader()); if (cc != null && cc.Count > 0) { foreach (Comment c in cc) { if (c.UserName == oldUserName) { c.UserName = newUserName; } if (c.ModifiedBy == oldUserName) { c.ModifiedBy = newUserName; } if (c.CreatedBy == oldUserName) { c.CreatedBy = newUserName; } c.Save(); } } //Check if the user has created any post versions VersionStoreCollection vsc = new VersionStoreCollection(); vsc = VersionStoreCollection.FetchAll(); if (vsc != null && vsc.Count > 0) { foreach (VersionStore v in vsc) { Post vp = ObjectManager.ConvertToObject <Graffiti.Core.Post>(v.Data); if (v.CreatedBy == oldUserName) { v.CreatedBy = newUserName; } if (v.Type == "post/xml") { if (vp.UserName == oldUserName) { vp.UserName = newUserName; } if (vp.ModifiedBy == oldUserName) { vp.ModifiedBy = newUserName; } if (vp.CreatedBy == oldUserName) { vp.CreatedBy = newUserName; } v.Data = vp.ToXML(); } v.Save(); } } ZCache.RemoveCache("user-" + oldUserName); // Clear roles cache if (user.Roles != null && user.Roles.Length > 0) { ZCache.RemoveByPattern("usersByRole-"); } }