"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Nodered = exports.Singin = exports.Audit = void 0;
var Config_1 = require("./Config");
var openflow_api_1 = require("@openiap/openflow-api");
var Crypt_1 = require("./Crypt");
var Audit = /** @class */ (function () {
    function Audit() {
    }
    Audit.LoginSuccess = function (user, type, provider, remoteip, clientagent, clientversion, parent) {
        var log = new Singin();
        openflow_api_1.Base.addRight(log, user._id, user.name, [openflow_api_1.Rights.read]);
        log.remoteip = remoteip;
        log.success = true;
        log.type = type;
        log.provider = provider;
        log.userid = user._id;
        log.name = user.name;
        log.username = user.username;
        log.clientagent = clientagent;
        log.clientversion = clientversion;
        Config_1.Config.db.InsertOne(log, "audit", 0, false, Crypt_1.Crypt.rootToken(), parent)
            .catch(function (error) { return console.error("failed InsertOne in LoginSuccess: " + error); });
    };
    Audit.ImpersonateSuccess = function (user, impostor, clientagent, clientversion, parent) {
        var log = new Singin();
        openflow_api_1.Base.addRight(log, user._id, user.name, [openflow_api_1.Rights.read]);
        openflow_api_1.Base.addRight(log, impostor._id, impostor.name, [openflow_api_1.Rights.read]);
        log.success = true;
        log.type = "impersonate";
        log.userid = user._id;
        log.name = user.name;
        log.username = user.username;
        log.impostoruserid = impostor._id;
        log.impostorname = impostor.name;
        log.impostorusername = impostor.username;
        log.clientagent = clientagent;
        log.clientversion = clientversion;
        Config_1.Config.db.InsertOne(log, "audit", 0, false, Crypt_1.Crypt.rootToken(), parent)
            .catch(function (error) { return console.error("failed InsertOne in ImpersonateSuccess: " + error); });
    };
    Audit.ImpersonateFailed = function (user, impostor, clientagent, clientversion, parent) {
        var log = new Singin();
        openflow_api_1.Base.addRight(log, user._id, user.name, [openflow_api_1.Rights.read]);
        openflow_api_1.Base.addRight(log, impostor._id, impostor.name, [openflow_api_1.Rights.read]);
        log.success = false;
        log.type = "impersonate";
        log.userid = user._id;
        log.name = user.name;
        log.username = user.username;
        log.impostoruserid = impostor._id;
        log.impostorname = impostor.name;
        log.clientagent = clientagent;
        log.clientversion = clientversion;
        Config_1.Config.db.InsertOne(log, "audit", 0, false, Crypt_1.Crypt.rootToken(), parent)
            .catch(function (error) { return console.error("failed InsertOne in ImpersonateFailed: " + error); });
    };
    Audit.LoginFailed = function (username, type, provider, remoteip, clientagent, clientversion, parent) {
        var log = new Singin();
        log.remoteip = remoteip;
        log.success = false;
        log.type = type;
        log.provider = provider;
        log.username = username;
        log.clientagent = clientagent;
        log.clientversion = clientversion;
        Config_1.Config.db.InsertOne(log, "audit", 0, false, Crypt_1.Crypt.rootToken(), parent)
            .catch(function (error) { return console.error("failed InsertOne in LoginFailed: " + error); });
    };
    Audit.NoderedAction = function (user, success, name, type, image, instancename, parent) {
        var log = new Nodered();
        openflow_api_1.Base.addRight(log, user._id, user.name, [openflow_api_1.Rights.read]);
        log.success = success;
        log.type = type;
        log.userid = user._id;
        log.name = name;
        log.username = user.username;
        log.instancename = instancename;
        log.image = image;
        if (!openflow_api_1.NoderedUtil.IsNullEmpty(image) && image.indexOf(':') > -1) {
            log.imagename = image.split(':')[0];
            log.imageversion = image.split(':')[1];
        }
        else {
            log.imagename = image;
        }
        if (!openflow_api_1.NoderedUtil.IsNullEmpty(instancename))
            log.name = instancename;
        Config_1.Config.db.InsertOne(log, "audit", 0, false, Crypt_1.Crypt.rootToken(), parent)
            .catch(function (error) { return console.error("failed InsertOne in LoginFailed: " + error); });
    };
    return Audit;
}());
exports.Audit = Audit;
var Singin = /** @class */ (function (_super) {
    __extends(Singin, _super);
    function Singin() {
        var _this = _super.call(this) || this;
        _this._type = "signin";
        return _this;
    }
    return Singin;
}(openflow_api_1.Base));
exports.Singin = Singin;
var Nodered = /** @class */ (function (_super) {
    __extends(Nodered, _super);
    function Nodered() {
        var _this = _super.call(this) || this;
        _this._type = "nodered";
        return _this;
    }
    return Nodered;
}(openflow_api_1.Base));
exports.Nodered = Nodered;
//# sourceMappingURL=Audit.js.map