Login with email address in Mura

This seems to be a highly requested feature, to allow logins with Email instead of username. The solution, I found is quite simple. The login method is in /requirements/mura/user/userUtility.cfc. Need to override that to check the email column as well. Using Mura’s Method Injection, this is super simple. In your local event handler, add this code. This method allows login with either email or username. You can change the query to just Email too if thats what you want. Look at the highlighted portion, thats the change. Remember to reload the application from the admin for the change to take effect.

<cffunction name="LoginWithEmailToo" output="false">
  <cfargument name="username" type="string" required="true" default="">
  <cfargument name="password" type="string" required="true" default="">
  <cfargument name="siteid" type="string" required="false" default="">
  <cfset var rolelist = "" />
  <cfset var rsUser = "" />
  <cfset var user = "" />
  <cfset var group = "" />
  <cfset var lastLogin = now() />
  <cfset var pluginEvent = createObject("component","mura.event").init(arguments) />
  <cfset var strikes = createObject("component","mura.user.userstrikes").init(arguments.username,variables.configBean) />

  <cflogout>
  <cfparam name="session.blockLoginUntil" type="string" default="#strikes.blockedUntil()#" />

  <cfif len(arguments.siteID)>
    <cfset variables.pluginManager.announceEvent('onSiteLogin',pluginEvent)/>
  <cfelse>
    <cfset variables.pluginManager.announceEvent('onGlobalLogin',pluginEvent)/>
  </cfif>
  <cfquery datasource="#application.configBean.getDatasource()#" name="rsUser" username="#variables.configBean.getDBUsername()#" password="#variables.configBean.getDBPassword()#">
  SELECT * FROM tusers WHERE
  (username=<cfqueryparam cfsqltype="cf_sql_varchar" value="#trim(arguments.username)#">
  OR email=<cfqueryparam cfsqltype="cf_sql_varchar" value="#trim(arguments.username)#">)
  AND
  (
    password=<cfqueryparam cfsqltype="cf_sql_varchar" value="#hash(trim(arguments.password))#">
    <cfif not variables.configBean.getEncryptPasswords() and len(trim(arguments.password)) neq 32>
    OR
    password=<cfqueryparam cfsqltype="cf_sql_varchar" value="#trim(arguments.password)#">
    </cfif>
  )
  AND Type = 2
  and inactive=0
  </cfquery>

  <cfif rsUser.RecordCount GREATER THAN 0
    and not strikes.isBlocked()>

      <cfif rsUser.isPublic and (arguments.siteid eq '' or variables.settingsManager.getSite(arguments.siteid).getPublicUserPoolID() neq rsUser.siteid)>

        <cfset strikes.addStrike()>

        <cfreturn false  >
      </cfif>

      <cfset session.blockLoginUntil=""/>

      <cfset loginByQuery(rsUser)/>
      <cfset strikes.clear()>
      <cfif len(arguments.siteID)>
        <cfset variables.pluginManager.announceEvent('onSiteLoginSuccess',pluginEvent)/>
      <cfelse>
        <cfset variables.pluginManager.announceEvent('onGlobalLoginSuccess',pluginEvent)/>
      </cfif>

      <cfreturn true />

  <cfelse>
    <cfif not strikes.isBlocked()>
      <cfset strikes.addStrike()>
    <cfelse>

      <cfif len(arguments.siteID)>
        <cfset variables.pluginManager.announceEvent('onSiteLoginBlocked',pluginEvent)/>
      <cfelse>
        <cfset variables.pluginManager.announceEvent('onGlobalLoginBlocked',pluginEvent)/>
      </cfif>

      <cfset session.blockLoginUntil=strikes.blockedUntil()/>

    </cfif>

  </cfif>

  <cfreturn false />
</cffunction>

<cffunction name="onApplicationLoad" output="false">
    <cfargument name="$">

    <cfset $.getBean("userUtility").injectMethod("Login",LoginWithEmailToo)/>

</cffunction>

Once this is done, you will want to update your label and the required field message. Open up includes/resourceBundles folder in your site directory. Copy the language file you are currently using (Hint: en.properties is for English) into the resourceBundles folder in your theme directory. At the end of the file, add two new values, user.loginusername and user.loginusernamerequired. Copy the displayobjects/dsp_login.cfm file to your themes display_objects folder and change the label and the required message rbfactory values for the Username to the ones we just created. And you’re done :)

user.username=Username or Email
user.usernamerequired=Please enter your 'Username' or 'Email Address'.