def sys_check_license(request): """Check seafile license expiration. """ if not is_pro_version(): raise Http404 content_type = 'application/json; charset=utf-8' result = {} license_dict = parse_license() if license_dict: try: expiration = license_dict['Expiration'] except KeyError as e: logger.error(e) result['error'] = str(e) return HttpResponse(json.dumps(result), status=500, content_type=content_type) struct_time = datetime.datetime.strptime(expiration, "%Y-%m-%d") expiration_timestamp = time.mktime(struct_time.timetuple()) if time.time() > expiration_timestamp: # already expired result['already_expired'] = True elif time.time() + 30 * 24 * 60 * 60 > expiration_timestamp: # will be expired in 30 days result['to_be_expired'] = True result['expiration_date'] = expiration return HttpResponse(json.dumps(result), content_type=content_type)
def test_parse_license(mock_get_license_path): license_file = os.path.join(os.getcwd(), 'tests/seahub/utils/seafile-license.txt') mock_get_license_path.return_value = license_file license_dict = parse_license() assert license_dict['Hash'] == 'hash value'
def post(self, request): if not request.user.admin_permissions.can_config_system(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') license_file = request.FILES.get('license', None) if not license_file: error_msg = 'license can not be found.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_type, ext = get_file_type_and_ext(license_file.name) if ext != 'txt': error_msg = file_type_error_msg(ext, 'txt') return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if license_file.size > 1024 * 1024 * 5: # 5mb error_msg = file_size_error_msg(license_file.size, 5 * 1024 * 1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) license_dir = os.path.dirname(LICENSE_PATH) try: if not os.path.exists(license_dir): error_msg = 'path %s invalid.' % LICENSE_PATH return api_error(status.HTTP_400_BAD_REQUEST, error_msg) with open(LICENSE_PATH, 'wb') as fd: fd.write(license_file.read()) ccnet_api.reload_license() except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) # get license info is_pro = is_pro_version() if is_pro: license_dict = parse_license() else: license_dict = {} if license_dict: try: max_users = int(license_dict.get('MaxUsers', 3)) except ValueError as e: logger.error(e) max_users = 0 else: max_users = 0 license_info = { 'license_expiration': license_dict.get('Expiration', ''), 'license_mode': license_dict.get('Mode', ''), 'license_maxusers': max_users, 'license_to': license_dict.get('Name', ''), } return Response(license_info, status=status.HTTP_200_OK)
def get(self, request, format=None): # count repos try: repos_count = seafile_api.count_repos() except SearpcError as e: logger.error(e) repos_count = 0 # count groups try: groups_count = len(ccnet_api.get_all_groups(-1, -1)) except Exception as e: logger.error(e) groups_count = 0 # count orgs if MULTI_TENANCY: multi_tenancy_enabled = True try: org_count = ccnet_api.count_orgs() except Exception as e: logger.error(e) org_count = 0 else: multi_tenancy_enabled = False org_count = 0 # count users try: active_db_users = ccnet_api.count_emailusers('DB') except Exception as e: logger.error(e) active_db_users = 0 try: active_ldap_users = ccnet_api.count_emailusers('LDAP') except Exception as e: logger.error(e) active_ldap_users = 0 try: inactive_db_users = ccnet_api.count_inactive_emailusers('DB') except Exception as e: logger.error(e) inactive_db_users = 0 try: inactive_ldap_users = ccnet_api.count_inactive_emailusers('LDAP') except Exception as e: logger.error(e) inactive_ldap_users = 0 active_users = active_db_users + active_ldap_users if \ active_ldap_users > 0 else active_db_users inactive_users = inactive_db_users + inactive_ldap_users if \ inactive_ldap_users > 0 else inactive_db_users # get license info is_pro = is_pro_version() if is_pro: license_dict = parse_license() else: license_dict = {} if license_dict: with_license = True try: max_users = int(license_dict.get('MaxUsers', 3)) except ValueError as e: logger.error(e) max_users = 0 else: with_license = False max_users = 0 # count total file number try: total_files_count = seafile_api.get_total_file_number() except Exception as e: logger.error(e) total_files_count = 0 # count total storage try: total_storage = seafile_api.get_total_storage() except Exception as e: logger.error(e) total_storage = 0 # count devices number try: total_devices_count = TokenV2.objects.get_total_devices_count() except Exception as e: logger.error(e) total_devices_count = 0 # count current connected devices try: current_connected_devices_count = TokenV2.objects.\ get_current_connected_devices_count() except Exception as e: logger.error(e) current_connected_devices_count = 0 info = { 'users_count': active_users + inactive_users, 'active_users_count': active_users, 'repos_count': repos_count, 'total_files_count': total_files_count, 'groups_count': groups_count, 'org_count': org_count, 'multi_tenancy_enabled': multi_tenancy_enabled, 'is_pro': is_pro, 'with_license': with_license, 'license_expiration': license_dict.get('Expiration', ''), 'license_mode': license_dict.get('Mode', ''), 'license_maxusers': max_users, 'license_to': license_dict.get('Name', ''), 'total_storage': total_storage, 'total_devices_count': total_devices_count, 'current_connected_devices_count': current_connected_devices_count } return Response(info)
def _get_license_dict(self): license_file = os.path.join(seahub.settings.PROJECT_ROOT, '../../seafile-license.txt') license_dict = parse_license(license_file) return license_dict
def get(self, request, format=None): # count repos try: repos_count = seafile_api.count_repos() except SearpcError as e: logger.error(e) repos_count = 0 # count groups try: groups_count = len(ccnet_api.get_all_groups(-1, -1)) except Exception as e: logger.error(e) groups_count = 0 # count orgs if MULTI_TENANCY: multi_tenancy_enabled = True try: org_count = ccnet_api.count_orgs() except Exception as e: logger.error(e) org_count = 0 else: multi_tenancy_enabled = False org_count = 0 # count users try: active_db_users = ccnet_api.count_emailusers('DB') except Exception as e: logger.error(e) active_db_users = 0 try: active_ldap_users = ccnet_api.count_emailusers('LDAP') except Exception as e: logger.error(e) active_ldap_users = 0 try: inactive_db_users = ccnet_api.count_inactive_emailusers('DB') except Exception as e: logger.error(e) inactive_db_users = 0 try: inactive_ldap_users = ccnet_api.count_inactive_emailusers('LDAP') except Exception as e: logger.error(e) inactive_ldap_users = 0 active_users = active_db_users + active_ldap_users if active_ldap_users > 0 \ else active_db_users inactive_users = inactive_db_users + inactive_ldap_users if inactive_ldap_users > 0 \ else inactive_db_users is_pro = is_pro_version() if is_pro: license_file = os.path.join(seahub.settings.PROJECT_ROOT, '../../seafile-license.txt') license_dict = parse_license(license_file) else: license_dict = {} if license_dict: with_license = True else: with_license = False info = { 'users_count': active_users + inactive_users, 'active_users_count': active_users, 'repos_count': repos_count, 'groups_count': groups_count, 'org_count': org_count, 'multi_tenancy_enabled': multi_tenancy_enabled, 'is_pro': is_pro, 'with_license': with_license, 'license': license_dict } return Response(info)
def get(self, request, format=None): # count repos try: repos_count = seafile_api.count_repos() except SearpcError as e: logger.error(e) repos_count = 0 # count groups try: groups_count = len(ccnet_api.get_all_groups(-1, -1)) except Exception as e: logger.error(e) groups_count = 0 # count orgs if MULTI_TENANCY: multi_tenancy_enabled = True try: org_count = ccnet_api.count_orgs() except Exception as e: logger.error(e) org_count = 0 else: multi_tenancy_enabled = False org_count = 0 # count users try: active_db_users = ccnet_api.count_emailusers('DB') except Exception as e: logger.error(e) active_db_users = 0 try: active_ldap_users = ccnet_api.count_emailusers('LDAP') except Exception as e: logger.error(e) active_ldap_users = 0 try: inactive_db_users = ccnet_api.count_inactive_emailusers('DB') except Exception as e: logger.error(e) inactive_db_users = 0 try: inactive_ldap_users = ccnet_api.count_inactive_emailusers('LDAP') except Exception as e: logger.error(e) inactive_ldap_users = 0 active_users = active_db_users + active_ldap_users if active_ldap_users > 0 \ else active_db_users inactive_users = inactive_db_users + inactive_ldap_users if inactive_ldap_users > 0 \ else inactive_db_users is_pro = is_pro_version() if is_pro: license_dict = parse_license() else: license_dict = {} if license_dict: with_license = True try: max_users = int(license_dict.get('MaxUsers', 3)) except ValueError as e: logger.error(e) max_users = 0 else: with_license = False max_users = 0 info = { 'users_count': active_users + inactive_users, 'active_users_count': active_users, 'repos_count': repos_count, 'groups_count': groups_count, 'org_count': org_count, 'multi_tenancy_enabled': multi_tenancy_enabled, 'is_pro': is_pro, 'with_license': with_license, 'license_expiration': license_dict.get('Expiration', ''), 'license_maxusers': max_users, 'license_to': license_dict.get('Name', ''), } return Response(info)
def get(self, request, format=None): # count groups try: groups_count = len(ccnet_api.get_all_groups(-1, -1)) except Exception as e: logger.error(e) groups_count = 0 # count orgs if MULTI_TENANCY: multi_tenancy_enabled = True try: org_count = ccnet_api.count_orgs() except Exception as e: logger.error(e) org_count = 0 else: multi_tenancy_enabled = False org_count = 0 # count users try: active_db_users = ccnet_api.count_emailusers('DB') except Exception as e: logger.error(e) active_db_users = 0 try: active_ldap_users = ccnet_api.count_emailusers('LDAP') except Exception as e: logger.error(e) active_ldap_users = 0 try: inactive_db_users = ccnet_api.count_inactive_emailusers('DB') except Exception as e: logger.error(e) inactive_db_users = 0 try: inactive_ldap_users = ccnet_api.count_inactive_emailusers('LDAP') except Exception as e: logger.error(e) inactive_ldap_users = 0 active_users = active_db_users + active_ldap_users if \ active_ldap_users > 0 else active_db_users inactive_users = inactive_db_users + inactive_ldap_users if \ inactive_ldap_users > 0 else inactive_db_users # get license info is_pro = is_pro_version() if is_pro: license_dict = parse_license() else: license_dict = {} if license_dict: with_license = True try: max_users = int(license_dict.get('MaxUsers', 3)) except ValueError as e: logger.error(e) max_users = 0 else: with_license = False max_users = 0 # count dtables try: dtables_count = DTables.objects.count() except Exception as e: logger.error(e) dtables_count = 0 info = { 'version': SEATABLE_VERSION, 'users_count': active_users + inactive_users, 'active_users_count': active_users, 'groups_count': groups_count, 'org_count': org_count, 'dtables_count': dtables_count, 'multi_tenancy_enabled': multi_tenancy_enabled, 'is_pro': is_pro, 'with_license': with_license, 'license_expiration': license_dict.get('Expiration', ''), 'license_mode': license_dict.get('Mode', ''), 'license_maxusers': max_users, 'license_to': license_dict.get('Name', ''), 'dtable_server_info': get_dtable_server_info(request.user.username) or {} } return Response(info)
def _get_license_dict(self): license_file = os.path.join(seahub.settings.PROJECT_ROOT, "../../seafile-license.txt") license_dict = parse_license(license_file) return license_dict