Blog

Some of my recent posts and ramblings.

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'.

4 comments.

  1. John Sieber

    Thanks for the write up on this. This is something on my todo list!


  2. admin

    You’re welcome, John. I wish Blue River would make it an option in the Site Settings in the future.


  3. John Sieber

    Quick question about best practices when working with the language files. Is it a good idea to keep the file in alphabetical order to make it easier to find entries or is it better to add to the bottom of the file with all new additions? Just curious as to what the preferred method is.


  4. admin

    IMHO, it’s better to have them at the bottom for maintenance reasons.


Leave a Reply

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>