Logging in to a user account with the user's eMail-address and master admin's password

Sometimes it would be uselful to be able to login in to a user account with the user’s eMail-address and the master admin’s password.

(E.g. to be able to add an order to a user account which came in by phone, fax, email, mail, whatever…).

The following module overloads the “login”-function of “oxuser” to allow this functionality.

Store the following code to “[B]modules/powertemplate/pt_user/pt_user.php[/B]”, and include the module with “[B]oxuser=>powertemplate/pt_user/pt_user[/B]” in the admin’s module area.

Have phun!

<?php
/**
 *    This file is part of OXID eShop Community Edition.
 *
 *    OXID eShop Community Edition is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    OXID eShop Community Edition is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with OXID eShop Community Edition.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @link http://www.oxid-esales.com
 * @package core
 * @copyright (C) OXID eSales AG 2003-2009
 * @version OXID eShop CE
 * $Id: oxuser.php 23173 2009-10-12 13:29:45Z sarunas $
 * 
 * @copyright Avenger 2010, [email protected]
 * 
 * Allow login to user account also with user email and admin password.
 * 
 * Include with: oxuser=>powertemplate/pt_user/pt_user
 */

/**
 * User manager.
 * Performs user managing function, as assigning to groups, updating
 * information, deletion and other.
 * @package core
 */
class pt_user extends pt_user_parent
{
    /**
     * Performs user login by username and password. Fetches user data from DB.
     * Registers in session. Returns true on success, FALSE otherwise.
     *
     * @param string $sUser     User username
     * @param string $sPassword User password
     * @param bool   $blCookie  (default false)
     *
     * @throws oxConnectionException, oxCookieException, oxUserException
     *
     * @return bool
     */
    public function login( $sUser, $sPassword, $blCookie = false)
    {
        if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
            $oEx = oxNew( 'oxCookieException' );
            $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
            throw $oEx;
        }

        $myConfig = $this->getConfig();
        if ( $sPassword ) {

            $sShopID = $myConfig->getShopId();
            $oDb = oxDb::getDb();

            $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
            $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
            $sShopSelect = "";

            // admin view: can only login with higher than 'user' rights
            if ( $this->isAdmin() ) {
                $sShopSelect = " and ( oxrights != 'user' ) ";
            }

            $sWhat = "oxid";
            $sSelect0 =  "select $sWhat from oxuser where oxuser.oxactive = 1 and ";              
            $sSelect =  $sSelect0."{$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
            if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
                if ( $sPassword == "admin" && $sUser == "admin" ) {
                    $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
                } else {
                    $oEx = oxNew( 'oxUserException' );
                    $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
                    throw $oEx;
                }
            }
            // load from DB
            $aData = $oDb->getAll( $sSelect );
            $sOXID = @$aData[0][0];
            //Avenger -- Try to login with master admins password start.
            $blIsInvalidValigLogin=!$sOXID; 
            for ($iLoginStep=1;$iLoginStep<=2;$iLoginStep++)
            {
              if ($blIsInvalidValigLogin) 
              {
                if ($iLoginStep==2)
                {
                  $oEx = oxNew( 'oxUserException' );
                  $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
                  throw $oEx;
                }
                else
                {
                  //Reduce login requirements as 1st step (only check for eMail-address)
                  $sSelect =  $sSelect0."{$sUserSelect} {$sShopSelect} ";
                  // load from DB
                  $aData = $oDb->getAll( $sSelect );
                  $sOXID = @$aData[0][0];
                  if ( $sOXID ) 
                  {
                    //User found by eMail-address, now check password against master admins' password....

                    //Get  master admins' password and password 'salt'
                    $sWhat .= ",oxpassword,oxpasssalt";
                    $sSelect =  "select $sWhat from oxuser where oxid='oxdefaultadmin'";
                    $aData = $oDb->getAll( $sSelect );
                    $sAdminOXID = @$aData[0][0];
                    $sAdminPassword = @$aData[0][1];
                    $sAdminPasswordSalt = @$aData[0][2];
                    //Check password entered against master admins' password
                    $blIsInvalidValigLogin=$sAdminPassword<>md5($sPassword.$this->unhex($sAdminPasswordSalt));
                  }
                  else
                  {
                    $blIsInvalidValigLogin=true;
                  }
                }
              }
              else
              {
                 $this->load( $sOXID );
                 break;
              }
            }
            //Avenger -- Try to login with master admins password end.
        }
        //login successfull?
        if ($this->oxuser__oxid->value ) {   // yes, successful login
            if ( $this->isAdmin() ) {
                oxSession::setVar( 'auth', $this->oxuser__oxid->value );
            } else {
                oxSession::setVar( 'usr', $this->oxuser__oxid->value );
            }

            // cookie must be set ?
            if ( $blCookie ) {
                oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
            }
            return true;
        } else {
            $oEx = oxNew( 'oxUserException' );
            $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
            throw $oEx;
        }
    }

   function unhex($sHex)
  {
    $sStr='';
    for ($i=0,$iHexLen=strlen($sHex);$i<$iHexLen;$i+=2)
    {
      $sStr.=chr(hexdec(substr($sHex,$i,2)));
    }
    return $sStr;
  }
}

[B]Without any guarantee!

Use at your own risk!

Before using the module, backup your database![/B]

Hi Avenger,

thanks alot!
Would you like to put it to eXchange and, for maintenance purposes, to the projects page?

Hi Avenger - very useful: just what I was looking for today…

hi avenger…
this modul is very useful for us…

thank’s a lot

Hallo Avenger
Bin begeistert tolles Modul

This module is very interesting and important for us because we accept phone orders, too. Unfortunately, it works for us is not correct, since the registration of our non-user is working properly, second only after registration of the user logged on correctly, I’ve noticed that I always need a reload after the registration of the browsers that I’m logged in correctly.

Can anyone help improve this script I think it is surely only a trifle to change, unfortunately I have very little experience with php and would be very grateful if someone could help me because of the forum or is there an alternative to this script

I thank you already in advance for your continued efforts

Hello, late, but better than never…

You can stop the need to reload with adding the following bold part into pt_user.php:

//login successfull?
if ($this->oxuser__oxid->value ) { // yes, successful login

//reseting active user
$this->setUser( null );

        if ( $this-&gt;isAdmin() ) {
            oxSession::setVar( 'auth', $this-&gt;oxuser__oxid-&gt;value );
        } else {
            oxSession::setVar( 'usr', $this-&gt;oxuser__oxid-&gt;value );
        }

it´s never too late for good solutions :slight_smile:

In 4.8.x replace

EXCEPTION_USER_NOVALIDLOGIN

with

ERROR_MESSAGE_USER_NOVALIDLOGIN

I have problems with this script/modul.

When I activate it, I can login with the users email and the admin password, but noone else can login.
When I deactivate it, the users can login again.

any suggestions?

I use 4.8.1.

thanks
peter

Hi,

I solved my problem. The code posted is not for oxid 4.8.1! You have to modify it, so that it fits.

Is there a github project for this module so that I can check it in?

If anyone is interested in the code, that is working for me:

peter

Thanks for sharing, a github project for this exists here: https://github.com/OXIDprojects/user_account_master_login

Ich brauche wirklich dieses Modul. Aber es funktioniert nicht in 4,9,7 :frowning: ein weiteres Modul Hilfe einzurichten oder zu beraten.

http://forum.oxid-esales.com/showthread.php?t=26865