EPiServer pages as users
There are times it can be convenient to have users defined as pages in EPiServer. There are several ways of archiving this. This approaches uses a DDS table with user data and attach to the publish events on the pages.
The code bellow assumes you are using some sort of strong typed pages and those pages you want to be considered users have to implement IAmUser
- public interface IAmAUser
- {
- bool EnsureUserData(UserData user);
- bool UpdateUser(MembershipUser user);
- }
These 2 methods will be called when a page is published, and when a user is saved thru the membership api.
This code attach to the publish event and checks if the current pages that are being saved have IAmUser and if it is It calls the EnsureUserData method,
- public class attachEvents: PlugInAttribute
- {
- public static void Start()
- {
- EPiServer.DataFactory.Instance.PublishedPage += new EPiServer.PageEventHandler(Instance_PublishedPage);
- }
- static void Instance_PublishedPage(object sender, EPiServer.PageEventArgs e)
- {
- IAmAUser page=(e.Page as IAmAUser);
- if (page!=null)
- {
- if (!e.PageLink.IsRemote())
- {
- var user = UserData.GetUserByPageID(e.PageLink.ID);
- if (user == null)
- user = new UserData();
- if (page.EnsureUserData(user))
- {
- user.EPiPageID = e.PageLink.ID;
- UserData.TryToSave(user);
- }
- }
- }
- }
- }
Then its possible to implement the interface on a pagetype. like this. Here is also a great example of how easy it is to add icons with IHavePageIcon.
- public class UserPageType : TypedPageData, IHavePageIcon, IAmAUser
- {
- #region IAmAUser Members
- public bool EnsureUserData(UserData user)
- {
- user.UserName = UserName;
- user.Email = Email;
- return true;
- }
- public bool UpdateUser(MembershipUser user)
- {
- UserName = user.UserName;
- Email = user.Email;
- return true;
- }
- #endregion
- #region IHavePageIcon Members
- public System.Web.UI.WebControls.Image PageIcon()
- {
- var img = new Image();
- img.ImageUrl = "/images/user.png";
- var users = UserData.GetUserByUserName(UserName);
- if (users != null && users.Length == 1 && users[0].EPiPageID.Equals(this.PageLink.ID))
- {
- //ok
- img.ToolTip = "User has an Unique ID and its " + UserName;
- }
- else
- {
- img.BackColor = System.Drawing.Color.Red;
- img.ToolTip = "";
- foreach (var user in users)
- {
- img.ToolTip += "User " + user.UserName + " is also defined in epi page " + user.EPiPageID + "\n";
- }
- }
- return img;
- }
The actually UserData class looks like this. There is more methods here but you need to download the zip to see those.
- public class UserData : IDynamicData
- {
- public UserData()
- {
- CreationDate = DateTime.Now;
- }
- public EPiServer.Data.Identity Id { get; set; }
- //Dates
- public virtual DateTime LastActivityDate { get; set; }
- public virtual DateTime CreationDate { get; set; }
- public virtual DateTime LastLockoutDate { get; set; }
- public virtual DateTime LastLoginDate { get; set; }
- public virtual DateTime LastPasswordChangeDate { get; set; }
- // Properties
- public virtual string Comment { get; set; }
- public virtual string Email { get; set; }
- public virtual bool IsApproved { get; set; }
- public virtual bool IsLockedOut { get; set; }
- public bool IsOnline { get; set; }
- public virtual string UserName { get; set; }
- public string HashedPassword { get; set; }
- public string HashedPasswordSalt { get; set; }
- public int EPiPageID { get; set; }
I have experienced that if you change the signature of your class (added properties) I have problem with retrieving those new properties from the old data that are already stored. Therefore I usually makes myself a delete all function that removes the whole store for my class., This I do in development of course :)
Then I need to make myself a Membership provider. The important function is the validate user method
- public override bool ValidateUser(string username, string password)
- {
- var users = UserData.GetUserByUserName(username);
- if (users.Length == 1)
- {
- var user = users[0];
- if (!string.IsNullOrEmpty(user.HashedPassword))
- if (UserData.CreatePasswordHash(password, user.HashedPasswordSalt).Equals(user.HashedPassword))
- return true;
- }
- return false;
- }
On the page that implements IAmUser I’m using a custom property that is username. This property will display if the username is already taken, and will show the pages that are using the same username. This is information that is easy to show in the page tree also as shown in PageIcon method on the UserPageType class above
- [Serializable]
- [PageDefinitionTypePlugIn]
- public class PropertyUnikUserName:PropertyString
- {
- public override IPropertyControl CreatePropertyControl()
- {
- return new PropertyUnikUserNameControl();
- }
- }
- public class PropertyUnikUserNameControl : PropertyStringControl
- {
- public override void CreateEditControls()
- {
- base.CreateEditControls();
- var ok = UserData.IsUserOk(ToString());
- if (ok.Ok)
- {
- EditControl.BackColor = System.Drawing.Color.Green;
- }
- else
- {
- EditControl.BackColor = System.Drawing.Color.Red;
- Literal l = new Literal();
- l.ID = this.Name + "Status";
- l.Text = "<div>"+ok.OkMessage.Replace("\n", "<br />")+"</div>";
- this.Controls.Add(l);
- }
- }
- }
What we have now is a membership provider that are based on pages in EPiServer. We don’t have a role provider for these users, but as shown in my blog post about multiplexing providers that is not necessarily. Since we can use the normal one.
Comments