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

feat(teams): Add hasTeamRole and hasSomeTeamRoles directives #324

Merged
merged 1 commit into from
Jul 10, 2023
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
76 changes: 76 additions & 0 deletions src/app/core/teams/directives/has-some-team-roles.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { NgIf } from '@angular/common';
import { Directive, Input, OnInit, inject } from '@angular/core';

import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';

import { AuthorizationService } from '../../auth/authorization.service';
import { SessionService } from '../../auth/session.service';
import { TeamAuthorizationService } from '../team-authorization.service';
import { TeamRole } from '../team-role.model';
import { Team } from '../team.model';

@UntilDestroy()
@Directive({
selector: '[hasSomeTeamRoles]',
hostDirectives: [
{
directive: NgIf,
inputs: ['ngIfElse: hasSomeTeamRolesElse', 'ngIfThen: hasSomeTeamRolesThen']
}
]
})
export class HasSomeTeamRolesDirective implements OnInit {
private team: Pick<Team, '_id'>;
private roles: Array<string | TeamRole> = [];
private andCondition = true;
private orCondition = false;

private ngIfDirective = inject(NgIf);
private sessionService = inject(SessionService);
private authorizationService = inject(AuthorizationService);
private teamAuthorizationService = inject(TeamAuthorizationService);

@Input()
set hasSomeTeamRoles(team: Pick<Team, '_id'>) {
this.team = team;
this.updateNgIf();
}

@Input()
set hasSomeTeamRolesRoles(roles: Array<string | TeamRole>) {
this.roles = roles;
this.updateNgIf();
}

@Input()
set hasSomeTeamRolesAnd(condition: boolean) {
this.andCondition = condition;
this.updateNgIf();
}

@Input()
set hasSomeTeamRolesOr(condition: boolean) {
this.orCondition = condition;
this.updateNgIf();
}

ngOnInit() {
this.sessionService
.getSession()
.pipe(untilDestroyed(this))
.subscribe(() => {
this.updateNgIf();
});
}

protected checkPermission(): boolean {
return (
this.authorizationService.isAdmin() ||
this.teamAuthorizationService.hasSomeRoles(this.team, this.roles)
);
}

private updateNgIf() {
this.ngIfDirective.ngIf = this.orCondition || (this.andCondition && this.checkPermission());
}
}
76 changes: 76 additions & 0 deletions src/app/core/teams/directives/has-team-role.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { NgIf } from '@angular/common';
import { Directive, Input, OnInit, inject } from '@angular/core';

import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';

import { AuthorizationService } from '../../auth/authorization.service';
import { SessionService } from '../../auth/session.service';
import { TeamAuthorizationService } from '../team-authorization.service';
import { TeamRole } from '../team-role.model';
import { Team } from '../team.model';

@UntilDestroy()
@Directive({
selector: '[hasTeamRole]',
hostDirectives: [
{
directive: NgIf,
inputs: ['ngIfElse: hasTeamRoleElse', 'ngIfThen: hasTeamRoleThen']
}
]
})
export class HasTeamRoleDirective implements OnInit {
private team: Pick<Team, '_id'>;
private role: string | TeamRole;
private andCondition = true;
private orCondition = false;

private ngIfDirective = inject(NgIf);
private sessionService = inject(SessionService);
private authorizationService = inject(AuthorizationService);
private teamAuthorizationService = inject(TeamAuthorizationService);

@Input()
set hasTeamRole(team: Pick<Team, '_id'>) {
this.team = team;
this.updateNgIf();
}

@Input()
set hasTeamRoleRole(role: string | TeamRole) {
this.role = role;
this.updateNgIf();
}

@Input()
set hasTeamRoleAnd(condition: boolean) {
this.andCondition = condition;
this.updateNgIf();
}

@Input()
set hasTeamRoleOr(condition: boolean) {
this.orCondition = condition;
this.updateNgIf();
}

ngOnInit() {
this.sessionService
.getSession()
.pipe(untilDestroyed(this))
.subscribe(() => {
this.updateNgIf();
});
}

protected checkPermission(): boolean {
return (
this.authorizationService.isAdmin() ||
this.teamAuthorizationService.hasRole(this.team, this.role)
);
}

private updateNgIf() {
this.ngIfDirective.ngIf = this.orCondition || (this.andCondition && this.checkPermission());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

<div class="mb-3 ml-auto">
<button
*ngIf="canManageTeam"
(click)="addMembers()"
type="button"
class="btn btn-primary ml-auto"
(click)="addMembers()"
*hasTeamRole="team; role: 'admin'"
>
<span class="fa-solid fa-plus"></span> Add New
</button>
Expand Down Expand Up @@ -71,7 +71,7 @@
class="dropdown dropdown-table-inline"
dropdown
container="body"
*ngIf="canManageTeam; else readOnlyRole"
*hasTeamRole="team; role: 'admin'; else: readOnlyRole"
>
<span class="dropdown-toggle" dropdownToggle>
<span class="mr-1">{{ member.roleDisplay }}</span>
Expand All @@ -95,12 +95,12 @@
<th cdk-header-cell *cdkHeaderCellDef></th>
<td cdk-cell *cdkCellDef="let member">
<a
*ngIf="member.explicit"
class="btn-icon"
tooltip="Remove Member"
container="body"
placement="bottom"
(click)="removeMember(member)"
*hasTeamRole="team; role: 'admin'; and: member.explicit"
>
<span class="fa-solid fa-lg fa-trash"></span>
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export class ListTeamMembersComponent implements OnChanges, OnDestroy, OnInit {

isUserAdmin = false;

canManageTeam = false;

teamRoleOptions: any[] = TeamRole.ROLES;

typeFilterOptions: ListFilterOption[] = [
Expand Down Expand Up @@ -97,9 +95,6 @@ export class ListTeamMembersComponent implements OnChanges, OnDestroy, OnInit {
}
this.alertService.clearAllAlerts();

this.canManageTeam =
this.authorizationService.isAdmin() || this.teamAuthorizationService.isAdmin(this.team);

this.sessionService
.getSession()
.pipe(isNotNullOrUndefined(), untilDestroyed(this))
Expand All @@ -108,9 +103,9 @@ export class ListTeamMembersComponent implements OnChanges, OnDestroy, OnInit {
this.isUserAdmin = this.authorizationService.isAdmin();
});

this.displayedColumns = this.columns
.filter((column) => this.canManageTeam || column !== 'actions')
.filter((column) => this.team.implicitMembers || column !== 'explicit');
this.displayedColumns = this.columns.filter(
(column) => this.team.implicitMembers || column !== 'explicit'
);
}

ngOnChanges(changes: SimpleChanges) {
Expand Down Expand Up @@ -210,7 +205,7 @@ export class ListTeamMembersComponent implements OnChanges, OnDestroy, OnInit {
)
.subscribe(() => {
// If we successfully removed the role from ourselves, redirect away
this.router.navigate(['/teams', { clearCachedFilter: true }]);
this.router.navigate(['/teams']);
});
} else if (!member.explicit) {
// Member is implicitly in team, should explicitly add this member with the desired role
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/teams/team-authorization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class TeamAuthorizationService {

private roleToString(role: string | TeamRole) {
if (typeof role !== 'string') {
return role.role;
return role?.role;
}
return role;
}
Expand Down
6 changes: 5 additions & 1 deletion src/app/core/teams/teams.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { SystemAlertModule } from '../../common/system-alert.module';
import { TableModule } from '../../common/table.module';
import { AddMembersModalComponent } from './add-members-modal/add-members-modal.component';
import { CreateTeamComponent } from './create-team/create-team.component';
import { HasSomeTeamRolesDirective } from './directives/has-some-team-roles.directive';
import { HasTeamRoleDirective } from './directives/has-team-role.directive';
import { TeamsHelpComponent } from './help/teams-help.component';
import { ListTeamMembersComponent } from './list-team-members/list-team-members.component';
import { ListSubTeamsComponent } from './list-teams/list-sub-teams.component';
Expand Down Expand Up @@ -57,7 +59,9 @@ import { ViewTeamComponent } from './view-team/view-team.component';
ViewTeamComponent,
TeamsHelpComponent,
TeamSelectInputComponent,
GeneralDetailsComponent
GeneralDetailsComponent,
HasTeamRoleDirective,
HasSomeTeamRolesDirective
],
providers: [TeamsHelpComponent]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ <h2>Details</h2>
class="btn btn-outline-secondary ml-3"
type="button"
[disabled]="isEditing"
*ngIf="canManageTeam"
(click)="edit()"
*hasTeamRole="team; role: 'admin'"
>
<span class="fa-solid fa-lg fa-pencil"></span>
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { first, map, switchMap, tap } from 'rxjs/operators';

import { SystemAlertService } from '../../../../common/system-alert/system-alert.service';
import { AuthorizationService } from '../../../auth/authorization.service';
import { SessionService } from '../../../auth/session.service';
import { ConfigService } from '../../../config.service';
import { TeamAuthorizationService } from '../../team-authorization.service';
import { Team } from '../../team.model';
import { TeamsService } from '../../teams.service';

Expand All @@ -25,8 +23,6 @@ export class GeneralDetailsComponent implements OnInit {
nestedTeamsEnabled = false;
implicitMembersStrategy?: string;

canManageTeam = false;

isEditing = false;

constructor(
Expand All @@ -35,9 +31,7 @@ export class GeneralDetailsComponent implements OnInit {
private alertService: SystemAlertService,
private configService: ConfigService,
private sessionService: SessionService,
private teamsService: TeamsService,
private authorizationService: AuthorizationService,
private teamAuthorizationService: TeamAuthorizationService
private teamsService: TeamsService
) {}

ngOnInit(): void {
Expand All @@ -62,8 +56,6 @@ export class GeneralDetailsComponent implements OnInit {
updateTeam(team: Team) {
if (team) {
this.team = team;
this.canManageTeam =
this.authorizationService.isAdmin() || this.teamAuthorizationService.isAdmin(team);
} else {
this.router.navigate(['resource/invalid', { type: 'team' }]);
}
Expand All @@ -86,7 +78,7 @@ export class GeneralDetailsComponent implements OnInit {
this.isEditing = false;
if (team) {
this.team = team;
this.alertService.addAlert('Updated team metadata', 'success', 5000);
this.alertService.addAlert('Team updated', 'success', 5000);
}
}),
switchMap(() => this.sessionService.reloadSession()),
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/teams/view-team/view-team.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1>{{ team.name }}</h1>
type="button"
class="btn btn-link ml-auto"
(click)="remove(team)"
*ngIf="canManageTeam"
*hasTeamRole="team; role: 'admin'"
>
Delete Team
</button>
Expand Down
Loading