Skip to content

Commit

Permalink
changing logging format to JSON, adding more time options for selfser…
Browse files Browse the repository at this point in the history
…ve and fixed an issue where the expired access is not revoked on service restart
  • Loading branch information
chenbishop committed Dec 15, 2023
1 parent 761730d commit 234f476
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 42 deletions.
2 changes: 1 addition & 1 deletion app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async function initApp(app) {
try {
const slackUserInfo = await client.users.info({user: event.user});
await refreshHome(event.user, slackUserInfo.user.profile.email);
logger.info(`${event.user} opened app home.`)
// logger.info(`${event.user} opened app home.`)
} catch (error) {
logger.error(error);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/SCHEMA.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This object defines a single profile
- _profileType_: Enum, only `oneOf` and `selfServeApproval` supported currently. _Default_: `oneOf`
- _applicableToGroup_: String, a Twingate group that a user must be in for them to see this profile. _Default_: "Everyone"
- _approverGroup_: String, `selfServerApproval` specific, a Twingate group a user must be in for them to approve the access request of the Profile. The group size should not exceed 20 members. _Required_.
- _timeOptions_: Enum, `selfServerApproval` specific, the duration options requesters can select during access request. Must be one of `Forever`, `1h`, `8h`, `24h`, `7d`, `30d`, `90d`. _Default_: `[Forever]`
- _timeOptions_: Enum, `selfServerApproval` specific, the duration options requesters can select during access request. Must be one of `Forever`, `1h`, `2h`, `4h`, `6h`, `8h`, `12h`, `24h`, `2d`, `3d`,`4d`, `5d`, `6d`, `7d`, `14d`,`21d`,`30d`, `60d`, `90d`. _Default_: `[Forever]`
- _groups_:
- _oneOf_: List of String, Twingate groups within the profile which the users can switch between. _Required_.
- _selfServeApproval_: List of String, Twingate groups within the profile which the users can request access to. _Required_
Expand Down
23 changes: 17 additions & 6 deletions profileFlows/OneOf.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {SlackProfileManager} from "../SlackProfileManager.mjs";
import {BaseProfile} from "./BaseProfile.mjs";
import {v4 as uuidv4} from "uuid";

export class OneOfProfile extends BaseProfile {
constructor(app, profileConfig, index) {
Expand All @@ -13,7 +14,7 @@ export class OneOfProfile extends BaseProfile {
app.view(`submit_profile-${index}`, this.submitProfileChange.bind(this));
}

async getAppHomeBlock(tgUser) {
async getAppHomeBlock(tgUser) {
const userGroupNames = tgUser.groups.map(group => group.name);
const currentActiveGroups = this.groups.filter(group => userGroupNames.includes(group))
let currentActiveGroupsString = currentActiveGroups.join(", ")
Expand Down Expand Up @@ -116,7 +117,17 @@ export class OneOfProfile extends BaseProfile {

await this.app.refreshHome(body.user.id, tgUser.email);

logger.info(`User '${tgUser.email}' changed profile '${this.profileName}' to group '${selectedGroup}'`)
const request = {
requestedProfile: this.profileName,
requestedProfileType: this.profileType,
requesterTwingateId: tgUser.id,
requesterEmail: tgUser.email,
newGroup: selectedGroup,
status: "Success"
}

// logger.info(`User '${tgUser.email}' changed profile '${this.profileName}' to group '${selectedGroup}'`)
console.log(JSON.stringify(request))
} catch (error) {
logger.error(error);
}
Expand All @@ -131,19 +142,19 @@ export class OneOfProfile extends BaseProfile {
;

if (groupNamesToRemove.length > 0) {
console.log(`User '${tgUser.email}' in profile '${this.profileName}' with selected group '${selectedGroup}' - removing group(s): ${groupNamesToRemove.map(g=>`'${g}'`).join(", ")}.`);
// console.log(`User '${tgUser.email}' in profile '${this.profileName}' with selected group '${selectedGroup}' - removing group(s): ${groupNamesToRemove.map(g=>`'${g}'`).join(", ")}.`);
const groupsIdsToRemove = await Promise.all(groupNamesToRemove.map(groupName => profileManager.lookupGroupByName(groupName)));
await Promise.all(groupsIdsToRemove.map(groupId => profileManager.removeUserFromGroup(groupId, tgUser.id)));
} else {
console.log(`User '${tgUser.email}' in profile '${this.profileName}' with selected group '${selectedGroup}' - no groups to remove.`);
// console.log(`User '${tgUser.email}' in profile '${this.profileName}' with selected group '${selectedGroup}' - no groups to remove.`);
}

if (typeof selectedGroup === "string" && !userGroupNames.includes(selectedGroup)) {
console.log(`User '${tgUser.email}' in profile '${this.profileName}' - adding group: ${selectedGroup}.`);
// console.log(`User '${tgUser.email}' in profile '${this.profileName}' - adding group: ${selectedGroup}.`);
const groupId = await profileManager.lookupGroupByName(selectedGroup);
await profileManager.addUserToGroup(groupId, tgUser.id)
} else {
console.log(`User '${tgUser.email}' in profile '${this.profileName}' with selected group '${selectedGroup}' - no group to add.`);
// console.log(`User '${tgUser.email}' in profile '${this.profileName}' with selected group '${selectedGroup}' - no group to add.`);
}

// sending group change message to user
Expand Down
Loading

0 comments on commit 234f476

Please sign in to comment.