让iwebshop超级管理员支持第三方安全登录

默认后台管理员只能通过账号密码访问链接 http://www.域名.com/admin 登录;
本方法,可以让后台管理员通过QQ、微信、微博等第三方安全的登陆后台。

第一步:

iwebshop_oauth_user表添加 is_admin字段,如图
在iwebshop_oauth_user表添加 is_admin字段.png

第二步:

打开iwebshop_oauth_user表,在需要设置管理员的用户上面把,is_admin 字段值改为1,保存即可
is_admin字段值改为1.png

第三步:

修改控制器/controllers/simple.phpbindUser()方法
1.在

$userObj      = new IModel('user');
$oauthUserObj = new IModel('oauth_user');
$oauthUserRow = $oauthUserObj->getObj("oauth_user_id = '{$userInfo['id']}' and oauth_id = '{$oauthId}' ",'user_id,is_admin');
if($oauthUserRow)
{
    //清理oauth_user和user表不同步匹配的问题
    $tempRow = $userObj->getObj("id = '{$oauthUserRow['user_id']}'");
    if(!$tempRow)
    {
    $oauthUserObj->del("oauth_user_id = '{$userInfo['id']}' and oauth_id = '{$oauthId}' ");
    }
}

的后面添加

// 如果此账号是admin 则设置后台登录状态
if ($oauthUserRow['is_admin']==1) {
    $adminObj = new IModel('admin');
    $adminRow = $adminObj->getObj('admin_name = "admin"');
    $dataArray = array(
    'last_ip'   => IClient::getIp(),
    'last_time' => ITime::getDateTime(),
    );
    $adminObj->setData($dataArray);
    $where = 'id = '.$adminRow["id"];
    $adminObj->update($where);

    //根据角色分配权限
    if($adminRow['role_id'] == 0)
    {
    ISafe::set('admin_role_name','超级管理员');
    }
    else
    {
    $roleObj = new IModel('admin_role');
    $where   = 'id = '.$adminRow["role_id"].' and is_del = 0';
    $roleRow = $roleObj->getObj($where);
    ISafe::set('admin_role_name',$roleRow['name']);
    }
    ISafe::set('admin_id',$adminRow['id']);
    ISafe::set('admin_name',$adminRow['admin_name']);
    ISafe::set('admin_pwd',$adminRow['password']);

    //通知事件
    plugin::trigger("adminLoginCallback",$adminRow);
}

改好后,如图所示:
改好后图.png
2.修改

//原来的
$this->redirect($callback);
 
//改为:
if ($oauthUserRow['is_admin']==1)
{
    $this->redirect('/system/default');
}
else
{
    $this->redirect($callback);
}

大功告成!

测试登陆功能正常后,可以关闭管理员的账号密码登陆功能,更安全!
修改/controllers/admin.php文件
第13,14,15,16行,任意注释一行,即可关闭账号密码登陆后台。
如图:
admin.php文件.png

最后修改:2019 年 08 月 05 日 09 : 58 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论