Bitrix Site Manager

OnUserLoginExternal event

mixed
event_handler(
  array &arParams
);

The OnUserLoginExternal event is used to check user login and password in an external authorization source. Handlers of this event are called from within the method CUser::Login if none of the OnBegoreUserLogin handlers return false, before a user authorization attempt and before the login arParams['LOGIN'] and password arParams['PASSWORD'] are both verified.

Parameters

ParameterDescription
arParams Array of fields used for user login and password verification, the following keys available:
  • LOGIN - user login;
  • PASSWORD - user password. If the value of PASSWORD_ORIGINAL is "Y", this parameter contains the original password. Otherwise, this parameter contains the MD5 hash value of the original password.
  • REMEMBER - if "Y", the user authorization information is to be stored in cookie.
  • PASSWORD_ORIGINAL - if "Y", the value of the PASSWORD field contains the original password typed by a user (not converted to MD5). If "N", the value of the PASSWORD field is converted to MD5.

Note
All parameters passed to this handler function and the arParams array are references to original variables. Therefore, all changes to parameters made within the handler affect values of the original variables.

Return Values

If a user is to be authorized, the handler should return the ID of this user.

See Also

Example



<?
// example of user authorization 
// based on tables of Innovision Power Board

// file /bitrix/php_interface/init.php
AddEventHandler("main", "OnUserLoginExternal", 
                Array("__IPBAuth", "OnUserLoginExternal"));
AddEventHandler("main", "OnExternalAuthList", 
                Array("__IPBAuth", "OnExternalAuthList"));

define("IPB_TABLE_PREFIX", "ibf_");
define("IPB_VERSION", "2");

class __IPBAuth
{
    function OnUserLoginExternal(&$arArgs)
    {
        $groups_map = Array(
            /*'IPB Group ID' => 'Local Group ID',*/
            '4' => '1'
            );
        $table_user = IPB_TABLE_PREFIX."members";
        $table_converge = IPB_TABLE_PREFIX."members_converge";

        global $DB, $USER, $APPLICATION;
     extract($arArgs);

        if(IPB_VERSION == '1')
        {
            $strSql = "SELECT * FROM ".$table_user.
                      " WHERE name='".$DB->ForSql($LOGIN).
                      "' AND password='".md5($PASSWORD)."'";
        }
        else
        {
            $strSql =
                "SELECT t1.* ".
                "FROM ".$table_user." t1, ".$table_converge." t2 ".
                "WHERE t1.name='".$DB->ForSql($LOGIN)."' ".
                "    AND t1.email = t2.converge_email ".
                "    AND t2.converge_pass_hash = 
                     MD5(CONCAT(MD5(t2.converge_pass_salt), 
                                '".md5($PASSWORD)."'))";
        }

        $dbAuthRes = $DB->Query($strSql);
        if($arAuthRes = $dbAuthRes->Fetch())
        {
            $arFields = Array(
                "LOGIN" => $LOGIN,
                "NAME" => $arAuthRes['title'],
                "PASSWORD" => $PASSWORD,
                "EMAIL" => $arAuthRes['email'],
                "ACTIVE" => "Y",
                "EXTERNAL_AUTH_ID"=>"IPB",
                "LID" => SITE_ID
                );

            $oUser = new CUser;
            $res = CUser::GetList($O, $B, 
                                  Array("LOGIN_EQUAL_EXACT"=>$LOGIN, 
                                        "EXTERNAL_AUTH_ID"=>"IPB"));
            if(!($ar_res = $res->Fetch()))
                $ID = $oUser->Add($arFields);
            else
            {
                $ID = $ar_res["ID"];
                $oUser->Update($ID, $arFields);
            }

            if($ID>0)
            {
                $USER->SetParam("IPB_USER_ID", $arAuthRes['id']);

                $user_group = $arAuthRes['mgroup'];
                $arUserGroups = CUser::GetUserGroup($ID);
                foreach($groups_map as $ext_group_id => $group_id)
                {
                    if($ext_group_id==$user_group)
                        $arUserGroups[] = $group_id;
                    else
                    {
                        $arUserGroupsTmp = Array();
                        foreach($arUserGroups as $grid)
                            if($grid != $group_id)
                                $arUserGroupsTmp[] = $grid;
                        $arUserGroups = $arUserGroupsTmp;
                    }
                }
                CUser::SetUserGroup($ID, $arUserGroups);
                $arParams["STORE_PASSWORD"] = "N";

                return $ID;
            }
        }
    }

    function OnExternalAuthList()
    {
        return Array(
            Array("ID"=>"IPB", "NAME"=>"Invision Power Board")
            );
    }
}
?>