def user_number_over_limit(new_users=0): logger = logging.getLogger(__name__) if is_pro_version(): try: # get license user limit license_dict = parse_license() max_users = int(license_dict.get('MaxUsers', 3)) # get active user number active_db_users = ccnet_api.count_emailusers('DB') active_ldap_users = ccnet_api.count_emailusers('LDAP') active_users = active_db_users + active_ldap_users if \ active_ldap_users > 0 else active_db_users if new_users < 0: logger.debug('`new_users` must be greater or equal to 0.') return False elif new_users == 0: return active_users >= max_users else: return active_users + new_users > max_users except Exception as e: logger.error(e) return False else: return False
def get(self, request): try: page = int(request.GET.get('page', '1')) per_page = int(request.GET.get('per_page', '25')) except ValueError: page = 1 per_page = 25 start = (page - 1) * per_page end = page * per_page + 1 users = ccnet_api.get_emailusers('DB', start, end) total_count = ccnet_api.count_emailusers('DB') + \ ccnet_api.count_inactive_emailusers('DB') data = [] for user in users: user_info = get_user_info(user.email) data.append(user_info) result = {'data': data, 'total_count': total_count} resp = Response(result) ## generate `Links` header for paginator base_url = reverse('api-v2.1-admin-users') links_header = generate_links_header_for_paginator( base_url, page, per_page, total_count) resp['Links'] = links_header return resp
def test_user_management(): email1 = '%s@%s.com' % (randstring(6), randstring(6)) email2 = '%s@%s.com' % (randstring(6), randstring(6)) passwd1 = 'randstring(6)' passwd2 = 'randstring(6)' ccnet_api.add_emailuser(email1, passwd1, 1, 1) ccnet_api.add_emailuser(email2, passwd2, 0, 0) ccnet_email1 = ccnet_api.get_emailuser(email1) ccnet_email2 = ccnet_api.get_emailuser(email2) assert ccnet_email1.is_active == True assert ccnet_email1.is_staff == True assert ccnet_email2.is_active == False assert ccnet_email2.is_staff == False assert ccnet_api.validate_emailuser(email1, passwd1) == 0 assert ccnet_api.validate_emailuser(email2, passwd2) == 0 users = ccnet_api.search_emailusers('DB',email1, -1, -1) assert len(users) == 1 user_ccnet = users[0] assert user_ccnet.email == email1 user_counts = ccnet_api.count_emailusers('DB') user_numbers = ccnet_api.get_emailusers('DB', -1, -1) ccnet_api.update_emailuser('DB', ccnet_email2.id, passwd2, 1, 1) email2_new = ccnet_api.get_emailuser(email2) assert email2_new.is_active == True assert email2_new.is_staff == True ccnet_api.remove_emailuser('DB', email1) ccnet_api.remove_emailuser('DB', email2)
def get(self, request): try: page = int(request.GET.get('page', '1')) per_page = int(request.GET.get('per_page', '25')) except ValueError: page = 1 per_page = 25 start = (page - 1) * per_page end = page * per_page + 1 users = ccnet_api.get_emailusers('DB', start, end) total_count = ccnet_api.count_emailusers('DB') + \ ccnet_api.count_inactive_emailusers('DB') data = [] for user in users: user_info = get_user_info(user.email) data.append(user_info) result = {'data': data, 'total_count': total_count} resp = Response(result) ## generate `Links` header for paginator base_url = reverse('api-v2.1-admin-users') links_header = generate_links_header_for_paginator(base_url, page, per_page, total_count) resp['Links'] = links_header return resp
def user_number_over_limit(new_users=0): if is_pro_version(): try: # get license user limit license_dict = parse_license() max_users = int(license_dict.get('MaxUsers', 3)) # get active user number active_db_users = ccnet_api.count_emailusers('DB') active_ldap_users = ccnet_api.count_emailusers('LDAP') active_users = active_db_users + active_ldap_users return active_users + new_users >= max_users except Exception as e: logger = logging.getLogger(__name__) logger.error(e) return False else: return False
def get(self, request): try: page = int(request.GET.get('page', '1')) per_page = int(request.GET.get('per_page', '25')) except ValueError: page = 1 per_page = 25 start = (page - 1) * per_page end = start + per_page users = ccnet_api.get_emailusers('DB', start, end) total_count = ccnet_api.count_emailusers('DB') + \ ccnet_api.count_inactive_emailusers('DB') data = [] for user in users: profile = Profile.objects.get_profile_by_user(user.email) info = {} info['email'] = user.email info['name'] = email2nickname(user.email) info['contact_email'] = profile.contact_email if profile and profile.contact_email else '' info['login_id'] = profile.login_id if profile and profile.login_id else '' info['is_staff'] = user.is_staff info['is_active'] = user.is_active info['quota_total'] = seafile_api.get_user_quota(user.email) info['quota_usage'] = seafile_api.get_user_self_usage(user.email) info['create_time'] = timestamp_to_isoformat_timestr(user.ctime) info['last_login'] = UserLastLogin.objects.get_by_username(user.email).last_login if UserLastLogin.objects.get_by_username(user.email) else '' if is_pro_version(): info['role'] = user.role data.append(info) result = {'data': data, 'total_count': total_count} return Response(result)
def get(self, request): """List all users in DB or LDAPImport Permission checking: 1. only admin can perform this action. """ if not request.user.admin_permissions.can_manage_user(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') # parameter check try: page = int(request.GET.get('page', '1')) per_page = int(request.GET.get('per_page', '25')) except ValueError: page = 1 per_page = 25 start = (page - 1) * per_page source = request.GET.get('source', 'DB').lower().strip() if source not in ['db', 'ldapimport']: # source: 'DB' or 'LDAPImport', default is 'DB' error_msg = 'source %s invalid.' % source return api_error(status.HTTP_400_BAD_REQUEST, error_msg) order_by = request.GET.get('order_by', '').lower().strip() if order_by: if order_by not in ('quota_usage'): error_msg = 'order_by invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) direction = request.GET.get('direction', 'desc').lower().strip() if direction not in ('asc', 'desc'): error_msg = 'direction invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if source == 'db': total_count = ccnet_api.count_emailusers('DB') + \ ccnet_api.count_inactive_emailusers('DB') if order_by: if total_count > 500 and \ not getattr(settings, 'ALWAYS_SORT_USERS_BY_QUOTA_USAGE', False): error_msg = _( "There are more than 500 users, and sort is not offered." ) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: data = self.get_info_of_users_order_by_quota_usage( source, direction, page, per_page) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) result = {'data': data, 'total_count': total_count} return Response(result) else: users = ccnet_api.get_emailusers('DB', start, per_page) elif source == 'ldapimport': # api param is 'LDAP', but actually get count of 'LDAPImport' users total_count = ccnet_api.count_emailusers('LDAP') + \ ccnet_api.count_inactive_emailusers('LDAP') if order_by: if total_count > 500 and \ not getattr(settings, 'ALWAYS_SORT_USERS_BY_QUOTA_USAGE', False): error_msg = _( "There are more than 500 users, and sort is not offered." ) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: data = self.get_info_of_users_order_by_quota_usage( source, direction, page, per_page) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) result = {'data': data, 'total_count': total_count} return Response(result) else: users = ccnet_api.get_emailusers('LDAPImport', start, per_page) data = [] for user in users: profile = Profile.objects.get_profile_by_user(user.email) info = {} info['email'] = user.email info['name'] = email2nickname(user.email) info['contact_email'] = email2contact_email(user.email) info[ 'login_id'] = profile.login_id if profile and profile.login_id else '' info['is_staff'] = user.is_staff info['is_active'] = user.is_active orgs = ccnet_api.get_orgs_by_user(user.email) try: if orgs: org_id = orgs[0].org_id info['org_id'] = org_id info['org_name'] = orgs[0].org_name info['quota_usage'] = seafile_api.get_org_user_quota_usage( org_id, user.email) info['quota_total'] = seafile_api.get_org_user_quota( org_id, user.email) else: info['quota_usage'] = seafile_api.get_user_self_usage( user.email) info['quota_total'] = seafile_api.get_user_quota( user.email) except Exception as e: logger.error(e) info['quota_usage'] = -1 info['quota_total'] = -1 info['create_time'] = timestamp_to_isoformat_timestr(user.ctime) last_login_obj = UserLastLogin.objects.get_by_username(user.email) info['last_login'] = datetime_to_isoformat_timestr( last_login_obj.last_login) if last_login_obj else '' info['role'] = get_user_role(user) if getattr(settings, 'MULTI_INSTITUTION', False): info['institution'] = profile.institution if profile else '' data.append(info) result = {'data': data, 'total_count': total_count} return Response(result)
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(self, request): """List all users in DB or LDAPImport Permission checking: 1. only admin can perform this action. """ try: page = int(request.GET.get('page', '1')) per_page = int(request.GET.get('per_page', '25')) except ValueError: page = 1 per_page = 25 start = (page - 1) * per_page # source: 'DB' or 'LDAPImport', default is 'DB' source = request.GET.get('source', 'DB') source = source.lower() if source not in ['db', 'ldapimport']: error_msg = 'source %s invalid.' % source return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if source == 'db': users = ccnet_api.get_emailusers('DB', start, per_page) total_count = ccnet_api.count_emailusers('DB') + \ ccnet_api.count_inactive_emailusers('DB') elif source == 'ldapimport': users = ccnet_api.get_emailusers('LDAPImport', start, per_page) # api param is 'LDAP', but actually get count of 'LDAPImport' users total_count = ccnet_api.count_emailusers('LDAP') + \ ccnet_api.count_inactive_emailusers('LDAP') data = [] for user in users: profile = Profile.objects.get_profile_by_user(user.email) info = {} info['email'] = user.email info['name'] = email2nickname(user.email) info['contact_email'] = email2contact_email(user.email) info[ 'login_id'] = profile.login_id if profile and profile.login_id else '' info['is_staff'] = user.is_staff info['is_active'] = user.is_active orgs = ccnet_api.get_orgs_by_user(user.email) try: if orgs: org_id = orgs[0].org_id info['org_id'] = org_id info['org_name'] = orgs[0].org_name except Exception as e: logger.error(e) info['create_time'] = timestamp_to_isoformat_timestr(user.ctime) last_login_obj = UserLastLogin.objects.get_by_username(user.email) info['last_login'] = datetime_to_isoformat_timestr( last_login_obj.last_login) if last_login_obj else '' info['role'] = get_user_role(user) info['storage_usage'] = Workspaces.objects.get_owner_total_storage( owner=user.email) if getattr(settings, 'MULTI_INSTITUTION', False): info['institution'] = profile.institution if profile else '' data.append(info) result = {'data': data, 'total_count': total_count} return Response(result)
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 = self._get_license_dict() else: license_dict = {} if license_dict: with_license = True try: max_users = int(license_dict.get('MaxUsers', '')) 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 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 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(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 = self._get_license_dict() else: license_dict = {} if license_dict: with_license = True try: max_users = int(license_dict.get("MaxUsers", "")) 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 test_user_management(repo): email1 = '%s@%s.com' % (randstring(6), randstring(6)) email2 = '%s@%s.com' % (randstring(6), randstring(6)) passwd1 = 'randstring(6)' passwd2 = 'randstring(6)' ccnet_api.add_emailuser(email1, passwd1, 1, 1) ccnet_api.add_emailuser(email2, passwd2, 0, 0) ccnet_email1 = ccnet_api.get_emailuser(email1) ccnet_email2 = ccnet_api.get_emailuser(email2) assert ccnet_email1.is_active == True assert ccnet_email1.is_staff == True assert ccnet_email2.is_active == False assert ccnet_email2.is_staff == False assert ccnet_api.validate_emailuser(email1, passwd1) == 0 assert ccnet_api.validate_emailuser(email2, passwd2) == 0 users = ccnet_api.search_emailusers('DB', email1, -1, -1) assert len(users) == 1 user_ccnet = users[0] assert user_ccnet.email == email1 user_counts = ccnet_api.count_emailusers('DB') user_numbers = ccnet_api.get_emailusers('DB', -1, -1) ccnet_api.update_emailuser('DB', ccnet_email2.id, passwd2, 1, 1) email2_new = ccnet_api.get_emailuser(email2) assert email2_new.is_active == True assert email2_new.is_staff == True #test group when update user id id1 = ccnet_api.create_group('group1', email1, parent_group_id=-1) assert id1 != -1 group1 = ccnet_api.get_group(id1) assert group1.parent_group_id == -1 # test shared repo when update user id api.share_repo(repo.id, USER, email1, "rw") assert api.repo_has_been_shared(repo.id) new_email1 = '%s@%s.com' % (randstring(6), randstring(6)) assert ccnet_api.update_emailuser_id(email1, new_email1) == 0 shared_users = api.list_repo_shared_to(USER, repo.id) assert len(shared_users) == 1 assert shared_users[0].repo_id == repo.id assert shared_users[0].user == new_email1 assert shared_users[0].perm == "rw" api.remove_share(repo.id, USER, new_email1) email1_groups = ccnet_api.get_groups(new_email1) assert len(email1_groups) == 1 assert email1_groups[0].id == id1 rm1 = ccnet_api.remove_group(id1) assert rm1 == 0 ccnet_api.remove_emailuser('DB', new_email1) ccnet_api.remove_emailuser('DB', email2)