From 44df0daddf03b597cf2744b345979c6fc4af71ce Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 31 Oct 2024 18:49:52 +0000 Subject: [PATCH] Fixed several issues with policy and logins --- .../impersonation.component.html | 2 +- .../impersonation/impersonation.component.ts | 2 +- .../policy-table/policy-table.component.html | 2 +- .../policy-table/policy-table.component.ts | 49 +++++++++++-------- .../api/api-authentication.service.ts | 4 +- 5 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/app/impersonation/impersonation.component.html b/src/app/impersonation/impersonation.component.html index 070a2a1..02c589f 100644 --- a/src/app/impersonation/impersonation.component.html +++ b/src/app/impersonation/impersonation.component.html @@ -9,7 +9,7 @@ User to impersonate {{getUsername()}} - @for (user of users; track user) { + @for (user of users; track user.id) { {{ user.name }} } diff --git a/src/app/impersonation/impersonation.component.ts b/src/app/impersonation/impersonation.component.ts index 1ee6107..729b902 100644 --- a/src/app/impersonation/impersonation.component.ts +++ b/src/app/impersonation/impersonation.component.ts @@ -36,7 +36,7 @@ export class ImpersonationComponent implements OnInit { }).subscribe((data: any) => { this.users = data.filter((d: any) => d.name != this.auth.getUsername()); const id = this.auth.getImpersonatedId(); - if (this.users.find(u => u.id == id)) { + if (id && this.users.find(u => u.id == id)) { this.impersonated = id; } }); diff --git a/src/app/policy-table/policy-table.component.html b/src/app/policy-table/policy-table.component.html index d43dd39..9e5335d 100644 --- a/src/app/policy-table/policy-table.component.html +++ b/src/app/policy-table/policy-table.component.html @@ -17,7 +17,7 @@ @if (policy.editing) { } - @if (!policy.editing) { + @if (!policy.editing && groups[policy.group_id]) { {{groups[policy.group_id].name}} } diff --git a/src/app/policy-table/policy-table.component.ts b/src/app/policy-table/policy-table.component.ts index 5f6780c..bb5c3e7 100644 --- a/src/app/policy-table/policy-table.component.ts +++ b/src/app/policy-table/policy-table.component.ts @@ -1,4 +1,4 @@ -import { Component, ElementRef, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'; +import { Component, ElementRef, Input, isDevMode, OnDestroy, OnInit, ViewChild } from '@angular/core'; import { MatTable, MatTableModule } from '@angular/material/table'; import { MatIconModule } from '@angular/material/icon'; import EventService from '../shared/services/EventService'; @@ -44,6 +44,8 @@ export class PolicyTableComponent implements OnInit, OnDestroy { for (let policy of response.data) { this.policies.push(new Policy(policy.id, policy.group_id, policy.path, policy.usage, policy.span, "", false, false)); } + if (isDevMode()) + console.log('policies', this.policies); this.table.renderRows(); } else if (response.request.type == "create_policy") { console.log("create policy", response); @@ -83,6 +85,7 @@ export class PolicyTableComponent implements OnInit, OnDestroy { } } else if (response.request.type == "get_permissions") { this.groups = Object.assign({}, ...response.data.groups.map((g: any) => ({ [g.id]: g }))); + console.log('groups', response.data) } }); this.hermes.fetchPolicies(); @@ -98,13 +101,21 @@ export class PolicyTableComponent implements OnInit, OnDestroy { if (!policy.editing) return; - policy.path = policy.old_path ?? ''; - policy.usage = policy.old_usage ?? 1; - policy.span = policy.old_span ?? 5000; - policy.old_path = undefined; - policy.old_span = undefined; - policy.old_usage = undefined; - policy.editing = false; + if (policy.isNew) { + const index = this.policies.indexOf(policy); + if (index >= 0) { + this.policies.splice(index, 1); + this.table.renderRows(); + } + } else { + policy.path = policy.old_path ?? ''; + policy.usage = policy.old_usage ?? 1; + policy.span = policy.old_span ?? 5000; + policy.old_path = undefined; + policy.old_span = undefined; + policy.old_usage = undefined; + policy.editing = false; + } } delete(policy: Policy) { @@ -124,12 +135,20 @@ export class PolicyTableComponent implements OnInit, OnDestroy { console.log('group must be valid.'); return; } - const group = Object.values(this.groups).find(g => g.name); + const group = Object.values(this.groups).find(g => g.name == policy.temp_group_name); if (group == null) { console.log('group does not exist.'); return; } + if (policy.isNew) { + const match = this.policies.find(p => p.group_id == group.id && p.path == policy.path); + if (match) { + console.log('policy already exists'); + return; + } + } + if (isNaN(policy.usage)) { console.log('usage must be a whole number.'); return; @@ -156,18 +175,6 @@ export class PolicyTableComponent implements OnInit, OnDestroy { return; } - let group_id = policy?.group_id; - for (let groupId in this.groups) { - if (this.groups[groupId].name == policy?.temp_group_name) { - group_id = groupId; - break; - } - } - if (policy?.temp_group_name != this.groups[group_id].name) { - console.log('no group found.'); - return; - } - if (policy.isNew) { this.hermes.createPolicy(group.id, policy.path, policy.usage, policy.span); } else { diff --git a/src/app/shared/services/api/api-authentication.service.ts b/src/app/shared/services/api/api-authentication.service.ts index 2836ff3..927da02 100644 --- a/src/app/shared/services/api/api-authentication.service.ts +++ b/src/app/shared/services/api/api-authentication.service.ts @@ -25,11 +25,11 @@ export class ApiAuthenticationService { } getImpersonatedId() { - return this.user.impersonation.id; + return this.user.impersonation?.id; } getUsername() { - return this.user.name; + return this.user?.name; } update() {