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: support for user linked project permissions #2390

Merged
merged 16 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export class HandleDefaultValuesUseCase {
return;
}

if (change.length != null && change.length === 1) {
change = change[0];
}

let parentEntity: Entity = await this.entityMapper.load(
Entity.extractTypeFromId(change),
change,
Expand All @@ -103,7 +107,7 @@ export class HandleDefaultValuesUseCase {
}

if (isArray) {
targetFormControl.setValue([parentEntity[defaultValueConfig.field]]);
targetFormControl.setValue([...parentEntity[defaultValueConfig.field]]);
} else {
targetFormControl.setValue(parentEntity[defaultValueConfig.field]);
}
Expand Down Expand Up @@ -193,7 +197,7 @@ export class HandleDefaultValuesUseCase {
return false;
}

if (isArray && formControl.value && formControl.value.size > 0) {
if (isArray && formControl.value && formControl.value.length > 0) {
return false;
}

Expand Down
20 changes: 15 additions & 5 deletions src/app/core/permissions/ability/ability.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { LoggingService } from "../../logging/logging.service";
import { get } from "lodash-es";
import { LatestEntityLoader } from "../../entity/latest-entity-loader";
import { SessionInfo, SessionSubject } from "../../session/auth/session-info";
import { CurrentUserSubject } from "../../session/current-user-subject";

/**
* This service sets up the `EntityAbility` injectable with the JSON defined rules for the currently logged in user.
Expand All @@ -20,6 +21,7 @@ export class AbilityService extends LatestEntityLoader<Config<DatabaseRules>> {
constructor(
private ability: EntityAbility,
private sessionInfo: SessionSubject,
private currentUser: CurrentUserSubject,
private permissionEnforcer: PermissionEnforcerService,
entityMapper: EntityMapperService,
logger: LoggingService,
Expand All @@ -41,10 +43,10 @@ export class AbilityService extends LatestEntityLoader<Config<DatabaseRules>> {
);
}

private updateAbilityWithUserRules(rules: DatabaseRules): Promise<any> {
private async updateAbilityWithUserRules(rules: DatabaseRules): Promise<any> {
// If rules object is empty, everything is allowed
const userRules: DatabaseRule[] = rules
? this.getRulesForUser(rules)
? await this.getRulesForUser(rules)
: [{ action: "manage", subject: "all" }];

if (userRules.length === 0) {
Expand All @@ -58,7 +60,7 @@ export class AbilityService extends LatestEntityLoader<Config<DatabaseRules>> {
return this.permissionEnforcer.enforcePermissionsOnLocalData(userRules);
}

private getRulesForUser(rules: DatabaseRules): DatabaseRule[] {
private async getRulesForUser(rules: DatabaseRules): Promise<DatabaseRule[]> {
const sessionInfo = this.sessionInfo.value;
if (!sessionInfo) {
return rules.public ?? [];
Expand All @@ -76,8 +78,16 @@ export class AbilityService extends LatestEntityLoader<Config<DatabaseRules>> {

private interpolateUser(
rules: DatabaseRule[],
user: SessionInfo,
sessionInfo: SessionInfo,
): DatabaseRule[] {
const user = this.currentUser.value;

if (user && user["projects"]) {
sessionInfo.projects = user["projects"];
} else {
sessionInfo.projects = [];
}

return JSON.parse(JSON.stringify(rules), (_that, rawValue) => {
if (rawValue[0] !== "$") {
return rawValue;
Expand All @@ -89,7 +99,7 @@ export class AbilityService extends LatestEntityLoader<Config<DatabaseRules>> {
// mapping the previously valid ${user.name} here for backwards compatibility
name = "user.entityId";
}
const value = get({ user }, name);
const value = get({ user: sessionInfo }, name);

if (typeof value === "undefined") {
throw new ReferenceError(`Variable ${name} is not defined`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export class PermissionEnforcerService {
private configService: ConfigService,
) {}

async enforcePermissionsOnLocalData(userRules: DatabaseRule[]) {
async enforcePermissionsOnLocalData(
userRules: DatabaseRule[],
): Promise<void> {
const userRulesString = JSON.stringify(userRules);
if (!this.sessionInfo.value || !this.userRulesChanged(userRulesString)) {
return;
Expand Down
5 changes: 5 additions & 0 deletions src/app/core/session/auth/session-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export interface SessionInfo {
*/
roles: string[];

/**
* List of linked projects
*/
projects?: string[];

/**
* ID of the entity which is connected with the user account.
*
Expand Down
Loading