@omnia/fx-models
Version:
Provide Omnia Fx Models Stuffs.
96 lines (95 loc) • 3.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArchiveSortColumn = exports.Identity = void 0;
const models_1 = require("@omnia/fx-models/internal-do-not-import-from-here/shared/models");
const IdentityTypes_1 = require("./IdentityTypes");
class Identity {
constructor(id) {
this.id = id;
}
toIdString() {
return Identity.getIdAsString(this.id, this.type);
}
static parse(identity) {
if (typeof identity === "string") {
return this.createFromString(identity);
}
return this.createFromString(Identity.getIdAsString(identity.id, identity.type));
}
static isValid(identity) {
try {
Identity.createFromString(identity.toIdString());
return true;
}
catch (ex) {
return false;
}
}
static createFromString(idAndType) {
if (!idAndType) {
return null;
}
// soft convert for current user token from old people picker
if (idAndType.toLowerCase().indexOf("2faa499e-907b-4fd2-b78b-420090ec813e") > -1) {
return this["createIdentity"](IdentityTypes_1.IdentityTypes.User, new models_1.Guid("2faa499e-907b-4fd2-b78b-420090ec813e")); // from Internal Identiy, to prevent circular reference
}
if (typeof idAndType === "object") {
if (!idAndType.toString()) {
throw new Error(`Can't create identity from string ${JSON.stringify(idAndType)} type is not valid`);
}
return this.createFromString(idAndType.toString());
}
let id = null;
let type = null;
const startIndex = idAndType.lastIndexOf("[");
const endIndex = idAndType.lastIndexOf("]");
if (startIndex > -1 && endIndex > -1) {
type = parseInt(idAndType.substring(startIndex + 1, endIndex));
id = this.getGuidValue(idAndType);
}
if (id === null) {
throw new Error(`Can't create identity from string ${idAndType} type is not valid`);
}
return this["createIdentity"](type, id); // from Internal Identity, to prevent circular reference
}
static getGuidValue(value) {
if (value) {
const matchs = value.match(/([A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12})/i);
if (matchs)
return matchs[0];
}
return null;
}
static getIdAsString(id, type) {
return `${id.toString().toLowerCase()}[${type}]`;
}
}
exports.Identity = Identity;
var ArchiveSortColumn;
(function (ArchiveSortColumn) {
ArchiveSortColumn[ArchiveSortColumn["DisplayName"] = 1] = "DisplayName";
ArchiveSortColumn[ArchiveSortColumn["Email"] = 2] = "Email";
ArchiveSortColumn[ArchiveSortColumn["Username"] = 3] = "Username";
ArchiveSortColumn[ArchiveSortColumn["IdentityType"] = 4] = "IdentityType";
ArchiveSortColumn[ArchiveSortColumn["ProviderId"] = 5] = "ProviderId";
ArchiveSortColumn[ArchiveSortColumn["TypeId"] = 6] = "TypeId";
})(ArchiveSortColumn = exports.ArchiveSortColumn || (exports.ArchiveSortColumn = {}));
// only add extension methods when running in browser, not in node.js
if (globalThis.omnia) {
Object.defineProperty(Object.prototype, "toIdString", {
value: function () {
return `${this.id.toString().toLowerCase()}[${this.type}]`;
},
writable: false,
configurable: false,
enumerable: false
});
Object.defineProperty(String.prototype, "toIdString", {
value: function () {
return this.toString();
},
writable: false,
configurable: false,
enumerable: false
});
}