add current user repo + mapping
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { UmbDocumentPermissionRepository } from '../../user-permissions/index.js';
|
||||
import { UmbDocumentItemRepository } from '../../repository/index.js';
|
||||
import { UmbUserGroupRepository } from '@umbraco-cms/backoffice/user-group';
|
||||
import { UmbUserGroupItemRepository } from '@umbraco-cms/backoffice/user-group';
|
||||
import { html, customElement, property, state, ifDefined, nothing } from '@umbraco-cms/backoffice/external/lit';
|
||||
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
|
||||
import type {
|
||||
@@ -39,7 +39,7 @@ export class UmbPermissionsModalElement extends UmbLitElement {
|
||||
_userGroupRefs: Array<UmbUserGroupRefData> = [];
|
||||
|
||||
#userPermissions: Array<any> = [];
|
||||
#userGroupRepository = new UmbUserGroupRepository(this);
|
||||
#userGroupIemRepository = new UmbUserGroupItemRepository(this);
|
||||
#documentPermissionRepository = new UmbDocumentPermissionRepository(this);
|
||||
#documentItemRepository = new UmbDocumentItemRepository(this);
|
||||
#modalManagerContext?: UmbModalManagerContext;
|
||||
@@ -83,12 +83,12 @@ export class UmbPermissionsModalElement extends UmbLitElement {
|
||||
|
||||
async #mapToUserGroupRefs() {
|
||||
const userGroupIds = [...new Set(this.#userPermissions.map((permission) => permission.target.userGroupId))];
|
||||
const { data } = await this.#userGroupRepository.requestItems(userGroupIds);
|
||||
const { data } = await this.#userGroupIemRepository.requestItems(userGroupIds);
|
||||
|
||||
const userGroups = data ?? [];
|
||||
|
||||
this._userGroupRefs = this.#userPermissions.map((entry) => {
|
||||
const userGroup = userGroups.find((userGroup) => userGroup.id == entry.target.userGroupId);
|
||||
const userGroup = userGroups.find((userGroup) => userGroup.unique == entry.target.userGroupId);
|
||||
return {
|
||||
id: entry.target.userGroupId,
|
||||
name: userGroup?.name,
|
||||
|
||||
@@ -44,7 +44,6 @@ export class UmbMediaServerDataSource implements UmbDetailDataSource<UmbMediaDet
|
||||
values: [],
|
||||
variants: [
|
||||
{
|
||||
state: null,
|
||||
culture: null,
|
||||
segment: null,
|
||||
name: '',
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
import type { UmbCurrentUser } from './types.js';
|
||||
import type { UmbCurrentUserModel } from './types.js';
|
||||
import { UmbCurrentUserRepository } from './repository/index.js';
|
||||
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
|
||||
import { UmbBaseController } from '@umbraco-cms/backoffice/class-api';
|
||||
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
|
||||
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
|
||||
import { firstValueFrom } from '@umbraco-cms/backoffice/external/rxjs';
|
||||
import { UserResource } from '@umbraco-cms/backoffice/backend-api';
|
||||
import { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth';
|
||||
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
|
||||
import { umbLocalizationRegistry } from '@umbraco-cms/backoffice/localization';
|
||||
|
||||
export class UmbCurrentUserContext extends UmbBaseController {
|
||||
#currentUser = new UmbObjectState<UmbCurrentUser | undefined>(undefined);
|
||||
#currentUser = new UmbObjectState<UmbCurrentUserModel | undefined>(undefined);
|
||||
readonly currentUser = this.#currentUser.asObservable();
|
||||
|
||||
readonly languageIsoCode = this.#currentUser.asObservablePart((user) => user?.languageIsoCode ?? 'en-us');
|
||||
readonly languageIsoCode = this.#currentUser.asObservablePart((user) => user?.languageIsoCode);
|
||||
|
||||
#authContext?: typeof UMB_AUTH_CONTEXT.TYPE;
|
||||
#currentUserRepository = new UmbCurrentUserRepository(this);
|
||||
|
||||
constructor(host: UmbControllerHost) {
|
||||
super(host);
|
||||
@@ -26,6 +26,7 @@ export class UmbCurrentUserContext extends UmbBaseController {
|
||||
});
|
||||
|
||||
this.observe(this.languageIsoCode, (currentLanguageIsoCode) => {
|
||||
if (!currentLanguageIsoCode) return;
|
||||
umbLocalizationRegistry.loadLanguage(currentLanguageIsoCode);
|
||||
});
|
||||
|
||||
@@ -33,22 +34,23 @@ export class UmbCurrentUserContext extends UmbBaseController {
|
||||
}
|
||||
|
||||
async requestCurrentUser() {
|
||||
// TODO: use repository
|
||||
const { data, error } = await tryExecuteAndNotify(this._host, UserResource.getUserCurrent());
|
||||
// TODO: add current user store
|
||||
this.#currentUser.setValue(data);
|
||||
return { data, error };
|
||||
const { data } = await this.#currentUserRepository.requestCurrentUser();
|
||||
|
||||
if (data) {
|
||||
// TODO: observe current user
|
||||
this.#currentUser.setValue(data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a user is the current user.
|
||||
*
|
||||
* @param userId The user id to check
|
||||
* @param userUnique The user id to check
|
||||
* @returns True if the user is the current user, otherwise false
|
||||
*/
|
||||
async isUserCurrentUser(userId: string): Promise<boolean> {
|
||||
async isUserCurrentUser(userUnique: string): Promise<boolean> {
|
||||
const currentUser = await firstValueFrom(this.currentUser);
|
||||
return currentUser?.id === userId;
|
||||
return currentUser?.unique === userUnique;
|
||||
}
|
||||
|
||||
#observeIsAuthorized() {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { UmbCurrentUserServerDataSource } from './current-user.server.data-source.js';
|
||||
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
|
||||
import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository';
|
||||
|
||||
/**
|
||||
* A repository for the current user
|
||||
* @export
|
||||
* @class UmbCurrentUserRepository
|
||||
* @extends {UmbRepositoryBase}
|
||||
*/
|
||||
export class UmbCurrentUserRepository extends UmbRepositoryBase {
|
||||
#currentUserSource: UmbCurrentUserServerDataSource;
|
||||
|
||||
constructor(host: UmbControllerHost) {
|
||||
super(host);
|
||||
|
||||
this.#currentUserSource = new UmbCurrentUserServerDataSource(host);
|
||||
}
|
||||
|
||||
/**
|
||||
* Request the current user
|
||||
* @return {*}
|
||||
* @memberof UmbCurrentUserRepository
|
||||
*/
|
||||
async requestCurrentUser() {
|
||||
// TODO: add observable option
|
||||
return this.#currentUserSource.getCurrentUser();
|
||||
}
|
||||
}
|
||||
|
||||
export default UmbCurrentUserRepository;
|
||||
@@ -0,0 +1,50 @@
|
||||
import type { UmbCurrentUserModel } from '../types.js';
|
||||
import { UserResource } from '@umbraco-cms/backoffice/backend-api';
|
||||
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
|
||||
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
|
||||
|
||||
/**
|
||||
* A data source for the current user that fetches data from the server
|
||||
* @export
|
||||
* @class UmbCurrentUserServerDataSource
|
||||
*/
|
||||
export class UmbCurrentUserServerDataSource {
|
||||
#host: UmbControllerHost;
|
||||
|
||||
/**
|
||||
* Creates an instance of UmbCurrentUserServerDataSource.
|
||||
* @param {UmbControllerHost} host
|
||||
* @memberof UmbCurrentUserServerDataSource
|
||||
*/
|
||||
constructor(host: UmbControllerHost) {
|
||||
this.#host = host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current user
|
||||
* @return {*}
|
||||
* @memberof UmbCurrentUserServerDataSource
|
||||
*/
|
||||
async getCurrentUser() {
|
||||
const { data, error } = await tryExecuteAndNotify(this.#host, UserResource.getUserCurrent());
|
||||
|
||||
if (data) {
|
||||
const user: UmbCurrentUserModel = {
|
||||
unique: data.id,
|
||||
email: data.email,
|
||||
userName: data.userName,
|
||||
name: data.name,
|
||||
languageIsoCode: data.languageIsoCode || 'en-us', // TODO: make global variable
|
||||
documentStartNodeIds: data.documentStartNodeIds,
|
||||
mediaStartNodeIds: data.mediaStartNodeIds,
|
||||
avatarUrls: data.avatarUrls,
|
||||
languages: data.languages,
|
||||
hasAccessToAllLanguages: data.hasAccessToAllLanguages,
|
||||
permissions: data.permissions,
|
||||
};
|
||||
return { data: user };
|
||||
}
|
||||
|
||||
return { error };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export { UmbCurrentUserRepository } from './current-user.repository.js';
|
||||
export { UMB_CURRENT_USER_REPOSITORY_ALIAS } from './manifests.js';
|
||||
@@ -0,0 +1,13 @@
|
||||
import { UmbCurrentUserRepository } from './current-user.repository.js';
|
||||
import type { ManifestRepository } from '@umbraco-cms/backoffice/extension-registry';
|
||||
|
||||
export const UMB_CURRENT_USER_REPOSITORY_ALIAS = 'Umb.Repository.CurrentUser';
|
||||
|
||||
const avatarRepository: ManifestRepository = {
|
||||
type: 'repository',
|
||||
alias: UMB_CURRENT_USER_REPOSITORY_ALIAS,
|
||||
name: 'Current User Repository',
|
||||
api: UmbCurrentUserRepository,
|
||||
};
|
||||
|
||||
export const manifests = [avatarRepository];
|
||||
@@ -2,12 +2,11 @@ import { UmbUserDetailRepository } from '../../repository/index.js';
|
||||
import type { UmbUserGroupInputElement } from '@umbraco-cms/backoffice/user-group';
|
||||
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
|
||||
import { css, html, customElement, query } from '@umbraco-cms/backoffice/external/lit';
|
||||
import type {
|
||||
UmbModalManagerContext} from '@umbraco-cms/backoffice/modal';
|
||||
import type { UmbModalManagerContext } from '@umbraco-cms/backoffice/modal';
|
||||
import {
|
||||
UmbModalBaseElement,
|
||||
UMB_MODAL_MANAGER_CONTEXT,
|
||||
UMB_CREATE_USER_SUCCESS_MODAL
|
||||
UMB_CREATE_USER_SUCCESS_MODAL,
|
||||
} from '@umbraco-cms/backoffice/modal';
|
||||
|
||||
@customElement('umb-user-create-modal')
|
||||
@@ -43,16 +42,19 @@ export class UmbUserCreateModalElement extends UmbModalBaseElement {
|
||||
const userGroupPicker = form.querySelector('#userGroups') as UmbUserGroupInputElement;
|
||||
const userGroups = userGroupPicker?.selectedIds;
|
||||
|
||||
// TODO: figure out when to use email or username
|
||||
const { data } = await this.#userDetailRepository.create({
|
||||
name,
|
||||
email,
|
||||
userName: email,
|
||||
userGroupIds: userGroups,
|
||||
});
|
||||
const { data: userScaffold } = await this.#userDetailRepository.createScaffold(null);
|
||||
if (!userScaffold) return;
|
||||
|
||||
if (data && data.userId && data.initialPassword) {
|
||||
this.#openSuccessModal(data.userId, data.initialPassword);
|
||||
userScaffold.name = name;
|
||||
userScaffold.email = email;
|
||||
userScaffold.userName = email;
|
||||
userScaffold.userGroupIds = userGroups;
|
||||
|
||||
// TODO: figure out when to use email or username
|
||||
const { data } = await this.#userDetailRepository.create(userScaffold);
|
||||
|
||||
if (data && data.unique && data.initialPassword) {
|
||||
this.#openSuccessModal(data.unique, data.initialPassword);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { UmbUserSetGroupDataSource } from '../../types.js';
|
||||
import { UserResource } from '@umbraco-cms/backoffice/backend-api';
|
||||
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
|
||||
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
|
||||
@@ -8,7 +7,7 @@ import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
|
||||
* @export
|
||||
* @class UmbUserSetGroupsServerDataSource
|
||||
*/
|
||||
export class UmbUserSetGroupsServerDataSource implements UmbUserSetGroupDataSource {
|
||||
export class UmbUserSetGroupsServerDataSource {
|
||||
#host: UmbControllerHost;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user