An access review compares what each account is allowed to do with what it actually did. Permissions that were never used are the first ones to remove.
What to build
Find permissions each user has but did not use in the review period.
Requirements
- Line 1: N. Then N lines
user permission(grants). Line N + 2: M. Then M log linesday user permission(uses). Last line: the review period asfrom to, both days inclusive. - A grant is used if the log has that user and permission on a day within the period.
- For each user, in order of their first grant, print
user: revoke p1, p2with the unused permissions in grant order, oruser: all used.
Input
As described.
Output
One line per user.
Constraints
Up to 100 grants and 500 log lines. Days are whole numbers.
Example
Input:
3
rahim read
rahim write
anika read
2
5 rahim read
6 anika read
1 90
Output:
rahim: revoke write
anika: all used
Why it works
Permissions nobody used in a long period are not needed for the job as it is done today, so removing them shrinks the damage a stolen account could do without stopping any real work.
Examples
Example 1 · Input
3 rahim read rahim write anika read 2 5 rahim read 6 anika read 1 90
Expected output
rahim: revoke write anika: all used
Example 2 · Input
1 svc admin 0 1 30
Expected output
svc: revoke admin
Hints
Hint 1
Keep a Set of `user|permission` pairs seen in the period.
Hint 2
Remember the order in which users first appear in the grants.
Solution
Show one correct solution (try it yourself first)
const n = Number(prompt());
const grants = [];
for (let k = 0; k < n; k++) {
const [user, perm] = prompt().trim().split(/\s+/);
grants.push({ user, perm });
}
const m = Number(prompt());
const logs = [];
for (let k = 0; k < m; k++) {
const [day, user, perm] = prompt().trim().split(/\s+/);
logs.push({ day: Number(day), user, perm });
}
const [from, to] = prompt().trim().split(/\s+/).map(Number);
const used = new Set(logs.filter((l) => l.day >= from && l.day <= to).map((l) => `${l.user}|${l.perm}`));
const users = [];
for (const g of grants) if (!users.includes(g.user)) users.push(g.user);
for (const u of users) {
const unused = [];
for (const g of grants) if (g.user === u && !used.has(`${u}|${g.perm}`) && !unused.includes(g.perm)) unused.push(g.perm);
console.log(unused.length ? `${u}: revoke ${unused.join(", ")}` : `${u}: all used`);
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step