1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-02 15:38:15 +00:00

Use email-user and binding in ccnet instead of userprofile in seahub

* Remove UserProfile from profile/models.
* Disable ccnet user id binding in profile page
This commit is contained in:
xiez
2012-04-01 21:55:33 +08:00
parent fc11ef20cb
commit 50fe2bb91a
12 changed files with 104 additions and 95 deletions

View File

@@ -13,7 +13,6 @@ from django import forms
from django.utils.translation import ugettext_lazy as _
from seahub.profile.models import UserProfile
from seaserv import ccnet_rpc
class EmailOrUsernameModelBackend(object):
@@ -108,11 +107,12 @@ class RegistrationBackend(object):
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site,
send_email=settings.REGISTRATION_SEND_MAIL)
# save email and password to EmailUser table
ccnet_rpc.add_emailuser(email, password)
userid = kwargs['userid']
if userid:
profile = UserProfile(user=new_user, ccnet_user_id=userid)
profile.save()
ccnet_rpc.add_binding(new_user.username, userid)
signals.user_registered.send(sender=self.__class__,
user=new_user,
@@ -139,8 +139,7 @@ class RegistrationBackend(object):
activated.backend='django.contrib.auth.backends.ModelBackend'
login(request, activated)
try:
profile = request.user.get_profile()
if profile.ccnet_user_id:
if request.user.user_id:
ccnet_rpc.add_client(ccnet_user_id)
except:
pass

View File

@@ -1,5 +1,4 @@
from seahub.profile.models import UserProfile
from seaserv import ccnet_rpc
class UseridMiddleware(object):
"""Store ccnet user id in request.user.user_id"""
@@ -9,9 +8,8 @@ class UseridMiddleware(object):
return None
try:
profile = request.user.get_profile()
request.user.user_id = profile.ccnet_user_id
except UserProfile.DoesNotExist:
request.user.user_id = ccnet_rpc.get_binding_userid(request.user.username)
except:
request.user.user_id = ''
return None

View File

@@ -1,4 +1,4 @@
from django.contrib import admin
from profile.models import UserProfile
#from profile.models import UserProfile
admin.site.register(UserProfile)
#admin.site.register(UserProfile)

View File

@@ -2,9 +2,9 @@ from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
ccnet_user_id = models.CharField(max_length=40, blank=True)
#class UserProfile(models.Model):
# user = models.ForeignKey(User, unique=True)
# ccnet_user_id = models.CharField(max_length=40, blank=True)
class UserCcnetConf(models.Model):
user = models.ForeignKey(User, unique=True)

View File

@@ -3,7 +3,6 @@
{% block left_panel %}
<ul>
<li><a href="{% url profile_setting %}">绑定个人 ID</a></li>
<li><a href="{{ SITE_ROOT }}accounts/password/change/">修改网站帐号密码</a></li>
</ul>
{% endblock %}
@@ -13,14 +12,12 @@
<h2>当前设置</h2>
<ul>
{% if profile.ccnet_user_id %}
<li><span class="bold">个人 ID</span>{{ profile.ccnet_user_id }}</li>
{% else %}
<li><span class="bold">个人 ID</span>您还没绑定。<a href="{% url profile_setting %}">现在绑定</a></li>
{% if user_id %}
<li><span class="bold">个人 ID</span>{{ user_id }}</li>
{% endif %}
{% if profile_timestamp %}
<li><span class="bold">帐号文件:</span>您的加密后的帐号文件上传于{{ profile_timestamp }} <a href="{{ SITE_ROOT }}profile/download/?user_id={{profile.ccnet_user_id}}">点此下载</a></li>
<li><span class="bold">帐号文件:</span>您的加密后的帐号文件上传于{{ profile_timestamp }} <a href="{{ SITE_ROOT }}profile/download/?user_id={{ user_id }}">点此下载</a></li>
{% else %}
<li><span class="bold">帐号文件:</span>您的帐号文件还未上传,请在本地 seafile 界面 "帐号" 标签下上传</li>
{% endif %}

View File

@@ -3,7 +3,7 @@ from django.conf.urls.defaults import *
urlpatterns = patterns('profile.views',
url(r'^$', 'show_profile'),
url(r'^ccnet/$', 'get_ccnet_profile'),
url(r'^edit/$', 'set_profile', name="profile_setting"),
# url(r'^edit/$', 'set_profile', name="profile_setting"),
url(r'^edit/ccnet/$', 'set_ccnet_profile', name="ccnet_profile_setting"),
url(r'^download/$', 'download_profile', name="profile_download"),
)

View File

@@ -8,16 +8,13 @@ from django.core.urlresolvers import reverse
import datetime
from forms import SetUserProfileForm
from models import UserProfile, UserCcnetConf
from models import UserCcnetConf
from seaserv import ccnet_rpc, translate_time_usec
@login_required
def show_profile(request):
try:
profile = request.user.get_profile()
except UserProfile.DoesNotExist:
profile = UserProfile(user=request.user)
profile.save()
user_id = ccnet_rpc.get_binding_userid(request.user.username)
try:
profile_timestamp = ccnet_rpc.get_user_profile_timestamp(profile.ccnet_user_id)
@@ -25,51 +22,42 @@ def show_profile(request):
except:
profile_timestamp = None
return render_to_response('profile/profile.html',
{ 'profile': profile,
return render_to_response('profile/profile.html', {
'user_id': user_id,
'profile_timestamp': profile_timestamp},
context_instance=RequestContext(request))
@login_required
def set_profile(request):
error_msg = None
origin_id = None
if request.method == 'POST':
ccnet_user_id = request.POST.get('ccnet_user_id', '').strip()
origin_id = ccnet_user_id
if not ccnet_user_id:
error_msg = "You must specify Key ID"
elif len(ccnet_user_id) != 40:
error_msg = "Key ID must be of length 40"
elif UserProfile.objects.filter(ccnet_user_id=ccnet_user_id).count() != 0:
error_msg = "Key ID has been used"
else:
try:
profile = request.user.get_profile()
except UserProfile.DoesNotExist:
profile = UserProfile(user=request.user)
profile.save()
try:
ccnet_rpc.add_client(ccnet_user_id)
except Exception, e:
error_msg = "Ccnet Daemon is not available, try again later"
else:
profile.ccnet_user_id = ccnet_user_id
profile.save()
return HttpResponseRedirect(reverse(show_profile))
else:
try:
profile = request.user.get_profile()
except UserProfile.DoesNotExist:
profile = UserProfile(user=request.user)
profile.save()
origin_id = profile.ccnet_user_id
return render_to_response('profile/set_profile.html',
{ 'error_msg': error_msg,
'origin_id': origin_id },
context_instance=RequestContext(request))
#@login_required
#def set_profile(request):
# error_msg = None
# origin_id = None
# if request.method == 'POST':
# ccnet_user_id = request.POST.get('ccnet_user_id', '').strip()
# origin_id = ccnet_user_id
# if not ccnet_user_id:
# error_msg = "You must specify Key ID"
# elif len(ccnet_user_id) != 40:
# error_msg = "Key ID must be of length 40"
# elif ccnet_rpc.get_binding_email(ccnet_user_id) != None:
# email = ccnet_rpc.get_binding_email(ccnet_user_id)
# # user id has been binded by an email
# error_msg = ("Key ID has been used by %s" % email)
# else:
# try:
# ccnet_rpc.add_client(ccnet_user_id)
# except Exception, e:
# error_msg = "Ccnet Daemon is not available, try again later"
# else:
# ccnet_rpc.add_binding(request.user.username, ccnet_user_id)
# return HttpResponseRedirect(reverse(show_profile))
# else:
# origin_id = ccnet_rpc.get_binding_userid(request.user.username)
#
# return render_to_response('profile/set_profile.html',
# { 'error_msg': error_msg,
# 'origin_id': origin_id },
# context_instance=RequestContext(request))
@login_required
@@ -83,12 +71,8 @@ def get_ccnet_profile(request):
@login_required
def set_ccnet_profile(request):
try:
profile = request.user.get_profile()
except UserProfile.DoesNotExist:
return HttpResponse("Error: You have not set seafile id yet.")
ccnet_id = profile.ccnet_user_id
ccnet_id = request.user.user_id
if request.method == 'POST':
ccnet_profile = request.POST.get('ccnet_profile', None)
try:
@@ -107,7 +91,7 @@ def set_ccnet_profile(request):
ccnet_profile = ccnet_conf.ccnet_profile
except UserCcnetConf.DoesNotExist:
ccnet_profile = ""
return render_to_response('profile/set_ccnet_conf.html',
{ 'ccnet_profile': ccnet_profile },
context_instance=RequestContext(request))

View File

@@ -145,4 +145,4 @@ else:
LOGIN_URL = SITE_ROOT + 'accounts/login'
# profile
AUTH_PROFILE_MODULE = "profile.UserProfile"
#AUTH_PROFILE_MODULE = "profile.UserProfile"

View File

@@ -9,12 +9,6 @@
{% block right_panel %}
{% if not user.user_id %}
<div class="notification">
你还没有绑定 Ccnet/Seafile 公钥 ID。 现在<a href="{{ SITE_ROOT }}profile/edit/">绑定</a>
</div>
{% endif %}
<h3>我拥有的同步目录</h3>
{% if owned_repos %}
<table class="repo-list">

View File

@@ -28,17 +28,16 @@
{% else %}
<td><button data="{{ SITE_ROOT }}useradmin/activate/{{ user.id }}/" class="activate">激活</button></td>
{% endif %}
{% if user.profile %}
<td>{{ user.profile.ccnet_user_id }}</td>
{% if user.user_id %}
<td>{{ user.user_id }}</td>
{% else %}
<td></td>
{% endif %}
<td>
{% for role in user.role_list %}
{{ role }} <!--<button data="{{ SITE_ROOT }}useradmin/{{ user.profile.ccnet_user_id }}/role/remove/?role={{ role }}" class="role-delete-btn">删除</button>--><br />
{% endfor %}
</td>
{% else %}
<td></td>
<td></td>
{% endif %}
<td>
{% if user.profile %}
<!-- <button class="add-role-btn" userid="{{ user.profile.ccnet_user_id }}" email="{{ user.email }}">添加角色</button> -->

View File

@@ -27,6 +27,8 @@ from django.conf.urls.defaults import *
from django.contrib.auth import views as auth_views
from seahub.views import password_change
urlpatterns = patterns('',
url(r'^login/$',
@@ -38,7 +40,7 @@ urlpatterns = patterns('',
{'template_name': 'registration/logout.html'},
name='auth_logout'),
url(r'^password/change/$',
auth_views.password_change,
password_change,
name='auth_password_change'),
url(r'^password/change/done/$',
auth_views.password_change_done,

View File

@@ -5,12 +5,13 @@ from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.db import IntegrityError
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_protect
from django.contrib.auth.forms import AuthenticationForm, PasswordResetForm, SetPasswordForm, PasswordChangeForm
from seaserv import cclient, ccnet_rpc, get_groups, get_users, get_repos, \
get_repo, get_commits, get_branches, \
seafserv_threaded_rpc
from seahub.profile.models import UserProfile
from seahub.share.models import GroupShare, UserShare
from seahub.share.forms import GroupAddRepoForm
from forms import AddUserForm
@@ -237,8 +238,7 @@ def seafadmin(request):
for repo in repos:
try:
owner_id = seafserv_threaded_rpc.get_repo_owner(repo.props.id)
owner = UserProfile.objects.get(ccnet_user_id=owner_id).user
repo.owner = owner.email
repo.owner = ccnet_rpc.get_binding_email(owner_id)
repo.owner_id = owner_id
except:
repo.owner = None
@@ -262,11 +262,10 @@ def useradmin(request):
users = User.objects.all()
for user in users:
try:
user.profile = user.get_profile()
user.user_id = ccnet_rpc.get_binding_userid(user.username)
user.ccnet_user = ccnet_rpc.get_user(user.profile.ccnet_user_id)
user.role_list = user.ccnet_user.props.role_list.split(',')
except:
user.profile = None
user.ccnet_user = None
return render_to_response(
@@ -307,6 +306,10 @@ def user_remove(request, user_id):
user = User.objects.get(id=user_id)
user.delete()
# Also remove from ccnet EmailUser table and Binding table
ccnet_rpc.remove_emailuser(user.username)
ccnet_rpc.remove_binding(user.username)
return HttpResponseRedirect(request.META['HTTP_REFERER'])
@@ -343,6 +346,10 @@ def user_add(request):
new_user = User.objects.create_user(username, email, password)
new_user.is_active = True
new_user.save()
# Also save to ccnet EmailUser table
ccnet_rpc.add_emailuser(username, password)
return HttpResponseRedirect(reverse('useradmin', args=[]))
else:
form = AddUserForm()
@@ -350,3 +357,32 @@ def user_add(request):
return render_to_response("add_user_form.html", {
'form': form,
}, context_instance=RequestContext(request))
@csrf_protect
@login_required
def password_change(request,
template_name='registration/password_change_form.html',
post_change_redirect=None,
password_change_form=PasswordChangeForm,
current_app=None, extra_context=None):
if post_change_redirect is None:
post_change_redirect = reverse('django.contrib.auth.views.password_change_done')
if request.method == "POST":
form = password_change_form(user=request.user, data=request.POST)
if form.is_valid():
form.save()
# Also change ccnet EmailUser table
email = request.user.username
passwd = request.POST.get('new_password1')
ccnet_rpc.change_emailuser(email, passwd)
return HttpResponseRedirect(post_change_redirect)
else:
form = password_change_form(user=request.user)
context = {
'form': form,
}
context.update(extra_context or {})
return render_to_response(template_name, context,
context_instance=RequestContext(request, current_app=current_app))