Skip to content

Commit

Permalink
[seafile-docs-custom] login via contact email
Browse files Browse the repository at this point in the history
1, org admin create org user
2, register org
  • Loading branch information
lian authored and imwhatiam committed Jun 27, 2023
1 parent 5d0b779 commit 536047d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
17 changes: 10 additions & 7 deletions seahub/organizations/api/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,22 @@ def post(self, request, org_id):
error_msg = "Name should not include '/'."
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

try:
user = User.objects.get(email=email)
error_msg = 'User %s already exists.' % email
from seahub.utils.auth import gen_user_virtual_id
contact_email = email
email = gen_user_virtual_id()

if Profile.objects.get_profile_by_contact_email(contact_email):
error_msg = "User %s already exists." % contact_email
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
except User.DoesNotExist:
pass

try:
user = User.objects.create_user(email, password, is_staff=False,
is_active=True)
Profile.objects.add_or_update(username=user.username,
contact_email=contact_email)
except User.DoesNotExist as e:
logger.error(e)
error_msg = 'Fail to add user %s.' % email
error_msg = 'Fail to add user %s.' % contact_email
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

if user and name:
Expand All @@ -276,7 +279,7 @@ def post(self, request, org_id):
if IS_EMAIL_CONFIGURED:
if SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER:
try:
send_user_add_mail(request, email, password)
send_user_add_mail(request, contact_email, password)
except Exception as e:
logger.error(str(e))

Expand Down
9 changes: 4 additions & 5 deletions seahub/organizations/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from seahub.base.accounts import User
from seahub.base.fields import LowerCaseCharField
from seahub.utils import is_valid_username
from seahub.profile.models import Profile

slug_re = re.compile(r'^[-a-zA-Z0-9_]+$')

Expand Down Expand Up @@ -39,12 +40,10 @@ def clean_email(self):
if not is_valid_username(email):
raise forms.ValidationError(_("Email address is not valid"))

try:
user = User.objects.get(email=email)
except User.DoesNotExist:
return email
if Profile.objects.get_profile_by_contact_email(email):
raise forms.ValidationError(_("A user with this email already exists."))

raise forms.ValidationError(_("A user with this email already exists."))
return email

def clean_url_prefix(self):
url_prefix = self.cleaned_data['url_prefix']
Expand Down
17 changes: 12 additions & 5 deletions seahub/organizations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,22 @@ def org_register(request):
org_name = form.cleaned_data['org_name']
url_prefix = form.cleaned_data['url_prefix']

new_user = User.objects.create_user(email, password,
is_staff=False, is_active=True)
from seahub.utils.auth import gen_user_virtual_id
contact_email = email
email = gen_user_virtual_id()

try:
new_user = User.objects.create_user(email, password,
is_staff=False, is_active=True)
Profile.objects.add_or_update(username=new_user.username,
contact_email=contact_email, nickname=name)
except User.DoesNotExist as e:
logger.error(e)

create_org(org_name, url_prefix, new_user.username)
new_org = get_org_by_url_prefix(url_prefix)
org_created.send(sender=None, org=new_org)

if name:
Profile.objects.add_or_update(new_user.username, name)

# login the user
new_user.backend = settings.AUTHENTICATION_BACKENDS[0]
login(request, new_user)
Expand Down

0 comments on commit 536047d

Please sign in to comment.