Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
sofyalaski committed Aug 26, 2024
1 parent 9fe14c3 commit 303c146
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/casl/action.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,4 @@ export enum Action {
InstrumentUpdate = "instrument_update",
InstrumentCreate = "instrument_create",
InstrumentDelete = "instrument_delete",
}
}
62 changes: 38 additions & 24 deletions src/casl/casl-ability.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export class CaslAbilityFactory {
const { can, cannot, build } = new AbilityBuilder(
createMongoAbility<PossibleAbilities, Conditions>,
);

if (!user) {
/**
* unauthenticated users
Expand Down Expand Up @@ -390,7 +390,6 @@ export class CaslAbilityFactory {
can(Action.JobCreate, JobClass);
can(Action.JobStatusUpdate, JobClass);
cannot(Action.JobDelete, JobClass);

} else if (
user.currentGroups.some((g) =>
configuration().deleteJobGroups.includes(g),
Expand Down Expand Up @@ -447,7 +446,6 @@ export class CaslAbilityFactory {
)
) {
can(Action.JobStatusUpdate, JobClass);

} else {
if (
configuration().jobConfiguration.some(
Expand Down Expand Up @@ -1360,28 +1358,28 @@ export class CaslAbilityFactory {
});
}

jobsInstanceAccess(user: JWTUser, jobConfiguration: JobConfig ) {
jobsInstanceAccess(user: JWTUser, jobConfiguration: JobConfig) {
const { can, build } = new AbilityBuilder(
createMongoAbility<PossibleAbilities, Conditions>,
);

if (!user) {
/**
* unauthenticated users
*/
if (jobConfiguration.create.auth === CreateJobAuth.All){
if (jobConfiguration.create.auth === CreateJobAuth.All) {
can(Action.JobCreateConfiguration, JobClass);
};
if (jobConfiguration.create.auth === CreateJobAuth.DatasetPublic){
}
if (jobConfiguration.create.auth === CreateJobAuth.DatasetPublic) {
can(Action.JobCreateConfiguration, JobClass, {
datasetsValidation: true,
});
};
if (jobConfiguration.statusUpdate.auth === StatusUpdateJobAuth.All){
}
if (jobConfiguration.statusUpdate.auth === StatusUpdateJobAuth.All) {
can(Action.JobStatusUpdateConfiguration, JobClass, {
ownerGroup: undefined,
});
};
}
} else {
/**
* authenticated users
Expand Down Expand Up @@ -1436,13 +1434,21 @@ export class CaslAbilityFactory {
ownerGroup: { $in: user.currentGroups },
ownerUser: user.username,
});
if (jobCreateInstanceAuthorizationValues.some((a) => jobConfiguration.create.auth === a)){
if (
jobCreateInstanceAuthorizationValues.some(
(a) => jobConfiguration.create.auth === a,
)
) {
can(Action.JobCreateConfiguration, JobClass);
};
if (jobCreateDatasetAuthorizationValues.some((a) => jobConfiguration.create.auth === a)){
}
if (
jobCreateDatasetAuthorizationValues.some(
(a) => jobConfiguration.create.auth === a,
)
) {
can(Action.JobCreateConfiguration, JobClass, {
datasetsValidation: true,
});
datasetsValidation: true,
});
}
}
const jobUpdateInstanceAuthorizationValues = [
Expand All @@ -1457,29 +1463,37 @@ export class CaslAbilityFactory {
configuration().statusUpdateJobGroups.includes(g),
)
) {
if (jobUpdateInstanceAuthorizationValues.some((a) => jobConfiguration.statusUpdate.auth === a)){
if (
jobUpdateInstanceAuthorizationValues.some(
(a) => jobConfiguration.statusUpdate.auth === a,
)
) {
can(Action.JobStatusUpdateConfiguration, JobClass);
};
}
can(Action.JobStatusUpdateOwner, JobClass, {
ownerUser: user.username,
});
can(Action.JobStatusUpdateOwner, JobClass, {
ownerGroup: { $in: user.currentGroups },
});
} else {
if (jobUpdateInstanceAuthorizationValues.some((a) => jobConfiguration.statusUpdate.auth === a)){
if (
jobUpdateInstanceAuthorizationValues.some(
(a) => jobConfiguration.statusUpdate.auth === a,
)
) {
can(Action.JobStatusUpdateConfiguration, JobClass);
};
if (jobConfiguration.statusUpdate.auth === "#jobOwnerUser"){
}
if (jobConfiguration.statusUpdate.auth === "#jobOwnerUser") {
can(Action.JobStatusUpdateConfiguration, JobClass, {
ownerUser: user.username,
});
}
if (jobConfiguration.statusUpdate.auth === "#jobOwnerGroup"){
if (jobConfiguration.statusUpdate.auth === "#jobOwnerGroup") {
can(Action.JobStatusUpdateConfiguration, JobClass, {
ownerGroup: { $in: user.currentGroups },
});
};
}
}
}
}
Expand Down Expand Up @@ -1766,4 +1780,4 @@ export class CaslAbilityFactory {
item.constructor as ExtractSubjectType<Subjects>,
});
}
}
}
3 changes: 1 addition & 2 deletions src/jobs/config/jobconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class JobConfig {
* @param data JSON
* @returns
*/

static parse(
jobData: Record<string, unknown>,
configVersion: string,
Expand Down Expand Up @@ -81,7 +81,6 @@ export class JobConfig {
}
}


/**
* Encapsulates all information for a particular job operation (eg "create", "statusUpdate")
*/
Expand Down
23 changes: 14 additions & 9 deletions src/jobs/jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import {
} from "src/common/utils";
import { JobCreateInterceptor } from "./interceptors/job-create.interceptor";
import { JobAction } from "./config/jobconfig";
import { ConnectableObservable } from "rxjs";

@ApiBearerAuth()
@ApiTags("jobs")
Expand Down Expand Up @@ -519,7 +518,10 @@ export class JobsController {
}

// instantiate the casl matrix for the user
const ability = this.caslAbilityFactory.jobsInstanceAccess(user,jobConfiguration);
const ability = this.caslAbilityFactory.jobsInstanceAccess(
user,
jobConfiguration,
);
// check if the user can create this job
const canCreate =
ability.can(Action.JobCreateAny, JobClass) ||
Expand Down Expand Up @@ -587,7 +589,7 @@ export class JobsController {
* Create job
*/
@UseGuards(PoliciesGuard)
@CheckPolicies("jobs",(ability: AppAbility) =>
@CheckPolicies("jobs", (ability: AppAbility) =>
ability.can(Action.JobCreate, JobClass),
)
// @UseInterceptors(JobCreateInterceptor)
Expand Down Expand Up @@ -641,7 +643,7 @@ export class JobsController {
* Update job status
*/
@UseGuards(PoliciesGuard)
@CheckPolicies("jobs",(ability: AppAbility) =>
@CheckPolicies("jobs", (ability: AppAbility) =>
ability.can(Action.JobStatusUpdate, JobClass),
)
@Patch(":id")
Expand Down Expand Up @@ -708,7 +710,9 @@ export class JobsController {
* Get job by id
*/
@UseGuards(PoliciesGuard)
@CheckPolicies("jobs",(ability: AppAbility) => ability.can(Action.JobRead, JobClass))
@CheckPolicies("jobs", (ability: AppAbility) =>
ability.can(Action.JobRead, JobClass),
)
@Get(":id")
@ApiOperation({
summary: "It returns the requested job.",
Expand Down Expand Up @@ -797,7 +801,9 @@ export class JobsController {
const jobsAccessible: JobClass[] = [];

for (const i in jobsFound) {
const jobConfiguration = this.getJobTypeConfiguration(jobsFound[i].type);
const jobConfiguration = this.getJobTypeConfiguration(
jobsFound[i].type,
);
const ability = this.caslAbilityFactory.jobsInstanceAccess(
request.user as JWTUser,
jobConfiguration,
Expand Down Expand Up @@ -829,9 +835,8 @@ export class JobsController {
* Delete a job
*/
@UseGuards(PoliciesGuard)
@CheckPolicies("jobs",
(ability: AppAbility) =>
ability.can(Action.JobDelete, JobClass),
@CheckPolicies("jobs", (ability: AppAbility) =>
ability.can(Action.JobDelete, JobClass),
)
@Delete(":id")
@ApiOperation({
Expand Down
1 change: 0 additions & 1 deletion src/proposals/proposals.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
ApiTags,
} from "@nestjs/swagger";
import { PoliciesGuard } from "src/casl/guards/policies.guard";
import { AuthenticatedPoliciesGuard } from "../casl/guards/auth-check.guard";
import { CheckPolicies } from "src/casl/decorators/check-policies.decorator";
import { AppAbility, CaslAbilityFactory } from "src/casl/casl-ability.factory";
import { Action } from "src/casl/action.enum";
Expand Down
3 changes: 2 additions & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export class UsersController {
viewedUserSchema._id = viewedUserId;
viewedUserSchema.id = viewedUserId;

const ability = this.caslAbilityFactory.userEndpointAccess(authenticatedUser);
const ability =
this.caslAbilityFactory.userEndpointAccess(authenticatedUser);
// const authorized = actions.map( action =>
// ability.can(action, viewedUserSchema)
// ) as Array<Boolean>;
Expand Down

0 comments on commit 303c146

Please sign in to comment.