Ejemplo n.º 1
0
def add_member(request, username):
    (teamUser, teamUserprofile) = _get_team_user_userprofile(request, username)
    teamMember = None
    username_or_email = request.POST.get('username_or_email', '')
    if '@' in username_or_email:
        user = GsuserManager.get_user_by_email(username_or_email)
        if not user:
            ref_hash = '%032x' % random.getrandbits(128)
            ref_message = u'用户 %s 邀请您注册Gitshell,成为团队 %s 的成员' % (
                request.user.username, username)
            userViaRef = UserViaRef(email=username_or_email,
                                    ref_type=REF_TYPE.VIA_TEAM_MEMBER,
                                    ref_hash=ref_hash,
                                    ref_message=ref_message,
                                    first_refid=teamUser.id,
                                    first_refname=teamUser.username)
            userViaRef.save()
            join_url = 'https://gitshell.com/join/ref/%s/' % ref_hash
            Mailer().send_join_via_team_addmember(request.user, teamUser,
                                                  username_or_email, join_url)
            return json_failed(301,
                               u'邮箱 %s 未注册,已经发送邮件邀请对方注册' % username_or_email)
        teamMember = TeamManager.add_teamMember_by_email(
            teamUser, username_or_email)
    else:
        teamMember = TeamManager.add_teamMember_by_username(
            teamUser, username_or_email)
    if not teamMember:
        return json_failed(404, u'没有相关用户,不能是团队帐号')
    return json_success(u'成功添加用户')
Ejemplo n.º 2
0
def resetpassword(request, step):
    if step is None:
        step = '0'
    error = u''
    title = u'重置密码'
    resetpasswordForm0 = ResetpasswordForm0()
    if step == '0' and request.method == 'POST':
        resetpasswordForm0 = ResetpasswordForm0(request.POST)
        if resetpasswordForm0.is_valid():
            email = resetpasswordForm0.cleaned_data['email']
            try:
                user = User.objects.get(email=email)
            except User.DoesNotExist:
                user = None
            if user is not None and user.is_active:
                Mailer().send_resetpassword(email)
                return HttpResponseRedirect('/resetpassword/1/')
            error = u'邮箱 %s 还没有注册' % email
        else:
            error = u'请检查邮箱,验证码是否正确'
    resetpasswordForm1 = ResetpasswordForm1()
    if len(step) > 1 and request.method == 'POST':
        resetpasswordForm1 = ResetpasswordForm1(request.POST)
        if resetpasswordForm1.is_valid():
            email = cache.get(step)
            if email is not None and email_re.match(email):
                password = resetpasswordForm1.cleaned_data['password']
                try:
                    user = User.objects.get(email=email)
                except User.DoesNotExist:
                    user = None
                if user is not None and user.is_active:
                    user.set_password(password)
                    user.save()
                    user = auth_authenticate(username=user.username,
                                             password=password)
                    auth_login(request, user)
                    cache.delete(step)
                    return HttpResponseRedirect('/resetpassword/3/')
            return HttpResponseRedirect('/resetpassword/4/')
    response_dictionary = {
        'step': step,
        'error': error,
        'title': title,
        'resetpasswordForm0': resetpasswordForm0,
        'resetpasswordForm1': resetpasswordForm1
    }
    return render_to_response('user/resetpassword.html',
                              response_dictionary,
                              context_instance=RequestContext(request))
Ejemplo n.º 3
0
def change(request):
    thirdpartyUser = GsuserManager.get_thirdpartyUser_by_id(request.user.id)
    user = None
    is_user_exist = True
    is_exist_repo = False
    username = request.POST.get('username')
    if username is not None and re.match(
            "^[a-zA-Z0-9_-]+$", username
    ) and username != request.user.username and username not in MAIN_NAVS and not username.startswith(
            '-'):
        repo_count = RepoManager.count_repo_by_userId(request.user.id)
        if repo_count > 0:
            return json_httpResponse({'is_exist_repo': True})
        user = GsuserManager.get_user_by_name(username)
        if user is None:
            request.user.username = username
            request.userprofile.username = username
            request.user.save()
            request.userprofile.save()
            for repo in RepoManager.list_repo_by_userId(
                    request.user.id, 0, 100):
                repo.username = username
                repo.save()
            is_user_exist = False
    goto = ''
    email = request.POST.get('email')
    if email is not None and email_re.match(email):
        user = GsuserManager.get_user_by_email(email)
        if user is None:
            Mailer().send_change_email(request.user, email)
            email_suffix = email.split('@')[-1]
            if email_suffix in COMMON_EMAIL_DOMAIN:
                goto = COMMON_EMAIL_DOMAIN[email_suffix]
            is_user_exist = False
    if thirdpartyUser is not None:
        thirdpartyUser.init = 1
        thirdpartyUser.save()
    if username == request.user.username:
        is_user_exist = False
    response_dictionary = {
        'is_exist_repo': is_exist_repo,
        'is_user_exist': is_user_exist,
        'goto': goto,
        'new_username': username,
        'new_email': email
    }
    return json_httpResponse(response_dictionary)
Ejemplo n.º 4
0
def email_verify(request, eid):
    usermail = GsuserManager.get_useremail_by_id(eid)
    email = usermail.email
    via = ''
    if usermail and usermail.is_verify == 0 and usermail.user_id == request.user.id:
        Mailer().send_verify_email(request.user, eid, email)
        email_suffix = email.split('@')[-1]
        if email_suffix in COMMON_EMAIL_DOMAIN:
            via = COMMON_EMAIL_DOMAIN[email_suffix]
        return json_httpResponse({
            'code': 200,
            'message': u'请尽快验证邮箱',
            'via': via
        })
    return json_httpResponse({
        'code': 500,
        'message': u'邮箱不对,或者没有相关权限',
        'via': via
    })
Ejemplo n.º 5
0
 def message_save_and_notif(self, notifMessage):
     notifMessage.save()
     userprofile = GsuserManager.get_userprofile_by_id(
         notifMessage.to_user_id)
     header = u'来自Gitshell的通知'
     notifSetting = self.get_notifsetting_by_userId(notifMessage.to_user_id)
     last_notif_time = notifSetting.last_notif_time
     if not last_notif_time:
         last_notif_time = datetime.now()
     notif_fqcy = notifSetting.notif_fqcy
     if notif_fqcy == 0:
         notifMessage = self._fillwith_notifMessages([notifMessage])[0]
         html = self.render_notifMessages_as_html(userprofile, header,
                                                  [notifMessage])
         Mailer().send_html_mail(header, html, None, [notifSetting.email])
     if notif_fqcy > 0:
         expect_notif_time = last_notif_time + timedelta(minutes=notif_fqcy)
         if expect_notif_time != notifSetting.expect_notif_time:
             notifSetting.expect_notif_time = expect_notif_time
             notifSetting.save()
Ejemplo n.º 6
0
def _send_notifMessages(notifMessages, notifSetting):
    userprofile = GsuserManager.get_userprofile_by_id(notifSetting.user_id)
    header = u'来自Gitshell的 %s 个通知' % len(notifMessages)
    html = FeedManager.render_notifMessages_as_html(userprofile, header, notifMessages)
    Mailer().send_html_mail(header, html, None, [notifSetting.email])
Ejemplo n.º 7
0
def _join(request, joinForm, joinVia, tip, step):
    if step is None:
        step = '0'
    error = u''
    title = u'注册'
    if step == '0' and request.method == 'POST':
        joinForm = JoinForm(request.POST)
        if joinForm.is_valid():
            email = joinForm.cleaned_data['email']
            username = joinForm.cleaned_data['username']
            password = joinForm.cleaned_data['password']
            ref_hash = joinForm.cleaned_data['ref_hash']
            user_by_email = GsuserManager.get_user_by_email(email)
            user_by_username = GsuserManager.get_user_by_name(username)
            if user_by_email is None and user_by_username is None:
                if ref_hash:
                    userViaRef = GsuserManager.get_userViaRef_by_refhash(
                        ref_hash)
                    if userViaRef and _create_user_and_authenticate(
                            request, username, email, password, ref_hash,
                            True):
                        return HttpResponseRedirect('/join/3/')
                client_ip = _get_client_ip(request)
                cache_join_client_ip_count = cache.get(
                    CacheKey.JOIN_CLIENT_IP % client_ip)
                if cache_join_client_ip_count is None:
                    cache_join_client_ip_count = 0
                cache_join_client_ip_count = cache_join_client_ip_count + 1
                cache.set(CacheKey.JOIN_CLIENT_IP % client_ip,
                          cache_join_client_ip_count)
                if cache_join_client_ip_count < 10 and _create_user_and_authenticate(
                        request, username, email, password, ref_hash, False):
                    return HttpResponseRedirect('/join/3/')
                Mailer().send_verify_account(email, username, password,
                                             ref_hash)
                goto = ''
                email_suffix = email.split('@')[-1]
                if email_suffix in COMMON_EMAIL_DOMAIN:
                    goto = COMMON_EMAIL_DOMAIN[email_suffix]
                return HttpResponseRedirect('/join/1/?goto=' + goto)
            error = u'欢迎回来, email: %s 或者 name: %s 已经注册过了, 您只需要直接登陆就行。' % (
                email, username)
        else:
            error = u'啊? 邮箱或验证码有误输入。注意大小写和前后空格。'
    if len(step) > 1:
        email = cache.get(step + '_email')
        username = cache.get(step + '_username')
        password = cache.get(step + '_password')
        ref_hash = cache.get(step + '_ref_hash')
        if email is None or username is None or password is None or not email_re.match(
                email) or not re.match("^[a-zA-Z0-9_-]+$",
                                       username) or username.startswith(
                                           '-') or username in MAIN_NAVS:
            return HttpResponseRedirect('/join/4/')
        if _create_user_and_authenticate(request, username, email, password,
                                         ref_hash, True):
            return HttpResponseRedirect('/join/3/')
        else:
            error = u'啊? 用户名或密码有误输入,注意大小写和前后空格。'
    response_dictionary = {
        'step': step,
        'error': error,
        'title': title,
        'joinForm': joinForm,
        'joinVia': joinVia,
        'tip': tip
    }
    return render_to_response('user/join.html',
                              response_dictionary,
                              context_instance=RequestContext(request))