mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-03 07:55:36 +00:00
Add org account feature
This commit is contained in:
155
base/accounts.py
155
base/accounts.py
@@ -9,7 +9,7 @@ from django.contrib.sites.models import Site
|
||||
from auth.models import get_hexdigest, check_password
|
||||
from auth import authenticate, login
|
||||
from registration import signals
|
||||
from registration.forms import RegistrationForm
|
||||
#from registration.forms import RegistrationForm
|
||||
from registration.models import RegistrationProfile
|
||||
from seaserv import ccnet_rpc, get_ccnetuser
|
||||
|
||||
@@ -29,13 +29,15 @@ def convert_to_ccnetuser(emailuser):
|
||||
ccnetuser.is_staff = emailuser.props.is_staff
|
||||
ccnetuser.is_active = emailuser.props.is_active
|
||||
ccnetuser.ctime = emailuser.props.ctime
|
||||
|
||||
ccnetuser.org = emailuser.org
|
||||
|
||||
return ccnetuser
|
||||
|
||||
class CcnetUser(object):
|
||||
is_staff = False
|
||||
is_active = False
|
||||
objects = UserManager()
|
||||
org = None
|
||||
|
||||
def __init__(self, username, raw_password):
|
||||
self.username = username
|
||||
@@ -225,12 +227,6 @@ class RegistrationBackend(object):
|
||||
# login the user
|
||||
activated.backend='auth.backends.ModelBackend'
|
||||
login(request, activated)
|
||||
# TODO: user.user_id should be change
|
||||
try:
|
||||
if request.user.user_id:
|
||||
ccnet_rpc.add_client(ccnet_user_id)
|
||||
except:
|
||||
pass
|
||||
|
||||
return activated
|
||||
|
||||
@@ -323,3 +319,146 @@ class RegistrationForm(forms.Form):
|
||||
raise forms.ValidationError(_("The two password fields didn't match."))
|
||||
return self.cleaned_data
|
||||
|
||||
class OrgRegistrationForm(RegistrationForm):
|
||||
"""
|
||||
Form for registering a business user account.
|
||||
|
||||
Validates that the requested email is not already in use, and
|
||||
requires the password to be entered twice to catch typos.
|
||||
"""
|
||||
|
||||
org_name = forms.CharField(max_length=256,
|
||||
widget=forms.TextInput(),
|
||||
label=_("Organization Name"))
|
||||
url_prefix = forms.RegexField(label=_("Url Prefix"), max_length=20,
|
||||
regex=r'^[a-z0-9]+$',
|
||||
error_message=_("This value must contain only letters or numbers."))
|
||||
|
||||
def clean_url_prefix(self):
|
||||
url_prefix = self.cleaned_data['url_prefix']
|
||||
org = ccnet_rpc.get_org_by_url_prefix(url_prefix)
|
||||
if not org:
|
||||
return url_prefix
|
||||
else:
|
||||
raise forms.ValidationError(_("A organization with this url prefix already"))
|
||||
|
||||
class OrgRegistrationBackend(object):
|
||||
def register(self, request, **kwargs):
|
||||
"""
|
||||
Given a username, email address and password, register a new
|
||||
user account, which will initially be inactive.
|
||||
|
||||
Along with the new ``User`` object, a new
|
||||
``registration.models.RegistrationProfile`` will be created,
|
||||
tied to that ``User``, containing the activation key which
|
||||
will be used for this account.
|
||||
|
||||
An email will be sent to the supplied email address; this
|
||||
email should contain an activation link. The email will be
|
||||
rendered using two templates. See the documentation for
|
||||
``RegistrationProfile.send_activation_email()`` for
|
||||
information about these templates and the contexts provided to
|
||||
them.
|
||||
|
||||
After the ``User`` and ``RegistrationProfile`` are created and
|
||||
the activation email is sent, the signal
|
||||
``registration.signals.user_registered`` will be sent, with
|
||||
the new ``User`` as the keyword argument ``user`` and the
|
||||
class of this backend as the sender.
|
||||
|
||||
"""
|
||||
email, password = kwargs['email'], kwargs['password1']
|
||||
username = email
|
||||
org_name, url_prefix = kwargs['org_name'], kwargs['url_prefix']
|
||||
|
||||
if Site._meta.installed:
|
||||
site = Site.objects.get_current()
|
||||
else:
|
||||
site = RequestSite(request)
|
||||
|
||||
|
||||
if settings.ACTIVATE_AFTER_REGISTRATION:
|
||||
# since user will be activated after registration,
|
||||
# so we will not use email sending, just create acitvated user
|
||||
new_user = RegistrationProfile.objects.create_active_user(username, email,
|
||||
password, site,
|
||||
send_email=False)
|
||||
# create orgnization account
|
||||
try:
|
||||
ccnet_rpc.create_org(org_name, url_prefix, username)
|
||||
except SearpcError, e:
|
||||
pass
|
||||
else:
|
||||
# login the user
|
||||
new_user.backend='auth.backends.ModelBackend'
|
||||
login(request, new_user)
|
||||
else:
|
||||
# create inactive user, user can be activated by admin, or through activated email
|
||||
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
|
||||
password, site,
|
||||
send_email=settings.REGISTRATION_SEND_MAIL)
|
||||
|
||||
signals.user_registered.send(sender=self.__class__,
|
||||
user=new_user,
|
||||
request=request)
|
||||
return new_user
|
||||
|
||||
def activate(self, request, activation_key):
|
||||
"""
|
||||
Given an an activation key, look up and activate the user
|
||||
account corresponding to that key (if possible).
|
||||
|
||||
After successful activation, the signal
|
||||
``registration.signals.user_activated`` will be sent, with the
|
||||
newly activated ``User`` as the keyword argument ``user`` and
|
||||
the class of this backend as the sender.
|
||||
|
||||
"""
|
||||
activated = RegistrationProfile.objects.activate_user(activation_key)
|
||||
if activated:
|
||||
signals.user_activated.send(sender=self.__class__,
|
||||
user=activated,
|
||||
request=request)
|
||||
# login the user
|
||||
activated.backend='auth.backends.ModelBackend'
|
||||
login(request, activated)
|
||||
|
||||
return activated
|
||||
|
||||
def registration_allowed(self, request):
|
||||
"""
|
||||
Indicate whether account registration is currently permitted,
|
||||
based on the value of the setting ``REGISTRATION_OPEN``. This
|
||||
is determined as follows:
|
||||
|
||||
* If ``REGISTRATION_OPEN`` is not specified in settings, or is
|
||||
set to ``True``, registration is permitted.
|
||||
|
||||
* If ``REGISTRATION_OPEN`` is both specified and set to
|
||||
``False``, registration is not permitted.
|
||||
|
||||
"""
|
||||
return getattr(settings, 'REGISTRATION_OPEN', True)
|
||||
|
||||
def get_form_class(self, request):
|
||||
"""
|
||||
Return the default form class used for user registration.
|
||||
|
||||
"""
|
||||
return RegistrationForm
|
||||
|
||||
def post_registration_redirect(self, request, user):
|
||||
"""
|
||||
Return the name of the URL to redirect to after successful
|
||||
user registration.
|
||||
|
||||
"""
|
||||
return ('registration_complete', (), {})
|
||||
|
||||
def post_activation_redirect(self, request, user):
|
||||
"""
|
||||
Return the name of the URL to redirect to after successful
|
||||
account activation.
|
||||
|
||||
"""
|
||||
return ('myhome', (), {})
|
||||
|
Reference in New Issue
Block a user