UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

311 lines 14.7 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { DEFINITIONS } from "../constants"; import { CultureLoadOption } from "../contracts/querying/culture-load-option"; import { EntityLoadConfiguration } from "../contracts/querying/entity-load-configuration"; import { ComparisonOperator, CompositeFilterOperator, CompositeQueryFilter, DefinitionQueryFilter, FilterDataType, PropertyQueryFilter, } from "../contracts/querying/filters"; import { PropertyLoadOption } from "../contracts/querying/property-load-option"; import { Query } from "../contracts/querying/query"; import { RelationLoadOption } from "../contracts/querying/relation-load-option"; import Guard from "../guard"; import SetPasswordRequestResource from "../models/set-password-request-resource"; import { TypeGuards } from "../type-guards"; import { ResponseHandler } from "./response-handler"; export class UsersClient { constructor(client) { this._client = client; } //#region Get users getUserIdAsync(username) { return __awaiter(this, void 0, void 0, function* () { Guard.stringNotNullOrEmpty(username); const user = yield this.getUserAsync(username, EntityLoadConfiguration.Minimal); return user != null ? user.id : null; }); } getUserIdsAsync(usernames) { return __awaiter(this, void 0, void 0, function* () { if (usernames == null) return null; if (usernames.length === 0) return []; Guard.arrayNoneNullOrEmptyString(usernames); const theQuery = new Query({ filter: new CompositeQueryFilter({ combineMethod: CompositeFilterOperator.And, children: [ new DefinitionQueryFilter({ name: DEFINITIONS["User"].definitionName, operator: ComparisonOperator.Equals, }), new PropertyQueryFilter({ operator: ComparisonOperator.Equals, dataType: FilterDataType.String, property: DEFINITIONS["User"].username, values: usernames, }), ], }), }); const result = yield this._client.querying.queryIdsAsync(theQuery); return result.items; }); } getUsernamesToIdsMapAsync(usernames) { return __awaiter(this, void 0, void 0, function* () { Guard.arrayNoneNullOrEmptyString(usernames); const loadConfiguration = new EntityLoadConfiguration(CultureLoadOption.None, new PropertyLoadOption(DEFINITIONS["User"].username)); const users = yield this.getUsersAsync(usernames, loadConfiguration); const userObject = {}; if (users.length) { users.forEach((user) => { const username = user.getPropertyValue(DEFINITIONS["User"].username); userObject[username] = user.id; }); } return userObject; }); } getUsernameAsync(id) { return __awaiter(this, void 0, void 0, function* () { Guard.validId(id); const loadConfiguration = new EntityLoadConfiguration(CultureLoadOption.None, new PropertyLoadOption(DEFINITIONS["User"].username), RelationLoadOption.None); const user = yield this.getUserAsync(id, loadConfiguration); if (user) { const username = user.getPropertyValue(DEFINITIONS["User"].username); return username; } else { return null; } }); } getUsernamesAsync(ids) { return __awaiter(this, void 0, void 0, function* () { Guard.validIds(ids); const loadConfiguration = new EntityLoadConfiguration(CultureLoadOption.None, new PropertyLoadOption(DEFINITIONS["User"].username), RelationLoadOption.None); const users = yield this.getUsersAsync(ids, loadConfiguration); const usersObject = {}; if (users && users.length) { users.forEach((user) => { const username = user.getPropertyValue(DEFINITIONS["User"].username); usersObject[user.id] = username; }); } return usersObject; }); } getUserAsync(param, loadConfiguration) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(param); if (typeof param === "string") { Guard.stringNotNullOrEmpty(param); const theQuery = new Query({ filter: new CompositeQueryFilter({ combineMethod: CompositeFilterOperator.And, children: [ new DefinitionQueryFilter({ name: DEFINITIONS["User"].definitionName, operator: ComparisonOperator.Equals, }), new PropertyQueryFilter({ operator: ComparisonOperator.Equals, dataType: FilterDataType.String, property: DEFINITIONS["User"].username, value: param, }), ], }), take: 1, }); const entity = yield this._client.querying.singleAsync(theQuery, loadConfiguration); return entity; } else { const user = yield this._client.entities.getAsync(param, loadConfiguration); return user; } }); } getUsersAsync(param, loadConfiguration) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(param); if (param.length === 0) { return []; } else if (TypeGuards.isNumberArray(param)) { Guard.validIds(param); const users = yield this._client.entities.getManyAsync(param, loadConfiguration); return users; } else { Guard.arrayNoneNullOrEmptyString(param); const theQuery = new Query({ filter: new CompositeQueryFilter({ combineMethod: CompositeFilterOperator.And, children: [ new DefinitionQueryFilter({ name: DEFINITIONS["User"].definitionName, operator: ComparisonOperator.Equals, }), new PropertyQueryFilter({ operator: ComparisonOperator.Equals, dataType: FilterDataType.String, property: DEFINITIONS["User"].username, values: param, }), ], }), }); const entities = yield this._client.querying.queryAsync(theQuery, loadConfiguration); return entities.items; } }); } //#endregion //#region Get user groups getUserGroupIdAsync(groupName) { return __awaiter(this, void 0, void 0, function* () { Guard.stringNotNullOrEmpty(groupName); const group = yield this.getUserGroupAsync(groupName, EntityLoadConfiguration.Minimal); return group != null ? group.id : null; }); } getUserGroupIdsAsync(groupNames) { return __awaiter(this, void 0, void 0, function* () { Guard.arrayNoneNullOrEmptyString(groupNames); const loadConfig = new EntityLoadConfiguration(CultureLoadOption.None, new PropertyLoadOption(DEFINITIONS["UserGroup"].groupName), RelationLoadOption.None); const userGroups = yield this.getUserGroupsAsync(groupNames, loadConfig); const userGroupObject = {}; if (userGroups && userGroups.length) { userGroups.forEach((userGroup) => { const groupName = userGroup.getPropertyValue(DEFINITIONS["UserGroup"].groupName); userGroupObject[groupName] = userGroup.id; }); } return userGroupObject; }); } getUserGroupNameAsync(id) { return __awaiter(this, void 0, void 0, function* () { Guard.validId(id); const loadConfig = new EntityLoadConfiguration(CultureLoadOption.None, new PropertyLoadOption(DEFINITIONS["UserGroup"].groupName), RelationLoadOption.None); const userGroup = yield this.getUserGroupAsync(id, loadConfig); if (!userGroup) { return null; } const groupName = userGroup.getPropertyValue(DEFINITIONS["UserGroup"].groupName); return groupName; }); } getUserGroupNamesAsync(ids) { return __awaiter(this, void 0, void 0, function* () { Guard.validIds(ids); const loadConfig = new EntityLoadConfiguration(CultureLoadOption.None, new PropertyLoadOption(DEFINITIONS["UserGroup"].groupName), RelationLoadOption.None); const userGroups = yield this.getUserGroupsAsync(ids, loadConfig); const userGroupObject = {}; if (userGroups && userGroups.length) { userGroups.forEach((userGroup) => { const groupName = userGroup.getPropertyValue(DEFINITIONS["UserGroup"].groupName); userGroupObject[userGroup.id] = groupName; }); } return userGroupObject; }); } getUserGroupAsync(param, loadConfiguration) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(param); if (typeof param === "string") { Guard.stringNotNullOrEmpty(param); const theQuery = new Query({ filter: new CompositeQueryFilter({ combineMethod: CompositeFilterOperator.And, children: [ new DefinitionQueryFilter({ name: DEFINITIONS["UserGroup"].definitionName, operator: ComparisonOperator.Equals, }), new PropertyQueryFilter({ operator: ComparisonOperator.Equals, dataType: FilterDataType.String, property: DEFINITIONS["UserGroup"].groupName, value: param, }), ], }), take: 1, }); const entity = yield this._client.querying.singleAsync(theQuery, loadConfiguration); return entity; } else { Guard.validId(param); const userGroup = this._client.entities.getAsync(param, loadConfiguration); return userGroup; } }); } getUserGroupsAsync(param, loadConfiguration) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(param); if (param.length === 0) { return []; } else if (TypeGuards.isNumberArray(param)) { Guard.validIds(param); const userGroups = yield this._client.entities.getManyAsync(param, loadConfiguration); return userGroups; } else { Guard.arrayNoneNullOrEmptyString(param); const theQuery = new Query({ filter: new CompositeQueryFilter({ combineMethod: CompositeFilterOperator.And, children: [ new DefinitionQueryFilter({ name: DEFINITIONS["UserGroup"].definitionName, operator: ComparisonOperator.Equals, }), new PropertyQueryFilter({ operator: ComparisonOperator.Equals, dataType: FilterDataType.String, property: DEFINITIONS["UserGroup"].groupName, values: param, }), ], }), }); const entities = yield this._client.querying.queryAsync(theQuery, loadConfiguration); return entities.items; } }); } //#endregion //#region Password management setPasswordAsync(id, password) { return __awaiter(this, void 0, void 0, function* () { Guard.validId(id); const link = yield this._client.linkHelper.setUserPasswordToLinkAsync(id); const content = new SetPasswordRequestResource(password); const response = yield this._client.raw.postAsync(link.href, content); ResponseHandler.handleErrors(response); }); } resetPasswordAsync(id) { return __awaiter(this, void 0, void 0, function* () { Guard.validId(id); const link = yield this._client.linkHelper.resetPasswordToLinkAsync(id); const response = yield this._client.raw.postAsync(link.href); ResponseHandler.handleErrors(response); }); } } //# sourceMappingURL=users-client.js.map