rbac-engine
Version:
Role-based access control engine with policy-based permissions
101 lines • 3.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatementBuilder = void 0;
const models_1 = require("../models");
const types_1 = require("./types");
class StatementBuilder {
constructor() {
this.actions = [];
this.resources = [];
}
allow(actions) {
this.effect = models_1.Effect.Allow;
this.actions = [...actions];
return this;
}
deny(actions) {
this.effect = models_1.Effect.Deny;
this.actions = [...actions];
return this;
}
on(resources) {
this.resources = [...resources];
return this;
}
when(conditions) {
this.conditions = { ...conditions };
return this;
}
activeFrom(startDate) {
this.startDate = startDate;
return this;
}
activeUntil(endDate) {
this.endDate = endDate;
return this;
}
validate() {
const errors = [];
if (!this.effect) {
errors.push('Effect must be set using either allow() or deny()');
}
if (this.actions.length === 0) {
errors.push('At least one action must be specified');
}
if (this.resources.length === 0) {
errors.push('At least one resource must be specified using on()');
}
if (this.actions.some(action => !action || typeof action !== 'string')) {
errors.push('All actions must be non-empty strings');
}
if (this.resources.some(resource => !resource || typeof resource !== 'string')) {
errors.push('All resources must be non-empty strings');
}
if (this.startDate) {
const startDate = new Date(this.startDate);
if (isNaN(startDate.getTime())) {
errors.push('StartDate must be a valid ISO format date string');
}
}
if (this.endDate) {
const endDate = new Date(this.endDate);
if (isNaN(endDate.getTime())) {
errors.push('EndDate must be a valid ISO format date string');
}
}
if (this.startDate && this.endDate) {
const startDate = new Date(this.startDate);
const endDate = new Date(this.endDate);
if (startDate.getTime() >= endDate.getTime()) {
errors.push('StartDate must be before EndDate');
}
}
return {
isValid: errors.length === 0,
errors
};
}
build() {
const validation = this.validate();
if (!validation.isValid) {
throw new types_1.BuilderValidationError('Invalid statement configuration', validation.errors);
}
const statement = {
Effect: this.effect,
Action: [...this.actions],
Resource: [...this.resources]
};
if (this.conditions) {
statement.Condition = { ...this.conditions };
}
if (this.startDate) {
statement.StartDate = this.startDate;
}
if (this.endDate) {
statement.EndDate = this.endDate;
}
return statement;
}
}
exports.StatementBuilder = StatementBuilder;
//# sourceMappingURL=statement-builder.js.map