The Unified and Autonomous Development Platform
Add a type MenuLink filter on the users page
Robin Shened799125231f
diff --git a/e2e-test/tests/filter-users-by-type.spec.js b/e2e-test/tests/filter-users-by-type.spec.jsnew file mode 100644index 0000000000..221273a14d--- /dev/null+++ b/e2e-test/tests/filter-users-by-type.spec.js@@ -0,0 +1,41 @@+import { expect, test } from '@playwright/test';+import { createUser, login } from './helpers.js';++test('admin can filter users by type', async ({ page }) => {+ test.setTimeout(120_000);++ const suffix = Date.now();+ const prefix = `typefilter${suffix}`;+ const ordinaryName = `${prefix}-ordinary`;+ const serviceName = `${prefix}-service`;+ const aiName = `${prefix}-ai`;++ await login(page, 'admin', 'admin');+ await createUser(page, { userName: ordinaryName });+ await createUser(page, { userName: serviceName, type: 'Service' });+ await createUser(page, { userName: aiName, type: 'AI' });++ await page.goto('~administration/users');+ await page.getByPlaceholder('Filter by name or email address').fill(prefix);+ const usersTable = page.locator('table.users');+ await expect(usersTable).toContainText(ordinaryName);+ await expect(usersTable).toContainText(serviceName);+ await expect(usersTable).toContainText(aiName);++ const typeFilter = page.locator('.user-list a.dropdown-link').filter({ hasNotText: 'Operations' });+ await expect(typeFilter).toContainText('All Types');++ await typeFilter.click();+ await page.locator('.floating.menu a').filter({ hasText: 'AI' }).click();+ await expect(typeFilter).toContainText('AI');+ await expect(usersTable).toContainText(aiName);+ await expect(usersTable).not.toContainText(ordinaryName);+ await expect(usersTable).not.toContainText(serviceName);++ await typeFilter.click();+ await page.locator('.floating.menu a').filter({ hasText: 'All Types' }).click();+ await expect(typeFilter).toContainText('All Types');+ await expect(usersTable).toContainText(ordinaryName);+ await expect(usersTable).toContainText(serviceName);+ await expect(usersTable).toContainText(aiName);+});diff --git a/e2e-test/tests/helpers.js b/e2e-test/tests/helpers.jsindex 8bfe06a2f8..51f79919cd 100644--- a/e2e-test/tests/helpers.js+++ b/e2e-test/tests/helpers.js@@ -53,9 +53,11 @@ export async function fillConfirmativePassword(root, password) {}/**- * Create a user via the admin UI. Caller must be logged in as an administrator.+ * Create a user. Ordinary and service accounts go through the admin UI.+ * AI accounts are created via REST so model settings do not need a live API.+ * Caller must be logged in as an administrator.* @param {import('@playwright/test').Page} page- * @param {{ userName?: string, password?: string, email?: string }} [options]+ * @param {{ userName?: string, password?: string, email?: string, type?: 'Ordinary' | 'Service' | 'AI', apiUser?: string, apiPassword?: string }} [options]* @returns {Promise<{ userName: string, password: string, email: string }>}*/export async function createUser(page, options = {}) {@@ -63,11 +65,41 @@ export async function createUser(page, options = {}) {const userName = options.userName ?? `user${suffix}`;const password = options.password ?? 'userpass1';const email = options.email ?? `${userName}@example.com`;+ const type = options.type ?? 'Ordinary';++ if (type === 'AI') {+ const apiUser = options.apiUser ?? 'admin';+ const apiPassword = options.apiPassword ?? 'admin';+ const response = await page.request.post('~api/users', {+ headers: {+ Authorization: `Basic ${Buffer.from(`${apiUser}:${apiPassword}`).toString('base64')}`,+ 'Content-Type': 'application/json',+ },+ data: {+ type: 'AI',+ name: userName,+ aiSetting: {+ modelSetting: {+ baseUrl: 'http://127.0.0.1:1',+ name: 'dummy',+ timeoutSeconds: 30,+ },+ },+ },+ });+ expect(response.ok()).toBeTruthy();+ return { userName, password, email };+ }await page.goto('~administration/users/new');+ if (type !== 'Ordinary') {+ await select2Choose(page, formGroup(page, 'Type'), type);+ }await fillLabeledInput(page, 'Login Name', userName);- await fillConfirmativePassword(page, password);- await fillLabeledInput(page, 'Email Address', email);+ if (type === 'Ordinary') {+ await fillConfirmativePassword(page, password);+ await fillLabeledInput(page, 'Email Address', email);+ }await page.getByRole('button', { name: 'Create' }).click();await expect(page.locator('#session-feedback')).toContainText('New user created');diff --git a/server-core/src/main/java/io/onedev/server/web/page/admin/usermanagement/UserListPage.html b/server-core/src/main/java/io/onedev/server/web/page/admin/usermanagement/UserListPage.htmlindex 5c70d80c76..d76e0643e5 100644--- a/server-core/src/main/java/io/onedev/server/web/page/admin/usermanagement/UserListPage.html+++ b/server-core/src/main/java/io/onedev/server/web/page/admin/usermanagement/UserListPage.html@@ -15,6 +15,10 @@<svg wicket:id="icon" class="icon"></svg><wicket:t>Include Disabled</wicket:t></a>+ <a wicket:id="filterType" class="text-gray mb-2 d-inline-block text-nowrap">+ <wicket:svg href="arrow" class="icon rotate-90"></wicket:svg>+ <span wicket:id="label"></span>+ </a></div><table wicket:id="users" class="users table table-hover"></table></div>diff --git a/server-core/src/main/java/io/onedev/server/web/page/admin/usermanagement/UserListPage.java b/server-core/src/main/java/io/onedev/server/web/page/admin/usermanagement/UserListPage.javaindex c75461e694..e112c4cc76 100644--- a/server-core/src/main/java/io/onedev/server/web/page/admin/usermanagement/UserListPage.java+++ b/server-core/src/main/java/io/onedev/server/web/page/admin/usermanagement/UserListPage.java@@ -69,6 +69,7 @@ import io.onedev.server.web.page.admin.AdministrationPage;import io.onedev.server.web.page.user.UserCssResourceReference;import io.onedev.server.web.page.user.profile.UserProfilePage;import io.onedev.server.web.util.LoadableDetachableDataProvider;+import io.onedev.server.web.util.TextUtils;import io.onedev.server.web.util.WicketUtils;import io.onedev.server.web.util.paginghistory.PagingHistorySupport;import io.onedev.server.web.util.paginghistory.ParamPagingHistorySupport;@@ -80,6 +81,8 @@ public class UserListPage extends AdministrationPage {private static final String PARAM_QUERY = "query";private static final String PARAM_INCLUDE_DISABLED = "includeDisabled";++ private static final String PARAM_TYPE = "type";private final IModel<List<User>> usersModel = new LoadableDetachableModel<List<User>>() {@@ -90,7 +93,11 @@ public class UserListPage extends AdministrationPage {var emailAddresses = new HashMap<Long, Collection<String>>();for (var emailAddress: emailAddressCache.values())emailAddresses.computeIfAbsent(emailAddress.getOwnerId(), key -> new HashSet<>()).add(emailAddress.getValue());- List<User> users = new ArrayList<>(userCache.getUsers(it->state.includeDisabled?true:!it.isDisabled()));+ List<User> users = new ArrayList<>(userCache.getUsers(it -> {+ if (!state.includeDisabled && it.isDisabled())+ return false;+ return state.type == null || it.getType() == state.type;+ }));users.sort(userCache.comparingDisplayName(Sets.newHashSet()));users = new Similarities<>(users) {@@ -113,6 +120,8 @@ public class UserListPage extends AdministrationPage {private TextField<String> searchField;private AjaxLink<?> includeDisabledLink;++ private MenuLink filterTypeLink;private DataTable<User, Void> usersTable;@@ -129,6 +138,9 @@ public class UserListPage extends AdministrationPage {state.query = params.get(PARAM_QUERY).toString();state.includeDisabled = params.get(PARAM_INCLUDE_DISABLED).toBoolean(false);+ var typeString = params.get(PARAM_TYPE).toString();+ if (typeString != null)+ state.type = User.Type.valueOf(typeString);}@Override@@ -138,9 +150,14 @@ public class UserListPage extends AdministrationPage {getPageParameters().set(PARAM_QUERY, state.query);getPageParameters().set(PARAM_INCLUDE_DISABLED, state.includeDisabled);+ if (state.type != null)+ getPageParameters().set(PARAM_TYPE, state.type.name());+ else+ getPageParameters().remove(PARAM_TYPE);target.add(searchField);if (WicketUtils.isSubscriptionActive())target.add(includeDisabledLink);+ target.add(filterTypeLink);selectionColumn.getSelections().clear();target.add(usersTable);}@@ -820,6 +837,32 @@ public class UserListPage extends AdministrationPage {}}));++ add(filterTypeLink = new MenuLink("filterType") {++ @Override+ protected void onInitialize() {+ super.onInitialize();+ add(new Label("label", new AbstractReadOnlyModel<String>() {++ @Override+ public String getObject() {+ return getTypeFilterLabel();+ }++ }));+ }++ @Override+ protected List<MenuItem> getMenuItems(FloatingPanel dropdown) {+ List<MenuItem> menuItems = new ArrayList<>();+ menuItems.add(newTypeFilterItem(dropdown, null));+ for (var type: User.Type.values())+ menuItems.add(newTypeFilterItem(dropdown, type));+ return menuItems;+ }++ });List<IColumn<User, Void>> columns = new ArrayList<>();@@ -994,6 +1037,55 @@ public class UserListPage extends AdministrationPage {WebConstants.PAGE_SIZE, pagingHistorySupport, true));}+ private MenuItem newTypeFilterItem(FloatingPanel dropdown, User.Type type) {+ return new MenuItem() {++ @Override+ public String getLabel() {+ return getTypeFilterLabel(type);+ }++ @Override+ public boolean isSelected() {+ return state.type == type;+ }++ @Override+ public WebMarkupContainer newLink(String id) {+ return new AjaxLink<Void>(id) {++ @Override+ public void onClick(AjaxRequestTarget target) {+ dropdown.close();+ state.type = type;+ var params = paramsOf(state, 0);+ var url = RequestCycle.get().urlFor(UserListPage.class, params).toString();+ pushState(target, url, state);++ target.add(filterTypeLink);++ usersTable.setCurrentPage(0);+ selectionColumn.getSelections().clear();+ target.add(usersTable);+ }++ };+ }++ };+ }++ private String getTypeFilterLabel() {+ return getTypeFilterLabel(state.type);+ }++ private String getTypeFilterLabel(User.Type type) {+ if (type != null)+ return _T(TextUtils.getDisplayValue(type));+ else+ return _T("All Types");+ }+private UserService getUserService() {return OneDev.getInstance(UserService.class);}@@ -1024,6 +1116,8 @@ public class UserListPage extends AdministrationPage {params.add(PARAM_QUERY, state.query);if (state.includeDisabled)params.add(PARAM_INCLUDE_DISABLED, state.includeDisabled);+ if (state.type != null)+ params.add(PARAM_TYPE, state.type.name());if (page != 0)params.add(PARAM_PAGE, page);return params;@@ -1041,6 +1135,8 @@ public class UserListPage extends AdministrationPage {public String query;public boolean includeDisabled;++ public User.Type type;}