def get_institution_user_info(user_obj, institution, is_check_admin=True): """ If this function is called in for loop, do not check admin in this function, but add is_institution_admin attribute out of this function. is_institution_admin() will invoke a database query. """ info = {} info['email'] = user_obj.email info['name'] = email2nickname(user_obj.email) info['contact_email'] = email2contact_email(user_obj.email) info['quota_usage'], info['quota_total'] = get_user_quota_usage_and_total( user_obj.email) info['create_time'] = timestamp_to_isoformat_timestr(user_obj.ctime) info['is_active'] = user_obj.is_active if is_check_admin: info['is_institution_admin'] = is_institution_admin( user_obj.email, institution) last_login_obj = UserLastLogin.objects.get_by_username(user_obj.email) info['last_login'] = datetime_to_isoformat_timestr( last_login_obj.last_login) if last_login_obj else '' return info
def put(self, request, institution_id, email): """ Update user of an institution """ if not request.user.admin_permissions.other_permission(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') try: institution = Institution.objects.get(id=institution_id) except Institution.DoesNotExist: error_msg = "institution %s not found." % institution_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) profile = Profile.objects.get_profile_by_user(email) if not profile or profile.institution != institution.name: error_msg = 'email %s invalid' % email return api_error(status.HTTP_400_BAD_REQUEST, error_msg) is_inst_admin = request.data.get('is_institution_admin') if is_inst_admin: is_inst_admin = is_inst_admin.lower() if is_inst_admin not in ('true', 'false'): error_msg = 'is_institution_admin %s invalid' % is_inst_admin return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: if is_inst_admin == 'true': if user.is_staff: error_msg = "Can't assign system admin as institution admin" return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # if user is already inst admin, cannot set to institution admin if is_institution_admin(email, institution): error_msg = 'user %s is already admin' % email return api_error(status.HTTP_400_BAD_REQUEST, error_msg) else: InstitutionAdmin.objects.create(institution=institution, user=email) elif is_inst_admin == 'false': InstitutionAdmin.objects.filter(institution=institution, user=email).delete() except Exception as e: logging.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response(get_institution_user_info(user, institution))
def get_institution_user_info(user_obj, institution): info = {} info['email'] = user_obj.email info['name'] = email2nickname(user_obj.email) info['contact_email'] = email2contact_email(user_obj.email) info['quota_usage'], info['quota_total'] = get_user_quota_usage_and_total(user_obj.email) info['create_time'] = timestamp_to_isoformat_timestr(user_obj.ctime) info['is_active'] = user_obj.is_active info['is_institution_admin'] = is_institution_admin(user_obj.email, institution) last_login_obj = UserLastLogin.objects.get_by_username(user_obj.email) info['last_login'] = datetime_to_isoformat_timestr(last_login_obj.last_login) if last_login_obj else '' return info
def test_is_institution_admin(self): assert is_institution_admin(self.user.username) == True assert is_institution_admin(self.admin.username) == False assert is_institution_admin(self.user.username, self.inst) == True assert is_institution_admin(self.admin.username, self.inst) == False