def user_set_quota(request, email): if request.method != 'POST': raise Http404 content_type = 'application/json; charset=utf-8' result = {} f = SetUserQuotaForm(request.POST) if f.is_valid(): space_quota_mb = f.cleaned_data['space_quota'] space_quota = space_quota_mb * get_file_size_unit('MB') org = ccnet_api.get_orgs_by_user(email) try: if not org: seafile_api.set_user_quota(email, space_quota) else: org_id = org[0].org_id org_quota_mb = seafserv_threaded_rpc.get_org_quota(org_id) / get_file_size_unit('MB') if space_quota_mb > org_quota_mb: result['error'] = _('Failed to set quota: maximum quota is %d MB' % \ org_quota_mb) return HttpResponse(json.dumps(result), status=400, content_type=content_type) else: seafile_api.set_org_user_quota(org_id, email, space_quota) except: result['error'] = _('Failed to set quota: internal server error') return HttpResponse(json.dumps(result), status=500, content_type=content_type) result['success'] = True return HttpResponse(json.dumps(result), content_type=content_type) else: result['error'] = str(list(f.errors.values())[0]) return HttpResponse(json.dumps(result), status=400, content_type=content_type)
def update_user_info(request, user, password, is_active, is_staff, role, nickname, login_id, contact_email, reference_id, quota_total_mb, institution_name): # update basic user info if is_active is not None: user.is_active = is_active if password: user.set_password(password) if is_staff is not None: user.is_staff = is_staff # update user user.save() email = user.username # update additional user info if is_pro_version() and role: User.objects.update_role(email, role) if nickname is not None: Profile.objects.add_or_update(email, nickname) key = normalize_cache_key(nickname, NICKNAME_CACHE_PREFIX) cache.set(key, nickname, NICKNAME_CACHE_TIMEOUT) if login_id is not None: Profile.objects.add_or_update(email, login_id=login_id) if contact_email is not None: Profile.objects.add_or_update(email, contact_email=contact_email) key = normalize_cache_key(email, CONTACT_CACHE_PREFIX) cache.set(key, contact_email, CONTACT_CACHE_TIMEOUT) if reference_id is not None: if reference_id.strip(): ccnet_api.set_reference_id(email, reference_id.strip()) else: # remove reference id ccnet_api.set_reference_id(email, None) if institution_name is not None: Profile.objects.add_or_update(email, institution=institution_name) if institution_name == '': InstitutionAdmin.objects.filter(user=email).delete() if quota_total_mb: quota_total = int(quota_total_mb) * get_file_size_unit('MB') orgs = ccnet_api.get_orgs_by_user(email) try: if orgs: org_id = orgs[0].org_id seafile_api.set_org_user_quota(org_id, email, quota_total) else: seafile_api.set_user_quota(email, quota_total) except Exception as e: logger.error(e) seafile_api.set_user_quota(email, -1)
def update_user_info(request): email = request.data.get("email") user = User.objects.get(email=email) # update basic user info password = request.data.get("password") if password: user.set_password(password) is_staff = request.data.get("is_staff") if is_staff: is_staff = to_python_boolean(is_staff) user.is_staff = is_staff is_active = request.data.get("is_active") if is_active: is_active = to_python_boolean(is_active) user.is_active = is_active # update user user.save() # update additional user info if is_pro_version(): role = request.data.get("role") if role: User.objects.update_role(email, role) name = request.data.get("name") if name: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.nickname = name profile.save() department = request.data.get("department") if department: d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email) if d_profile is None: d_profile = DetailedProfile(user=email) d_profile.department = department d_profile.save() quota_total_mb = request.data.get("quota_total") if quota_total_mb: quota_total = int(quota_total_mb) * get_file_size_unit('MB') if is_org_context(request): org_id = request.user.org.org_id seafile_api.set_org_user_quota(org_id, email, quota_total) else: seafile_api.set_user_quota(email, quota_total)
def create_user_info(request, email, role, nickname, contact_email, quota_total_mb): # update additional user info if is_pro_version() and role: User.objects.update_role(email, role) if nickname is not None: Profile.objects.add_or_update(email, nickname) key = normalize_cache_key(nickname, NICKNAME_CACHE_PREFIX) cache.set(key, nickname, NICKNAME_CACHE_TIMEOUT) if contact_email is not None: Profile.objects.add_or_update(email, contact_email=contact_email) key = normalize_cache_key(email, CONTACT_CACHE_PREFIX) cache.set(key, contact_email, CONTACT_CACHE_TIMEOUT) if quota_total_mb: quota_total = int(quota_total_mb) * get_file_size_unit('MB') if is_org_context(request): org_id = request.user.org.org_id seafile_api.set_org_user_quota(org_id, email, quota_total) else: seafile_api.set_user_quota(email, quota_total)
def update_user_info(request, user): # update basic user info password = request.data.get("password") if password: user.set_password(password) is_staff = request.data.get("is_staff") if is_staff: is_staff = to_python_boolean(is_staff) user.is_staff = is_staff is_active = request.data.get("is_active") if is_active: is_active = to_python_boolean(is_active) user.is_active = is_active # update user user.save() email = user.username # update additional user info if is_pro_version(): role = request.data.get("role") if role: User.objects.update_role(email, role) name = request.data.get("name") if name: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.nickname = name profile.save() # update account login_id login_id = request.data.get("login_id", None) if login_id is not None: login_id = login_id.strip() profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.login_id = None if login_id == "" else login_id profile.save() reference_id = request.data.get("reference_id", None) if reference_id is not None: reference_id = reference_id.strip() ccnet_api.set_reference_id(email, reference_id) department = request.data.get("department") if department: d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email) if d_profile is None: d_profile = DetailedProfile(user=email) d_profile.department = department d_profile.save() quota_total_mb = request.data.get("quota_total") if quota_total_mb: quota_total = int(quota_total_mb) * get_file_size_unit('MB') if is_org_context(request): org_id = request.user.org.org_id seafile_api.set_org_user_quota(org_id, email, quota_total) else: seafile_api.set_user_quota(email, quota_total)
def put(self, request, org_id, email): """ update name of an org user. Permission checking: 1. only admin can perform this action. """ # resource check org_id = int(org_id) if not ccnet_api.get_org_by_id(org_id): error_msg = 'Organization %s not found.' % org_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) try: user = User.objects.get(email=email) except User.DoesNotExist: error_msg = 'User %s not found.' % email return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check if not is_org_user(email, org_id): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # update user's name name = request.data.get("name", None) if name is not None: name = name.strip() if len(name) > 64: error_msg = 'Name is too long (maximum is 64 characters).' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if "/" in name: error_msg = "Name should not include '/'." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: Profile.objects.add_or_update(email, nickname=name) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) # update user's contact email contact_email = request.data.get("contact_email", None) if contact_email is not None: contact_email = contact_email.strip() if contact_email != '' and not is_valid_email(contact_email): error_msg = 'contact_email invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: Profile.objects.add_or_update(email, contact_email=contact_email) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) is_staff = request.data.get("is_staff", None) if is_staff is not None: try: is_staff = to_python_boolean(is_staff) except ValueError: error_msg = 'is_staff invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if is_staff: if is_org_staff(org_id, user.username): error_msg = '%s is already organization staff.' % email return api_error(status.HTTP_400_BAD_REQUEST, error_msg) set_org_staff(org_id, user.username) if not is_staff: if not is_org_staff(org_id, user.username): error_msg = '%s is not organization staff.' % email return api_error(status.HTTP_400_BAD_REQUEST, error_msg) unset_org_staff(org_id, user.username) quota_total_mb = request.data.get("quota_total", None) if quota_total_mb: try: quota_total_mb = int(quota_total_mb) except ValueError: error_msg = "Must be an integer that is greater than or equal to 0." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if quota_total_mb < 0: error_msg = "Space quota is too low (minimum value is 0)." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) org_quota = seafile_api.get_org_quota(org_id) org_quota_mb = org_quota / get_file_size_unit('MB') # -1 means org has unlimited quota if org_quota > 0 and quota_total_mb > org_quota_mb: error_msg = _(u'Failed to set quota: maximum quota is %d MB' % org_quota_mb) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) quota_total = int(quota_total_mb) * get_file_size_unit('MB') try: seafile_api.set_org_user_quota(org_id, email, quota_total) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) info = get_user_info(email, org_id) info['is_active'] = user.is_active info['id'] = user.id info['ctime'] = timestamp_to_isoformat_timestr(user.ctime) try: last_login = UserLastLogin.objects.get(username=user.email) info['last_login'] = datetime_to_isoformat_timestr( last_login.last_login) except UserLastLogin.DoesNotExist: info['last_login'] = None # these two fields are designed to be compatible with the old API info['self_usage'] = info.get('quota_usage') info['quota'] = info.get('quota_total') return Response(info)
def update_user_info(request, user): # update basic user info password = request.data.get("password") if password: user.set_password(password) is_staff = request.data.get("is_staff") if is_staff: is_staff = to_python_boolean(is_staff) user.is_staff = is_staff is_active = request.data.get("is_active") if is_active: is_active = to_python_boolean(is_active) user.is_active = is_active # update user user.save() email = user.username # update additional user info if is_pro_version(): role = request.data.get("role") if role: User.objects.update_role(email, role) nickname = request.data.get("name", None) if nickname is not None: Profile.objects.add_or_update(email, nickname) # update account login_id login_id = request.data.get("login_id", None) if login_id is not None: Profile.objects.add_or_update(email, login_id=login_id) # update account contact email contact_email = request.data.get('contact_email', None) if contact_email is not None: Profile.objects.add_or_update(email, contact_email=contact_email) key = normalize_cache_key(email, CONTACT_CACHE_PREFIX) cache.set(key, contact_email, CONTACT_CACHE_TIMEOUT) reference_id = request.data.get("reference_id", None) if reference_id is not None: if reference_id.strip(): ccnet_api.set_reference_id(email, reference_id.strip()) else: # remove reference id ccnet_api.set_reference_id(email, None) department = request.data.get("department") if department: d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email) if d_profile is None: d_profile = DetailedProfile(user=email) d_profile.department = department d_profile.save() quota_total_mb = request.data.get("quota_total") if quota_total_mb: quota_total = int(quota_total_mb) * get_file_size_unit('MB') if is_org_context(request): org_id = request.user.org.org_id seafile_api.set_org_user_quota(org_id, email, quota_total) else: seafile_api.set_user_quota(email, quota_total)
def put(self, request, org_id, email): """ update base info of a org user Permission checking: 1. only admin can perform this action. """ try: user = User.objects.get(email=email) except User.DoesNotExist: error_msg = 'User %s not found.' % email return api_error(status.HTTP_404_NOT_FOUND, error_msg) # update active active = request.data.get('active', None) if active: active = active.lower() if active not in ('true', 'false'): error_msg = "active invalid, should be 'true' or 'false'." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if active == 'true': user.is_active = True else: user.is_active = False try: # update user status result_code = user.save() except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if result_code == -1: error_msg = 'Fail to update user %s.' % email return api_error(status.HTTP_403_FORBIDDEN, error_msg) # update name name = request.data.get('name', None) if name: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.nickname = name profile.save() # update contact_email contact_email = request.data.get('contact_email', None) if contact_email: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.contact_email = contact_email profile.save() # update user quota user_quota_mb = request.data.get("quota_total", None) if user_quota_mb: try: user_quota_mb = int(user_quota_mb) except Exception as e: logger.error(e) error_msg = "quota_total invalid." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) user_quota = int(user_quota_mb) * get_file_size_unit('MB') org_quota = seafile_api.get_org_quota(org_id) # -1 means org has unlimited quota if org_quota > 0: org_quota_mb = org_quota / get_file_size_unit('MB') if user_quota_mb > org_quota_mb: error_msg = 'Failed to set quota: maximum quota is %d MB' % org_quota_mb return api_error(status.HTTP_400_BAD_REQUEST, error_msg) seafile_api.set_org_user_quota(org_id, email, user_quota) user_info = get_org_user_info(org_id, email) user_info['active'] = user.is_active return Response(user_info)
def put(self, request, org_id, email): """ update base info of a org user Permission checking: 1. only admin can perform this action. """ if not request.user.admin_permissions.other_permission(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') try: user = User.objects.get(email=email) except User.DoesNotExist: error_msg = 'User %s not found.' % email return api_error(status.HTTP_404_NOT_FOUND, error_msg) # update active active = request.data.get('active', None) if active: active = active.lower() if active not in ('true', 'false'): error_msg = "active invalid, should be 'true' or 'false'." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if active == 'true': user.is_active = True else: user.is_active = False try: # update user status result_code = user.save() except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if result_code == -1: error_msg = 'Fail to update user %s.' % email return api_error(status.HTTP_403_FORBIDDEN, error_msg) # update name name = request.data.get('name', None) if name: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.nickname = name profile.save() # update contact_email contact_email = request.data.get('contact_email', None) if contact_email: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.contact_email = contact_email profile.save() # update user quota user_quota_mb = request.data.get("quota_total", None) if user_quota_mb: try: user_quota_mb = int(user_quota_mb) except Exception as e: logger.error(e) error_msg = "quota_total invalid." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) user_quota = int(user_quota_mb) * get_file_size_unit('MB') org_quota = seafile_api.get_org_quota(org_id) # -1 means org has unlimited quota if org_quota > 0: org_quota_mb = org_quota / get_file_size_unit('MB') if user_quota_mb > org_quota_mb: error_msg = 'Failed to set quota: maximum quota is %d MB' % org_quota_mb return api_error(status.HTTP_400_BAD_REQUEST, error_msg) seafile_api.set_org_user_quota(org_id, email, user_quota) user_info = get_org_user_info(org_id, user) user_info['active'] = user.is_active return Response(user_info)