Showing posts with label Sitecore User. Show all posts
Showing posts with label Sitecore User. Show all posts

Enforce Password Expiration - Sitecore

To enforce the current user to change the password is straightforward and only needs a little coding. Following the following three step will take you through the implementation

  1. Defining when the user should change the password
  2. Create the functionality to handling the current user who tries to log into Sitecore
  3. At the loggingin processor add the processor setting to the functionality at step 2

Defining when the user should change the password

First, I created an item to handle the timespan from when users have to change their password. And also an checkbox to (de-)activate the “Force New Password” functionality.


Create the functionality

Sitecore security model uses the ASP.NET membership. The ASP.NET membership includes different information about authentications of the users – the creation date of the users, last login date, last logout date, but also last password changed date of each user. Knowing this, it is straightforward to implement the “Force New Password” functionality. It only requires a couple of lines of coding.

When authenticating the user logging into Sitecore, the trick is to check the “LastPasswordChangedDate”:

 currentUser.LastPasswordChangedDate

The Codings:


using System;
using System.Web;
using System.Web.Security;
using Sitecore.Data.Items;
using Sitecore.Pipelines.LoggingIn;

namespace MyProject.SitecoreExtensions.Pipelines.LoggingIn
{
    public class ForceNewPassword
    {
        private Item itmForceNewPasswordSetting =
            Sitecore.Configuration.Factory.GetDatabase("master").GetItem("/sitecore/system/Settings/Security/Security Settings/Force New Password");

        public void Process(LoggingInArgs args)
        {
            if (!IsActivated())
                return;

            int monthsToExpire = 0;
                int.TryParse(itmForceNewPasswordSetting["TimeToExpire"], out monthsToExpire);

            if(monthsToExpire.Equals(0))
                return;

            MembershipUser currentUser = Membership.GetUser(args.Username);

            if (currentUser != null)
            {
                if ((DateTime.Now.AddMonths(-monthsToExpire)) > currentUser.LastPasswordChangedDate)
                {
                    Sitecore.Diagnostics.Log.Audit(string.Format("Force New Password: User {0}, has been forced to change password", user.UserName), this);

                    //Redirect to Sitecore default Change Password Site
                    HttpContext.Current.Response.Redirect("/sitecore/login/changepassword.aspx");
                }
            }
        }

        private bool IsActivated()
        {
            if (itmForceNewPasswordSetting == null)
                return false;

            if (string.IsNullOrEmpty(itmForceNewPasswordSetting["ActivateForcePassword"]))
                return false;
            
            if (itmForceNewPasswordSetting["ActivateForcePassword"].Equals("0"))
                return false;

            return true;
        }
    }
}


Add a processor tag to the loggingin processor

The final step is to add the functionality checking whether the current user needs to change the password, into the loggingin processor.

You could do this by adding the processor tag manually directly into the web.config file, but best practice is to include the modifications into a separate include file (stored under “/App_Config/Include” folder):


Notice the changes should be included as the first processor in the loggingin processor (this is done by using the “patch:before” setting).


   
     
            
           
                     
     
   



Test the “Force New Password”

Implementing the above functionality should do the trick … Notice the “Last Password Changed” value.

Before changing the password:


After changing the password:





Duplicate User Module - Sitecore

Do you have a web organization based on users helping creating the content in a given period, and afterwards the needs of disabling the same users? Or do you need to duplicate a user who is on vacation, or a longer leave, letting a temporary user (placed at the same domain, containing the same domains to administrate and associated with the same roles) to do the work of the user, who is on leave? And are you tired of manually administrate who should be enabled and who should be disabled?


Would it not be nice to have a Sitecore Client Application allowing users (with access to the User Manager) to duplicate a given user in Sitecore into a new user, where the new user is placed in the same domain, containing the same domains to administrate and associated with the same roles as the original user? And finally letting the new user containing an expiration date, from when the new user will be disabled?


The objective of my Duplicate User module is to be able to do this in a quick way with functionality integrated directly into the User Manager in Sitecore.

Getting started with the Duplicate User Module

The Duplicate User application is into the User Manager. So getting started with the Duplicate User module, open the User Manager and you will find the "Duplicate Users" chunk and the "Duplicate User" command.


Select a user to duplicate and select the command, Sitecore will open the Duplicate User dialog containing the standard user data (Username, comment, fullname and Email). In addition, an expiration date, for the newly created user, is added. Furthermore, the Duplicate User dialog displays information about the user who is duplicated, who is duplicating the user (Created by) as well as the security settings of the duplicated user (The new user is created based on the custom user profile "Duplicated User". The Duplicate User diaolog:


Duplicate User Module Settings

The module settings are stored at “/sitecore/System/Modules/Duplicate User/”. The “Duplicated User Settings” item contain the ProfileItemID of the ProfileItem, which will be used as profileItem whenever a user is duplicated.

The folder “Domains” contains the domains, as sub items, from where the module will check for duplicated users to disable.

Sitecore Task

The Duplicate User module uses a single Sitecore task, to check duplicated users to disabled. By default, the Disable Duplicate Users task is scheduled to run one time pr. The schedule is placed at “/sitecore/system/tasks/Schedules/Duplicated User/Check Duplicated Users” and the command is placed at “/sitecore/system/tasks/Commands/Duplicated User/Disable duplicated users”.


You can find the Duplicate User Module at Sitecores Marketplace.