@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
309 lines • 10.7 kB
JavaScript
import { __awaiter } from "tslib";
import { Service } from '../core';
var ChildType;
(function (ChildType) {
ChildType["ROLES"] = "roles";
ChildType["USERS"] = "users";
})(ChildType || (ChildType = {}));
/**
* @description
* This service allows for managing user groups.
*/
export class UserGroupService extends Service {
constructor() {
super(...arguments);
this.baseUrl = 'user';
this.propertyName = 'groups';
}
get listUrl() {
return `${this.client.tenant}/groups`;
}
/**
* Gets the details of given user group.
*
* @param {string|number|IUserGroup} entityOrId Group's id or role object.
*
* @returns Returns promise object that is resolved with the IUserGroup wrapped by IResult.
*
* **Example**
* ```typescript
*
* const groupId: number = 1;
*
* (async () => {
* const {data, res} = await userGroupService.detail(roleId);
* })();
* ```
*/
detail(entityOrId) {
const _super = Object.create(null, {
detail: { get: () => super.detail }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.detail.call(this, entityOrId);
});
}
/**
* Creates a new user group.
*
* @param {IUserGroup} entity User Group object.
*
* @returns {IResult<IUserGroup>} Returns promise object that is resolved with
* the details of newly created user group.
*
* **Example**
* ```typescript
*
* const userGroupObject: IUserGroup = {
* name: "new user group"
* };
*
* (async () => {
* const {data, res} = await userGroupService.create(userGroupObject);
* })();
* ```
*/
create(entity) {
const _super = Object.create(null, {
create: { get: () => super.create }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.create.call(this, entity);
});
}
/**
* Updates user group data.
*
* @param {Partial<IUserGroup>} entity User group is partially updatable.
*
* @returns {IResult<IUserGroup>} Returns promise object that is resolved with the saved user group object.
*
* **Example**
* ```typescript
*
* const partialUpdateObject: Partial<IUserGroup> = {
* "id" : 1,
* "self" : "[URL to this resource]",
* "name" : "PlatformAdministrators",
* ...
* }
*
* (async () => {
* const {data, res} = await userGroupService.update(partialUpdateObject);
* })();
* ```
*/
update(entity) {
const _super = Object.create(null, {
update: { get: () => super.update }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.update.call(this, entity);
});
}
/**
* Gets the list of user groups filtered by parameters.
*
* @param {object} filter Object containing filters for querying User Groups.
*
* @returns Returns promise object that is resolved with the IUserGroup wrapped by IResultList.
*
* **Example**
* ```typescript
*
* const filter: object = {
* severity: Severity.MAJOR,
* pageSize: 100,
* withTotalPages: true
* };
*
* (async () => {
* const {data, res, paging} = await userGroupService.list(filter);
* })();
* ```
*/
list() {
const _super = Object.create(null, {
list: { get: () => super.list }
});
return __awaiter(this, arguments, void 0, function* (filter = {}) {
return _super.list.call(this, filter);
});
}
/**
* Removes user group.
*
* @param {number | IIdentified} entityOrId User group's id or user group object.
*
* @returns Returns promise object that is resolved with the IResult of null.
*
* **Example**
* ```typescript
*
* const userGroupId: number = 1;
*
* (async () => {
* const {data, res} = await userGroupService.delete(userGroupId);
* })();
* ```
* When group is removed, suitable audit records are created with type 'User'
* and activity 'User updated' with information that user has been removed from group.
*
* Please, note that the ADMINS and DEVICES groups can not be deleted.
*/
delete(entityOrId) {
const _super = Object.create(null, {
delete: { get: () => super.delete }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.delete.call(this, entityOrId);
});
}
/**
* Assign role to user group.
*
* @param {string | number | Partial<IUserGroup>} entityOrId User group's id or user group object.
* @param {string | Partial<ISource>} childEntityOrSelf Url to role resource or IRoleReference object.
*
* @returns Returns promise object that is resolved with the IRoleReference wrapped by IResult.
*
* **Example**
* ```typescript
*
* const userGroupId: number = 1;
* const roleResource: string = "[URL to the Role resource]";
*
* (async () => {
* const {data, res} = await userGroupService.addRoleToGroup(userGroupId, roleResource);
* })();
* ```
* When role is assigned to user, suitable audit record is created with type 'User' and activity 'User updated'.
*/
addRoleToGroup(entityOrId, childEntityOrSelf) {
return __awaiter(this, void 0, void 0, function* () {
return this.addChild(ChildType.ROLES, entityOrId, childEntityOrSelf);
});
}
/**
* Unassign role from user
*
* @param {string | number | Partial<IUserGroup>} entityOrId User group's id or user group object.
* @param {string | Partial<ISource>} childEntityOrSelf Url to user resource or IRoleReference object.
*
* @returns Returns promise object that is resolved with the IResult of null.
*
* **Example**
* ```typescript
*
* const userGroupId: number = 1;
* const userResource: string = "[URL to the Role resource]";
*
* (async () => {
* const {data, res} = await userGroupService.removeRoleFromGroup(userGroupId, userResource);
* })();
* ```
*/
removeRoleFromGroup(entityOrId, childEntityOrSelf) {
return __awaiter(this, void 0, void 0, function* () {
return this.removeChild(ChildType.ROLES, entityOrId, childEntityOrSelf);
});
}
/**
* Assign user to user group.
*
* @param {string | number | Partial<IUserGroup>} entityOrId User group's id or user group object.
* @param {string | Partial<ISource>} childEntityOrSelf Url to user resource or IUserReference object.
*
* @returns Returns promise object that is resolved with the IUserReference wrapped by IResult.
*
* **Example**
* ```typescript
*
* const userGroupId: number = 1;
* const userResource: string = "[URL to the User resource]";
*
* (async () => {
* const {data, res} = await userGroupService.addUserToGroup(userGroupId, userResource);
* })();
* ```
* When user is added to group, suitable audit record is created with type 'User' and activity 'User updated'.
*/
addUserToGroup(entityOrId, childEntityOrSelf) {
return __awaiter(this, void 0, void 0, function* () {
return this.addChild(ChildType.USERS, entityOrId, childEntityOrSelf);
});
}
/**
* Remove user from a group
*
* @param {string | number | Partial<IUserGroup>} entityOrId User group's id or user group object.
* @param {string | Partial<ISource>} childEntityOrSelf Url to user resource or IUserReference object.
*
* @returns Returns promise object that is resolved with the IResult of null.
*
* **Example**
* ```typescript
*
* const userGroupId: number = 1;
* const userResource: string = "[URL to the User resource]";
*
* (async () => {
* const {data, res} = await userGroupService.removeUserFromGroup(userGroupId, userResource);
* })();
* ```
* When user is removed from group, suitable audit record is created with type 'User' and activity 'User updated'.
*/
removeUserFromGroup(entityOrId, childEntityOrSelf) {
return __awaiter(this, void 0, void 0, function* () {
return this.removeChild(ChildType.USERS, entityOrId, childEntityOrSelf);
});
}
getSelf(childReference) {
if (typeof childReference === 'object' && childReference.self) {
return childReference.self;
}
else {
return childReference;
}
}
getChildUrl(type, userGroupOrId) {
return `${this.getDetailUrl(userGroupOrId)}/${type}`;
}
getChildReferenceAsBody(type, childReference) {
const childSelf = this.getSelf(childReference);
switch (type) {
case ChildType.ROLES:
return JSON.stringify({ role: { self: String(childSelf) } });
case ChildType.USERS:
return JSON.stringify({ user: { self: String(childSelf) } });
}
throw new Error('UserGroupService -> getChild -> unsupported child type');
}
addChild(type, userGroupOrId, childReference) {
return __awaiter(this, void 0, void 0, function* () {
const url = this.getChildUrl(type, userGroupOrId);
const method = 'POST';
const body = this.getChildReferenceAsBody(type, childReference);
const headers = {
accept: 'application/json',
'content-type': 'application/json'
};
const res = yield this.fetch(url, { method, body, headers });
let data = yield res.json();
data = data.managedObject;
return { res, data };
});
}
removeChild(type, userGroupOrId, childReference) {
return __awaiter(this, void 0, void 0, function* () {
const childId = this.getIdString(childReference);
const url = `${this.getChildUrl(type, userGroupOrId)}/${encodeURIComponent(String(childId))}`;
const method = 'DELETE';
const headers = { accept: 'application/json' };
const res = yield this.fetch(url, { method, headers });
const data = null;
return { res, data };
});
}
}
//# sourceMappingURL=UserGroupService.js.map