Sso.class.php 11.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
<?php
/**
 * 社区与sso相关逻辑
 */
class Lib_Utils_Sso
{
    private static $mapGender = array(MALE => 'male', FEMALE => 'female', UNKNOW => 'unknow');
    private static $mapUserStatus = array ('y' => USER_STATUS_VALID, 'n' => USER_STATUS_INVALID );
    private static $_status_map = array (USER_STATUS_INVALID => 'n', USER_STATUS_VALID => 'y' );
    
    /**
     * 用户登录
     * 
     * @param string $uid
     * @param $uid $rememberMe
     * @return string
     */
    public static function login($uid, $rememberMe)
    {
        $sessionKey = '';
        if(USE_SSO) 
        {
            $session = Lib_Api_Sso::loginPure ( $uid, $rememberMe );
            if ($session && $session ['code'] == 200 && is_array ( $session ['data'] )) 
            {
                $sessionKey = $session ['data'] ['sesskey'];
                Facade_Memory_Session::createSessionSso ( $session ['data'] ['sesskey'], $uid, $session ['data'] ['expire'], $session ['data'] ['postid'], $session ['data'] ['logoutid'], $session ['data'] ['createtime'], $session ['data'] ['createip'] );
            } 
        }
        return $sessionKey;
    }
    
    /**
     * 用户登出
     * 
     * @param string $uid
     * @param string $sessionkey
     * @return boolean
     */
    public static function logout($uid, $sessionkey)
    {
        if(USE_SSO) 
        {
            Lib_Api_Sso::logout ( $uid, $sessionkey );
        }
        return true;
    }
    
    /**
     * 检查SSO用户在线状态
     * 
     * @param integer $uid
     * @param string $sesskey
     * @return boolean
     */
    public static function checkSsoStatus($uid, $sesskey) 
    {
        if(USE_SSO)
        {
            $info = Lib_Api_Sso::checkOnline ( $uid, $sesskey );
            if ($info && $info ['code'] == 200)
            {
                if (! $info ['data'] || ($info ['data'] ['expire'] > 0 && $info ['data'] ['expire'] < time ())) 
                { 
                    //如果SSO已经踢出用户
                    Facade_Memory_Session::deleteSession ( $sesskey );
                    Facade_Passport_Auth::unsetAuthedCookie ();
                    return false;
                } 
                else 
                {
                    Facade_Passport_Auth::setAuthedCookie ( $uid, $sesskey, $info ['data'] ['expire'] > 0 );
                    return true;
                }
            }
        }
        return true;
    }
    
    /**
     * 设置用户信息
     * 
     * @param string $member_id
     * @param int $sex
     * @param string $birthday_year
     * @param string $birthday_month
     * @param string $birthday_day
     * @return true
     */
    public static function setUserInfo($member_id, $sex, $birthday_year, $birthday_month, $birthday_day)
    {
        if(USE_SSO)
        {
            Lib_Api_Sso::setUserInfo ( $member_id, self::$mapGender [$sex], $birthday_year, $birthday_month, $birthday_day );
        }
        return true;
    }
    
    /**
     * 添加用户soo中心
     * 
     * @param string $email
     * @param string $mobile
     * @param string $passwd
     * @param string $encryptPasswd
     * @param int $createTime
     * @param int $status
     * @return int
     */
    public static function addUser($email, $mobile = '', $passwd, $encryptPasswd, $createTime, $status)
    {
        $uid = '';
        if(USE_SSO)
        {
            //如果已经在SSO注册
            if(empty($mobile))
            {
                $sLogin = Lib_Api_Sso::checkLogin($email); 
            }
            else
            {
                $sLogin = Lib_Api_Sso::checkLogin ( $mobile ); 
            }
            if ($sLogin && $sLogin ['code'] == 200 && is_array ( $sLogin ['data'] ))
            {
                $uid = $sLogin ['data'] ['uid'];
                self::cloneUserFromSSO ( $uid );
            } 
            else 
            { 
                //如果SSO中没有该用户
                $ret = Lib_Api_Sso::registerPure ( $passwd, self::$_status_map [$status]);
                if ($ret && $ret ['code'] == 200) 
                {
                    $service = new Service_Passport_Userinfo();
                    $service->setUserBaseInfoSso ( $uid, $email, $encryptPasswd, $createTime, $status );
                    $uid = $ret ['data'] ['uid'];
                    Lib_Api_Sso::bindNewPass ( $uid, $email, 'email' );
                    if(!empty($mobile))
                    {
                        Lib_Api_Sso::bindNewPass ( $uid, $mobile, 'mobile');
                    }
                }
            }
        }
        return $uid;
    }
    
    /**
     * 从SSO中克隆用户信息
     * 
     * @param integer $sUid
     * @return void
     */
    public static function cloneUserFromSSO($sUid) 
    {   
        if(!USE_SSO)
        {
            return;
        }
        $uBase = Lib_Api_Sso::getUserBase ( $sUid );
        $uLogins = Lib_Api_Sso::getLogins ( $sUid ); 
        if ($uBase ['code'] == 200 && $uLogins ['code'] == 200 && ! empty ( $uLogins ['data'] )) 
        {
            foreach ( $uLogins ['data'] as $l ) 
            {
                if ($l ['tp'] === 'email') 
                {
                    $email = $l ['pass'];
                } 
                elseif ($l ['tp'] === 'mobile') 
                {
                    $mobile = $l ['pass'];
                } 
                elseif ($l ['tp'] === 'name') 
                {
                    $name = $l ['pass'];
                }
            }
            $userinfoService = new Service_Passport_Userinfo();
            //增加有货手机号保存
            if(!$mobile)
            {
                $authMobileUid = $userinfoService->getUidByMobile ( $mobile );
                if(empty($authMobileUid)||$sUid == $authMobileUid)
                {
                    $userinfoService->setUserMobile($sUid, $mobile, $uBase ['data'] ['createtime']);
                    //增加如果有货没有传邮箱,并且手机号也存在,社区默认生成一个手机号的系统邮箱
                    if(!$email && $mobile)
                    {
                        $email = $mobile."@".MOBILE_USER_DEFAULT_EMAIL_SUFFIX;
                        //如果是只有手机号登陆方式,添加手机相关的邮箱登陆方式
                        Lib_Api_Sso::bindNewPass($sUid, $email, 'email');
                    }
                }
                else
                {
                    return true;
                }
            }
            $userinfoService->setUserBaseInfoSso ( $sUid, $email, $uBase ['data'] ['pwd'], $uBase ['data'] ['createtime'], $map [$uBase ['data'] ['valid']] );
            $userinfoService->setUserType($sUid, USER_TYPE_YOHOBUY, $uBase ['data'] ['createtime']);
            //增加默认激活用户的账号
            $userinfoService->setUserStatus ( $sUid, USER_ACTIVE_YES, time () );
        }
        
        $uInfo = Lib_Api_Sso::getUserInfo ( $sUid );
        if ($uInfo ['code'] == 200 && ! empty ( $uInfo ['data'] ))
        {
             $service = new Service_Passport_Personal();
             $service->setPersonal ( $sUid, '', self::$mapGender [$uInfo ['data'] ['gender']], $uInfo ['data'] ['birthyear'], $uInfo ['data'] ['birthmonth'], $uInfo ['data'] ['birthday'], 0, 0, 0, 0, '', $uBase ['data'] ['createtime'], true );
        }
        $uHeader = Lib_Api_Sso::getHeaderIcon ( $sUid );
        if ($uHeader ['code'] == 200) 
        {
            $userinfoService->setUserHeadpic ( $sUid, $uHeader ['data'], true );
        }
    }
    
    /**
     * 设置用户可用状态
     * 
     * @param int $uid
     * @param int $status 1:可用 2:禁用3:禁用
     * @return boolean
     */
    public static function updateUserValid($uid, $status) 
    {
        if(!USE_SSO)
        {
            return;
        }
        if ($status == USER_STATUS_INVALID)
        {
            $ret = Lib_Api_Sso::kickUser ( $uid );
        } 
        else 
        {
            $ret = Lib_Api_Sso::recoverUser ( $uid );
        }
        if ($ret && $ret ['code'] == 200 && $ret ['data']) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * 修改密码
     * 
     * @param string $uid
     * @param string $passwd
     * @param string $textpass
     * @return boolean
     */
    public static function updatePassword($uid, $passwd, $textpass)
    {
        if(!USE_SSO)
        {
            return true;
        }
        $ret = Lib_Api_Sso::modifyPassword ( $uid, $passwd, $textpass );
        if ($ret && $ret ['code'] == 200 && $ret ['data'])
        {
            return true;
        }
        else 
        {
            return false;
        }
    }
    
    /**
     * 设置 用户昵称
     * 
     * @param int $uid
     * @param string $nickname
     * @return boolean
     */
    public static function setUserNick($uid, $nickname)
    {
        if(!USE_SSO)
        {
            return true;
        }
        $ret = false;
        $oldNickname = '';
        $logins = Lib_Api_Sso::getLogins($uid);
        if(!empty($logins['data']) && $logins['code'] == 200)
        {
            foreach($logins['data'] as $key => $login)
            {
                if($login['tp'] == 'name')
                {
                    $oldNickname = $login['pass'];
                }
            }
        }
        if(empty($oldNickname))
        {
            $ret = Lib_Api_Sso::bindNewPass ( $uid, $nickname, 'name' );
        }
        else
        {
            //同步用户统一中心SSO
            if ($nickname != $oldNickname) 
            {
                $ret = Lib_Api_Sso::modifyPass ( $uid, $oldNickname, $nickname, 'name' );
            }
        }
        return $ret;
    }
    
    /**
     * 设置 用户手机
     * 
     * @param int $uid
     * @param int $mobile
     * @return boolean
     */
    public static function setUserMobile($uid, $mobile)
    {
        if(!USE_SSO)
        {
            return true;
        }
        $oldMobile = '';
        $logins = Lib_Api_Sso::getLogins($uid);
        if(!empty($logins['data']) && $logins['code'] == 200)
        {
            foreach($logins['data'] as $key => $login)
            {
                if($login['tp'] == 'mobile')
                {
                    $oldMobile = $login['pass'];
                }
            }
        }
        if(empty($oldMobile))
        {
            Lib_Api_Sso::bindNewPass ( $uid, $mobile, 'mobile' );
            //同时新增系统邮箱账号
            Lib_Api_Sso::bindNewPass ($uid, $mobile."@".MOBILE_USER_DEFAULT_EMAIL_SUFFIX, 'email'); 
        }
        else
        {
            Lib_Api_Sso::modifyPass ( $uid, $oldMobile, $mobile, 'mobile');
            //同时修改老的系统邮箱账号 
            $oldEmail = $oldMobile."@".MOBILE_USER_DEFAULT_EMAIL_SUFFIX;
            $newEmail = $mobile."@".MOBILE_USER_DEFAULT_EMAIL_SUFFIX;
            Lib_Api_Sso::modifyPass ( $uid, $oldEmail, $newEmail, 'email');
        }
    }
    
    /**
     * 删除用户手机
     * 
     * @param int $uid
     * @param string $mobile
     * @return boolean
     */
    public static function delUserMobile($uid, $mobile)
    {
        if(!USE_SSO)
        {
            return true;
        }
        Lib_Api_Sso::removePass($uid, $mobile);
        //同时新增系统邮箱账号
        Lib_Api_Sso::removePass ($uid, $mobile."@".MOBILE_USER_DEFAULT_EMAIL_SUFFIX); 
    }
    
    /**
     * 删除用户昵称
     * 
     * @param int $uid
     * @param string $nickname
     * @return boolean|void
     */
    public static function delUserNickname($uid, $nickname)
    {
        if(!USE_SSO)
        {
            return true;
        }
        Lib_Api_Sso::removePass($uid, $nickname);
    }
    
    /**
     * 设置用户头像
     * 
     * @param string $uid
     * @param string $orginal
     * @return boolean
     */
    public static function setUserHeadpic($uid, $orginal)
    {
        if(USE_SSO) 
        {
            Lib_Api_Sso::setHeaderIcon ( $uid, $orginal);
        }
        return true;
    }
}