def make_profile(self, user, shib_meta): """ Extrat nickname(givenname surname), contact_email, institution from Shib attributs, and add those to user profile. """ # use `display_name` as nickname in shib_meta first nickname = shib_meta.get('display_name', None) if nickname is None: # otherwise, fallback to givenname plus surname in shib_meta givenname = shib_meta.get('givenname', '') surname = shib_meta.get('surname', '') nickname = "%s %s" % (givenname, surname) institution = shib_meta.get('institution', None) contact_email = shib_meta.get('contact_email', None) p = Profile.objects.get_profile_by_user(user.username) if not p: p = Profile(user=user.username) if nickname.strip(): # set nickname when it's not empty p.nickname = nickname if institution: p.institution = institution if contact_email: p.contact_email = contact_email p.save()
def update_user_profile(self, user_info): email = user_info.get('email', '') if not email: return name = user_info.get('name', '') institution = user_info.get('institution', '') contact_email = user_info.get('contact_email', '') profile = Profile.objects.get_profile_by_user(email) if not profile: profile = Profile(user=email) if name.strip(): # if have 'HTTP_DISPLAYNAME' header, # then use its value as user's name profile.nickname = name else: # or use values of "HTTP_GIVENNAME" and "HTTP_SN" headers # for shibboleth givenname = user_info.get('givenname', '') surname = user_info.get('surname', '') if givenname.strip() and surname.strip(): name = "%s %s" % (givenname, surname) profile.nickname = name if institution: profile.institution = institution if contact_email: profile.contact_email = contact_email profile.save()
def make_profile(self, user, shib_meta): """ Extrat nickname(givenname surname), contact_email, institution from Shib attributs, and add those to user profile. """ givenname = shib_meta.get("givenname", "") surname = shib_meta.get("surname", "") nickname = "%s %s" % (givenname, surname) institution = shib_meta.get("institution", None) contact_email = shib_meta.get("contact_email", None) p = Profile.objects.get_profile_by_user(user.username) if not p: p = Profile(user=user.username) p.nickname = nickname if institution: p.institution = institution if contact_email: p.contact_email = contact_email p.save()
def post(self, request): """ Set user quota, set user institution, delete users, in batch. Permission checking: 1. admin user. """ # argument check emails = request.POST.getlist('email', None) if not emails: error_msg = 'email invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) operation = request.POST.get('operation', None) if operation not in ('set-quota', 'delete-user', 'set-institution'): error_msg = "operation can only be 'set-quota', 'delete-user', or 'set-institution'." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) result = {} result['failed'] = [] result['success'] = [] existed_users = [] for email in emails: try: user = User.objects.get(email=email) existed_users.append(user) except User.DoesNotExist: result['failed'].append({ 'email': email, 'error_msg': 'User %s not found.' % email }) continue if operation == 'set-quota': quota_total_mb = request.POST.get('quota_total', None) if not quota_total_mb: error_msg = 'quota_total invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) 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) quota_total_byte = quota_total_mb * get_file_size_unit('MB') for user in existed_users: email = user.email try: seafile_api.set_user_quota(email, quota_total_byte) except Exception as e: logger.error(e) result['failed'].append({ 'email': email, 'error_msg': 'Internal Server Error' }) continue result['success'].append({ 'email': email, 'quota_total': seafile_api.get_user_quota(email), }) if operation == 'delete-user': for user in existed_users: email = user.email try: user.delete() except Exception as e: logger.error(e) result['failed'].append({ 'email': email, 'error_msg': 'Internal Server Error' }) continue result['success'].append({ 'email': email, }) # send admin operation log signal admin_op_detail = { "email": email, } admin_operation.send(sender=None, admin_name=request.user.username, operation=USER_DELETE, detail=admin_op_detail) if operation == 'set-institution': institution = request.POST.get('institution', None) if institution is None: error_msg = 'Institution can not be blank.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if institution != '': try: obj_insti = Institution.objects.get(name=institution) except Institution.DoesNotExist: error_msg = 'Institution %s does not exist' % institution return api_error(status.HTTP_400_BAD_REQUEST, error_msg) for user in existed_users: email = user.email profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.institution = institution profile.save() result['success'].append({ 'email': email, 'institution': institution }) return Response(result)
def _update_account_additional_info(self, request, email): # update account name name = request.data.get("name", None) if name is not None: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.nickname = name profile.save() # update account list_in_address_book list_in_address_book = request.data.get("list_in_address_book", None) if list_in_address_book is not None: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.list_in_address_book = list_in_address_book.lower( ) == 'true' profile.save() # update account loginid loginid = request.data.get("login_id", '').strip() if loginid != '': profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.login_id = loginid profile.save() # update account detailed profile department = request.data.get("department", None) if department is not None: 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() # update user quota space_quota_mb = request.data.get("storage", None) if space_quota_mb is not None: space_quota = int(space_quota_mb) * get_file_size_unit('MB') if is_org_context(request): org_id = request.user.org.org_id seaserv.seafserv_threaded_rpc.set_org_user_quota( org_id, email, space_quota) else: seafile_api.set_user_quota(email, space_quota) # update user institution institution = request.data.get("institution", None) if institution is not None: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.institution = institution profile.save() # update is_trial is_trial = request.data.get("is_trial", None) if is_trial is not None: try: from seahub_extra.trialaccount.models import TrialAccount except ImportError: pass else: if is_trial is True: expire_date = timezone.now() + relativedelta(days=7) TrialAccount.object.create_or_update(email, expire_date) else: TrialAccount.objects.filter(user_or_org=email).delete()
def _update_account_additional_info(self, request, email): # update account name name = request.data.get("name", None) if name is not None: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.nickname = name profile.save() # update account list_in_address_book list_in_address_book = request.data.get("list_in_address_book", None) if list_in_address_book is not None: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.list_in_address_book = list_in_address_book.lower( ) == 'true' profile.save() # update account loginid loginid = request.data.get("login_id", '').strip() if loginid != '': profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.login_id = loginid profile.save() # update account detailed profile department = request.data.get("department", None) if department is not None: 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() # update user quota space_quota_mb = request.data.get("storage", None) if space_quota_mb is not None: space_quota = int(space_quota_mb) * get_file_size_unit('MB') if is_org_context(request): org_id = request.user.org.org_id seaserv.seafserv_threaded_rpc.set_org_user_quota( org_id, email, space_quota) else: seafile_api.set_user_quota(email, space_quota) # update user institution institution = request.data.get("institution", None) if institution is not None: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.institution = institution profile.save()
def _update_account_additional_info(self, request, email): # update account name name = request.data.get("name", None) if name is not None: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.nickname = name profile.save() # update account list_in_address_book list_in_address_book = request.data.get("list_in_address_book", None) if list_in_address_book is not None: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.list_in_address_book = list_in_address_book.lower() == 'true' profile.save() # update account loginid loginid = request.data.get("login_id", '').strip() if loginid != '': profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.login_id = loginid profile.save() # update account detailed profile department = request.data.get("department", None) if department is not None: 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() # update user quota space_quota_mb = request.data.get("storage", None) if space_quota_mb is not None: space_quota = int(space_quota_mb) * get_file_size_unit('MB') if is_org_context(request): org_id = request.user.org.org_id seaserv.seafserv_threaded_rpc.set_org_user_quota(org_id, email, space_quota) else: seafile_api.set_user_quota(email, space_quota) # update user institution institution = request.data.get("institution", None) if institution is not None: profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) profile.institution = institution profile.save() # update is_trial is_trial = request.data.get("is_trial", None) if is_trial is not None: try: from seahub_extra.trialaccount.models import TrialAccount except ImportError: pass else: if is_trial is True: expire_date = timezone.now() + relativedelta(days=7) TrialAccount.object.create_or_update(email, expire_date) else: TrialAccount.objects.filter(user_or_org=email).delete()