Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/IoT-1155 user group at user creation #174

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .github/workflows/on-push-pr.action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ jobs:
vulnerabilities-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
name: Checkout repository
- uses: debricked/actions/scan@v1
name: Run a vulnerability scan
- uses: actions/checkout@v4
- uses: debricked/actions@v4
env:
# Token must have API access scope to run scans
DEBRICKED_TOKEN: ${{ secrets.DEBRICKED_TOKEN }}
code-build:
runs-on: ubuntu-latest
Expand Down
14 changes: 13 additions & 1 deletion src/app/admin/permission/permission.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class PermissionService {
orderByColumn?: string,
orderByDirection?: string,
userId?: number,
organisationId?: number
organisationId?: number,
ignoreGlobalAdmin?: boolean
): Observable<PermissionGetManyResponse> {
if (userId || organisationId) {
return this.restService.get(this.endpoint, {
Expand All @@ -61,17 +62,28 @@ export class PermissionService {
sort: orderByDirection,
userId: userId,
organisationId: organisationId,
ignoreGlobalAdmin: ignoreGlobalAdmin,
});
} else {
return this.restService.get(this.endpoint, {
limit: limit,
offset: offset,
orderOn: orderByColumn,
sort: orderByDirection,
ignoreGlobalAdmin: ignoreGlobalAdmin,
});
}
}

getPermissionsWithoutUsers(userId?: number): Observable<PermissionGetManyResponse> {
return this.restService.get(this.endpoint + "/getAllPermissionsWithoutUsers", {
limit: 1000,
offset: 0,
userId: userId ?? undefined,
ignoreGlobalAdmin: true,
});
}

deletePermission(id: number) {
return this.restService.delete(this.endpoint, id);
}
Expand Down
19 changes: 19 additions & 0 deletions src/app/admin/users/user-edit/user-edit.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@
/>
</div>
</div>

<div class="row mb-5">
<div class="form-group mt-3 col-12">
<label class="form-label" id="permissions" for="permissions">{{ "QUESTION.PERMISSION.SELECT-PERMISSION" | translate }}</label
>*
<mat-select
class="form-control"
augusthjerrild marked this conversation as resolved.
Show resolved Hide resolved
name="permissions"
[compareWith]="compare"
[(ngModel)]="user.permissionIds"
[multiple]="true"
>
<mat-option *ngFor="let permission of permissions" [value]="permission.id">
{{ permission.name }}
</mat-option>
</mat-select>
</div>
</div>

<div class="row mb-5">
<mat-slide-toggle [(ngModel)]="user.active" id="active" name="active">
{{ "USERS.FORM.ACTIVE" | translate }}</mat-slide-toggle
Expand Down
66 changes: 48 additions & 18 deletions src/app/admin/users/user-edit/user-edit.component.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { HttpErrorResponse } from "@angular/common/http";
import { Component, OnInit } from "@angular/core";
import { UntypedFormGroup } from "@angular/forms";
import { Component, OnDestroy, OnInit } from "@angular/core";
import { UntypedFormControl, UntypedFormGroup } from "@angular/forms";
import { ActivatedRoute } from "@angular/router";
import { UserRequest } from "../user.model";
import { TranslateService } from "@ngx-translate/core";
import { UserService } from "../user.service";
import { Subscription } from "rxjs";
import { Subject, Subscription } from "rxjs";
import { Location } from "@angular/common";
import { PermissionType } from "@app/admin/permission/permission.model";
import { AuthService, CurrentUserInfoResponse } from "@auth/auth.service";
import { SharedVariableService } from "@shared/shared-variable/shared-variable.service";
import { PermissionResponse, PermissionType } from "@app/admin/permission/permission.model";
import { MeService } from "@shared/services/me.service";
import { OrganizationAccessScope } from "@shared/enums/access-scopes";
import { PermissionService } from "@app/admin/permission/permission.service";
import { SharedVariableService } from "@shared/shared-variable/shared-variable.service";

@Component({
selector: "app-user-edit",
templateUrl: "./user-edit.component.html",
styleUrls: ["./user-edit.component.scss"],
})
export class UserEditComponent implements OnInit {
user = new UserRequest();
export class UserEditComponent implements OnInit, OnDestroy {
public user = new UserRequest();
public permissions: PermissionResponse[];
public permissionsSubscription: Subscription;
public errorMessage: string;
public errorMessages: any;
public errorFields: string[];
Expand All @@ -28,20 +30,22 @@ export class UserEditComponent implements OnInit {
public backButtonTitle = "";
public title = "";
public submitButton = "";
id: number;
subscription: Subscription;
isGlobalAdmin = false;
isKombit: boolean;
canEdit: boolean;
public id: number;
public subscription: Subscription;
public isGlobalAdmin = false;
public isKombit: boolean;
public canEdit: boolean;
public permissionMultiCtrl: UntypedFormControl = new UntypedFormControl();
private _onDestroy = new Subject<void>();

constructor(
private translate: TranslateService,
private route: ActivatedRoute,
private userService: UserService,
private location: Location,
private authService: AuthService,
private sharedVariableService: SharedVariableService,
private meService: MeService
private meService: MeService,
private permissionService: PermissionService,
private sharedVariableService: SharedVariableService
) {}

ngOnInit(): void {
Expand All @@ -60,6 +64,7 @@ export class UserEditComponent implements OnInit {
}
this.amIGlobalAdmin();
this.canEdit = this.meService.hasAccessToTargetOrganization(OrganizationAccessScope.UserAdministrationWrite);
this.getPermissions(this.sharedVariableService.getUserInfo().user.id);
}

private getUser(id: number) {
Expand All @@ -70,6 +75,8 @@ export class UserEditComponent implements OnInit {
this.user.active = response.active;
this.user.globalAdmin = response.permissions.some(perm => perm.name === PermissionType.GlobalAdmin);
this.isKombit = response.nameId != null;
this.user.permissionIds = response.permissions.map(pm => pm.id);

// We cannot set the password.
});
}
Expand All @@ -80,8 +87,7 @@ export class UserEditComponent implements OnInit {

private create(): void {
this.userService.post(this.user).subscribe(
response => {
console.log(response);
() => {
this.routeBack();
},
(error: HttpErrorResponse) => {
Expand Down Expand Up @@ -132,4 +138,28 @@ export class UserEditComponent implements OnInit {
routeBack(): void {
this.location.back();
}

public compare(matOptionValue: number, ngModelObject: number): boolean {
return matOptionValue === ngModelObject;
}

private getPermissions(userId: number) {
this.permissionsSubscription = this.permissionService
.getPermissionsWithoutUsers(this.meService.hasGlobalAdmin() ? undefined : userId)
.subscribe(res => {
this.permissions = res.data.sort((a, b) => a.name.localeCompare(b.name, "da-DK", { numeric: true }));
if (!this.id) {
this.permissionMultiCtrl.setValue(this.user.permissionIds);
}
});
}

ngOnDestroy() {
// prevent memory leak by unsubscribing
if (this.permissionsSubscription) {
this.permissionsSubscription.unsubscribe();
}
this._onDestroy.next();
this._onDestroy.complete();
}
}
1 change: 1 addition & 0 deletions src/app/admin/users/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class UserRequest {
active: boolean;
globalAdmin: boolean;
showWelcomeScreen: boolean;
permissionIds: number[]
}

export interface UserResponse {
Expand Down
Loading