def batch_user_make_admin(request): result = {} content_type = 'application/json; charset=utf-8' set_admin_emails = request.POST.get('set_admin_emails') set_admin_emails = string2list(set_admin_emails) success = [] failed = [] for email in set_admin_emails: try: user = User.objects.get(email=email) user.is_staff = True user.save() success.append(email) except User.DoesNotExist: failed.append(email) for item in success: messages.success(request, _(u'Successfully set %s as admin') % item) for item in failed: messages.error(request, _(u'Failed set %s as admin') % item) result['success'] = True return HttpResponse(json.dumps(result), content_type=content_type)
def batch_user_make_admin(request): """Batch make users as admins. """ if request.method != 'POST': raise Http404 content_type = 'application/json; charset=utf-8' set_admin_emails = request.POST.get('set_admin_emails') set_admin_emails = string2list(set_admin_emails) success = [] failed = [] for email in set_admin_emails: try: user = User.objects.get(email=email) except User.DoesNotExist: failed.append(email) continue user.is_staff = True user.save() success.append(email) for item in success: messages.success(request, _('Successfully set %s as admin.') % item) for item in failed: messages.error( request, _('Failed to set %s as admin: user does not exist.') % item) return HttpResponse(json.dumps({ 'success': True, }), content_type=content_type)
def send_shared_upload_link(request): """ Handle ajax post request to send shared upload link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({'error':_(u'Sending shared upload link failed. Email service is not properly configured, please contact administrator.')}) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = UploadLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] shared_upload_link = form.cleaned_data['shared_upload_link'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'shared_upload_link': shared_upload_link, } if extra_msg: c['extra_msg'] = extra_msg if REPLACE_FROM_EMAIL: from_email = request.user.username else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = request.user.username else: reply_to = None try: send_html_email(_(u'An upload link is shared to you on %s') % SITE_NAME, 'shared_upload_link_email.html', c, from_email, [to_email], reply_to=reply_to) except Exception, e: logger.error(str(e)) data = json.dumps({'error':_(u'Internal server error. Send failed.')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps({"msg": _(u'Successfully sent.')}) return HttpResponse(data, status=200, content_type=content_type)
def post(self, request): """ Add admin in batch """ emails = request.data.get('emails', None) if not emails: error_msg = 'emails invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) emails = string2list(emails) new_admin_info = [] for email in emails: if not is_valid_username(email): error_msg = 'email %s invalid.' % email return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: user = User.objects.get(email=email) except User.DoesNotExist: error_msg = 'email %s invalid.' % email return api_error(status.HTTP_400_BAD_REQUEST, error_msg) user.is_staff = True user.save() profile = Profile.objects.get_profile_by_user(user.email) user_info = {} user_info['email'] = user.email user_info['name'] = email2nickname(user.email) user_info[ 'contact_email'] = profile.contact_email if profile and profile.contact_email else '' user_info[ 'login_id'] = profile.login_id if profile and profile.login_id else '' user_info['is_staff'] = user.is_staff user_info['is_active'] = user.is_active user_info['quota_total'] = seafile_api.get_user_quota(user.email) user_info['quota_usage'] = seafile_api.get_user_self_usage( user.email) user_info['create_time'] = timestamp_to_isoformat_timestr( user.ctime) user_info['last_login'] = UserLastLogin.objects.get_by_username( user.email ).last_login if UserLastLogin.objects.get_by_username( user.email) else '' try: admin_role = AdminRole.objects.get_admin_role(user.email) user_info['admin_role'] = admin_role.role except AdminRole.DoesNotExist: user_info['admin_role'] = DEFAULT_ADMIN new_admin_info.append(user_info) return Response({'new_admin_user_list': new_admin_info})
def get(self, request, repo_id, format=None): repo = seafile_api.get_repo(repo_id) if not repo: error_msg = 'Library %s not found.' % repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) # argument checking parent_dir = request.GET.get('parent_dir', None) dirent_name_string = request.GET.get('dirents', None) if not parent_dir: error_msg = 'parent_dir invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not dirent_name_string: error_msg = 'dirents invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # folder exist checking if not seafile_api.get_dir_id_by_path(repo_id, parent_dir): error_msg = 'Folder %s not found.' % parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission checking if check_folder_permission(request, repo_id, parent_dir) is None: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) dirent_name_list = string2list(dirent_name_string) dirent_list = [] for dirent_name in dirent_name_list: dirent_list.append(dirent_name.strip('/')) fake_obj_id = {} fake_obj_id['file_list'] = dirent_list fake_obj_id['parent_dir'] = parent_dir username = request.user.username try: token = seafile_api.get_fileserver_access_token( repo_id, json.dumps(fake_obj_id), 'download-multi', username, False) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if len(dirent_list) > 10: send_file_access_msg(request, repo, parent_dir, 'web') else: for dirent_name in dirent_list: full_dirent_path = posixpath.join(parent_dir, dirent_name) send_file_access_msg(request, repo, full_dirent_path, 'web') download_url = '%s/files/%s' % (get_fileserver_root(), token) return Response({'url': download_url})
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.is_ajax() and not request.method == 'POST': raise Http404 result = {} content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({ 'error': _(u'Sending shared link failed. Email service is not properly configured, please contact administrator.' ) }) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] t = loader.get_template('shared_link_email.html') to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'site_name': SITE_NAME, } try: send_mail(_(u'Your friend shared a file to you on Seafile'), t.render(Context(c)), None, [to_email], fail_silently=False) except Exception, e: logger.error(str(e)) data = json.dumps( {'error': _(u'Internal server error. Send failed.')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps({"msg": _(u'Successfully sent.')}) return HttpResponse(data, status=200, content_type=content_type)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({'error':_(u'Sending shared link failed. Email service is not properly configured, please contact administrator.')}) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] file_shared_name = form.cleaned_data['file_shared_name'] file_shared_type = form.cleaned_data['file_shared_type'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'file_shared_name': file_shared_name, } if extra_msg: c['extra_msg'] = extra_msg try: if file_shared_type == 'f': c['file_shared_type'] = "file" send_html_email(_(u'A file is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, None, [to_email]) else: c['file_shared_type'] = "directory" send_html_email(_(u'A directory is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, None, [to_email]) except Exception, e: logger.error(str(e)) data = json.dumps({'error':_(u'Internal server error. Send failed.')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps({"msg": _(u'Successfully sent.')}) return HttpResponse(data, status=200, content_type=content_type)
def batch_org_user_make_admin(request): """Batch set urg users as organization admin.""" if request.method != 'POST': raise Http404 result = {} content_type = 'application/json; charset=utf-8' set_admin_emails = request.POST.get('set_admin_emails') set_admin_emails = string2list(set_admin_emails) # replace contact email with ccnet email if any set_admin_emails = convert_contact_emails(set_admin_emails) success = [] failed = [] already_admin = [] user_name_map = {} for e in Profile.objects.filter(user__in=set_admin_emails): user_name_map[e.user] = e.nickname org = request.user.org for email in set_admin_emails: if org_user_exists(org.org_id, email): try: name = user_name_map[email] except KeyError: name = email if is_org_staff(org.org_id, email): already_admin.append(name) else: set_org_staff(org.org_id, email) success.append(name) else: failed.append(email) for item in success + already_admin: messages.success(request, _(u'Successfully set %s as admin.') % item) for item in failed: messages.error( request, _(u'Failed to set %s as admin: user does not exist.') % item) result['success'] = True return HttpResponse(json.dumps(result), content_type=content_type)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.is_ajax() and not request.method == 'POST': raise Http404 result = {} content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({'error':_(u'Sending shared link failed. Email service is not properly configured, please contact administrator.')}) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] t = loader.get_template('shared_link_email.html') to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'site_name': SITE_NAME, } try: send_mail(_(u'Your friend shared a file to you on Seafile'), t.render(Context(c)), None, [to_email], fail_silently=False) except Exception, e: logger.error(str(e)) data = json.dumps({'error':_(u'Internal server error. Send failed.')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps({"msg": _(u'Successfully sent.')}) return HttpResponse(data, status=200, content_type=content_type)
def group_add_admin(request, group_id): """ Add group admin. """ group_id = int(group_id) # Checked by URL Conf if request.method != 'POST' or not request.is_ajax(): raise Http404 result = {} content_type = 'application/json; charset=utf-8' member_name_str = request.POST.get('user_name', '') member_list = string2list(member_name_str) for member_name in member_list: # Add user to contacts. mail_sended.send(sender=None, user=request.user.username, email=member_name) if not is_registered_user(member_name): err_msg = _(u'Failed to add, %s is not registrated.') % member_name result['error'] = err_msg return HttpResponse(json.dumps(result), status=400, content_type=content_type) # Check whether user is in the group if is_group_user(group_id, member_name): try: ccnet_threaded_rpc.group_set_admin(group_id, member_name) except SearpcError, e: result['error'] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type) else: try: ccnet_threaded_rpc.group_add_member(group_id, request.user.username, member_name) ccnet_threaded_rpc.group_set_admin(group_id, member_name) except SearpcError, e: result['error'] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.is_ajax() and not request.method == 'POST': raise Http404 result = {} content_type = 'application/json; charset=utf-8' from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] t = loader.get_template('shared_link_email.html') to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'site_name': SITE_NAME, } try: send_mail(_(u'Your friend sharing a file to you on Seafile'), t.render(Context(c)), None, [to_email], fail_silently=False) except: data = json.dumps({'error':_(u'Failed to send mail')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps("success") return HttpResponse(data, status=200, content_type=content_type) else: return HttpResponseBadRequest(json.dumps(form.errors), content_type=content_type)
def batch_user_make_admin(request): """Batch make users as admins. """ if request.method != 'POST': raise Http404 result = {} content_type = 'application/json; charset=utf-8' set_admin_emails = request.POST.get('set_admin_emails') set_admin_emails = string2list(set_admin_emails) success = [] failed = [] already_admin = [] if len(get_emailusers('LDAP', 0, 1)) > 0: messages.error(request, _(u'Using LDAP now, can not add admin.')) result['success'] = True return HttpResponse(json.dumps(result), content_type=content_type) for email in set_admin_emails: try: user = User.objects.get(email=email) if user.is_staff is True: already_admin.append(email) else: user.is_staff = True user.save() success.append(email) except User.DoesNotExist: failed.append(email) for item in success + already_admin: messages.success(request, _(u'Successfully set %s as admin.') % item) for item in failed: messages.error( request, _(u'Failed to set %s as admin: user does not exist.') % item) result['success'] = True return HttpResponse(json.dumps(result), content_type=content_type)
def batch_user_make_admin(request): """Batch make users as admins. """ if request.method != 'POST': raise Http404 content_type = 'application/json; charset=utf-8' set_admin_emails = request.POST.get('set_admin_emails') set_admin_emails = string2list(set_admin_emails) success = [] failed = [] for email in set_admin_emails: try: user = User.objects.get(email=email) except User.DoesNotExist: failed.append(email) continue if user.source == 'DB': # check if is DB user first user.is_staff = True user.save() else: # if is LDAP user, add this 'email' as a DB user first # then set admin ccnet_threaded_rpc.add_emailuser(email, '!', 1, 1) success.append(email) for item in success: messages.success(request, _(u'Successfully set %s as admin.') % item) for item in failed: messages.error( request, _(u'Failed to set %s as admin: user does not exist.') % item) return HttpResponse(json.dumps({ 'success': True, }), content_type=content_type)
def batch_user_make_admin(request): """Batch make users as admins. """ if not request.is_ajax() or request.method != 'POST': raise Http404 result = {} content_type = 'application/json; charset=utf-8' set_admin_emails = request.POST.get('set_admin_emails') set_admin_emails = string2list(set_admin_emails) success = [] failed = [] already_admin = [] if len(get_emailusers('LDAP', 0, 1)) > 0: messages.error(request, _(u'Using LDAP now, can not add admin.')) result['success'] = True return HttpResponse(json.dumps(result), content_type=content_type) for email in set_admin_emails: try: user = User.objects.get(email=email) if user.is_staff is True: already_admin.append(email) else: user.is_staff = True user.save() success.append(email) except User.DoesNotExist: failed.append(email) for item in success + already_admin: messages.success(request, _(u'Successfully set %s as admin.') % item) for item in failed: messages.error(request, _(u'Failed to set %s as admin: user does not exist.') % item) result['success'] = True return HttpResponse(json.dumps(result), content_type=content_type)
def batch_user_make_admin(request): """Batch make users as admins. """ if request.method != 'POST': raise Http404 content_type = 'application/json; charset=utf-8' set_admin_emails = request.POST.get('set_admin_emails') set_admin_emails = string2list(set_admin_emails) success = [] failed = [] for email in set_admin_emails: try: user = User.objects.get(email=email) except User.DoesNotExist: failed.append(email) continue if user.source == 'DB': # check if is DB user first user.is_staff = True user.save() else: # if is LDAP user, add this 'email' as a DB user first # then set admin ccnet_threaded_rpc.add_emailuser(email, '!', 1, 1) success.append(email) for item in success: messages.success(request, _(u'Successfully set %s as admin.') % item) for item in failed: messages.error(request, _(u'Failed to set %s as admin: user does not exist.') % item) return HttpResponse(json.dumps({'success': True,}), content_type=content_type)
def post(self, request): if not IS_EMAIL_CONFIGURED: error_msg = _(u'Sending shared link failed. Email service is not properly configured, please contact administrator.') return api_error(status.HTTP_403_FORBIDDEN, error_msg) # check args email = request.POST.get('email', None) if not email: error_msg = 'email invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) token = request.POST.get('token', None) if not token: error_msg = 'token invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) extra_msg = request.POST.get('extra_msg', '') # check if token exists try: link = UploadLinkShare.objects.get(token=token) except UploadLinkShare.DoesNotExist: error_msg = 'token %s not found.' % token return api_error(status.HTTP_404_NOT_FOUND, error_msg) # check if is upload link owner username = request.user.username if not link.is_owner(username): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) result = {} result['failed'] = [] result['success'] = [] to_email_list = string2list(email) # use contact_email, if present useremail = Profile.objects.get_contact_email_by_user(request.user.username) for to_email in to_email_list: failed_info = {} if not is_valid_email(to_email): failed_info['email'] = to_email failed_info['error_msg'] = 'email invalid.' result['failed'].append(failed_info) continue # prepare basic info c = { 'email': username, 'to_email': to_email, 'extra_msg': extra_msg, } if REPLACE_FROM_EMAIL: from_email = useremail else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = useremail else: reply_to = None c['shared_upload_link'] = gen_shared_upload_link(token) title = _(u'An upload link is shared to you on %s') % SITE_NAME template = 'shared_upload_link_email.html' # send email try: send_html_email(title, template, c, from_email, [to_email], reply_to=reply_to) result['success'].append(to_email) except Exception as e: logger.error(e) failed_info['email'] = to_email failed_info['error_msg'] = 'Internal Server Error' result['failed'].append(failed_info) return Response(result)
def post(self, request): if not IS_EMAIL_CONFIGURED: error_msg = _('Sending shared link failed. Email service is not properly configured, please contact administrator.') return api_error(status.HTTP_403_FORBIDDEN, error_msg) # check args email = request.POST.get('email', None) if not email: error_msg = 'email invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) token = request.POST.get('token', None) if not token: error_msg = 'token invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) extra_msg = request.POST.get('extra_msg', '') # check if token exists try: link = FileShare.objects.get(token=token) except FileShare.DoesNotExist: error_msg = 'token %s not found.' % token return api_error(status.HTTP_404_NOT_FOUND, error_msg) # check if is share link owner username = request.user.username if not link.is_owner(username): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) result = {} result['failed'] = [] result['success'] = [] to_email_list = string2list(email) # use contact_email, if present useremail = Profile.objects.get_contact_email_by_user(request.user.username) for to_email in to_email_list: failed_info = {} if not is_valid_email(to_email): failed_info['email'] = to_email failed_info['error_msg'] = 'email invalid.' result['failed'].append(failed_info) continue # prepare basic info c = { 'email': username, 'to_email': to_email, 'extra_msg': extra_msg, } if REPLACE_FROM_EMAIL: from_email = useremail else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = useremail else: reply_to = None c['file_shared_link'] = gen_shared_link(token, link.s_type) c['file_shared_name'] = os.path.basename(link.path.rstrip('/')) template = 'shared_link_email.html' if link.s_type == 'f': c['file_shared_type'] = _("file") title = _('A file is shared to you on %s') % get_site_name() else: c['file_shared_type'] = _("directory") title = _('A directory is shared to you on %s') % get_site_name() # send email try: send_html_email(title, template, c, from_email, [to_email], reply_to=reply_to) result['success'].append(to_email) except Exception as e: logger.error(e) failed_info['email'] = to_email failed_info['error_msg'] = 'Internal Server Error' result['failed'].append(failed_info) return Response(result)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({'error':_(u'Sending shared link failed. Email service is not properly configured, please contact administrator.')}) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] file_shared_name = form.cleaned_data['file_shared_name'] file_shared_type = form.cleaned_data['file_shared_type'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) send_success, send_failed = [], [] for to_email in to_email_list: if not is_valid_email(to_email): send_failed.append(to_email) continue # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'file_shared_name': file_shared_name, } if extra_msg: c['extra_msg'] = extra_msg if REPLACE_FROM_EMAIL: from_email = request.user.username else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = request.user.username else: reply_to = None try: if file_shared_type == 'f': c['file_shared_type'] = _(u"file") send_html_email(_(u'A file is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, from_email, [to_email], reply_to=reply_to ) else: c['file_shared_type'] = _(u"directory") send_html_email(_(u'A directory is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, from_email, [to_email], reply_to=reply_to) send_success.append(to_email) except Exception: send_failed.append(to_email) if len(send_success) > 0: data = json.dumps({"send_success": send_success, "send_failed": send_failed}) return HttpResponse(data, status=200, content_type=content_type) else: data = json.dumps({"error": _("Internal server error, or please check the email(s) you entered")}) return HttpResponse(data, status=400, content_type=content_type) else: return HttpResponseBadRequest(json.dumps(form.errors), content_type=content_type)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({ 'error': _(u'Sending shared link failed. Email service is not properly configured, please contact administrator.' ) }) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] file_shared_name = form.cleaned_data['file_shared_name'] file_shared_type = form.cleaned_data['file_shared_type'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) send_success, send_failed = [], [] for to_email in to_email_list: if not is_valid_email(to_email): send_failed.append(to_email) continue # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'file_shared_name': file_shared_name, } if extra_msg: c['extra_msg'] = extra_msg if REPLACE_FROM_EMAIL: from_email = request.user.username else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = request.user.username else: reply_to = None try: if file_shared_type == 'f': c['file_shared_type'] = _(u"file") send_html_email(_(u'A file is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, from_email, [to_email], reply_to=reply_to) else: c['file_shared_type'] = _(u"directory") send_html_email(_(u'A directory is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, from_email, [to_email], reply_to=reply_to) send_success.append(to_email) except Exception: send_failed.append(to_email) if len(send_success) > 0: data = json.dumps({ "send_success": send_success, "send_failed": send_failed }) return HttpResponse(data, status=200, content_type=content_type) else: data = json.dumps({ "error": _("Internal server error, or please check the email(s) you entered" ) }) return HttpResponse(data, status=400, content_type=content_type) else: return HttpResponseBadRequest(json.dumps(form.errors), content_type=content_type)
fs.username = request.user.username fs.repo_id = repo_id fs.path = path fs.token = token try: fs.save() except IntegrityError, e: return api_err(request, '501') domain = RequestSite(request).domain file_shared_link = 'http://%s%sf/%s/' % (domain, settings.SITE_ROOT, token) t = loader.get_template('shared_link_email.html') to_email_list = string2list(emails) for to_email in to_email_list: mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, } try: send_mail('您的好友通过SeaCloud分享了一个文件给您', t.render(Context(c)), None, [to_email], fail_silently=False) except: return api_error(request, '502') return HttpResponse(json.dumps(file_shared_link), status=200, content_type=json_content_type)
def share_repo(request): """ Handle POST method to share a repo to public/groups/users based on form data. Return to ``myhome`` page and notify user whether success or failure. """ next = request.META.get('HTTP_REFERER', None) if not next: next = SITE_ROOT form = RepoShareForm(request.POST) if not form.is_valid(): # TODO: may display error msg on form raise Http404 email_or_group = form.cleaned_data['email_or_group'] repo_id = form.cleaned_data['repo_id'] permission = form.cleaned_data['permission'] repo = seafile_api.get_repo(repo_id) if not repo: raise Http404 # Test whether user is the repo owner. username = request.user.username if not seafile_api.is_repo_owner(username, repo_id) and \ not is_org_repo_owner(username, repo_id): msg = _(u'Only the owner of the library has permission to share it.') messages.error(request, msg) return HttpResponseRedirect(next) # Parsing input values. share_to_all, share_to_groups, share_to_users = False, [], [] user_groups = request.user.joined_groups share_to_list = string2list(email_or_group) for share_to in share_to_list: if share_to == 'all': share_to_all = True elif share_to.find('@') == -1: for user_group in user_groups: if user_group.group_name == share_to: share_to_groups.append(user_group) else: share_to = share_to.lower() if is_valid_username(share_to): share_to_users.append(share_to) if share_to_all: share_to_public(request, repo, permission) if not check_user_share_quota(username, repo, users=share_to_users, groups=share_to_groups): messages.error(request, _('Failed to share "%s", no enough quota. <a href="http://seafile.com/">Upgrade account.</a>') % repo.name) return HttpResponseRedirect(next) for group in share_to_groups: share_to_group(request, repo, group, permission) for email in share_to_users: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=email) share_to_user(request, repo, email, permission) return HttpResponseRedirect(next)
def org_repo_share(request, url_prefix): """ Share org repo to members or groups in current org. """ if request.method != "POST": raise Http404 org = get_user_current_org(request.user.username, url_prefix) if not org: return HttpResponseRedirect(reverse(myhome)) form = RepoShareForm(request.POST) if not form.is_valid(): # TODO: may display error msg on form raise Http404 email_or_group = form.cleaned_data["email_or_group"] repo_id = form.cleaned_data["repo_id"] permission = form.cleaned_data["permission"] from_email = request.user.username # Test whether user is the repo owner if not validate_org_repo_owner(org.org_id, repo_id, request.user.username): return render_permission_error( request, _(u"Only the owner of this library has permission to share it."), extra_ctx={"org": org, "base_template": "org_base.html"}, ) share_to_list = string2list(email_or_group) for share_to in share_to_list: if share_to == "all": """ Share to public """ try: seafserv_threaded_rpc.set_org_inner_pub_repo(org.org_id, repo_id, permission) except: msg = _(u"Failed to share to all members") messages.add_message(request, messages.ERROR, msg) continue msg = _(u'Shared to all members successfully, you can go check it at <a href="%s">Share</a>.') % ( reverse("org_shareadmin", args=[org.url_prefix]) ) messages.add_message(request, messages.INFO, msg) elif share_to.find("@") == -1: """ Share repo to group """ # TODO: if we know group id, then we can simplly call group_share_repo group_name = share_to # Get all org groups. groups = get_org_groups(org.org_id, -1, -1) find = False for group in groups: # for every group that user joined, if group name and # group creator matchs, then has finded the group if group.props.group_name == group_name: seafserv_threaded_rpc.add_org_group_repo(repo_id, org.org_id, group.id, from_email, permission) find = True msg = _( u'Shared to %(group)s successfully,you can go check it at <a href="%(share)s">Share</a>.' ) % {"group": group_name, "share": reverse("org_shareadmin", args=[org.url_prefix])} messages.add_message(request, messages.INFO, msg) break if not find: msg = _(u"Failed to share to %s.") % group_name messages.add_message(request, messages.ERROR, msg) else: """ Share repo to user """ # Test whether share_to is in this org if not org_user_exists(org.org_id, share_to): msg = _(u"Failed to share to %s: this user does not exist in the organization.") % share_to messages.add_message(request, messages.ERROR, msg) continue # Record share info to db. try: seafserv_threaded_rpc.add_share(repo_id, from_email, share_to, permission) msg = _( u'Shared to %(share_to)s successfully,you can go check it at <a href="%(share)s">Share</a>.' ) % {"share_to": share_to, "share": reverse("org_shareadmin", args=[org.url_prefix])} messages.add_message(request, messages.INFO, msg) except SearpcError, e: msg = _(u"Failed to share to %s.") % share_to messages.add_message(request, messages.ERROR, msg) continue
def share_repo(request): """ Handle POST method to share a repo to public/groups/users based on form data. Return to ``myhome`` page and notify user whether success or failure. """ next = request.META.get('HTTP_REFERER', None) if not next: next = SITE_ROOT form = RepoShareForm(request.POST) if not form.is_valid(): # TODO: may display error msg on form raise Http404 email_or_group = form.cleaned_data['email_or_group'] repo_id = form.cleaned_data['repo_id'] permission = form.cleaned_data['permission'] repo = seafile_api.get_repo(repo_id) if not repo: raise Http404 # Test whether user is the repo owner. username = request.user.username if not seafile_api.is_repo_owner(username, repo_id) and \ not is_org_repo_owner(username, repo_id): msg = _(u'Only the owner of the library has permission to share it.') messages.error(request, msg) return HttpResponseRedirect(next) # Parsing input values. share_to_all, share_to_groups, share_to_users = False, [], [] user_groups = request.user.joined_groups share_to_list = string2list(email_or_group) for share_to in share_to_list: if share_to == 'all': share_to_all = True elif share_to.find('@') == -1: for user_group in user_groups: if user_group.group_name == share_to: share_to_groups.append(user_group) else: share_to = share_to.lower() if is_valid_username(share_to): share_to_users.append(share_to) if share_to_all: share_to_public(request, repo, permission) if not check_user_share_quota( username, repo, users=share_to_users, groups=share_to_groups): messages.error( request, _('Failed to share "%s", no enough quota. <a href="http://seafile.com/">Upgrade account.</a>' ) % repo.name) return HttpResponseRedirect(next) for group in share_to_groups: share_to_group(request, repo, group, permission) for email in share_to_users: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=email) share_to_user(request, repo, email, permission) return HttpResponseRedirect(next)
def share_repo(request): """ Handle POST method to share a repo to public/groups/users based on form data. Return to ``myhome`` page and notify user whether success or failure. """ next = request.META.get('HTTP_REFERER', None) if not next: next = settings.SITE_ROOT form = RepoShareForm(request.POST) if not form.is_valid(): # TODO: may display error msg on form raise Http404 email_or_group = form.cleaned_data['email_or_group'] repo_id = form.cleaned_data['repo_id'] permission = form.cleaned_data['permission'] from_email = request.user.username repo = seafile_api.get_repo(repo_id) if not repo: raise Http404 # Test whether user is the repo owner. if not validate_owner(request, repo_id): msg = _(u'Only the owner of the library has permission to share it.') messages.error(request, msg) return HttpResponseRedirect(next) # Parsing input values. share_to_list = string2list(email_or_group) share_to_all, share_to_group_names, share_to_users = False, [], [] for share_to in share_to_list: if share_to == 'all': share_to_all = True elif share_to.find('@') == -1: share_to_group_names.append(share_to) else: share_to_users.append(share_to) share_to_groups = [] # get all personal groups for group in seaserv.get_personal_groups_by_user(from_email): # for every group that user joined, if group name matchs, # then has find the group if group.group_name in share_to_group_names: share_to_groups.append(group) if share_to_all and not CLOUD_MODE: share_to_public(request, repo, permission) if not check_user_share_quota(from_email, repo, users=share_to_users, groups=share_to_groups): messages.error(request, _('Failed to share "%s", no enough quota. <a href="http://seafile.com/">Upgrade account.</a>') % repo.name) return HttpResponseRedirect(next) for group in share_to_groups: share_to_group(request, repo, from_email, group, permission) for email in share_to_users: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=email) share_to_user(request, repo, from_email, email, permission) return HttpResponseRedirect(next)
def post(self, request, group_id): """ Bulk add group members. """ username = request.user.username try: if not is_group_admin_or_owner(group_id, username): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) emails_str = request.data.get('emails', '') emails_list = string2list(emails_str) emails_list = [x.lower() for x in emails_list] result = {} result['failed'] = [] result['success'] = [] emails_need_add = [] org_id = None if is_org_context(request): org_id = request.user.org.org_id for email in emails_list: try: User.objects.get(email=email) except User.DoesNotExist: result['failed'].append({ 'email': email, 'error_msg': 'User %s not found.' % email }) continue if seaserv.is_group_user(group_id, email): result['failed'].append({ 'email': email, 'error_msg': _(u'User %s is already a group member.') % email }) continue # Can only invite organization users to group if org_id and not \ seaserv.ccnet_threaded_rpc.org_user_exists(org_id, email): result['failed'].append({ 'email': email, 'error_msg': _(u'User %s not found in organization.') % email }) continue emails_need_add.append(email) # Add user to group. for email in emails_need_add: try: seaserv.ccnet_threaded_rpc.group_add_member(group_id, username, email) member_info = get_group_member_info(request, group_id, email) result['success'].append(member_info) except SearpcError as e: logger.error(e) result['failed'].append({ 'email': email, 'error_msg': 'Internal Server Error' }) return Response(result)
def group_manage(request, group_id): group_id = int(group_id) # Checked by URL Conf group = get_group(group_id) if not group: return HttpResponseRedirect(reverse("group_list", args=[])) user = request.user.username if request.method == "POST": """ Add group members. """ result = {} content_type = "application/json; charset=utf-8" member_name_str = request.POST.get("user_name", "") member_list = string2list(member_name_str) # Add users to contacts. for email in member_list: mail_sended.send(sender=None, user=user, email=email) mail_sended_list = [] if request.cloud_mode: if request.user.org: # Can only invite org users to group. org_id = request.user.org["org_id"] for email in member_list: if not ccnet_threaded_rpc.org_user_exists(org_id, email): err_msg = _(u"Failed to add, %s is not in current organization.") % email result["error"] = err_msg return HttpResponse(json.dumps(result), status=400, content_type=content_type) else: try: ccnet_threaded_rpc.group_add_member(group_id, user, email) except SearpcError, e: result["error"] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type) else: # Can invite unregistered user to group. for email in member_list: if not is_registered_user(email): use_https = request.is_secure() domain = RequestSite(request).domain t = loader.get_template("group/add_member_email.html") c = { "email": user, "to_email": email, "group": group, "domain": domain, "protocol": use_https and "https" or "http", "site_name": SITE_NAME, } try: send_mail( _(u"Your friend added you to a group in SeaCloud."), t.render(Context(c)), None, [email], fail_silently=False, ) mail_sended_list.append(email) except: data = json.dumps({"error": _(u"Failed to send mail.")}) return HttpResponse(data, status=500, content_type=content_type) # Add user to group, unregistered user will see the group # when he logs in. try: ccnet_threaded_rpc.group_add_member(group_id, user, email) except SearpcError, e: result["error"] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type)
def get(self, request, repo_id, format=None): repo = seafile_api.get_repo(repo_id) if not repo: error_msg = 'Library %s not found.' % repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) # argument checking parent_dir = request.GET.get('parent_dir', None) dirent_name_string = request.GET.get('dirents', None) if not parent_dir: error_msg = 'parent_dir invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not dirent_name_string: error_msg = 'dirents invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # folder exist checking if not seafile_api.get_dir_id_by_path(repo_id, parent_dir): error_msg = 'Folder %s not found.' % parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission checking if check_folder_permission(request, repo_id, parent_dir) is None: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) dirent_name_list = string2list(dirent_name_string) dirent_list = [] total_size = 0 for dirent_name in dirent_name_list: dirent_name = dirent_name.strip('/') dirent_list.append(dirent_name) full_dirent_path = posixpath.join(parent_dir, dirent_name) current_dirent = seafile_api.get_dirent_by_path(repo_id, full_dirent_path) if stat.S_ISDIR(current_dirent.mode): total_size += seafile_api.get_dir_size(repo.store_id, repo.version, current_dirent.obj_id) else: total_size += current_dirent.size if total_size > seaserv.MAX_DOWNLOAD_DIR_SIZE: error_msg = _('Total size exceeds limit.') return api_error(status.HTTP_400_BAD_REQUEST, error_msg) fake_obj_id = {} fake_obj_id['file_list'] = dirent_list fake_obj_id['parent_dir'] = parent_dir username = request.user.username try: token = seafile_api.get_fileserver_access_token(repo_id, json.dumps(fake_obj_id), 'download-multi', username, False) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if len(dirent_list) > 10: send_file_access_msg(request, repo, parent_dir, 'web') else: for dirent_name in dirent_list: full_dirent_path = posixpath.join(parent_dir, dirent_name) send_file_access_msg(request, repo, full_dirent_path, 'web') download_url = '%s/files/%s' % (get_fileserver_root(), token) return Response({'url': download_url})
def ajax_private_share_dir(request): content_type = 'application/json; charset=utf-8' repo_id = request.POST.get('repo_id', '') path = request.POST.get('path', '') username = request.user.username result = {} repo = seafile_api.get_repo(repo_id) if not repo: result['error'] = _(u'Library does not exist.') return HttpResponse(json.dumps(result), status=400, content_type=content_type) if seafile_api.get_dir_id_by_path(repo_id, path) is None: result['error'] = _(u'Directory does not exist.') return HttpResponse(json.dumps(result), status=400, content_type=content_type) if path != '/': # if share a dir, check sub-repo first try: if is_org_context(request): org_id = request.user.org.org_id sub_repo = seaserv.seafserv_threaded_rpc.get_org_virtual_repo( org_id, repo_id, path, username) else: sub_repo = seafile_api.get_virtual_repo(repo_id, path, username) except SearpcError as e: result['error'] = e.msg return HttpResponse(json.dumps(result), status=500, content_type=content_type) if not sub_repo: name = os.path.basename(path) # create a sub-lib try: # use name as 'repo_name' & 'repo_desc' for sub_repo if is_org_context(request): org_id = request.user.org.org_id sub_repo_id = seaserv.seafserv_threaded_rpc.create_org_virtual_repo( org_id, repo_id, path, name, name, username) else: sub_repo_id = seafile_api.create_virtual_repo(repo_id, path, name, name, username) sub_repo = seafile_api.get_repo(sub_repo_id) except SearpcError as e: result['error'] = e.msg return HttpResponse(json.dumps(result), status=500, content_type=content_type) shared_repo_id = sub_repo.id shared_repo = sub_repo else: shared_repo_id = repo_id shared_repo = repo emails_string = request.POST.get('emails', '') groups_string = request.POST.get('groups', '') perm = request.POST.get('perm', '') emails = string2list(emails_string) groups = string2list(groups_string) # Test whether user is the repo owner. if not seafile_api.is_repo_owner(username, shared_repo_id) and \ not is_org_repo_owner(username, shared_repo_id): result['error'] = _(u'Only the owner of the library has permission to share it.') return HttpResponse(json.dumps(result), status=500, content_type=content_type) # Parsing input values. # no 'share_to_all' share_to_groups, share_to_users, shared_success, shared_failed = [], [], [], [] for email in emails: email = email.lower() if is_valid_username(email): share_to_users.append(email) else: shared_failed.append(email) for group_id in groups: share_to_groups.append(seaserv.get_group(group_id)) for email in share_to_users: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=email) if share_to_user(request, shared_repo, email, perm): shared_success.append(email) else: shared_failed.append(email) for group in share_to_groups: if share_to_group(request, shared_repo, group, perm): shared_success.append(group.group_name) else: shared_failed.append(group.group_name) if len(shared_success) > 0: return HttpResponse(json.dumps({ "shared_success": shared_success, "shared_failed": shared_failed }), content_type=content_type) else: # for case: only share to users and the emails are not valid data = json.dumps({"error": _("Please check the email(s) you entered")}) return HttpResponse(data, status=400, content_type=content_type)
def share_repo(request): """ Handle POST method to share a repo to public/groups/users based on form data. Return to ``myhome`` page and notify user whether success or failure. """ form = RepoShareForm(request.POST) if not form.is_valid(): # TODO: may display error msg on form raise Http404 email_or_group = form.cleaned_data['email_or_group'] repo_id = form.cleaned_data['repo_id'] permission = form.cleaned_data['permission'] from_email = request.user.username repo = get_repo(repo_id) if not repo: raise Http404 # Test whether user is the repo owner. if not validate_owner(request, repo_id): msg = _(u'Only the owner of the library has permission to share it.') messages.error(request, msg) return HttpResponseRedirect(reverse('myhome')) # Parsing input values. share_to_list = string2list(email_or_group) share_to_all, share_to_group_names, share_to_users = False, [], [] for share_to in share_to_list: if share_to == 'all': share_to_all = True elif share_to.find('@') == -1: share_to_group_names.append(share_to) else: share_to_users.append(share_to) share_to_groups = [] # get all personal groups for group in seaserv.get_personal_groups_by_user(from_email): # for every group that user joined, if group name matchs, # then has find the group if group.group_name in share_to_group_names: share_to_groups.append(group) if share_to_all and not CLOUD_MODE: share_to_public(request, repo, permission) if not check_user_share_quota( from_email, repo, users=share_to_users, groups=share_to_groups): messages.error( request, _('Failed to share "%s", no enough quota. <a href="http://seafile.com/">Upgrade account.</a>' ) % repo.name) return HttpResponseRedirect(reverse('myhome')) for group in share_to_groups: share_to_group(request, repo, from_email, group, permission) for email in share_to_users: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=email) share_to_user(request, repo, from_email, email, permission) return HttpResponseRedirect(reverse('myhome'))
def share_repo(request): """ Handle POST method to share a repo to public/groups/users based on form data. Return to ``myhome`` page and notify user whether success or failure. """ next = request.META.get('HTTP_REFERER', None) if not next: next = SITE_ROOT form = RepoShareForm(request.POST) if not form.is_valid(): # TODO: may display error msg on form raise Http404 email_or_group = form.cleaned_data['email_or_group'] repo_id = form.cleaned_data['repo_id'] permission = form.cleaned_data['permission'] repo = seafile_api.get_repo(repo_id) if not repo: raise Http404 # Test whether user is the repo owner. username = request.user.username if not seafile_api.is_repo_owner(username, repo_id) and \ not is_org_repo_owner(username, repo_id): msg = _(u'Only the owner of the library has permission to share it.') messages.error(request, msg) return HttpResponseRedirect(next) # Parsing input values. share_to_all, share_to_groups, share_to_users = False, [], [] user_groups = request.user.joined_groups share_to_list = string2list(email_or_group) for share_to in share_to_list: if share_to == 'all': share_to_all = True elif share_to.find('@') == -1: for user_group in user_groups: if user_group.group_name == share_to: share_to_groups.append(user_group) else: share_to = share_to.lower() if is_valid_username(share_to): share_to_users.append(share_to) origin_repo_id, origin_path = get_origin_repo_info(repo.id) if origin_repo_id is not None: perm_repo_id = origin_repo_id perm_path = origin_path else: perm_repo_id = repo.id perm_path = '/' if share_to_all: share_to_public(request, repo, permission) send_perm_audit_msg('add-repo-perm', username, 'all', \ perm_repo_id, perm_path, permission) for group in share_to_groups: if share_to_group(request, repo, group, permission): send_perm_audit_msg('add-repo-perm', username, group.id, \ perm_repo_id, perm_path, permission) for email in share_to_users: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=email) if share_to_user(request, repo, email, permission): send_perm_audit_msg('add-repo-perm', username, email, \ perm_repo_id, perm_path, permission) return HttpResponseRedirect(next)
def post(self, request, group_id): """ Bulk add group members. """ username = request.user.username try: if not is_group_admin_or_owner(group_id, username): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) emails_str = request.data.get('emails', '') emails_list = string2list(emails_str) emails_list = [x.lower() for x in emails_list] result = {} result['failed'] = [] result['success'] = [] emails_need_add = [] org_id = None if is_org_context(request): org_id = request.user.org.org_id for email in emails_list: try: User.objects.get(email=email) except User.DoesNotExist: result['failed'].append({ 'email': email, 'error_msg': 'User %s not found.' % email }) continue if seaserv.is_group_user(group_id, email): result['failed'].append({ 'email': email, 'error_msg': _(u'User %s is already a group member.') % email }) continue # Can only invite organization users to group if org_id and not \ seaserv.ccnet_threaded_rpc.org_user_exists(org_id, email): result['failed'].append({ 'email': email, 'error_msg': _(u'User %s not found in organization.') % email }) continue emails_need_add.append(email) # Add user to group. for email in emails_need_add: try: seaserv.ccnet_threaded_rpc.group_add_member( group_id, username, email) member_info = get_group_member_info(request, group_id, email) result['success'].append(member_info) except SearpcError as e: logger.error(e) result['failed'].append({ 'email': email, 'error_msg': 'Internal Server Error' }) return Response(result)
def send_shared_upload_link(request): """ Handle ajax post request to send shared upload link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({ 'error': _('Failed to send email, email service is not properly configured, please contact administrator.' ) }) return HttpResponse(data, status=500, content_type=content_type) form = UploadLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] shared_upload_link = form.cleaned_data['shared_upload_link'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) send_success, send_failed = [], [] # use contact_email, if present username = Profile.objects.get_contact_email_by_user( request.user.username) for to_email in to_email_list: if not is_valid_email(to_email): send_failed.append(to_email) continue # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'shared_upload_link': shared_upload_link, } if extra_msg: c['extra_msg'] = extra_msg if REPLACE_FROM_EMAIL: from_email = username else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = username else: reply_to = None try: send_html_email(_('An upload link is shared to you on %s') % get_site_name(), 'shared_upload_link_email.html', c, from_email, [to_email], reply_to=reply_to) send_success.append(to_email) except Exception: send_failed.append(to_email) if len(send_success) > 0: data = json.dumps({ "send_success": send_success, "send_failed": send_failed }) return HttpResponse(data, status=200, content_type=content_type) else: data = json.dumps({ "error": _("Internal server error, or please check the email(s) you entered" ) }) return HttpResponse(data, status=400, content_type=content_type) else: return HttpResponseBadRequest(json.dumps(form.errors), content_type=content_type)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({ 'error': _(u'Sending shared link failed. Email service is not properly configured, please contact administrator.' ) }) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] file_shared_name = form.cleaned_data['file_shared_name'] file_shared_type = form.cleaned_data['file_shared_type'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'file_shared_name': file_shared_name, } if extra_msg: c['extra_msg'] = extra_msg try: if file_shared_type == 'f': c['file_shared_type'] = "file" send_html_email( _(u'A file is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, None, [to_email]) else: c['file_shared_type'] = "directory" send_html_email( _(u'A directory is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, None, [to_email]) except Exception, e: logger.error(str(e)) data = json.dumps( {'error': _(u'Internal server error. Send failed.')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps({"msg": _(u'Successfully sent.')}) return HttpResponse(data, status=200, content_type=content_type)
def ajax_private_share_dir(request): content_type = 'application/json; charset=utf-8' repo_id = request.POST.get('repo_id', '') path = request.POST.get('path', '') username = request.user.username result = {} repo = seafile_api.get_repo(repo_id) if not repo: result['error'] = _(u'Library does not exist.') return HttpResponse(json.dumps(result), status=400, content_type=content_type) if seafile_api.get_dir_id_by_path(repo_id, path) is None: result['error'] = _(u'Directory does not exist.') return HttpResponse(json.dumps(result), status=400, content_type=content_type) if path != '/': # if share a dir, check sub-repo first try: if is_org_context(request): org_id = request.user.org.org_id sub_repo = seaserv.seafserv_threaded_rpc.get_org_virtual_repo( org_id, repo_id, path, username) else: sub_repo = seafile_api.get_virtual_repo( repo_id, path, username) except SearpcError as e: result['error'] = e.msg return HttpResponse(json.dumps(result), status=500, content_type=content_type) if not sub_repo: name = os.path.basename(path) # create a sub-lib try: # use name as 'repo_name' & 'repo_desc' for sub_repo if is_org_context(request): org_id = request.user.org.org_id sub_repo_id = seaserv.seafserv_threaded_rpc.create_org_virtual_repo( org_id, repo_id, path, name, name, username) else: sub_repo_id = seafile_api.create_virtual_repo( repo_id, path, name, name, username) sub_repo = seafile_api.get_repo(sub_repo_id) except SearpcError as e: result['error'] = e.msg return HttpResponse(json.dumps(result), status=500, content_type=content_type) shared_repo_id = sub_repo.id shared_repo = sub_repo else: shared_repo_id = repo_id shared_repo = repo emails_string = request.POST.get('emails', '') groups_string = request.POST.get('groups', '') perm = request.POST.get('perm', '') emails = string2list(emails_string) groups = string2list(groups_string) # Test whether user is the repo owner. if not seafile_api.is_repo_owner(username, shared_repo_id) and \ not is_org_repo_owner(username, shared_repo_id): result['error'] = _( u'Only the owner of the library has permission to share it.') return HttpResponse(json.dumps(result), status=500, content_type=content_type) # Parsing input values. # no 'share_to_all' share_to_groups, share_to_users, shared_success, shared_failed = [], [], [], [] for email in emails: email = email.lower() if is_valid_username(email): share_to_users.append(email) else: shared_failed.append(email) for group_id in groups: share_to_groups.append(seaserv.get_group(group_id)) for email in share_to_users: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=email) if share_to_user(request, shared_repo, email, perm): shared_success.append(email) else: shared_failed.append(email) for group in share_to_groups: if share_to_group(request, shared_repo, group, perm): shared_success.append(group.group_name) else: shared_failed.append(group.group_name) if len(shared_success) > 0: return HttpResponse(json.dumps({ "shared_success": shared_success, "shared_failed": shared_failed }), content_type=content_type) else: # for case: only share to users and the emails are not valid data = json.dumps( {"error": _("Please check the email(s) you entered")}) return HttpResponse(data, status=400, content_type=content_type)
def org_admin(request, url_prefix): """ List and add org users. """ if request.method == "POST": emails = request.POST.get("added-member-name") email_list = string2list(emails) for email in email_list: if not email or email.find("@") <= 0: continue org_id = request.user.org["org_id"] try: User.objects.get(email=email) email = email.strip(" ") org_id = request.user.org["org_id"] add_org_user(org_id, email, 0) # send signal org_user_added.send(sender=None, org_id=org_id, from_email=request.user.username, to_email=email) except User.DoesNotExist: # User is not registered, just create account and # add that account to org password = gen_token(max_length=6) if Site._meta.installed: site = Site.objects.get_current() else: site = RequestSite(request) RegistrationProfile.objects.create_active_user(email, email, password, site, send_email=False) add_org_user(org_id, email, 0) if hasattr(seahub_settings, "EMAIL_HOST"): org_name = request.user.org["org_name"] send_org_user_add_mail(request, email, password, org_name) # Make sure page request is an int. If not, deliver first page. try: current_page = int(request.GET.get("page", "1")) per_page = int(request.GET.get("per_page", "25")) except ValueError: current_page = 1 per_page = 25 users_plus_one = get_org_users_by_url_prefix(url_prefix, per_page * (current_page - 1), per_page + 1) if len(users_plus_one) == per_page + 1: page_next = True else: page_next = False org = get_user_current_org(request.user.username, url_prefix) if not org: return HttpResponseRedirect(reverse(myhome)) users = users_plus_one[:per_page] for user in users: if user.props.id == request.user.id: user.is_self = True try: user.quota_usage = seafserv_threaded_rpc.get_org_user_quota_usage(org.org_id, user.email) except: user.quota_usage = -1 # My contacts contacts = Contact.objects.filter(user_email=request.user.username) org_quota_usage = seafserv_threaded_rpc.get_org_quota_usage(org.org_id) org_quota = seafserv_threaded_rpc.get_org_quota(org.org_id) return render_to_response( "organizations/org_admin.html", { "users": users, "contacts": contacts, "current_page": current_page, "prev_page": current_page - 1, "next_page": current_page + 1, "per_page": per_page, "page_next": page_next, "org_quota_usage": org_quota_usage, "org_quota": org_quota, }, context_instance=RequestContext(request), )
def share_repo(request): """ Handle repo share request """ if request.method != 'POST': raise Http404 form = RepoShareForm(request.POST) if not form.is_valid(): # TODO: may display error msg on form raise Http404 email_or_group = form.cleaned_data['email_or_group'] repo_id = form.cleaned_data['repo_id'] permission = form.cleaned_data['permission'] from_email = request.user.username repo = get_repo(repo_id) if not repo: raise Http404 is_encrypted = True if repo.encrypted else False # Test whether user is the repo owner. if not validate_owner(request, repo_id): return render_permission_error(request, _(u'Only the owner of the library has permission to share it.')) to_email_list = string2list(email_or_group) for to_email in to_email_list: if to_email == 'all': ''' Share to public ''' # ignore 'all' if we're running in cloud mode if not CLOUD_MODE: try: seafserv_threaded_rpc.set_inner_pub_repo(repo_id, permission) except: msg = _(u'Failed to share to all members') message.add_message(request, message.ERROR, msg) continue msg = _(u'Shared to all members successfully, go check it at <a href="%s">Share</a>.') % \ (reverse('share_admin')) messages.add_message(request, messages.INFO, msg) elif to_email.find('@') == -1: ''' Share repo to group ''' # TODO: if we know group id, then we can simplly call group_share_repo group_name = to_email # get all personal groups groups = get_personal_groups(-1, -1) find = False for group in groups: # for every group that user joined, if group name matchs, # then has find the group if group.props.group_name == group_name: from seahub.group.views import group_share_repo group_share_repo(request, repo_id, int(group.props.id), from_email, permission) find = True msg = _(u'Shared to %(group)s successfully,go check it at <a href="%(share)s">Share</a>.') % \ {'group':group_name, 'share':reverse('share_admin')} messages.add_message(request, messages.INFO, msg) break if not find: msg = _(u'Failed to share to %s,as it does not exists.') % group_name messages.add_message(request, messages.ERROR, msg) else: ''' Share repo to user ''' # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) if not is_registered_user(to_email): # Generate shared link and send mail if user has not registered. # kwargs = {'repo_id': repo_id, # 'repo_owner': from_email, # 'anon_email': to_email, # 'is_encrypted': is_encrypted, # } # anonymous_share(request, **kwargs) msg = _(u'Failed to share to %s, as the email is not registered.') % to_email messages.add_message(request, messages.ERROR, msg) continue else: # Record share info to db. try: seafserv_threaded_rpc.add_share(repo_id, from_email, to_email, permission) except SearpcError, e: msg = _(u'Failed to share to %s .') % to_email messages.add_message(request, messages.ERROR, msg) continue msg = _(u'Shared to %(email)s successfully,go check it at <a href="%(share)s">Share</a>.') % \ {'email':to_email, 'share':reverse('share_admin')} messages.add_message(request, messages.INFO, msg)
def group_manage(request, group_id): group_id = int(group_id) # Checked by URL Conf group = get_group(group_id) if not group: return HttpResponseRedirect(reverse('group_list', args=[])) user = request.user.username if request.method == 'POST': """ Add group members. """ result = {} content_type = 'application/json; charset=utf-8' member_name_str = request.POST.get('user_name', '') member_list = string2list(member_name_str) # Add users to contacts. for email in member_list: mail_sended.send(sender=None, user=user, email=email) mail_sended_list = [] if request.cloud_mode: if request.user.org: # Can only invite org users to group. org_id = request.user.org['org_id'] for email in member_list: if not ccnet_threaded_rpc.org_user_exists(org_id, email): err_msg = _(u'Failed to add, %s is not in current organization.') % email result['error'] = err_msg return HttpResponse(json.dumps(result), status=400, content_type=content_type) else: try: ccnet_threaded_rpc.group_add_member(group_id, user, email) except SearpcError, e: result['error'] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type) else: # Can invite unregistered user to group. for email in member_list: if not is_registered_user(email): use_https = request.is_secure() domain = RequestSite(request).domain t = loader.get_template('group/add_member_email.html') c = { 'email': user, 'to_email': email, 'group': group, 'domain': domain, 'protocol': use_https and 'https' or 'http', 'site_name': SITE_NAME, } try: send_mail(_(u'Your friend added you to a group at Seafile.'), t.render(Context(c)), None, [email], fail_silently=False) mail_sended_list.append(email) except: data = json.dumps({'error': _(u'Failed to send mail.')}) return HttpResponse(data, status=500, content_type=content_type) # Add user to group, unregistered user will see the group # when he logs in. try: ccnet_threaded_rpc.group_add_member(group_id, user, email) except SearpcError, e: result['error'] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type)