def github_authenticate(thirdpartyUser): tp_id, tp_username, tp_email, github_user_info = thirdpartyUser.tp_id, thirdpartyUser.tp_username, thirdpartyUser.tp_email, thirdpartyUser.github_user_info thirdpartyUser_find = GsuserManager.get_thirdpartyUser_by_type_tpId(ThirdpartyUser.GITHUB, tp_id) if thirdpartyUser_find is not None: if thirdpartyUser_find.access_token != thirdpartyUser.access_token: thirdpartyUser_find.access_token = thirdpartyUser.access_token thirdpartyUser_find.save() user_id = thirdpartyUser_find.id user = GsuserManager.get_user_by_id(user_id) return user username = __get_uniq_username(tp_username) email = __get_uniq_email(tp_email) password = __get_random_password() if username is None or email is None or password is None: return None create_user = None try: create_user = User.objects.create_user(username, email, password) if create_user is not None and create_user.is_active: userprofile = Userprofile(username = create_user.username, email = create_user.email, imgurl = hashlib.md5(create_user.email.lower()).hexdigest()) _fill_github_user_info(userprofile, github_user_info) userprofile.id = create_user.id userprofile.save() if username == tp_username and email == tp_email: thirdpartyUser.init = 1 thirdpartyUser.user_type = ThirdpartyUser.GITHUB thirdpartyUser.id = create_user.id thirdpartyUser.save() except IntegrityError, e: logger.exception(e)
def login_github_apply(request): error = u'' code = request.GET.get('code') if code is None: error = u'GitHub 关联失败,没有相关 code' access_token = github_oauth_access_token(code) if access_token == '': error = u'GitHub 关联失败,获取不到 access_token,请再次重试' thirdpartyUser = github_get_thirdpartyUser(access_token) if thirdpartyUser is None or thirdpartyUser.tp_id is None or thirdpartyUser.tp_username is None: error = u'获取不到 GitHub 信息,请再次重试' thirdpartyUser_find = GsuserManager.get_thirdpartyUser_by_type_tpId( ThirdpartyUser.GITHUB, thirdpartyUser.tp_id) if thirdpartyUser_find is not None: error = u'该 GitHub 账户已经关联 Gitshell,请直接使用 GitHub 账户登录' if error != '': return HttpResponseRedirect( '/%s/-/repo/create/?%s#via-github' % (request.user.username, urllib.urlencode({'apply_error': error.encode('utf8')}))) thirdpartyUser.user_type = ThirdpartyUser.GITHUB thirdpartyUser.access_token = access_token thirdpartyUser.id = request.user.id thirdpartyUser.init = 1 thirdpartyUser.save() return HttpResponseRedirect('/%s/-/repo/create/#via-github' % request.user.username)
def github_authenticate(thirdpartyUser): tp_id, tp_username, tp_email, github_user_info = thirdpartyUser.tp_id, thirdpartyUser.tp_username, thirdpartyUser.tp_email, thirdpartyUser.github_user_info thirdpartyUser_find = GsuserManager.get_thirdpartyUser_by_type_tpId( ThirdpartyUser.GITHUB, tp_id) if thirdpartyUser_find is not None: if thirdpartyUser_find.access_token != thirdpartyUser.access_token: thirdpartyUser_find.access_token = thirdpartyUser.access_token thirdpartyUser_find.save() user_id = thirdpartyUser_find.id user = GsuserManager.get_user_by_id(user_id) return user username = __get_uniq_username(tp_username) email = __get_uniq_email(tp_email) password = __get_random_password() if username is None or email is None or password is None: return None create_user = None try: create_user = User.objects.create_user(username, email, password) if create_user is not None and create_user.is_active: userprofile = Userprofile( username=create_user.username, email=create_user.email, imgurl=hashlib.md5(create_user.email.lower()).hexdigest()) _fill_github_user_info(userprofile, github_user_info) userprofile.id = create_user.id userprofile.save() if username == tp_username and email == tp_email: thirdpartyUser.init = 1 thirdpartyUser.user_type = ThirdpartyUser.GITHUB thirdpartyUser.id = create_user.id thirdpartyUser.save() except IntegrityError, e: logger.exception(e)
def login_github_apply(request): error = u'' code = request.GET.get('code') if code is None: error = u'GitHub 关联失败,没有相关 code' access_token = github_oauth_access_token(code) if access_token == '': error = u'GitHub 关联失败,获取不到 access_token,请再次重试' thirdpartyUser = github_get_thirdpartyUser(access_token) if thirdpartyUser is None or thirdpartyUser.tp_id is None or thirdpartyUser.tp_username is None: error = u'获取不到 GitHub 信息,请再次重试' thirdpartyUser_find = GsuserManager.get_thirdpartyUser_by_type_tpId(ThirdpartyUser.GITHUB, thirdpartyUser.tp_id) if thirdpartyUser_find is not None: error = u'该 GitHub 账户已经关联 Gitshell,请直接使用 GitHub 账户登录' if error != '': return HttpResponseRedirect('/%s/-/repo/create/?%s#via-github' % (request.user.username, urllib.urlencode({'apply_error': error.encode('utf8')}))) thirdpartyUser.user_type = ThirdpartyUser.GITHUB thirdpartyUser.access_token = access_token thirdpartyUser.id = request.user.id thirdpartyUser.init = 1 thirdpartyUser.save() return HttpResponseRedirect('/%s/-/repo/create/#via-github' % request.user.username)