import React from 'react';
import {compact, isString} from 'lodash';
import ResourceLink from '../generic/resource_link';
import RoleLink from '../generic/role_link';
function humanizeKindResource(e) {
var msg;
if (e.action === 'check') {
if (e.allowed) {
msg = [
'performed ',
({e.privilege}),
' on ',
];
} else {
msg = [
'was ',
(denied permission),
' to ',
({e.privilege}),
' ',
];
}
} else if (e.action === 'create') {
msg = [
' created ',
,
' owned by ',
];
} else if (e.action === 'update') {
// this is suspicious, but CLI audit does this
msg = [
' gave ',
,
' to ',
];
} else if (e.action === 'destroy') {
msg = [
' deleted ',
];
} else if (e.action === 'permit') {
msg = [
' permitted ',
,
' to ',
({e.privilege}),
' ',
];
if (e.grant_option) {
msg.push(' with grant option');
}
} else if (e.action === 'deny') {
msg = [
' denied ',
({e.privilege}),
' from ',
,
' on ',
];
} else if (e.action === 'permitted_roles') {
msg = [
' listed roles permitted to ',
({e.privilege}),
' on ',
];
}
return msg;
}
function humanizeKindRole(e) {
var msg;
if (e.action === 'check') {
if (e.allowed) {
msg = [
' performed ',
({e.privilege}),
' on ',
];
} else {
msg = [
' was ',
(denied permission),
' to ',
({e.privilege}),
' on ',
];
}
} else if (e.action === 'grant') {
/* what was the point of commenting this out? */
msg = [
' granted role ',
,
' to ',
];
if (e.admin_option) {
msg.push(' with admin permission');
} else {
msg.push(' without admin permission');
}
} else if (e.action === 'revoke') {
msg = [
' revoked role ',
,
' from ',
];
} else if (e.action === 'create') {
msg = [
' created role ',
];
}
return msg;
}
function humanizeKindAnnotation(e) {
var msg;
if (e.action === 'update') {
msg = [
' updated annotation on ',
];
}
return msg;
}
function humanizeKindAudit(e) {
var msg,
actionPart = compact([e.facility, e.action]).join(':'),
parts = [actionPart];
if (isString(e.role)) {
parts.push(' by ');
parts.push();
}
if (isString(e.resource_id)) {
parts.push(' on');
parts.push();
}
if (isString(e.allowed)) {
parts.push(' (allowed: ' + e.allowed + ')');
}
msg = [' reported ', parts];
if (isString(e.audit_message)) {
msg.push('; message: ');
msg.push(e.audit_message);
}
return msg;
}
// TODO: make message a separate React Class
export default function humanizeEvent(e) {
// copy of SHORT_FORMATS logic from cli-ruby:lib/conjur/command/audit.rb
var msg = [];
if (e.kind === 'resource') {
msg = humanizeKindResource(e);
} else if (e.kind === 'role') {
msg = humanizeKindRole(e);
} else if (e.kind === 'annotation') {
msg = humanizeKindAnnotation(e);
} else if (e.kind === 'audit') {
msg = humanizeKindAudit(e);
} else {
msg = [' unknown event: ' + e.kind + ':' + e.action + '!'];
}
if (isString(e.error)) {
msg.push(' (failed with ' + e.error + ')');
}
return msg;
}