2023-09-18 13:05:58 +02:00
|
|
|
import { UmbEntityData } from './entity.data.js';
|
|
|
|
|
import { umbUserGroupData } from './user-group.data.js';
|
2023-11-06 15:32:55 +01:00
|
|
|
import { arrayFilter, stringFilter, queryFilter } from './utils.js';
|
2023-11-02 14:52:34 +01:00
|
|
|
import { UmbId } from '@umbraco-cms/backoffice/id';
|
2023-06-27 14:05:09 +02:00
|
|
|
import { UmbLoggedInUser } from '@umbraco-cms/backoffice/auth';
|
2023-09-26 10:10:41 +02:00
|
|
|
import {
|
2023-10-16 14:16:23 +02:00
|
|
|
CreateUserRequestModel,
|
2023-10-16 18:05:48 +02:00
|
|
|
CreateUserResponseModel,
|
2023-10-16 14:16:23 +02:00
|
|
|
InviteUserRequestModel,
|
2023-11-02 14:52:34 +01:00
|
|
|
PagedUserResponseModel,
|
2023-09-26 10:42:57 +02:00
|
|
|
UpdateUserGroupsOnUserRequestModel,
|
2023-09-26 10:10:41 +02:00
|
|
|
UserItemResponseModel,
|
|
|
|
|
UserResponseModel,
|
|
|
|
|
UserStateModel,
|
|
|
|
|
} from '@umbraco-cms/backoffice/backend-api';
|
|
|
|
|
|
|
|
|
|
const createUserItem = (item: UserResponseModel): UserItemResponseModel => {
|
|
|
|
|
return {
|
|
|
|
|
name: item.name,
|
|
|
|
|
id: item.id,
|
|
|
|
|
};
|
|
|
|
|
};
|
2022-10-04 15:15:45 +02:00
|
|
|
|
2023-11-06 15:48:15 +01:00
|
|
|
const userGroupFilter = (filterOptions: any, item: UserResponseModel) => arrayFilter(filterOptions.userGroupIds, item.userGroupIds);
|
|
|
|
|
const userStateFilter = (filterOptions: any, item: UserResponseModel) => stringFilter(filterOptions.userStates, item.state);
|
|
|
|
|
const userQueryFilter = (filterOptions: any, item: UserResponseModel) => queryFilter(filterOptions.filter, item.name);
|
2023-11-06 15:32:55 +01:00
|
|
|
|
2022-10-05 11:49:37 +02:00
|
|
|
// Temp mocked database
|
2023-09-18 13:05:58 +02:00
|
|
|
class UmbUserData extends UmbEntityData<UserResponseModel> {
|
2023-04-26 01:04:41 +12:00
|
|
|
constructor(data: UserResponseModel[]) {
|
2022-10-05 11:49:37 +02:00
|
|
|
super(data);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 14:16:23 +02:00
|
|
|
/**
|
|
|
|
|
* Create user
|
|
|
|
|
* @param {CreateUserRequestModel} data
|
|
|
|
|
* @memberof UmbUserData
|
|
|
|
|
*/
|
2023-10-16 18:05:48 +02:00
|
|
|
createUser = (data: CreateUserRequestModel): CreateUserResponseModel => {
|
|
|
|
|
const userId = UmbId.new();
|
|
|
|
|
const initialPassword = 'mocked-initial-password';
|
|
|
|
|
|
2023-10-16 14:16:23 +02:00
|
|
|
const user: UserResponseModel = {
|
2023-10-16 18:05:48 +02:00
|
|
|
id: userId,
|
2023-10-16 14:16:23 +02:00
|
|
|
languageIsoCode: null,
|
|
|
|
|
contentStartNodeIds: [],
|
|
|
|
|
mediaStartNodeIds: [],
|
|
|
|
|
avatarUrls: [],
|
|
|
|
|
state: UserStateModel.INACTIVE,
|
|
|
|
|
failedLoginAttempts: 0,
|
|
|
|
|
createDate: new Date().toUTCString(),
|
|
|
|
|
updateDate: new Date().toUTCString(),
|
|
|
|
|
lastLoginDate: null,
|
|
|
|
|
lastLockoutDate: null,
|
|
|
|
|
lastPasswordChangeDate: null,
|
|
|
|
|
...data,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.insert(user);
|
2023-10-16 18:05:48 +02:00
|
|
|
|
|
|
|
|
return { userId, initialPassword };
|
2023-10-16 14:16:23 +02:00
|
|
|
};
|
|
|
|
|
|
2023-10-11 15:07:27 +02:00
|
|
|
/**
|
|
|
|
|
* Get user items
|
|
|
|
|
* @param {Array<string>} ids
|
|
|
|
|
* @return {*} {Array<UserItemResponseModel>}
|
|
|
|
|
* @memberof UmbUserData
|
|
|
|
|
*/
|
2023-09-26 10:10:41 +02:00
|
|
|
getItems(ids: Array<string>): Array<UserItemResponseModel> {
|
|
|
|
|
const items = this.data.filter((item) => ids.includes(item.id ?? ''));
|
|
|
|
|
return items.map((item) => createUserItem(item));
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 15:07:27 +02:00
|
|
|
/**
|
|
|
|
|
* Set user groups
|
|
|
|
|
* @param {UpdateUserGroupsOnUserRequestModel} data
|
|
|
|
|
* @memberof UmbUserData
|
|
|
|
|
*/
|
2023-09-26 10:42:57 +02:00
|
|
|
setUserGroups(data: UpdateUserGroupsOnUserRequestModel): void {
|
|
|
|
|
const users = this.data.filter((user) => data.userIds?.includes(user.id ?? ''));
|
|
|
|
|
users.forEach((user) => {
|
|
|
|
|
user.userGroupIds = data.userGroupIds;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 15:07:27 +02:00
|
|
|
/**
|
|
|
|
|
* Get current user
|
|
|
|
|
* @return {*} {UmbLoggedInUser}
|
|
|
|
|
* @memberof UmbUserData
|
|
|
|
|
*/
|
2023-06-13 11:20:20 +02:00
|
|
|
getCurrentUser(): UmbLoggedInUser {
|
|
|
|
|
const firstUser = this.data[0];
|
2023-09-18 13:05:58 +02:00
|
|
|
const permissions = firstUser.userGroupIds?.length ? umbUserGroupData.getPermissions(firstUser.userGroupIds) : [];
|
2023-06-13 11:20:20 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: firstUser.id,
|
|
|
|
|
name: firstUser.name,
|
|
|
|
|
email: firstUser.email,
|
|
|
|
|
userName: firstUser.email,
|
|
|
|
|
avatarUrls: [],
|
|
|
|
|
hasAccessToAllLanguages: true,
|
|
|
|
|
languageIsoCode: firstUser.languageIsoCode,
|
|
|
|
|
languages: [],
|
|
|
|
|
contentStartNodeIds: firstUser.contentStartNodeIds,
|
|
|
|
|
mediaStartNodeIds: firstUser.mediaStartNodeIds,
|
2023-09-18 13:05:58 +02:00
|
|
|
permissions,
|
2023-06-13 11:20:20 +02:00
|
|
|
};
|
|
|
|
|
}
|
2023-10-11 15:07:27 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disable users
|
|
|
|
|
* @param {Array<string>} ids
|
|
|
|
|
* @memberof UmbUserData
|
|
|
|
|
*/
|
|
|
|
|
disable(ids: Array<string>): void {
|
|
|
|
|
const users = this.data.filter((user) => ids.includes(user.id ?? ''));
|
|
|
|
|
users.forEach((user) => {
|
|
|
|
|
user.state = UserStateModel.DISABLED;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enable users
|
|
|
|
|
* @param {Array<string>} ids
|
|
|
|
|
* @memberof UmbUserData
|
|
|
|
|
*/
|
|
|
|
|
enable(ids: Array<string>): void {
|
|
|
|
|
const users = this.data.filter((user) => ids.includes(user.id ?? ''));
|
|
|
|
|
users.forEach((user) => {
|
|
|
|
|
user.state = UserStateModel.ACTIVE;
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-10-12 19:42:46 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unlock users
|
|
|
|
|
* @param {Array<string>} ids
|
|
|
|
|
* @memberof UmbUserData
|
|
|
|
|
*/
|
|
|
|
|
unlock(ids: Array<string>): void {
|
|
|
|
|
const users = this.data.filter((user) => ids.includes(user.id ?? ''));
|
|
|
|
|
users.forEach((user) => {
|
|
|
|
|
user.failedLoginAttempts = 0;
|
|
|
|
|
user.state = UserStateModel.ACTIVE;
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-10-16 14:16:23 +02:00
|
|
|
|
2023-11-02 14:52:34 +01:00
|
|
|
/**
|
|
|
|
|
* Invites a user
|
|
|
|
|
* @param {InviteUserRequestModel} data
|
|
|
|
|
* @memberof UmbUserData
|
|
|
|
|
*/
|
2023-10-16 14:16:23 +02:00
|
|
|
invite(data: InviteUserRequestModel): void {
|
|
|
|
|
const invitedUser = {
|
|
|
|
|
status: UserStateModel.INVITED,
|
|
|
|
|
...data,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.createUser(invitedUser);
|
|
|
|
|
}
|
2023-11-02 14:52:34 +01:00
|
|
|
|
2023-11-06 15:32:55 +01:00
|
|
|
filter (options: any): PagedUserResponseModel {
|
|
|
|
|
const { items: allItems } = this.getAll();
|
|
|
|
|
|
|
|
|
|
const filterOptions = {
|
|
|
|
|
skip: options.skip || 0,
|
|
|
|
|
take: options.take || 25,
|
|
|
|
|
orderBy: options.orderBy || 'name',
|
|
|
|
|
orderDirection: options.orderDirection || 'asc',
|
|
|
|
|
userGroupIds: options.userGroupIds,
|
|
|
|
|
userStates: options.userStates,
|
|
|
|
|
filter: options.filter,
|
2023-11-02 16:34:49 +01:00
|
|
|
};
|
|
|
|
|
|
2023-11-06 15:32:55 +01:00
|
|
|
const filteredItems = allItems.filter((item) => userGroupFilter(filterOptions, item) && userStateFilter(filterOptions, item) && userQueryFilter(filterOptions, item));
|
|
|
|
|
const totalItems = filteredItems.length;
|
|
|
|
|
|
|
|
|
|
const paginatedItems = filteredItems.slice(filterOptions.skip, filterOptions.skip + filterOptions.take);
|
2023-11-02 16:34:49 +01:00
|
|
|
|
2023-11-06 15:32:55 +01:00
|
|
|
return { total: totalItems, items: paginatedItems };
|
2023-11-02 14:52:34 +01:00
|
|
|
};
|
2022-10-05 11:49:37 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-26 01:04:41 +12:00
|
|
|
export const data: Array<UserResponseModel & { type: string }> = [
|
2022-11-02 17:03:07 +01:00
|
|
|
{
|
2023-04-04 13:19:19 +02:00
|
|
|
id: 'bca6c733-a63d-4353-a271-9a8b6bcca8bd',
|
2022-11-02 17:03:07 +01:00
|
|
|
type: 'user',
|
2023-04-26 01:04:41 +12:00
|
|
|
contentStartNodeIds: [],
|
|
|
|
|
mediaStartNodeIds: [],
|
2023-06-13 11:20:20 +02:00
|
|
|
name: 'Umbraco User',
|
|
|
|
|
email: 'noreply@umbraco.com',
|
|
|
|
|
languageIsoCode: 'en-US',
|
2023-04-26 01:04:41 +12:00
|
|
|
state: UserStateModel.ACTIVE,
|
2022-11-02 17:03:07 +01:00
|
|
|
lastLoginDate: '9/10/2022',
|
2023-05-01 11:35:04 +02:00
|
|
|
lastLockoutDate: '11/23/2021',
|
2022-10-04 15:15:45 +02:00
|
|
|
lastPasswordChangeDate: '1/10/2022',
|
2022-11-02 17:03:07 +01:00
|
|
|
updateDate: '2/10/2022',
|
|
|
|
|
createDate: '3/13/2022',
|
|
|
|
|
failedLoginAttempts: 946,
|
2023-09-18 13:05:58 +02:00
|
|
|
userGroupIds: [
|
|
|
|
|
'c630d49e-4e7b-42ea-b2bc-edc0edacb6b1',
|
|
|
|
|
'9d24dc47-a4bf-427f-8a4a-b900f03b8a12',
|
|
|
|
|
'f4626511-b0d7-4ab1-aebc-a87871a5dcfa',
|
|
|
|
|
],
|
2022-10-04 15:15:45 +02:00
|
|
|
},
|
|
|
|
|
{
|
2023-04-26 01:04:41 +12:00
|
|
|
id: '82e11d3d-b91d-43c9-9071-34d28e62e81d',
|
|
|
|
|
type: 'user',
|
2023-10-11 10:10:37 +02:00
|
|
|
contentStartNodeIds: ['simple-document-id'],
|
2023-10-11 10:20:03 +02:00
|
|
|
mediaStartNodeIds: ['f2f81a40-c989-4b6b-84e2-057cecd3adc1'],
|
2023-04-26 01:04:41 +12:00
|
|
|
name: 'Amelie Walker',
|
|
|
|
|
email: 'awalker1@domain.com',
|
|
|
|
|
languageIsoCode: 'Japanese',
|
|
|
|
|
state: UserStateModel.INACTIVE,
|
2023-10-16 08:26:58 +02:00
|
|
|
lastLoginDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
lastLockoutDate: null,
|
|
|
|
|
lastPasswordChangeDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
updateDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
createDate: '2023-10-12T18:30:32.879Z',
|
2023-04-26 01:04:41 +12:00
|
|
|
failedLoginAttempts: 0,
|
2023-05-16 16:33:02 +02:00
|
|
|
userGroupIds: ['c630d49e-4e7b-42ea-b2bc-edc0edacb6b1'],
|
2023-04-26 01:04:41 +12:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'aa1d83a9-bc7f-47d2-b288-58d8a31f5017',
|
|
|
|
|
type: 'user',
|
|
|
|
|
contentStartNodeIds: [],
|
|
|
|
|
mediaStartNodeIds: [],
|
|
|
|
|
name: 'Oliver Kim',
|
|
|
|
|
email: 'okim1@domain.com',
|
|
|
|
|
languageIsoCode: 'Russian',
|
|
|
|
|
state: UserStateModel.ACTIVE,
|
2023-10-16 08:26:58 +02:00
|
|
|
lastLoginDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
lastLockoutDate: null,
|
|
|
|
|
lastPasswordChangeDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
updateDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
createDate: '2023-10-12T18:30:32.879Z',
|
2023-04-26 01:04:41 +12:00
|
|
|
failedLoginAttempts: 0,
|
2023-05-16 16:33:02 +02:00
|
|
|
userGroupIds: ['c630d49e-4e7b-42ea-b2bc-edc0edacb6b1'],
|
2023-04-26 01:04:41 +12:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'ff2f4a50-d3d4-4bc4-869d-c7948c160e54',
|
|
|
|
|
type: 'user',
|
|
|
|
|
contentStartNodeIds: [],
|
|
|
|
|
mediaStartNodeIds: [],
|
|
|
|
|
name: 'Eliana Nieves',
|
|
|
|
|
email: 'enieves1@domain.com',
|
|
|
|
|
languageIsoCode: 'Spanish',
|
|
|
|
|
state: UserStateModel.INVITED,
|
2023-10-16 08:26:58 +02:00
|
|
|
lastLoginDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
lastLockoutDate: null,
|
|
|
|
|
lastPasswordChangeDate: null,
|
|
|
|
|
updateDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
createDate: '2023-10-12T18:30:32.879Z',
|
2023-04-26 01:04:41 +12:00
|
|
|
failedLoginAttempts: 0,
|
2023-05-16 16:33:02 +02:00
|
|
|
userGroupIds: ['c630d49e-4e7b-42ea-b2bc-edc0edacb6b1'],
|
2023-04-26 01:04:41 +12:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'c290c6d9-9f12-4838-8567-621b52a178de',
|
|
|
|
|
type: 'user',
|
|
|
|
|
contentStartNodeIds: [],
|
|
|
|
|
mediaStartNodeIds: [],
|
|
|
|
|
name: 'Jasmine Patel',
|
|
|
|
|
email: 'jpatel1@domain.com',
|
|
|
|
|
languageIsoCode: 'Hindi',
|
2023-10-12 20:38:02 +02:00
|
|
|
state: UserStateModel.LOCKED_OUT,
|
|
|
|
|
lastLoginDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
lastLockoutDate: '2023-10-12T18:30:32.879Z',
|
2023-10-16 08:26:58 +02:00
|
|
|
lastPasswordChangeDate: null,
|
2023-10-12 20:38:02 +02:00
|
|
|
updateDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
createDate: '2023-10-12T18:30:32.879Z',
|
|
|
|
|
failedLoginAttempts: 25,
|
2023-05-16 16:33:02 +02:00
|
|
|
userGroupIds: ['c630d49e-4e7b-42ea-b2bc-edc0edacb6b1'],
|
2022-10-04 15:15:45 +02:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2023-09-18 12:01:15 +02:00
|
|
|
export const umbUsersData = new UmbUserData(data);
|