@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
275 lines • 9.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserGroupService = void 0;
const index_js_1 = require("../core/index.js");
var ChildType;
(function (ChildType) {
ChildType["ROLES"] = "roles";
ChildType["USERS"] = "users";
})(ChildType || (ChildType = {}));
/**
* @description
* This service allows for managing user groups.
*/
class UserGroupService extends index_js_1.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);
* })();
* ```
*/
async detail(entityOrId) {
return super.detail(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);
* })();
* ```
*/
async create(entity) {
return super.create(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);
* })();
* ```
*/
async update(entity) {
return super.update(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);
* })();
* ```
*/
async list(filter = {}) {
return super.list(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.
*/
async delete(entityOrId) {
return super.delete(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'.
*/
async addRoleToGroup(entityOrId, childEntityOrSelf) {
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);
* })();
* ```
*/
async removeRoleFromGroup(entityOrId, childEntityOrSelf) {
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'.
*/
async addUserToGroup(entityOrId, childEntityOrSelf) {
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'.
*/
async removeUserFromGroup(entityOrId, childEntityOrSelf) {
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');
}
async addChild(type, userGroupOrId, childReference) {
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 = await this.fetch(url, { method, body, headers });
let data = await res.json();
data = data.managedObject;
return { res, data };
}
async removeChild(type, userGroupOrId, childReference) {
const childId = this.getIdString(childReference);
const url = `${this.getChildUrl(type, userGroupOrId)}/${encodeURIComponent(String(childId))}`;
const method = 'DELETE';
const headers = { accept: 'application/json' };
const res = await this.fetch(url, { method, headers });
const data = null;
return { res, data };
}
}
exports.UserGroupService = UserGroupService;
//# sourceMappingURL=UserGroupService.js.map