-
Notifications
You must be signed in to change notification settings - Fork 2.9k
User Groups: Add ability to manage users directly from the group workspace #22215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7ad3ed6
add users section into user group
LanThuyNguyen be35cf3
fix test failed
LanThuyNguyen d86a38b
fix unchange issue
LanThuyNguyen 4bea90a
Merge branch 'main' of https://github.com/umbraco/Umbraco-CMS into v1…
LanThuyNguyen 9446192
add notification
LanThuyNguyen 5a3ec5e
add remainging count
LanThuyNguyen cf49815
update take 100
LanThuyNguyen 4b06947
split user list into separate element
LanThuyNguyen 772e6be
Merge branch 'main' of https://github.com/umbraco/Umbraco-CMS into v1…
LanThuyNguyen 5f0fd9e
Merge branch 'main' into v17/feature/manage-users-from-group
AndyButland 4d191c3
add localization for text
LanThuyNguyen 0074244
Merge branch 'v17/feature/manage-users-from-group' of https://github.…
LanThuyNguyen e525b37
Merge branch 'main' into v17/feature/manage-users-from-group
NguyenThuyLan 445a979
add repository for user list in user group
LanThuyNguyen b324a8e
Merge branch 'v17/feature/manage-users-from-group' of https://github.…
LanThuyNguyen 9ba4404
Merge branch 'main' of https://github.com/umbraco/Umbraco-CMS into v1…
LanThuyNguyen 321eba0
Merge branch 'main' into v17/feature/manage-users-from-group
AndyButland c3d6e00
update key message
LanThuyNguyen aa68e7d
Merge branch 'v17/feature/manage-users-from-group' of https://github.…
LanThuyNguyen 1e099e5
remove remainingCount from user-input
LanThuyNguyen 77657cc
Merge branch 'main' of https://github.com/umbraco/Umbraco-CMS into v1…
LanThuyNguyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/Umbraco.Web.UI.Client/src/packages/user/user-group/repository/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from './detail/index.js'; | ||
| export * from './item/index.js'; | ||
| export * from './users/index.js'; |
2 changes: 2 additions & 0 deletions
2
src/Umbraco.Web.UI.Client/src/packages/user/user-group/repository/users/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './user-group-users.repository.js'; | ||
| export * from './user-group-users.server.data-source.js'; |
24 changes: 24 additions & 0 deletions
24
...eb.UI.Client/src/packages/user/user-group/repository/users/user-group-users.repository.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { UmbUserGroupUsersServerDataSource } from './user-group-users.server.data-source.js'; | ||
| import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; | ||
| import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository'; | ||
|
|
||
| export class UmbUserGroupUsersRepository extends UmbRepositoryBase { | ||
| #source: UmbUserGroupUsersServerDataSource; | ||
|
|
||
| constructor(host: UmbControllerHost) { | ||
| super(host); | ||
| this.#source = new UmbUserGroupUsersServerDataSource(host); | ||
| } | ||
|
|
||
| async requestUsersInGroup(groupId: string, take = 100) { | ||
| return this.#source.getUsersInGroup(groupId, take); | ||
| } | ||
|
|
||
| async addUsersToGroup(groupId: string, userIds: string[]) { | ||
| return this.#source.addUsersToGroup(groupId, userIds); | ||
| } | ||
|
|
||
| async removeUsersFromGroup(groupId: string, userIds: string[]) { | ||
| return this.#source.removeUsersFromGroup(groupId, userIds); | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
...ient/src/packages/user/user-group/repository/users/user-group-users.server.data-source.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { UserGroupService, UserService } from '@umbraco-cms/backoffice/external/backend-api'; | ||
| import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; | ||
| import { tryExecute } from '@umbraco-cms/backoffice/resources'; | ||
|
|
||
| export class UmbUserGroupUsersServerDataSource { | ||
| #host: UmbControllerHost; | ||
|
|
||
| constructor(host: UmbControllerHost) { | ||
| this.#host = host; | ||
| } | ||
|
|
||
| async getUsersInGroup(groupId: string, take: number) { | ||
| if (!groupId) throw new Error('Group id is missing'); | ||
|
|
||
| const { data, error } = await tryExecute( | ||
| this.#host, | ||
| UserService.getFilterUser({ query: { userGroupIds: [groupId], take } }), | ||
| ); | ||
|
|
||
| if (error || !data) return { error }; | ||
|
|
||
| return { | ||
| data: { | ||
| uniques: data.items.map((u) => u.id), | ||
| total: data.total, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| async addUsersToGroup(groupId: string, userIds: string[]) { | ||
| if (!groupId) throw new Error('Group id is missing'); | ||
| if (!userIds.length) return { data: undefined, error: undefined }; | ||
|
|
||
| return tryExecute( | ||
| this.#host, | ||
| UserGroupService.postUserGroupByIdUsers({ | ||
| path: { id: groupId }, | ||
| body: userIds.map((id) => ({ id })), | ||
| }), | ||
| { disableNotifications: true }, | ||
| ); | ||
| } | ||
|
|
||
| async removeUsersFromGroup(groupId: string, userIds: string[]) { | ||
| if (!groupId) throw new Error('Group id is missing'); | ||
| if (!userIds.length) return { data: undefined, error: undefined }; | ||
|
|
||
| return tryExecute( | ||
| this.#host, | ||
| UserGroupService.deleteUserGroupByIdUsers({ | ||
| path: { id: groupId }, | ||
| body: userIds.map((id) => ({ id })), | ||
| }), | ||
| { disableNotifications: true }, | ||
| ); | ||
| } | ||
| } |
167 changes: 167 additions & 0 deletions
167
...ges/user/user-group/workspace/user-group/components/user-group-workspace-users.element.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import { UMB_USER_GROUP_WORKSPACE_CONTEXT } from '../user-group-workspace.context-token.js'; | ||
| import { UmbUserGroupUsersRepository } from '../../../repository/users/user-group-users.repository.js'; | ||
| import type { UmbUserInputElement } from '../../../../user/components/user-input/user-input.element.js'; | ||
| import type { UmbChangeEvent } from '@umbraco-cms/backoffice/event'; | ||
| import { css, html, customElement, state } from '@umbraco-cms/backoffice/external/lit'; | ||
| import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; | ||
| import { UmbTextStyles } from '@umbraco-cms/backoffice/style'; | ||
| import { UMB_NOTIFICATION_CONTEXT } from '@umbraco-cms/backoffice/notification'; | ||
|
|
||
| import '../../../../user/components/user-input/user-input.element.js'; | ||
|
|
||
| @customElement('umb-user-group-workspace-users') | ||
| export class UmbUserGroupWorkspaceUsersElement extends UmbLitElement { | ||
| @state() | ||
| private _userUniques: string[] = []; | ||
|
|
||
| @state() | ||
| private _usersRemainingCount = 0; | ||
|
|
||
| #unique?: string; | ||
| #isNew = false; | ||
| #persistedUserUniques: string[] = []; | ||
| #notificationContext?: typeof UMB_NOTIFICATION_CONTEXT.TYPE; | ||
| #usersRepository = new UmbUserGroupUsersRepository(this); | ||
| #workspaceContext?: typeof UMB_USER_GROUP_WORKSPACE_CONTEXT.TYPE; | ||
|
|
||
| constructor() { | ||
| super(); | ||
|
|
||
| this.consumeContext(UMB_NOTIFICATION_CONTEXT, (context) => { | ||
| this.#notificationContext = context; | ||
| }); | ||
|
|
||
| this.consumeContext(UMB_USER_GROUP_WORKSPACE_CONTEXT, (instance) => { | ||
| this.#workspaceContext = instance; | ||
| this.#observe(); | ||
| }); | ||
| } | ||
|
|
||
| #observe() { | ||
| this.observe( | ||
| this.#workspaceContext?.unique, | ||
| (value) => { | ||
|
NguyenThuyLan marked this conversation as resolved.
Outdated
|
||
| this.#unique = value ?? undefined; | ||
| if (value && !this.#isNew) { | ||
| this.#loadUsers(value); | ||
| } | ||
| }, | ||
| '_observeUnique', | ||
| ); | ||
|
|
||
| this.observe( | ||
| this.#workspaceContext?.isNew, | ||
| (isNew) => { | ||
| const wasNew = this.#isNew; | ||
| this.#isNew = isNew ?? false; | ||
|
|
||
| // When the group transitions from newly created to persisted, save any pending user changes. | ||
| if (wasNew === true && isNew === false) { | ||
| this.#savePendingUserChanges(); | ||
| } | ||
| }, | ||
| '_observeIsNew', | ||
| ); | ||
| } | ||
|
|
||
| async #loadUsers(unique: string) { | ||
| const { data } = await this.#usersRepository.requestUsersInGroup(unique); | ||
| this.#persistedUserUniques = [...(data?.uniques ?? [])]; | ||
| this._userUniques = [...(data?.uniques ?? [])]; | ||
| const total = data?.total ?? 0; | ||
| this._usersRemainingCount = Math.max(0, total - this._userUniques.length); | ||
| } | ||
|
|
||
| async #savePendingUserChanges() { | ||
| const unique = this.#unique; | ||
| if (!unique || this._userUniques.length === 0) return; | ||
| await this.#persistUserChanges(unique, this._userUniques); | ||
| } | ||
|
|
||
| async #persistUserChanges(unique: string, newSelection: string[]): Promise<boolean> { | ||
| const toAdd = newSelection.filter((u) => !this.#persistedUserUniques.includes(u)); | ||
| const toRemove = this.#persistedUserUniques.filter((u) => !newSelection.includes(u)); | ||
|
|
||
| const [{ error: addError }, { error: removeError }] = await Promise.all([ | ||
| this.#usersRepository.addUsersToGroup(unique, toAdd), | ||
| this.#usersRepository.removeUsersFromGroup(unique, toRemove), | ||
| ]); | ||
|
|
||
| if (addError) { | ||
| this.#notificationContext?.peek('danger', { | ||
| data: { | ||
| headline: this.localize.term('speechBubbles_operationFailedHeader'), | ||
| message: this.localize.term('user_addUsersToGroupError'), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| if (removeError) { | ||
| this.#notificationContext?.peek('danger', { | ||
| data: { | ||
| headline: this.localize.term('speechBubbles_operationFailedHeader'), | ||
| message: this.localize.term('user_removeUsersFromGroupError'), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| if (!addError && !removeError) { | ||
| this.#persistedUserUniques = [...newSelection]; | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| async #onUsersChange(event: UmbChangeEvent) { | ||
| event.stopPropagation(); | ||
| const target = event.target as UmbUserInputElement; | ||
| const newSelection = target.selection; | ||
|
|
||
| // For new (unsaved) groups, track locally — users will be persisted when the group is saved. | ||
| if (this.#isNew) { | ||
| this._userUniques = newSelection; | ||
| return; | ||
| } | ||
|
|
||
| const unique = this.#unique; | ||
| if (!unique) return; | ||
|
|
||
| const previousSelection = [...this._userUniques]; | ||
| this._userUniques = newSelection; // optimistic update | ||
|
|
||
| const success = await this.#persistUserChanges(unique, newSelection); | ||
| if (!success) { | ||
| this._userUniques = previousSelection; // revert on error | ||
| } | ||
| } | ||
|
|
||
| override render() { | ||
| return html` | ||
| <uui-box> | ||
| <div slot="headline"><umb-localize key="general_users"></umb-localize></div> | ||
| <umb-user-input | ||
| .selection=${this._userUniques} | ||
| .remainingCount=${this._usersRemainingCount} | ||
| @change=${this.#onUsersChange}></umb-user-input> | ||
| </uui-box> | ||
| `; | ||
| } | ||
|
|
||
| static override styles = [ | ||
| UmbTextStyles, | ||
| css` | ||
| :host { | ||
| display: block; | ||
| } | ||
| `, | ||
| ]; | ||
| } | ||
|
|
||
| export { UmbUserGroupWorkspaceUsersElement as element }; | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| 'umb-user-group-workspace-users': UmbUserGroupWorkspaceUsersElement; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.